change the API to be block num based

This commit is contained in:
Jianrong 2023-12-22 10:37:32 +11:00
parent e28b550a24
commit 6740545358
6 changed files with 30 additions and 23 deletions

View file

@ -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) { func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) {
header := chain.GetHeaderByHash(hash)
switch x.config.BlockConsensusVersion(header.Number, header.Extra, ExtraFieldCheck) { switch x.config.BlockConsensusVersion(header.Number, header.Extra, ExtraFieldCheck) {
case params.ConsensusEngineVersion2: case params.ConsensusEngineVersion2:
return x.EngineV2.CalculateMissingRounds(chain, header) return x.EngineV2.CalculateMissingRounds(chain, header)

View file

@ -224,16 +224,7 @@ func (api *API) GetV2BlockByHeader(header *types.Header, uncle bool) *V2BlockInf
} }
func (api *API) GetV2BlockByNumber(number *rpc.BlockNumber) *V2BlockInfo { func (api *API) GetV2BlockByNumber(number *rpc.BlockNumber) *V2BlockInfo {
var header *types.Header header := api.getHeaderFromApiBlockNum(number)
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()))
}
if header == nil { if header == nil {
return &V2BlockInfo{ return &V2BlockInfo{
Number: big.NewInt(number.Int64()), 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. 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) { func (api *API) GetMissiedRoundsInEpochByBlockNum(number *rpc.BlockNumber) (*utils.PublicApiMissedRoundsMetadata, error) {
return api.XDPoS.CalculateMissingRounds(api.chain, hash) 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) { func calculateSigners(message map[string]SignerTypes, pool map[string]map[common.Hash]utils.PoolObj, masternodes []common.Address) {

View file

@ -50,7 +50,6 @@ func (x *XDPoS_v2) yourturn(chain consensus.ChainReader, round types.Round, pare
return false, nil return false, nil
} }
// TODO: USE BELOW FOR THE NEW API
leaderIndex := uint64(round) % x.config.Epoch % uint64(len(masterNodes)) leaderIndex := uint64(round) % x.config.Epoch % uint64(len(masterNodes))
x.whosTurn = masterNodes[leaderIndex] x.whosTurn = masterNodes[leaderIndex]
if x.whosTurn != signer { if x.whosTurn != signer {

View file

@ -184,19 +184,21 @@ func (x *XDPoS_v2) CalculateMissingRounds(chain consensus.ChainReader, header *t
if err != nil { if err != nil {
return nil, err 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 { if parentRound+1 != currRound {
// We need to loop through between parentRound and the currRound to assess which miner did not mine // We need to iterate from the parentRound to the currRound to determine which miner did not perform mining.
for i := parentRound; i < currRound; i++ { for i := parentRound + 1; i < currRound; i++ {
leaderIndex := uint64(i) % x.config.Epoch % uint64(len(masternodes)) leaderIndex := uint64(i) % x.config.Epoch % uint64(len(masternodes))
whosTerm := masternodes[leaderIndex] whosTurn := masternodes[leaderIndex]
missedRounds = append( missedRounds = append(
missedRounds, missedRounds,
utils.MissedRoundInfo{ utils.MissedRoundInfo{
Round: i, Round: i,
Miner: whosTerm, Miner: whosTurn,
CurrentBlockHash: nextHeader.Hash(), CurrentBlockHash: nextHeader.Hash(),
CurrentBlockNum: nextHeader.Number,
ParentBlockHash: parentHeader.Hash(), ParentBlockHash: parentHeader.Hash(),
ParentBlockNum: parentHeader.Number,
}, },
) )
} }

View file

@ -62,7 +62,9 @@ type MissedRoundInfo struct {
Round types.Round Round types.Round
Miner common.Address Miner common.Address
CurrentBlockHash common.Hash CurrentBlockHash common.Hash
CurrentBlockNum *big.Int
ParentBlockHash common.Hash ParentBlockHash common.Hash
ParentBlockNum *big.Int
} }
type PublicApiMissedRoundsMetadata struct { type PublicApiMissedRoundsMetadata struct {
EpochRound types.Round EpochRound types.Round

View file

@ -157,9 +157,10 @@ web3._extend({
call: 'XDPoS_getLatestPoolStatus' call: 'XDPoS_getLatestPoolStatus'
}), }),
new web3._extend.Method({ new web3._extend.Method({
name: 'GetMissiedRoundsInEpochByBlockHash', name: 'GetMissiedRoundsInEpochByBlockNum',
call: 'XDPoS_GetMissiedRoundsInEpochByBlockHash', call: 'XDPoS_GetMissiedRoundsInEpochByBlockNum',
params: 1 params: 1,
inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter]
}), }),
], ],
properties: [ properties: [