github.com/amazechain/amc@v0.1.3/turbo/rpchelper/helper.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rpchelper 18 19 import ( 20 "fmt" 21 "github.com/amazechain/amc/common/types" 22 "github.com/amazechain/amc/modules/rawdb" 23 "github.com/amazechain/amc/modules/rpc/jsonrpc" 24 "github.com/holiman/uint256" 25 "github.com/ledgerwatch/erigon-lib/kv" 26 ) 27 28 // unable to decode supplied params, or an invalid number of parameters 29 type nonCanonocalHashError struct{ hash types.Hash } 30 31 func (e nonCanonocalHashError) ErrorCode() int { return -32603 } 32 33 func (e nonCanonocalHashError) Error() string { 34 return fmt.Sprintf("hash %x is not currently canonical", e.hash) 35 } 36 37 func GetBlockNumber(blockNrOrHash jsonrpc.BlockNumberOrHash, tx kv.Tx) (*uint256.Int, types.Hash, error) { 38 if tx == nil { 39 40 } 41 return _GetBlockNumber(blockNrOrHash.RequireCanonical, blockNrOrHash, tx) 42 } 43 44 func GetCanonicalBlockNumber(blockNrOrHash jsonrpc.BlockNumberOrHash, tx kv.Tx) (*uint256.Int, types.Hash, error) { 45 return _GetBlockNumber(true, blockNrOrHash, tx) 46 } 47 48 func _GetBlockNumber(requireCanonical bool, blockNrOrHash jsonrpc.BlockNumberOrHash, tx kv.Tx) (blockNumber *uint256.Int, hash types.Hash, err error) { 49 50 var ok bool 51 hash, ok = blockNrOrHash.Hash() 52 if !ok { 53 number := *blockNrOrHash.BlockNumber 54 switch number { 55 case jsonrpc.LatestBlockNumber: 56 if blockNumber, err = GetLatestBlockNumber(tx); err != nil { 57 return nil, types.Hash{}, err 58 } 59 case jsonrpc.EarliestBlockNumber: 60 blockNumber = uint256.NewInt(0) 61 case jsonrpc.FinalizedBlockNumber: 62 blockNumber, err = GetFinalizedBlockNumber(tx) 63 if err != nil { 64 return nil, types.Hash{}, err 65 } 66 case jsonrpc.SafeBlockNumber: 67 blockNumber, err = GetSafeBlockNumber(tx) 68 if err != nil { 69 return nil, types.Hash{}, err 70 } 71 case jsonrpc.PendingBlockNumber: 72 //todo 73 if blockNumber, err = GetLatestBlockNumber(tx); err != nil { 74 return nil, types.Hash{}, err 75 } 76 default: 77 blockNumber = uint256.NewInt(uint64(number.Int64())) 78 } 79 hash, err = rawdb.ReadCanonicalHash(tx, blockNumber.Uint64()) 80 if err != nil { 81 return nil, types.Hash{}, err 82 } 83 } else { 84 number := rawdb.ReadHeaderNumber(tx, hash) 85 if number == nil { 86 return nil, types.Hash{}, fmt.Errorf("block %x not found", hash) 87 } 88 blockNumber = uint256.NewInt(*number) 89 90 ch, err := rawdb.ReadCanonicalHash(tx, blockNumber.Uint64()) 91 if err != nil { 92 return nil, types.Hash{}, err 93 } 94 if requireCanonical && ch != hash { 95 return nil, types.Hash{}, nonCanonocalHashError{hash} 96 } 97 } 98 return blockNumber, hash, nil 99 }