@@ -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
604605An API exclusively for V2 consensus, designed to assist in getting rewards of the epoch number.
605606Given 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+ }
0 commit comments