Add API to get latest committed blockInfo, also send it via 'block' message type to stats server (#113)

This commit is contained in:
Jerome 2022-07-16 16:14:36 +10:00 committed by GitHub
parent 765d962d8b
commit 4eea723717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 0 deletions

View file

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

View file

@ -1019,3 +1019,7 @@ func (x *XDPoS_v2) periodicJob() {
}
}()
}
func (x *XDPoS_v2) GetLatestCommittedBlockInfo() *types.BlockInfo {
return x.highestCommitBlock
}

View file

@ -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"`
@ -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},
}

View file

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