Skip to content

test(consensus): fix flaky TestIsYourTurnConsensusV2CrossConfig - #2531

Open
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-TestIsYourTurnConsensusV2CrossConfig
Open

test(consensus): fix flaky TestIsYourTurnConsensusV2CrossConfig #2531
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-TestIsYourTurnConsensusV2CrossConfig

Conversation

@gzliudan

Copy link
Copy Markdown
Collaborator

Proposed changes

TestIsYourTurnConsensusV2CrossConfig was flaky in CI, intermittently failing on the first assert.False with "Should be false". The failure is a timing/rounding bug in the test, not a consensus-logic bug.

https://github.com/XinFinOrg/XDPoSChain/actions/runs/32529339583/job/96918041990?pr=2520

2026-08-21T21:44:49.8937306Z --- FAIL: TestIsYourTurnConsensusV2CrossConfig (3.33s)
2026-08-21T21:44:49.8937658Z     authorised_masternode_test.go:108: 
2026-08-21T21:44:49.8939392Z         	Error Trace:	/home/runner/work/XDPoSChain/XDPoSChain/src/XDPoSChain/consensus/tests/engine_v2_tests/authorised_masternode_test.go:108
2026-08-21T21:44:49.8939702Z         	Error:      	Should be false
2026-08-21T21:44:49.8940350Z         	Test:       	TestIsYourTurnConsensusV2CrossConfig

Root cause

YourTurn derives the mine period via Config(currentRound). Once the test advances the round to 10, that resolves to the switched-to config (UnitTestV2Configs[10], MinePeriod = 3s) even before UpdateParams runs. The check compares now.Unix() - parent.Time against that period using Unix-second granularity, and parent.Time is floor-rounded. On slower CI machines InsertBlock(910) can straddle a second boundary, pushing the first waitedTime up to 3 (>= MinePeriod), so YourTurn returns true and the assert.False fails — matching the CI log [yourturn] Yes ... round=10. Separately, UpdateParams only repoints the engine's CurrentConfig, so reading MinePeriod from blockchain.Config() stays at the pre-switch value (2s), making the second sleep under-computed.

Fix

Look up the switched-to mine period by round (Config(10) -> 3s) and sanity-check it is larger than the previous one. Sleep firstMinePeriod - 1 before the first YourTurn so waitedTime stays strictly below the new period (reports false), and sleep newMinePeriod before the second YourTurn so waitedTime is comfortably past it (reports true).

Verification

The patched test passes consistently (ran multiple times), sibling tests TestIsAuthorisedMNForConsensusV2 and TestIsYourTurnConsensusV2 pass, and gofmt, goimports, go vet, make tidy, and make quick-test are all clean. The original CI failure was reproduced on unpatched dev-upgrade by simulating the same second-boundary timing, confirming the fix addresses the real root cause.

Compatibility

Test-only change; no consensus, RPC, or network-format impact.

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Changes that don't change source code or tests
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: Revert something
  • style: Changes that do not affect the meaning of the code
  • test: Adding missing tests or correcting existing tests

Impacted Components

Which parts of the codebase does this PR touch?
Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Tested on a private network from the genesis block and monitored the chain operating correctly for multiple epochs.
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a9f20f96-17d2-4056-971c-8dc35ff9fa28

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gzliudan gzliudan changed the title test(consensus): fix flaky cross-config yourturn timing test(consensus): fix flaky TestIsYourTurnConsensusV2CrossConfig Aug 23, 2026
@gzliudan
gzliudan force-pushed the fix-TestIsYourTurnConsensusV2CrossConfig branch from 52d0214 to 21083e2 Compare August 23, 2026 15:37
TestIsYourTurnConsensusV2CrossConfig was flaky in CI: the first YourTurn
call could be rounded up to the switched-to config's mine period (round
10 -> 3s) because the check compares against Unix-second granularity,
returning true and failing the assert.False. The second check also
under-slept since UpdateParams only repoints the engine's CurrentConfig,
so MinePeriod read from blockchain.Config() stayed at the pre-switch
value (2s).

Derive the effective mine period from Config(round 10), wait strictly
inside it for the first check, and comfortably past it for the second.
@gzliudan
gzliudan force-pushed the fix-TestIsYourTurnConsensusV2CrossConfig branch from 21083e2 to 1d84a68 Compare August 24, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants