test(consensus): add tests for getTCEpochInfo() (#2045)

* add tests for getTCEpochInfo()

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Wanwiset Peerapatanapokin 2026-02-20 14:34:29 +07:00 committed by GitHub
parent e324a78d94
commit f77d4e5668
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 0 deletions

View file

@ -86,3 +86,8 @@ func (x *XDPoS_v2) AuthorizeFaker(signer common.Address) {
func (x *XDPoS_v2) GetForensicsFaker() *Forensics {
return x.ForensicsProcessor
}
// WARN: This function is designed for testing purpose only!
func (x *XDPoS_v2) GetTCEpochInfoFaker(chain consensus.ChainReader, timeoutRound types.Round) (*types.EpochSwitchInfo, error) {
return x.getTCEpochInfo(chain, timeoutRound)
}

View file

@ -1,6 +1,7 @@
package engine_v2_tests
import (
"math/big"
"strconv"
"strings"
"testing"
@ -368,3 +369,63 @@ func TestTimeoutPoolKeyGoodHygiene(t *testing.T) {
}
}
}
func TestGetTCEpochInfo(t *testing.T) {
// First epoch, round 1, switch block 901
// Second epoch, round 901, block 1800
blockchain, _, _, _, _, _ := PrepareXDCTestBlockChainForV2Engine(t, 1805, params.TestXDPoSMockChainConfig, nil)
engineV2 := blockchain.Engine().(*XDPoS.XDPoS).EngineV2
// Test invalid round zero
epochInfo, err := engineV2.GetTCEpochInfoFaker(blockchain, types.Round(0))
assert.NotNil(t, err)
assert.Nil(t, epochInfo)
// Test first round
epochInfo, err = engineV2.GetTCEpochInfoFaker(blockchain, types.Round(1))
assert.Nil(t, err)
assert.NotNil(t, epochInfo)
assert.Equal(t, big.NewInt(901), epochInfo.EpochSwitchBlockInfo.Number)
assert.Equal(t, types.Round(1), epochInfo.EpochSwitchBlockInfo.Round)
assert.True(t, len(epochInfo.Masternodes) > 0, "should have masternodes")
// Test one round before epochSwitch
epochInfo, err = engineV2.GetTCEpochInfoFaker(blockchain, types.Round(899))
assert.Nil(t, err)
assert.NotNil(t, epochInfo)
assert.Equal(t, big.NewInt(901), epochInfo.EpochSwitchBlockInfo.Number)
assert.Equal(t, types.Round(1), epochInfo.EpochSwitchBlockInfo.Round)
assert.True(t, len(epochInfo.Masternodes) > 0, "should have masternodes")
// Test round exactly on epochSwitch
epochInfo, err = engineV2.GetTCEpochInfoFaker(blockchain, types.Round(900))
assert.Nil(t, err)
assert.NotNil(t, epochInfo)
assert.Equal(t, big.NewInt(1800), epochInfo.EpochSwitchBlockInfo.Number)
assert.Equal(t, types.Round(900), epochInfo.EpochSwitchBlockInfo.Round)
assert.True(t, len(epochInfo.Masternodes) > 0, "should have masternodes")
// Test round in second epoch
epochInfo, err = engineV2.GetTCEpochInfoFaker(blockchain, types.Round(903))
assert.Nil(t, err)
assert.NotNil(t, epochInfo)
assert.Equal(t, big.NewInt(1800), epochInfo.EpochSwitchBlockInfo.Number)
assert.Equal(t, types.Round(900), epochInfo.EpochSwitchBlockInfo.Round)
assert.True(t, len(epochInfo.Masternodes) > 0, "should have masternodes")
// Test after few timeout rounds
epochInfo, err = engineV2.GetTCEpochInfoFaker(blockchain, types.Round(920))
assert.Nil(t, err)
assert.NotNil(t, epochInfo)
assert.Equal(t, big.NewInt(1800), epochInfo.EpochSwitchBlockInfo.Number)
assert.Equal(t, types.Round(900), epochInfo.EpochSwitchBlockInfo.Round)
assert.True(t, len(epochInfo.Masternodes) > 0, "should have masternodes")
// Test far away round
epochInfo, err = engineV2.GetTCEpochInfoFaker(blockchain, types.Round(10000))
assert.Nil(t, err)
assert.NotNil(t, epochInfo)
assert.Equal(t, big.NewInt(1800), epochInfo.EpochSwitchBlockInfo.Number)
assert.Equal(t, types.Round(900), epochInfo.EpochSwitchBlockInfo.Round)
assert.True(t, len(epochInfo.Masternodes) > 0, "should have masternodes")
}