add rpc call for latest header information

pull/1612/head
chao 5 years ago
parent 266a4e42d8
commit 73c31f542b
  1. 6
      internal/hmyapi/blockchain.go
  2. 24
      internal/hmyapi/types.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)
}

@ -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{

Loading…
Cancel
Save