From dcbce178dc621ab5188001d2dbd8e3dad4e7cc5b Mon Sep 17 00:00:00 2001 From: wyjDoraemon <46176410+wyjDoraemon@users.noreply.github.com> Date: Fri, 25 Jun 2021 15:27:33 +0800 Subject: [PATCH] api add irreversible height (#1992) * api add irreversible height * opt Co-authored-by: Paladz --- api/nodeinfo.go | 46 +++++++++++++++++++++++++++++----------------- protocol/protocol.go | 8 +++++++- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/api/nodeinfo.go b/api/nodeinfo.go index 561a836a..3c495d56 100644 --- a/api/nodeinfo.go +++ b/api/nodeinfo.go @@ -18,25 +18,32 @@ type VersionInfo struct { // NetInfo indicate net information type NetInfo struct { - Listening bool `json:"listening"` - Syncing bool `json:"syncing"` - Mining bool `json:"mining"` - PeerCount int `json:"peer_count"` - CurrentBlock uint64 `json:"current_block"` - HighestBlock uint64 `json:"highest_block"` - NetWorkID string `json:"network_id"` - Version *VersionInfo `json:"version_info"` + Listening bool `json:"listening"` + Syncing bool `json:"syncing"` + Mining bool `json:"mining"` + PeerCount int `json:"peer_count"` + CurrentBlock uint64 `json:"current_block"` + HighestBlock uint64 `json:"highest_block"` + FinalizedBlock uint64 `json:"finalized_block"` + NetWorkID string `json:"network_id"` + Version *VersionInfo `json:"version_info"` } // GetNodeInfo return net information -func (a *API) GetNodeInfo() *NetInfo { +func (a *API) GetNodeInfo() (*NetInfo, error) { + finalizedBlockHeader, err := a.chain.LastFinalizedHeader() + if err != nil { + return nil, err + } + info := &NetInfo{ - Listening: a.sync.IsListening(), - Syncing: !a.sync.IsCaughtUp(), - Mining: a.blockProposer.IsProposing(), - PeerCount: a.sync.PeerCount(), - CurrentBlock: a.chain.BestBlockHeight(), - NetWorkID: a.sync.GetNetwork(), + Listening: a.sync.IsListening(), + Syncing: !a.sync.IsCaughtUp(), + Mining: a.blockProposer.IsProposing(), + PeerCount: a.sync.PeerCount(), + CurrentBlock: a.chain.BestBlockHeight(), + FinalizedBlock: finalizedBlockHeader.Height, + NetWorkID: a.sync.GetNetwork(), Version: &VersionInfo{ Version: version.Version, Update: version.Status.VersionStatus(), @@ -49,7 +56,7 @@ func (a *API) GetNodeInfo() *NetInfo { if info.CurrentBlock > info.HighestBlock { info.HighestBlock = info.CurrentBlock } - return info + return info, nil } // return the currently connected peers with net address @@ -89,7 +96,12 @@ func (a *API) connectPeerByIpAndPort(ip string, port uint16) (*peers.PeerInfo, e // getNetInfo return network information func (a *API) getNetInfo() Response { - return NewSuccessResponse(a.GetNodeInfo()) + nodeInfo, err := a.GetNodeInfo() + if err != nil { + return NewErrorResponse(err) + } + + return NewSuccessResponse(nodeInfo) } // isMining return is in mining or not diff --git a/protocol/protocol.go b/protocol/protocol.go index c895dc4f..0bb9bf07 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -111,12 +111,18 @@ func newCasper(store Store, storeStatus *BlockStoreState, rollbackCh chan *rollb return NewCasper(store, checkpoints, rollbackCh), nil } -// LastFinalizedHeader returns the last finalized block header of the block chain +// LastJustifiedHeader return the last justified block header of the block chain func (c *Chain) LastJustifiedHeader() (*types.BlockHeader, error) { _, hash := c.casper.LastJustified() return c.store.GetBlockHeader(&hash) } +// LastFinalizedHeader return the last finalized block header of the block chain +func (c *Chain) LastFinalizedHeader() (*types.BlockHeader, error) { + _, hash := c.casper.LastFinalized() + return c.store.GetBlockHeader(&hash) +} + // ProcessBlockVerification process block verification func (c *Chain) ProcessBlockVerification(v *Verification) error { if err := c.casper.AuthVerification(v); err != nil { -- 2.11.0