@@ -377,13 +377,42 @@ func (s *BlockChainAPI) GetTransactionAndReceiptProof(ctx context.Context, hash
377377 return fields , nil
378378}
379379
380+ // GetHeaderByNumber returns the requested canonical block header.
381+ // - When blockNr is -1 the chain pending header is returned.
382+ // - When blockNr is -2 the chain latest header is returned.
383+ // - When blockNr is -3 the chain finalized header is returned.
384+ // - When blockNr is -4 the chain safe header is returned.
385+ func (api * BlockChainAPI ) GetHeaderByNumber (ctx context.Context , number rpc.BlockNumber ) (map [string ]interface {}, error ) {
386+ header , err := api .b .HeaderByNumber (ctx , number )
387+ if header != nil && err == nil {
388+ response := RPCMarshalHeader (header )
389+ if number == rpc .PendingBlockNumber {
390+ // Pending header need to nil out a few fields
391+ for _ , field := range []string {"hash" , "nonce" , "miner" } {
392+ response [field ] = nil
393+ }
394+ }
395+ return response , err
396+ }
397+ return nil , err
398+ }
399+
400+ // GetHeaderByHash returns the requested header by hash.
401+ func (api * BlockChainAPI ) GetHeaderByHash (ctx context.Context , hash common.Hash ) map [string ]interface {} {
402+ header , _ := api .b .HeaderByHash (ctx , hash )
403+ if header != nil {
404+ return RPCMarshalHeader (header )
405+ }
406+ return nil
407+ }
408+
380409// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all
381410// transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
382- func (s * BlockChainAPI ) GetBlockByNumber (ctx context.Context , blockNr rpc.BlockNumber , fullTx bool ) (map [string ]interface {}, error ) {
383- block , err := s .b .BlockByNumber (ctx , blockNr )
411+ func (s * BlockChainAPI ) GetBlockByNumber (ctx context.Context , number rpc.BlockNumber , fullTx bool ) (map [string ]interface {}, error ) {
412+ block , err := s .b .BlockByNumber (ctx , number )
384413 if block != nil {
385- response , err := s .rpcOutputBlock (block , true , fullTx )
386- if err == nil && blockNr == rpc .PendingBlockNumber {
414+ response , err := s .rpcMarshalBlock (block , true , fullTx )
415+ if err == nil && number == rpc .PendingBlockNumber {
387416 // Pending blocks need to nil out a few fields
388417 for _ , field := range []string {"hash" , "nonce" , "miner" } {
389418 response [field ] = nil
@@ -396,10 +425,10 @@ func (s *BlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.BlockN
396425
397426// GetBlockByHash returns the requested block. When fullTx is true all transactions in the block are returned in full
398427// detail, otherwise only the transaction hash is returned.
399- func (s * BlockChainAPI ) GetBlockByHash (ctx context.Context , blockHash common.Hash , fullTx bool ) (map [string ]interface {}, error ) {
400- block , err := s .b .GetBlock (ctx , blockHash )
428+ func (s * BlockChainAPI ) GetBlockByHash (ctx context.Context , hash common.Hash , fullTx bool ) (map [string ]interface {}, error ) {
429+ block , err := s .b .GetBlock (ctx , hash )
401430 if block != nil {
402- return s .rpcOutputBlock (block , true , fullTx )
431+ return s .rpcMarshalBlock (block , true , fullTx )
403432 }
404433 return nil , err
405434}
@@ -415,7 +444,7 @@ func (s *BlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, block
415444 return nil , nil
416445 }
417446 block = types .NewBlockWithHeader (uncles [index ])
418- return s .rpcOutputBlock (block , false , false )
447+ return s .rpcMarshalBlock (block , false , false )
419448 }
420449 return nil , err
421450}
@@ -432,7 +461,7 @@ func (s *BlockChainAPI) GetUncleByBlockHashAndIndex(ctx context.Context, blockHa
432461 return nil , nil
433462 }
434463 block = types .NewBlockWithHeader (uncles [index ])
435- return s .rpcOutputBlock (block , false , false )
464+ return s .rpcMarshalBlock (block , false , false )
436465 }
437466 return nil , err
438467}
@@ -1414,20 +1443,20 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} {
14141443// RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
14151444// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
14161445// transaction hashes.
1417- func RPCMarshalBlock (b * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
1418- fields := RPCMarshalHeader (b .Header ())
1419- fields ["size" ] = hexutil .Uint64 (b .Size ())
1446+ func RPCMarshalBlock (block * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
1447+ fields := RPCMarshalHeader (block .Header ())
1448+ fields ["size" ] = hexutil .Uint64 (block .Size ())
14201449
14211450 if inclTx {
14221451 formatTx := func (tx * types.Transaction ) (interface {}, error ) {
14231452 return tx .Hash (), nil
14241453 }
14251454 if fullTx {
14261455 formatTx = func (tx * types.Transaction ) (interface {}, error ) {
1427- return newRPCTransactionFromBlockHash (b , tx .Hash ()), nil
1456+ return newRPCTransactionFromBlockHash (block , tx .Hash ()), nil
14281457 }
14291458 }
1430- txs := b .Transactions ()
1459+ txs := block .Transactions ()
14311460 transactions := make ([]interface {}, len (txs ))
14321461 var err error
14331462 for i , tx := range txs {
@@ -1437,20 +1466,18 @@ func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]inter
14371466 }
14381467 fields ["transactions" ] = transactions
14391468 }
1440-
1441- uncles := b .Uncles ()
1469+ uncles := block .Uncles ()
14421470 uncleHashes := make ([]common.Hash , len (uncles ))
14431471 for i , uncle := range uncles {
14441472 uncleHashes [i ] = uncle .Hash ()
14451473 }
14461474 fields ["uncles" ] = uncleHashes
1447-
14481475 return fields , nil
14491476}
14501477
1451- // rpcOutputBlock uses the generalized output filler, then adds the total difficulty field, which requires
1478+ // rpcMarshalBlock uses the generalized output filler, then adds the total difficulty field, which requires
14521479// a `BlockChainAPI`.
1453- func (s * BlockChainAPI ) rpcOutputBlock (b * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
1480+ func (s * BlockChainAPI ) rpcMarshalBlock (b * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
14541481 fields , err := RPCMarshalBlock (b , inclTx , fullTx )
14551482 if err != nil {
14561483 return nil , err
0 commit comments