From f77d4e5668e4616585a092c004f2af29f22f946d Mon Sep 17 00:00:00 2001 From: Wanwiset Peerapatanapokin Date: Fri, 20 Feb 2026 14:34:29 +0700 Subject: [PATCH] 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> --- .../XDPoS/engines/engine_v2/testing_utils.go | 5 ++ .../tests/engine_v2_tests/timeout_test.go | 61 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/consensus/XDPoS/engines/engine_v2/testing_utils.go b/consensus/XDPoS/engines/engine_v2/testing_utils.go index 36d6164220..97b5912b7c 100644 --- a/consensus/XDPoS/engines/engine_v2/testing_utils.go +++ b/consensus/XDPoS/engines/engine_v2/testing_utils.go @@ -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) +} diff --git a/consensus/tests/engine_v2_tests/timeout_test.go b/consensus/tests/engine_v2_tests/timeout_test.go index a990e77df5..dccb569252 100644 --- a/consensus/tests/engine_v2_tests/timeout_test.go +++ b/consensus/tests/engine_v2_tests/timeout_test.go @@ -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") +}