@@ -616,13 +616,43 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre
616
616
}, state .Error ()
617
617
}
618
618
619
- // GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all
620
- // transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
621
- func (s * PublicBlockChainAPI ) GetBlockByNumber (ctx context.Context , blockNr rpc.BlockNumber , fullTx bool ) (map [string ]interface {}, error ) {
622
- block , err := s .b .BlockByNumber (ctx , blockNr )
623
- if block != nil {
624
- response , err := s .rpcOutputBlock (block , true , fullTx )
625
- if err == nil && blockNr == rpc .PendingBlockNumber {
619
+ // GetHeaderByNumber returns the requested canonical block header.
620
+ // * When blockNr is -1 the chain head is returned.
621
+ // * When blockNr is -2 the pending chain head is returned.
622
+ func (s * PublicBlockChainAPI ) GetHeaderByNumber (ctx context.Context , number rpc.BlockNumber ) (map [string ]interface {}, error ) {
623
+ header , err := s .b .HeaderByNumber (ctx , number )
624
+ if header != nil && err == nil {
625
+ response := s .rpcMarshalHeader (header )
626
+ if number == rpc .PendingBlockNumber {
627
+ // Pending header need to nil out a few fields
628
+ for _ , field := range []string {"hash" , "nonce" , "miner" } {
629
+ response [field ] = nil
630
+ }
631
+ }
632
+ return response , err
633
+ }
634
+ return nil , err
635
+ }
636
+
637
+ // GetHeaderByHash returns the requested header by hash.
638
+ func (s * PublicBlockChainAPI ) GetHeaderByHash (ctx context.Context , hash common.Hash ) map [string ]interface {} {
639
+ header := s .b .GetHeader (ctx , hash )
640
+ if header != nil {
641
+ return s .rpcMarshalHeader (header )
642
+ }
643
+ return nil
644
+ }
645
+
646
+ // GetBlockByNumber returns the requested canonical block.
647
+ // * When blockNr is -1 the chain head is returned.
648
+ // * When blockNr is -2 the pending chain head is returned.
649
+ // * When fullTx is true all transactions in the block are returned, otherwise
650
+ // only the transaction hash is returned.
651
+ func (s * PublicBlockChainAPI ) GetBlockByNumber (ctx context.Context , number rpc.BlockNumber , fullTx bool ) (map [string ]interface {}, error ) {
652
+ block , err := s .b .BlockByNumber (ctx , number )
653
+ if block != nil && err == nil {
654
+ response , err := s .rpcMarshalBlock (block , true , fullTx )
655
+ if err == nil && number == rpc .PendingBlockNumber {
626
656
// Pending blocks need to nil out a few fields
627
657
for _ , field := range []string {"hash" , "nonce" , "miner" } {
628
658
response [field ] = nil
@@ -635,10 +665,10 @@ func (s *PublicBlockChainAPI) GetBlockByNumber(ctx context.Context, blockNr rpc.
635
665
636
666
// GetBlockByHash returns the requested block. When fullTx is true all transactions in the block are returned in full
637
667
// detail, otherwise only the transaction hash is returned.
638
- func (s * PublicBlockChainAPI ) GetBlockByHash (ctx context.Context , blockHash common.Hash , fullTx bool ) (map [string ]interface {}, error ) {
639
- block , err := s .b .GetBlock (ctx , blockHash )
668
+ func (s * PublicBlockChainAPI ) GetBlockByHash (ctx context.Context , hash common.Hash , fullTx bool ) (map [string ]interface {}, error ) {
669
+ block , err := s .b .GetBlock (ctx , hash )
640
670
if block != nil {
641
- return s .rpcOutputBlock (block , true , fullTx )
671
+ return s .rpcMarshalBlock (block , true , fullTx )
642
672
}
643
673
return nil , err
644
674
}
@@ -654,7 +684,7 @@ func (s *PublicBlockChainAPI) GetUncleByBlockNumberAndIndex(ctx context.Context,
654
684
return nil , nil
655
685
}
656
686
block = types .NewBlockWithHeader (uncles [index ])
657
- return s .rpcOutputBlock (block , false , false )
687
+ return s .rpcMarshalBlock (block , false , false )
658
688
}
659
689
return nil , err
660
690
}
@@ -670,7 +700,7 @@ func (s *PublicBlockChainAPI) GetUncleByBlockHashAndIndex(ctx context.Context, b
670
700
return nil , nil
671
701
}
672
702
block = types .NewBlockWithHeader (uncles [index ])
673
- return s .rpcOutputBlock (block , false , false )
703
+ return s .rpcMarshalBlock (block , false , false )
674
704
}
675
705
return nil , err
676
706
}
@@ -933,14 +963,11 @@ func FormatLogs(logs []vm.StructLog) []StructLogRes {
933
963
return formatted
934
964
}
935
965
936
- // RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
937
- // returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
938
- // transaction hashes.
939
- func RPCMarshalBlock (b * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
940
- head := b .Header () // copies the header once
941
- fields := map [string ]interface {}{
966
+ // RPCMarshalHeader converts the given header to the RPC output .
967
+ func RPCMarshalHeader (head * types.Header ) map [string ]interface {} {
968
+ return map [string ]interface {}{
942
969
"number" : (* hexutil .Big )(head .Number ),
943
- "hash" : b .Hash (),
970
+ "hash" : head .Hash (),
944
971
"parentHash" : head .ParentHash ,
945
972
"nonce" : head .Nonce ,
946
973
"mixHash" : head .MixDigest ,
@@ -950,24 +977,32 @@ func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]inter
950
977
"miner" : head .Coinbase ,
951
978
"difficulty" : (* hexutil .Big )(head .Difficulty ),
952
979
"extraData" : hexutil .Bytes (head .Extra ),
953
- "size" : hexutil .Uint64 (b .Size ()),
980
+ "size" : hexutil .Uint64 (head .Size ()),
954
981
"gasLimit" : hexutil .Uint64 (head .GasLimit ),
955
982
"gasUsed" : hexutil .Uint64 (head .GasUsed ),
956
983
"timestamp" : hexutil .Uint64 (head .Time ),
957
984
"transactionsRoot" : head .TxHash ,
958
985
"receiptsRoot" : head .ReceiptHash ,
959
986
}
987
+ }
988
+
989
+ // RPCMarshalBlock converts the given block to the RPC output which depends on fullTx. If inclTx is true transactions are
990
+ // returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
991
+ // transaction hashes.
992
+ func RPCMarshalBlock (block * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
993
+ fields := RPCMarshalHeader (block .Header ())
994
+ fields ["size" ] = block .Size ()
960
995
961
996
if inclTx {
962
997
formatTx := func (tx * types.Transaction ) (interface {}, error ) {
963
998
return tx .Hash (), nil
964
999
}
965
1000
if fullTx {
966
1001
formatTx = func (tx * types.Transaction ) (interface {}, error ) {
967
- return newRPCTransactionFromBlockHash (b , tx .Hash ()), nil
1002
+ return newRPCTransactionFromBlockHash (block , tx .Hash ()), nil
968
1003
}
969
1004
}
970
- txs := b .Transactions ()
1005
+ txs := block .Transactions ()
971
1006
transactions := make ([]interface {}, len (txs ))
972
1007
var err error
973
1008
for i , tx := range txs {
@@ -977,8 +1012,7 @@ func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]inter
977
1012
}
978
1013
fields ["transactions" ] = transactions
979
1014
}
980
-
981
- uncles := b .Uncles ()
1015
+ uncles := block .Uncles ()
982
1016
uncleHashes := make ([]common.Hash , len (uncles ))
983
1017
for i , uncle := range uncles {
984
1018
uncleHashes [i ] = uncle .Hash ()
@@ -988,9 +1022,17 @@ func RPCMarshalBlock(b *types.Block, inclTx bool, fullTx bool) (map[string]inter
988
1022
return fields , nil
989
1023
}
990
1024
991
- // rpcOutputBlock uses the generalized output filler, then adds the total difficulty field, which requires
1025
+ // rpcMarshalHeader uses the generalized output filler, then adds the total difficulty field, which requires
1026
+ // a `PublicBlockchainAPI`.
1027
+ func (s * PublicBlockChainAPI ) rpcMarshalHeader (header * types.Header ) map [string ]interface {} {
1028
+ fields := RPCMarshalHeader (header )
1029
+ fields ["totalDifficulty" ] = (* hexutil .Big )(s .b .GetTd (header .Hash ()))
1030
+ return fields
1031
+ }
1032
+
1033
+ // rpcMarshalBlock uses the generalized output filler, then adds the total difficulty field, which requires
992
1034
// a `PublicBlockchainAPI`.
993
- func (s * PublicBlockChainAPI ) rpcOutputBlock (b * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
1035
+ func (s * PublicBlockChainAPI ) rpcMarshalBlock (b * types.Block , inclTx bool , fullTx bool ) (map [string ]interface {}, error ) {
994
1036
fields , err := RPCMarshalBlock (b , inclTx , fullTx )
995
1037
if err != nil {
996
1038
return nil , err
0 commit comments