From 73c31f542b3ca28b871c1a26e9efc82361cbbf40 Mon Sep 17 00:00:00 2001 From: chao Date: Tue, 17 Sep 2019 14:58:06 -0700 Subject: [PATCH] add rpc call for latest header information --- internal/hmyapi/blockchain.go | 6 ++++++ internal/hmyapi/types.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/internal/hmyapi/blockchain.go b/internal/hmyapi/blockchain.go index 6a0a23704..6b66c5e85 100644 --- a/internal/hmyapi/blockchain.go +++ b/internal/hmyapi/blockchain.go @@ -225,3 +225,9 @@ func doCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb } return res, gas, failed, err } + +// LatestHeader returns the latest header information +func (s *PublicBlockChainAPI) LatestHeader(ctx context.Context) *HeaderInformation { + header, _ := s.b.HeaderByNumber(context.Background(), rpc.LatestBlockNumber) // latest header should always be available + return newHeaderInformation(header) +} diff --git a/internal/hmyapi/types.go b/internal/hmyapi/types.go index 66511b954..45bbea3e1 100644 --- a/internal/hmyapi/types.go +++ b/internal/hmyapi/types.go @@ -6,7 +6,9 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/core/types" + common2 "github.com/harmony-one/harmony/internal/common" ) // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction @@ -41,6 +43,28 @@ type RPCCXReceipt struct { Amount *hexutil.Big `json:"value"` } +// HeaderInformation represents the latest consensus information +type HeaderInformation struct { + BlockHash common.Hash `json:"blockHash"` + BlockNumber uint64 `json:"blockNumber"` + ShardID uint32 `json:"shardID"` + Leader string `json:"leader"` + ViewID uint64 `json:"viewID"` + Epoch uint64 `json:"epoch"` +} + +func newHeaderInformation(header *block.Header) *HeaderInformation { + result := &HeaderInformation{ + BlockHash: header.Hash(), + BlockNumber: header.Number().Uint64(), + ShardID: header.ShardID(), + Leader: common2.MustAddressToBech32(header.Coinbase()), + ViewID: header.ViewID().Uint64(), + Epoch: header.Epoch().Uint64(), + } + return result +} + // newRPCCXReceipt returns a CXReceipt that will serialize to the RPC representation func newRPCCXReceipt(cx *types.CXReceipt, blockHash common.Hash, blockNumber uint64) *RPCCXReceipt { result := &RPCCXReceipt{