Skip to content

Commit

Permalink
add last block number for epoch api (#681)
Browse files Browse the repository at this point in the history
Co-authored-by: Liam Lai <liam@home>
  • Loading branch information
benjamin202410 and Liam Lai authored Oct 21, 2024
1 parent 3dac4dc commit 042c02b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
16 changes: 10 additions & 6 deletions consensus/XDPoS/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,17 @@ An API exclusively for V2 consensus, designed to assist in getting rewards of th
Given the epoch number, search the epoch switch block.
*/
func (api *API) GetBlockInfoByEpochNum(epochNumber uint64) (*utils.EpochNumInfo, error) {
result, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber)
thisEpoch, nextEpoch, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber)
if err != nil {
return nil, err
}
return &utils.EpochNumInfo{
EpochBlockHash: result.Hash,
EpochRound: result.Round,
EpochBlockNumber: result.Number,
}, nil
info := &utils.EpochNumInfo{
EpochBlockHash: thisEpoch.Hash,
EpochRound: thisEpoch.Round,
EpochFirstBlockNumber: thisEpoch.Number,
}
if nextEpoch != nil {
info.EpochLastBlockNumber = new(big.Int).Sub(nextEpoch.Number, big.NewInt(1))
}
return info, nil
}
22 changes: 13 additions & 9 deletions consensus/XDPoS/engines/engine_v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,22 @@ func (x *XDPoS_v2) CalculateMissingRounds(chain consensus.ChainReader, header *t
return missedRoundsMetadata, nil
}

func (x *XDPoS_v2) GetBlockByEpochNumber(chain consensus.ChainReader, targetEpochNum uint64) (*types.BlockInfo, error) {
func (x *XDPoS_v2) GetBlockByEpochNumber(chain consensus.ChainReader, targetEpochNum uint64) (*types.BlockInfo, *types.BlockInfo, error) {
currentHeader := chain.CurrentHeader()
epochSwitchInfo, err := x.getEpochSwitchInfo(chain, currentHeader, currentHeader.Hash())
if err != nil {
return nil, err
return nil, nil, err
}
epochNum := x.config.V2.SwitchBlock.Uint64()/x.config.Epoch + uint64(epochSwitchInfo.EpochSwitchBlockInfo.Round)/x.config.Epoch
// since below function GetEpochSwitchInfoBetween(chain, start, end) return nil if start == end, we early return the result
if targetEpochNum == epochNum {
return epochSwitchInfo.EpochSwitchBlockInfo, nil
return epochSwitchInfo.EpochSwitchBlockInfo, nil, nil
}
if targetEpochNum > epochNum {
return nil, errors.New("input epoch number > current epoch number")
return nil, nil, errors.New("input epoch number > current epoch number")
}
if targetEpochNum < x.config.V2.SwitchBlock.Uint64()/x.config.Epoch {
return nil, errors.New("input epoch number < v2 begin epoch number")
return nil, nil, errors.New("input epoch number < v2 begin epoch number")
}
epoch := big.NewInt(int64(x.config.Epoch))
estblockNumDiff := new(big.Int).Mul(epoch, big.NewInt(int64(epochNum-targetEpochNum)))
Expand All @@ -246,13 +246,17 @@ func (x *XDPoS_v2) GetBlockByEpochNumber(chain consensus.ChainReader, targetEpoc
estBlockHeader := chain.GetHeaderByNumber(estBlockNum.Uint64())
epochSwitchInfos, err := x.GetEpochSwitchInfoBetween(chain, estBlockHeader, currentHeader)
if err != nil {
return nil, err
return nil, nil, err
}
for _, info := range epochSwitchInfos {
for i, info := range epochSwitchInfos {
epochNum := x.config.V2.SwitchBlock.Uint64()/x.config.Epoch + uint64(info.EpochSwitchBlockInfo.Round)/x.config.Epoch
if epochNum == targetEpochNum {
return info.EpochSwitchBlockInfo, nil
if i < len(epochSwitchInfos)-1 {
nextEpoch := epochSwitchInfos[i+1].EpochSwitchBlockInfo
return info.EpochSwitchBlockInfo, nextEpoch, nil
}
return info.EpochSwitchBlockInfo, nil, nil
}
}
return nil, errors.New("input epoch number not found (all rounds in this epoch are missed, which is very rare)")
return nil, nil, errors.New("input epoch number not found (all rounds in this epoch are missed, which is very rare)")
}
7 changes: 4 additions & 3 deletions consensus/XDPoS/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ type PublicApiMissedRoundsMetadata struct {

// Given an epoch number, this struct records the epoch switch block (first block in epoch) infos such as block number
type EpochNumInfo struct {
EpochBlockHash common.Hash `json:"hash"`
EpochRound types.Round `json:"round"`
EpochBlockNumber *big.Int `json:"number"`
EpochBlockHash common.Hash `json:"hash"`
EpochRound types.Round `json:"round"`
EpochFirstBlockNumber *big.Int `json:"firstBlock"`
EpochLastBlockNumber *big.Int `json:"lastBlock"`
}
8 changes: 8 additions & 0 deletions consensus/tests/engine_v2_tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,20 @@ func TestGetBlockByEpochNumber(t *testing.T) {
assert.Nil(t, info)

info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(1)
assert.Equal(t, info.EpochFirstBlockNumber.Int64(), int64(901))
assert.Equal(t, info.EpochLastBlockNumber.Int64(), int64(1799))
assert.Equal(t, info.EpochRound, types.Round(1))
assert.Nil(t, err)

info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(2)
assert.Equal(t, info.EpochFirstBlockNumber.Int64(), int64(1800))
assert.Equal(t, info.EpochLastBlockNumber.Int64(), int64(1802))
assert.Equal(t, info.EpochRound, types.Round(900))
assert.Nil(t, err)

info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(3)
assert.Equal(t, info.EpochFirstBlockNumber.Int64(), int64(1803))
assert.Equal(t, info.EpochLastBlockNumber.Int64(), int64(1803))
assert.Equal(t, info.EpochRound, types.Round(largeRound))
assert.Nil(t, err)

Expand All @@ -221,6 +227,8 @@ func TestGetBlockByEpochNumber(t *testing.T) {

info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(5)
assert.Equal(t, info.EpochRound, types.Round(largeRound2))
assert.Equal(t, info.EpochFirstBlockNumber.Int64(), int64(1804))
assert.Nil(t, info.EpochLastBlockNumber)
assert.Nil(t, err)

info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(6)
Expand Down

0 comments on commit 042c02b

Please sign in to comment.