Skip to content

Commit be7df25

Browse files
wanwiset25JukLee0ira
authored andcommitted
make XDPoS_getBlockInfoByEpochNum work with v1 epoch number
1 parent f74924f commit be7df25

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

consensus/XDPoS/api.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/base64"
2020
"encoding/json"
2121
"errors"
22+
"fmt"
2223
"math/big"
2324
"os"
2425
"path/filepath"
@@ -468,7 +469,7 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
468469
startIndex := sort.SearchInts(epochNumbers, int(beginHeader.Number.Int64()))
469470
endIndex := sort.SearchInts(epochNumbers, int(endHeader.Number.Int64()))
470471
if endIndex == len(epochNumbers) {
471-
endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block
472+
endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block
472473
}
473474

474475
var rewardfileNamesInRange []rewardFileName
@@ -604,15 +605,16 @@ func (api *API) GetEpochNumbersBetween(begin, end *rpc.BlockNumber) ([]uint64, e
604605
An API exclusively for V2 consensus, designed to assist in getting rewards of the epoch number.
605606
Given the epoch number, search the epoch switch block.
606607
*/
607-
func (api *API) GetBlockInfoByEpochNum(epochNumber uint64) (*utils.EpochNumInfo, error) {
608+
func (api *API) GetBlockInfoByV2EpochNum(epochNumber uint64) (*utils.EpochNumInfo, error) {
608609
thisEpoch, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber)
609610
if err != nil {
610611
return nil, err
611612
}
612613
info := &utils.EpochNumInfo{
613614
EpochBlockHash: thisEpoch.Hash,
614-
EpochRound: thisEpoch.Round,
615+
EpochRound: &thisEpoch.Round,
615616
EpochFirstBlockNumber: thisEpoch.Number,
617+
EpochConsensusVersion: "v2",
616618
}
617619
nextEpoch, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber+1)
618620

@@ -621,3 +623,28 @@ func (api *API) GetBlockInfoByEpochNum(epochNumber uint64) (*utils.EpochNumInfo,
621623
}
622624
return info, nil
623625
}
626+
627+
func (api *API) CalculateBlockInfoByV1EpochNum(targetEpochNum uint64) (*utils.EpochNumInfo, error) {
628+
epoch := api.XDPoS.config.Epoch //900
629+
epochBlockNum := targetEpochNum*epoch + 1
630+
currentBlock := api.chain.CurrentHeader().Number.Uint64()
631+
if currentBlock < epochBlockNum {
632+
return nil, fmt.Errorf("epoch not reached: current block number %d, epoch block number %d", currentBlock, epochBlockNum)
633+
}
634+
635+
epochLastBlockNum := epochBlockNum + epoch - 1
636+
637+
return &utils.EpochNumInfo{
638+
EpochBlockHash: api.chain.GetHeaderByNumber(epochBlockNum).Hash(),
639+
EpochFirstBlockNumber: big.NewInt(int64(epochBlockNum)),
640+
EpochLastBlockNumber: big.NewInt(int64(epochLastBlockNum)),
641+
EpochConsensusVersion: "v1",
642+
}, nil
643+
}
644+
645+
func (api *API) GetBlockInfoByEpochNum(epochNumber uint64) (*utils.EpochNumInfo, error) {
646+
if epochNumber < api.XDPoS.config.V2.SwitchEpoch {
647+
return api.CalculateBlockInfoByV1EpochNum(epochNumber)
648+
}
649+
return api.GetBlockInfoByV2EpochNum(epochNumber)
650+
}

consensus/XDPoS/utils/types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ type PublicApiMissedRoundsMetadata struct {
7575

7676
// Given an epoch number, this struct records the epoch switch block (first block in epoch) infos such as block number
7777
type EpochNumInfo struct {
78-
EpochBlockHash common.Hash `json:"hash"`
79-
EpochRound types.Round `json:"round"`
80-
EpochFirstBlockNumber *big.Int `json:"firstBlock"`
81-
EpochLastBlockNumber *big.Int `json:"lastBlock"`
78+
EpochBlockHash common.Hash `json:"hash"`
79+
EpochRound *types.Round `json:"round,omitempty"`
80+
EpochFirstBlockNumber *big.Int `json:"firstBlock"`
81+
EpochLastBlockNumber *big.Int `json:"lastBlock"`
82+
EpochConsensusVersion string `json:"consensusVersion"`
8283
}
8384

8485
type SigLRU = lru.Cache[common.Hash, common.Address]

consensus/tests/engine_v2_tests/api_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func TestGetEpochNumbersBetween(t *testing.T) {
169169
assert.EqualError(t, err, "illegal begin block number")
170170
}
171171
func TestGetBlockByEpochNumber(t *testing.T) {
172+
//todo
172173
blockchain, _, currentBlock, signer, signFn := PrepareXDCTestBlockChainWithPenaltyForV2Engine(t, 1802, params.TestXDPoSMockChainConfig)
173174

174175
blockCoinBase := "0x111000000000000000000000000000000123"

0 commit comments

Comments
 (0)