Skip to content

Commit

Permalink
Add API to get latest committed blockInfo, also send it via 'block' m…
Browse files Browse the repository at this point in the history
…essage type to stats server (ethereum#113)
  • Loading branch information
wjrjerome authored Jul 16, 2022
1 parent 765d962 commit 4eea723
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions consensus/XDPoS/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func (api *API) GetSignersAtHash(hash common.Hash) ([]common.Address, error) {
return api.XDPoS.GetAuthorisedSignersFromSnapshot(api.chain, header)
}

// Get the latest v2 committed block information. Note: This only applies to v2 engine. it doesn't make sense for v1
func (api *API) GetLatestCommittedBlockHeader() *types.BlockInfo {
return api.XDPoS.EngineV2.GetLatestCommittedBlockInfo()
}

func (api *API) NetworkInformation() NetworkInformation {
info := NetworkInformation{}
info.NetworkId = api.chain.Config().ChainId
Expand Down
4 changes: 4 additions & 0 deletions consensus/XDPoS/engines/engine_v2/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,3 +1019,7 @@ func (x *XDPoS_v2) periodicJob() {
}
}()
}

func (x *XDPoS_v2) GetLatestCommittedBlockInfo() *types.BlockInfo {
return x.highestCommitBlock
}
18 changes: 18 additions & 0 deletions ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ type blockStats struct {
Uncles uncleStats `json:"uncles"`
}

// blockStats is the information to report about individual blocks.
type latestCommittedBlockStats struct {
Number *big.Int `json:"number"`
Hash common.Hash `json:"hash"`
Round uint64 `json:"round"`
}

// txStats is the information to report about individual transactions.
type txStats struct {
Hash common.Hash `json:"hash"`
Expand Down Expand Up @@ -539,6 +546,17 @@ func (s *Service) reportBlock(conn *websocket.Conn, block *types.Block) error {
"id": s.node,
"block": details,
}

// Get the latest committed block information
if (s.engine.(*XDPoS.XDPoS).EngineV2 != nil) && (s.engine.(*XDPoS.XDPoS).EngineV2.GetLatestCommittedBlockInfo() != nil) {
latestCommittedBlockInfo := s.engine.(*XDPoS.XDPoS).EngineV2.GetLatestCommittedBlockInfo()
stats["latestCommittedBlockInfo"] = &latestCommittedBlockStats{
Number: latestCommittedBlockInfo.Number,
Round: uint64(latestCommittedBlockInfo.Round),
Hash: latestCommittedBlockInfo.Hash,
}
}

report := map[string][]interface{}{
"emit": {"block", stats},
}
Expand Down
4 changes: 4 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ web3._extend({
call: 'XDPoS_getSignersAtHash',
params: 1
}),
new web3._extend.Method({
name: 'getLatestCommittedBlockInfo',
call: 'XDPoS_getLatestCommittedBlockHeader'
}),
],
properties: [
new web3._extend.Property({
Expand Down

0 comments on commit 4eea723

Please sign in to comment.