Skip to content

Commit e79a05e

Browse files
gupadhyayaLeo Chen
authored andcommitted
add rpc for exposing raw block header information
1 parent b4b3161 commit e79a05e

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

rpc/blockchain.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,42 @@ func (s *PublicBlockchainService) getBlockOptions(opts interface{}) (*rpc_common
10201020
}
10211021
}
10221022

1023+
func (s *PublicBlockchainService) GetFullHeader(
1024+
ctx context.Context, blockNumber BlockNumber,
1025+
) (response StructuredResponse, err error) {
1026+
// Process number based on version
1027+
blockNum := blockNumber.EthBlockNumber()
1028+
1029+
// Ensure valid block number
1030+
if isBlockGreaterThanLatest(s.hmy, blockNum) {
1031+
return nil, ErrRequestedBlockTooHigh
1032+
}
1033+
1034+
// Fetch Header
1035+
header, err := s.hmy.HeaderByNumber(ctx, blockNum)
1036+
if err != nil {
1037+
return nil, err
1038+
}
1039+
1040+
var rpcHeader interface{}
1041+
switch s.version {
1042+
case V2:
1043+
rpcHeader, err = v2.NewBlockHeader(header)
1044+
default:
1045+
return nil, ErrUnknownRPCVersion
1046+
}
1047+
if err != nil {
1048+
return nil, err
1049+
}
1050+
1051+
response, err = NewStructuredResponse(rpcHeader)
1052+
if err != nil {
1053+
return nil, err
1054+
}
1055+
1056+
return response, nil
1057+
}
1058+
10231059
func isBlockGreaterThanLatest(hmy *hmy.Harmony, blockNum rpc.BlockNumber) bool {
10241060
// rpc.BlockNumber is int64 (latest = -1. pending = -2) and currentBlockNum is uint64.
10251061
if blockNum == rpc.PendingBlockNumber {

rpc/v2/types.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/ethereum/go-ethereum/common"
1111
"github.com/ethereum/go-ethereum/common/hexutil"
1212
ethtypes "github.com/ethereum/go-ethereum/core/types"
13+
"github.com/harmony-one/harmony/block"
1314
"github.com/harmony-one/harmony/core/types"
1415
"github.com/harmony-one/harmony/crypto/bls"
1516
internal_common "github.com/harmony-one/harmony/internal/common"
@@ -46,6 +47,34 @@ type BlockWithTxHash struct {
4647
Signers []string `json:"signers,omitempty"`
4748
}
4849

50+
// BlockHeader represents a block header that will serialize to the RPC representation of a block header
51+
type BlockHeader struct {
52+
ParentHash common.Hash `json:"parentHash"`
53+
Miner string `json:"miner"`
54+
StateRoot common.Hash `json:"stateRoot"`
55+
TransactionsRoot common.Hash `json:"transactionsRoot"`
56+
ReceiptsRoot common.Hash `json:"receiptsRoot"`
57+
OutgoingReceiptsRoot common.Hash `json:"outgoingReceiptsRoot"`
58+
IncomingReceiptsRoot common.Hash `json:"incomingReceiptsRoot"`
59+
LogsBloom ethtypes.Bloom `json:"logsBloom"`
60+
Number *big.Int `json:"number"`
61+
GasLimit uint64 `json:"gasLimit"`
62+
GasUsed uint64 `json:"gasUsed"`
63+
Timestamp *big.Int `json:"timestamp"`
64+
ExtraData hexutil.Bytes `json:"extraData"`
65+
MixHash common.Hash `json:"mixHash"`
66+
ViewID *big.Int `json:"viewID"`
67+
Epoch *big.Int `json:"epoch"`
68+
ShardID uint32 `json:"shardID"`
69+
LastCommitSignature hexutil.Bytes `json:"lastCommitSignature"`
70+
LastCommitBitmap hexutil.Bytes `json:"lastCommitBitmap"`
71+
Vrf hexutil.Bytes `json:"vrf"`
72+
Vdf hexutil.Bytes `json:"vdf"`
73+
ShardState hexutil.Bytes `json:"shardState"`
74+
CrossLink hexutil.Bytes `json:"crossLink"`
75+
Slashes hexutil.Bytes `json:"slashes"`
76+
}
77+
4978
// BlockWithFullTx represents a block that will serialize to the RPC representation of a block
5079
// having FULL transactions in the Transaction & Staking transaction fields.
5180
type BlockWithFullTx struct {
@@ -679,6 +708,42 @@ func blockWithFullTxFromBlock(b *types.Block) (*BlockWithFullTx, error) {
679708
return blk, nil
680709
}
681710

711+
func NewBlockHeader(
712+
head *block.Header,
713+
) (*BlockHeader, error) {
714+
lastSig := head.LastCommitSignature()
715+
blk := &BlockHeader{
716+
ParentHash: head.ParentHash(),
717+
Miner: head.Coinbase().Hex(),
718+
StateRoot: head.Root(),
719+
TransactionsRoot: head.TxHash(),
720+
ReceiptsRoot: head.ReceiptHash(),
721+
OutgoingReceiptsRoot: head.OutgoingReceiptHash(),
722+
IncomingReceiptsRoot: head.IncomingReceiptHash(),
723+
LogsBloom: head.Bloom(),
724+
725+
Number: head.Number(),
726+
GasLimit: head.GasLimit(),
727+
GasUsed: head.GasUsed(),
728+
Timestamp: head.Time(),
729+
ExtraData: hexutil.Bytes(head.Extra()),
730+
MixHash: head.MixDigest(),
731+
732+
ViewID: head.ViewID(),
733+
Epoch: head.Epoch(),
734+
ShardID: head.ShardID(),
735+
736+
LastCommitSignature: hexutil.Bytes(lastSig[:]),
737+
LastCommitBitmap: head.LastCommitBitmap(),
738+
Vrf: head.Vrf(),
739+
Vdf: head.Vdf(),
740+
ShardState: head.ShardState(),
741+
CrossLink: head.CrossLinks(),
742+
Slashes: head.Slashes(),
743+
}
744+
return blk, nil
745+
}
746+
682747
// NewTransactionFromHash returns a transaction that will serialize to the RPC representation.
683748
func NewTransactionFromHash(b *types.Block, hash common.Hash) (*Transaction, error) {
684749
for idx, tx := range b.Transactions() {

0 commit comments

Comments
 (0)