make XDPoS_getBlockInfoByEpochNum work with v1 epoch number

This commit is contained in:
wanwiset25 2025-07-15 02:13:39 +07:00
parent c46302cf65
commit 11ae49b35d
3 changed files with 36 additions and 7 deletions

View file

@ -19,6 +19,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"math/big" "math/big"
"os" "os"
"path/filepath" "path/filepath"
@ -468,7 +469,7 @@ func (api *API) getRewardFileNamesInRange(begin, end *rpc.BlockNumber) ([]reward
startIndex := sort.SearchInts(epochNumbers, int(beginHeader.Number.Int64())) startIndex := sort.SearchInts(epochNumbers, int(beginHeader.Number.Int64()))
endIndex := sort.SearchInts(epochNumbers, int(endHeader.Number.Int64())) endIndex := sort.SearchInts(epochNumbers, int(endHeader.Number.Int64()))
if endIndex == len(epochNumbers) { if endIndex == len(epochNumbers) {
endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block endIndex-- //this is to prevent endIndex out of bounds when endInput is higher than last reward(epoch) block but lower than latest block
} }
var rewardfileNamesInRange []rewardFileName var rewardfileNamesInRange []rewardFileName
@ -604,15 +605,16 @@ func (api *API) GetEpochNumbersBetween(begin, end *rpc.BlockNumber) ([]uint64, e
An API exclusively for V2 consensus, designed to assist in getting rewards of the epoch number. An API exclusively for V2 consensus, designed to assist in getting rewards of the epoch number.
Given the epoch number, search the epoch switch block. Given the epoch number, search the epoch switch block.
*/ */
func (api *API) GetBlockInfoByEpochNum(epochNumber uint64) (*utils.EpochNumInfo, error) { func (api *API) GetBlockInfoByV2EpochNum(epochNumber uint64) (*utils.EpochNumInfo, error) {
thisEpoch, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber) thisEpoch, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber)
if err != nil { if err != nil {
return nil, err return nil, err
} }
info := &utils.EpochNumInfo{ info := &utils.EpochNumInfo{
EpochBlockHash: thisEpoch.Hash, EpochBlockHash: thisEpoch.Hash,
EpochRound: thisEpoch.Round, EpochRound: &thisEpoch.Round,
EpochFirstBlockNumber: thisEpoch.Number, EpochFirstBlockNumber: thisEpoch.Number,
EpochConsensusVersion: "v2",
} }
nextEpoch, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber+1) nextEpoch, err := api.XDPoS.EngineV2.GetBlockByEpochNumber(api.chain, epochNumber+1)
@ -621,3 +623,28 @@ func (api *API) GetBlockInfoByEpochNum(epochNumber uint64) (*utils.EpochNumInfo,
} }
return info, nil return info, nil
} }
func (api *API) CalculateBlockInfoByV1EpochNum(targetEpochNum uint64) (*utils.EpochNumInfo, error) {
epoch := api.XDPoS.config.Epoch //900
epochBlockNum := targetEpochNum*epoch + 1
currentBlock := api.chain.CurrentHeader().Number.Uint64()
if currentBlock < epochBlockNum {
return nil, fmt.Errorf("epoch not reached: current block number %d, epoch block number %d", currentBlock, epochBlockNum)
}
epochLastBlockNum := epochBlockNum + epoch - 1
return &utils.EpochNumInfo{
EpochBlockHash: api.chain.GetHeaderByNumber(epochBlockNum).Hash(),
EpochFirstBlockNumber: big.NewInt(int64(epochBlockNum)),
EpochLastBlockNumber: big.NewInt(int64(epochLastBlockNum)),
EpochConsensusVersion: "v1",
}, nil
}
func (api *API) GetBlockInfoByEpochNum(epochNumber uint64) (*utils.EpochNumInfo, error) {
if epochNumber < api.XDPoS.config.V2.SwitchEpoch {
return api.CalculateBlockInfoByV1EpochNum(epochNumber)
}
return api.GetBlockInfoByV2EpochNum(epochNumber)
}

View file

@ -75,10 +75,11 @@ type PublicApiMissedRoundsMetadata struct {
// Given an epoch number, this struct records the epoch switch block (first block in epoch) infos such as block number // Given an epoch number, this struct records the epoch switch block (first block in epoch) infos such as block number
type EpochNumInfo struct { type EpochNumInfo struct {
EpochBlockHash common.Hash `json:"hash"` EpochBlockHash common.Hash `json:"hash"`
EpochRound types.Round `json:"round"` EpochRound *types.Round `json:"round,omitempty"`
EpochFirstBlockNumber *big.Int `json:"firstBlock"` EpochFirstBlockNumber *big.Int `json:"firstBlock"`
EpochLastBlockNumber *big.Int `json:"lastBlock"` EpochLastBlockNumber *big.Int `json:"lastBlock"`
EpochConsensusVersion string `json:"consensusVersion"`
} }
type SigLRU = lru.Cache[common.Hash, common.Address] type SigLRU = lru.Cache[common.Hash, common.Address]

View file

@ -169,6 +169,7 @@ func TestGetEpochNumbersBetween(t *testing.T) {
assert.EqualError(t, err, "illegal begin block number") assert.EqualError(t, err, "illegal begin block number")
} }
func TestGetBlockByEpochNumber(t *testing.T) { func TestGetBlockByEpochNumber(t *testing.T) {
//todo
blockchain, _, currentBlock, signer, signFn := PrepareXDCTestBlockChainWithPenaltyForV2Engine(t, 1802, params.TestXDPoSMockChainConfig) blockchain, _, currentBlock, signer, signFn := PrepareXDCTestBlockChainWithPenaltyForV2Engine(t, 1802, params.TestXDPoSMockChainConfig)
blockCoinBase := "0x111000000000000000000000000000000123" blockCoinBase := "0x111000000000000000000000000000000123"