From 6740545358bafd4a75e6dc5369b1e0c38396648e Mon Sep 17 00:00:00 2001 From: Jianrong Date: Fri, 22 Dec 2023 10:37:32 +1100 Subject: [PATCH] change the API to be block num based --- consensus/XDPoS/XDPoS.go | 3 +-- consensus/XDPoS/api.go | 28 ++++++++++++--------- consensus/XDPoS/engines/engine_v2/mining.go | 1 - consensus/XDPoS/engines/engine_v2/utils.go | 12 +++++---- consensus/XDPoS/utils/types.go | 2 ++ internal/web3ext/web3ext.go | 7 +++--- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/consensus/XDPoS/XDPoS.go b/consensus/XDPoS/XDPoS.go index c06f3c53ff..26cadc3555 100644 --- a/consensus/XDPoS/XDPoS.go +++ b/consensus/XDPoS/XDPoS.go @@ -433,8 +433,7 @@ func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNum } } -func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, hash common.Hash) (*utils.PublicApiMissedRoundsMetadata, error) { - header := chain.GetHeaderByHash(hash) +func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) { switch x.config.BlockConsensusVersion(header.Number, header.Extra, ExtraFieldCheck) { case params.ConsensusEngineVersion2: return x.EngineV2.CalculateMissingRounds(chain, header) diff --git a/consensus/XDPoS/api.go b/consensus/XDPoS/api.go index aec4fc4f6c..17f939faae 100644 --- a/consensus/XDPoS/api.go +++ b/consensus/XDPoS/api.go @@ -224,16 +224,7 @@ func (api *API) GetV2BlockByHeader(header *types.Header, uncle bool) *V2BlockInf } func (api *API) GetV2BlockByNumber(number *rpc.BlockNumber) *V2BlockInfo { - var header *types.Header - if number == nil || *number == rpc.LatestBlockNumber { - header = api.chain.CurrentHeader() - } else if *number == rpc.CommittedBlockNumber { - hash := api.XDPoS.EngineV2.GetLatestCommittedBlockInfo().Hash - header = api.chain.GetHeaderByHash(hash) - } else { - header = api.chain.GetHeaderByNumber(uint64(number.Int64())) - } - + header := api.getHeaderFromApiBlockNum(number) if header == nil { return &V2BlockInfo{ Number: big.NewInt(number.Int64()), @@ -288,8 +279,21 @@ func (api *API) NetworkInformation() NetworkInformation { /* An API exclusively for V2 consensus, designed to assist in troubleshooting miners by identifying who mined during their allocated term. */ -func (api *API) GetMissiedRoundsInEpochByBlockHash(hash common.Hash) (*utils.PublicApiMissedRoundsMetadata, error) { - return api.XDPoS.CalculateMissingRounds(api.chain, hash) +func (api *API) GetMissiedRoundsInEpochByBlockNum(number *rpc.BlockNumber) (*utils.PublicApiMissedRoundsMetadata, error) { + return api.XDPoS.CalculateMissingRounds(api.chain, api.getHeaderFromApiBlockNum(number)) +} + +func (api *API) getHeaderFromApiBlockNum(number *rpc.BlockNumber) *types.Header { + var header *types.Header + if number == nil || *number == rpc.LatestBlockNumber { + header = api.chain.CurrentHeader() + } else if *number == rpc.CommittedBlockNumber { + hash := api.XDPoS.EngineV2.GetLatestCommittedBlockInfo().Hash + header = api.chain.GetHeaderByHash(hash) + } else { + header = api.chain.GetHeaderByNumber(uint64(number.Int64())) + } + return header } func calculateSigners(message map[string]SignerTypes, pool map[string]map[common.Hash]utils.PoolObj, masternodes []common.Address) { diff --git a/consensus/XDPoS/engines/engine_v2/mining.go b/consensus/XDPoS/engines/engine_v2/mining.go index 546de844a8..69c861acc7 100644 --- a/consensus/XDPoS/engines/engine_v2/mining.go +++ b/consensus/XDPoS/engines/engine_v2/mining.go @@ -50,7 +50,6 @@ func (x *XDPoS_v2) yourturn(chain consensus.ChainReader, round types.Round, pare return false, nil } - // TODO: USE BELOW FOR THE NEW API leaderIndex := uint64(round) % x.config.Epoch % uint64(len(masterNodes)) x.whosTurn = masterNodes[leaderIndex] if x.whosTurn != signer { diff --git a/consensus/XDPoS/engines/engine_v2/utils.go b/consensus/XDPoS/engines/engine_v2/utils.go index 395493f7e5..e54b2b59b0 100644 --- a/consensus/XDPoS/engines/engine_v2/utils.go +++ b/consensus/XDPoS/engines/engine_v2/utils.go @@ -184,19 +184,21 @@ func (x *XDPoS_v2) CalculateMissingRounds(chain consensus.ChainReader, header *t if err != nil { return nil, err } - // This means there is a missing round incrementation when producing blocks + // This indicates that an increment in the round number is missing during the block production process. if parentRound+1 != currRound { - // We need to loop through between parentRound and the currRound to assess which miner did not mine - for i := parentRound; i < currRound; i++ { + // We need to iterate from the parentRound to the currRound to determine which miner did not perform mining. + for i := parentRound + 1; i < currRound; i++ { leaderIndex := uint64(i) % x.config.Epoch % uint64(len(masternodes)) - whosTerm := masternodes[leaderIndex] + whosTurn := masternodes[leaderIndex] missedRounds = append( missedRounds, utils.MissedRoundInfo{ Round: i, - Miner: whosTerm, + Miner: whosTurn, CurrentBlockHash: nextHeader.Hash(), + CurrentBlockNum: nextHeader.Number, ParentBlockHash: parentHeader.Hash(), + ParentBlockNum: parentHeader.Number, }, ) } diff --git a/consensus/XDPoS/utils/types.go b/consensus/XDPoS/utils/types.go index 3b60688c36..4073fb522b 100644 --- a/consensus/XDPoS/utils/types.go +++ b/consensus/XDPoS/utils/types.go @@ -62,7 +62,9 @@ type MissedRoundInfo struct { Round types.Round Miner common.Address CurrentBlockHash common.Hash + CurrentBlockNum *big.Int ParentBlockHash common.Hash + ParentBlockNum *big.Int } type PublicApiMissedRoundsMetadata struct { EpochRound types.Round diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index e479fea342..e0b0018b78 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -157,9 +157,10 @@ web3._extend({ call: 'XDPoS_getLatestPoolStatus' }), new web3._extend.Method({ - name: 'GetMissiedRoundsInEpochByBlockHash', - call: 'XDPoS_GetMissiedRoundsInEpochByBlockHash', - params: 1 + name: 'GetMissiedRoundsInEpochByBlockNum', + call: 'XDPoS_GetMissiedRoundsInEpochByBlockNum', + params: 1, + inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter] }), ], properties: [