package commands import ( "context" "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/common/hexutil" "github.com/ledgerwatch/erigon/core/rawdb" "github.com/ledgerwatch/erigon/rpc" "github.com/ledgerwatch/erigon/turbo/adapter/ethapi" ) // GetBlockByNumber implements eth_getBlockByNumber. Returns information about a block given the block's number. func (api *APIImpl) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } defer tx.Rollback() b, err := api.getBlockByNumber(number, tx) if err != nil { return nil, err } if b == nil { return nil, nil } additionalFields := make(map[string]interface{}) td, err := rawdb.ReadTd(tx, b.Hash(), b.NumberU64()) if err != nil { return nil, err } additionalFields["totalDifficulty"] = (*hexutil.Big)(td) response, err := ethapi.RPCMarshalBlock(b, true, fullTx, additionalFields) if err == nil && number == rpc.PendingBlockNumber { // Pending blocks need to nil out a few fields for _, field := range []string{"hash", "nonce", "miner"} { response[field] = nil } } return response, err } // GetBlockByHash implements eth_getBlockByHash. Returns information about a block given the block's hash. func (api *APIImpl) GetBlockByHash(ctx context.Context, numberOrHash rpc.BlockNumberOrHash, fullTx bool) (map[string]interface{}, error) { if numberOrHash.BlockHash == nil { // some web3.js based apps (like ethstats client) for some reason call // eth_getBlockByHash with a block number as a parameter // so no matter how weird that is, we would love to support that. if numberOrHash.BlockNumber == nil { return nil, nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645 } return api.GetBlockByNumber(ctx, *numberOrHash.BlockNumber, fullTx) } hash := *numberOrHash.BlockHash tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } defer tx.Rollback() additionalFields := make(map[string]interface{}) block, err := rawdb.ReadBlockByHash(tx, hash) if err != nil { return nil, err } if block == nil { return nil, nil // not error, see https://github.com/ledgerwatch/turbo-geth/issues/1645 } number := block.NumberU64() td, err := rawdb.ReadTd(tx, hash, number) if err != nil { return nil, err } additionalFields["totalDifficulty"] = (*hexutil.Big)(td) response, err := ethapi.RPCMarshalBlock(block, true, fullTx, additionalFields) if err == nil && int64(number) == rpc.PendingBlockNumber.Int64() { // Pending blocks need to nil out a few fields for _, field := range []string{"hash", "nonce", "miner"} { response[field] = nil } } return response, err } // GetBlockTransactionCountByNumber implements eth_getBlockTransactionCountByNumber. Returns the number of transactions in a block given the block's block number. func (api *APIImpl) GetBlockTransactionCountByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*hexutil.Uint, error) { tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } defer tx.Rollback() if blockNr == rpc.PendingBlockNumber { b, err := api.getBlockByNumber(blockNr, tx) if err != nil { return nil, err } if b == nil { return nil, nil } n := hexutil.Uint(len(b.Transactions())) return &n, nil } blockNum, err := getBlockNumber(blockNr, tx) if err != nil { return nil, err } body, _, txAmount, err := rawdb.ReadBodyByNumber(tx, blockNum) if err != nil { return nil, err } if body == nil { return nil, nil } n := hexutil.Uint(txAmount) return &n, nil } // GetBlockTransactionCountByHash implements eth_getBlockTransactionCountByHash. Returns the number of transactions in a block given the block's block hash. func (api *APIImpl) GetBlockTransactionCountByHash(ctx context.Context, blockHash common.Hash) (*hexutil.Uint, error) { tx, err := api.db.BeginRo(ctx) if err != nil { return nil, err } defer tx.Rollback() num := rawdb.ReadHeaderNumber(tx, blockHash) if num == nil { return nil, nil } body, _, txAmount := rawdb.ReadBodyWithoutTransactions(tx, blockHash, *num) if body == nil { return nil, nil } n := hexutil.Uint(txAmount) return &n, nil }