@@ -377,13 +377,42 @@ func (s *BlockChainAPI) GetTransactionAndReceiptProof(ctx context.Context, hash
377
377
return fields , nil
378
378
}
379
379
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
+
380
409
// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all
381
410
// 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 )
384
413
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 {
387
416
// Pending blocks need to nil out a few fields
388
417
for _ , field := range []string {"hash" , "nonce" , "miner" } {
389
418
response [field ] = nil
@@ -396,10 +425,10 @@ func (s *BlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.BlockN
396
425
397
426
// GetBlockByHash returns the requested block. When fullTx is true all transactions in the block are returned in full
398
427
// 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 )
401
430
if block != nil {
402
- return s .rpcOutputBlock (block , true , fullTx )
431
+ return s .rpcMarshalBlock (block , true , fullTx )
403
432
}
404
433
return nil , err
405
434
}
@@ -415,7 +444,7 @@ func (s *BlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context, block
415
444
return nil , nil
416
445
}
417
446
block = types .NewBlockWithHeader (uncles [index ])
418
- return s .rpcOutputBlock (block , false , false )
447
+ return s .rpcMarshalBlock (block , false , false )
419
448
}
420
449
return nil , err
421
450
}
@@ -432,7 +461,7 @@ func (s *BlockChainAPI) GetUncleByBlockHashAndIndex(ctx context.Context, blockHa
432
461
return nil , nil
433
462
}
434
463
block = types .NewBlockWithHeader (uncles [index ])
435
- return s .rpcOutputBlock (block , false , false )
464
+ return s .rpcMarshalBlock (block , false , false )
436
465
}
437
466
return nil , err
438
467
}
@@ -1414,20 +1443,20 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} {
1414
1443
// RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
1415
1444
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
1416
1445
// 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 ())
1420
1449
1421
1450
if inclTx {
1422
1451
formatTx := func (tx * types.Transaction ) (interface {}, error ) {
1423
1452
return tx .Hash (), nil
1424
1453
}
1425
1454
if fullTx {
1426
1455
formatTx = func (tx * types.Transaction ) (interface {}, error ) {
1427
- return newRPCTransactionFromBlockHash (b , tx .Hash ()), nil
1456
+ return newRPCTransactionFromBlockHash (block , tx .Hash ()), nil
1428
1457
}
1429
1458
}
1430
- txs := b .Transactions ()
1459
+ txs := block .Transactions ()
1431
1460
transactions := make ([]interface {}, len (txs ))
1432
1461
var err error
1433
1462
for i , tx := range txs {
@@ -1437,20 +1466,18 @@ func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]inter
1437
1466
}
1438
1467
fields ["transactions" ] = transactions
1439
1468
}
1440
-
1441
- uncles := b .Uncles ()
1469
+ uncles := block .Uncles ()
1442
1470
uncleHashes := make ([]common.Hash , len (uncles ))
1443
1471
for i , uncle := range uncles {
1444
1472
uncleHashes [i ] = uncle .Hash ()
1445
1473
}
1446
1474
fields ["uncles" ] = uncleHashes
1447
-
1448
1475
return fields , nil
1449
1476
}
1450
1477
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
1452
1479
// 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 ) {
1454
1481
fields , err := RPCMarshalBlock (b , inclTx , fullTx )
1455
1482
if err != nil {
1456
1483
return nil , err
0 commit comments