mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
Merge pull request #1230 from XinFinOrg/v1compatible-getblockbyepochnumber
make XDPoS_getBlockInfoByEpochNum work with v1 epoch number
This commit is contained in:
commit
957d6093a3
3 changed files with 45 additions and 13 deletions
|
|
@ -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"
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,9 +76,10 @@ 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]
|
||||||
|
|
|
||||||
|
|
@ -200,19 +200,22 @@ func TestGetBlockByEpochNumber(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
info, err := engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(0)
|
info, err := engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(0)
|
||||||
assert.NotNil(t, err)
|
assert.Equal(t, info.EpochConsensusVersion, "v1")
|
||||||
assert.Nil(t, info)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(1)
|
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(1)
|
||||||
assert.Equal(t, info.EpochRound, types.Round(1))
|
assert.Equal(t, *info.EpochRound, types.Round(1))
|
||||||
|
assert.Equal(t, info.EpochConsensusVersion, "v2")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(2)
|
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(2)
|
||||||
assert.Equal(t, info.EpochRound, types.Round(900))
|
assert.Equal(t, *info.EpochRound, types.Round(900))
|
||||||
|
assert.Equal(t, info.EpochConsensusVersion, "v2")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(3)
|
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(3)
|
||||||
assert.Equal(t, info.EpochRound, types.Round(largeRound))
|
assert.Equal(t, *info.EpochRound, types.Round(largeRound))
|
||||||
|
assert.Equal(t, info.EpochConsensusVersion, "v2")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(4)
|
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(4)
|
||||||
|
|
@ -220,7 +223,8 @@ func TestGetBlockByEpochNumber(t *testing.T) {
|
||||||
assert.Nil(t, info)
|
assert.Nil(t, info)
|
||||||
|
|
||||||
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(5)
|
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(5)
|
||||||
assert.Equal(t, info.EpochRound, types.Round(largeRound2))
|
assert.Equal(t, *info.EpochRound, types.Round(largeRound2))
|
||||||
|
assert.Equal(t, info.EpochConsensusVersion, "v2")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(6)
|
info, err = engine.APIs(blockchain)[0].Service.(*XDPoS.API).GetBlockInfoByEpochNum(6)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue