mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 18:30:45 +00:00
Add tests to cover the new GetMissiedRoundsInEpochByBlockNum API
This commit is contained in:
parent
14f6b267c4
commit
e83c0a0954
3 changed files with 123 additions and 1 deletions
|
|
@ -18,7 +18,6 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConfigApi(t *testing.T) {
|
func TestConfigApi(t *testing.T) {
|
||||||
|
|
||||||
bc := backends.NewXDCSimulatedBackend(core.GenesisAlloc{
|
bc := backends.NewXDCSimulatedBackend(core.GenesisAlloc{
|
||||||
voterAddr: {Balance: new(big.Int).SetUint64(10000000000)},
|
voterAddr: {Balance: new(big.Int).SetUint64(10000000000)},
|
||||||
}, 10000000, params.TestXDPoSMockChainConfig)
|
}, 10000000, params.TestXDPoSMockChainConfig)
|
||||||
|
|
|
||||||
111
consensus/tests/engine_v2_tests/api_test.go
Normal file
111
consensus/tests/engine_v2_tests/api_test.go
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
package engine_v2_tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
|
||||||
|
"github.com/XinFinOrg/XDPoSChain/core/types"
|
||||||
|
"github.com/XinFinOrg/XDPoSChain/params"
|
||||||
|
"github.com/XinFinOrg/XDPoSChain/rpc"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetMissiedRoundsInEpochByBlockNumOnlyForV2Consensus(t *testing.T) {
|
||||||
|
_, bc, _, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig)
|
||||||
|
|
||||||
|
engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS)
|
||||||
|
blockNum := rpc.BlockNumber(123)
|
||||||
|
|
||||||
|
data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissiedRoundsInEpochByBlockNum(&blockNum)
|
||||||
|
|
||||||
|
assert.EqualError(t, err, "Not supported in the v1 consensus")
|
||||||
|
assert.Nil(t, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMissiedRoundsInEpochByBlockNumReturnEmptyForV2(t *testing.T) {
|
||||||
|
_, bc, cb, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig)
|
||||||
|
|
||||||
|
engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS)
|
||||||
|
blockNum := rpc.BlockNumber(cb.NumberU64())
|
||||||
|
|
||||||
|
data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissiedRoundsInEpochByBlockNum(&blockNum)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, types.Round(900), data.EpochRound)
|
||||||
|
assert.Equal(t, big.NewInt(1800), data.EpochBlockNumber)
|
||||||
|
assert.Equal(t, 0, len(data.MissedRounds))
|
||||||
|
|
||||||
|
blockNum = rpc.BlockNumber(1800)
|
||||||
|
|
||||||
|
data, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissiedRoundsInEpochByBlockNum(&blockNum)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, types.Round(900), data.EpochRound)
|
||||||
|
assert.Equal(t, big.NewInt(1800), data.EpochBlockNumber)
|
||||||
|
assert.Equal(t, 0, len(data.MissedRounds))
|
||||||
|
|
||||||
|
blockNum = rpc.BlockNumber(1801)
|
||||||
|
|
||||||
|
data, err = engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissiedRoundsInEpochByBlockNum(&blockNum)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, types.Round(900), data.EpochRound)
|
||||||
|
assert.Equal(t, big.NewInt(1800), data.EpochBlockNumber)
|
||||||
|
assert.Equal(t, 0, len(data.MissedRounds))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMissiedRoundsInEpochByBlockNumReturnEmptyForV2FistEpoch(t *testing.T) {
|
||||||
|
_, bc, _, _, _ := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig)
|
||||||
|
|
||||||
|
engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS)
|
||||||
|
blockNum := rpc.BlockNumber(901)
|
||||||
|
|
||||||
|
data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissiedRoundsInEpochByBlockNum(&blockNum)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, types.Round(1), data.EpochRound)
|
||||||
|
assert.Equal(t, big.NewInt(901), data.EpochBlockNumber)
|
||||||
|
assert.Equal(t, 0, len(data.MissedRounds))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMissiedRoundsInEpochByBlockNum(t *testing.T) {
|
||||||
|
blockchain, bc, currentBlock, signer, signFn := PrepareXDCTestBlockChainWith128Candidates(t, 1802, params.TestXDPoSMockChainConfig)
|
||||||
|
chainConfig := params.TestXDPoSMockChainConfig
|
||||||
|
engine := bc.GetBlockChain().Engine().(*XDPoS.XDPoS)
|
||||||
|
blockCoinBase := signer.Hex()
|
||||||
|
|
||||||
|
startingBlockNum := currentBlock.Number().Int64() + 1
|
||||||
|
// Skipped the round
|
||||||
|
roundNumber := startingBlockNum - chainConfig.XDPoS.V2.SwitchBlock.Int64() + 2
|
||||||
|
block := CreateBlock(blockchain, chainConfig, currentBlock, int(startingBlockNum), roundNumber, blockCoinBase, signer, signFn, nil, nil, "b345a8560bd51926803dd17677c9f0751193914a851a4ec13063d6bf50220b53")
|
||||||
|
err := blockchain.InsertBlock(block)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Signer as there is no previous signer assigned
|
||||||
|
err = UpdateSigner(blockchain)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
blockNum := rpc.BlockNumber(1803)
|
||||||
|
|
||||||
|
data, err := engine.APIs(bc.GetBlockChain())[0].Service.(*XDPoS.API).GetMissiedRoundsInEpochByBlockNum(&blockNum)
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, types.Round(900), data.EpochRound)
|
||||||
|
assert.Equal(t, big.NewInt(1800), data.EpochBlockNumber)
|
||||||
|
assert.Equal(t, 2, len(data.MissedRounds))
|
||||||
|
assert.NotEmpty(t, data.MissedRounds[0].Miner)
|
||||||
|
assert.Equal(t, data.MissedRounds[0].Round, types.Round(903))
|
||||||
|
assert.Equal(t, data.MissedRounds[0].CurrentBlockNum, big.NewInt(1803))
|
||||||
|
assert.Equal(t, data.MissedRounds[0].ParentBlockNum, big.NewInt(1802))
|
||||||
|
assert.NotEmpty(t, data.MissedRounds[1].Miner)
|
||||||
|
assert.Equal(t, data.MissedRounds[1].Round, types.Round(904))
|
||||||
|
assert.Equal(t, data.MissedRounds[0].CurrentBlockNum, big.NewInt(1803))
|
||||||
|
assert.Equal(t, data.MissedRounds[0].ParentBlockNum, big.NewInt(1802))
|
||||||
|
|
||||||
|
assert.NotEqual(t, data.MissedRounds[0].Miner, data.MissedRounds[1].Miner)
|
||||||
|
}
|
||||||
|
|
@ -545,6 +545,18 @@ func PrepareXDCTestBlockChainWith128Candidates(t *testing.T, numOfBlocks int, ch
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// First v2 block
|
||||||
|
if (int64(i) - chainConfig.XDPoS.V2.SwitchBlock.Int64()) == 1 {
|
||||||
|
lastv1BlockNumber := block.Header().Number.Uint64() - 1
|
||||||
|
checkpointBlockNumber := lastv1BlockNumber - lastv1BlockNumber%chainConfig.XDPoS.Epoch
|
||||||
|
checkpointHeader := blockchain.GetHeaderByNumber(checkpointBlockNumber)
|
||||||
|
err := engine.EngineV2.Initial(blockchain, checkpointHeader)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
currentBlock = block
|
currentBlock = block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue