Repository navigation
fix: GetIteration may return a non-lowest iteration - #1279
Open
CrewcutSpace wants to merge 1 commit into
Open
CrewcutSpace wants to merge 1 commit into
CrewcutSpace wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
GetIterationcan return an iteration that is not the lowest one at which the staker is elected. Since the block with the lowest iteration wins the epoch, a staker running the current code loses blocks it should have won.Cause.
getIterationConcurrentlyruns 10 goroutines over interleaved iterations (routinerchecksr, r+10, r+20, …in each batch of 1000). The first goroutine that finds an elected iteration signalsdone, the others stop at once, andGetIterationreturns the smallest of whatever was found by then. Which goroutine finds a hit first depends on scheduling, not on the iteration value: if routine 5 reaches 45 before routine 2 reaches 12, the node proposes with 45.Effect. Replaying 1235 mainnet epochs from a staker's debug logs (salt, stake, biggest stake and number of stakers are all logged by
Propose) and recomputingisElectedProposerfrom iteration 0 showed that the node proposed with a non-lowest iteration in 698 of them (57%).Fix. Search sequentially from iteration 0 and return the first hit, which is by construction the lowest. The state timeout is still checked, once per
BatchSizeiterations.getIterationConcurrentlyand thesyncimport are removed since nothing else uses them. In the same replay the sequential search returned the lowest iteration in all 1235 epochs and finished well within the propose state: the lowest iteration had a median of 12 and a maximum of 113.Nothing else changes: commit, reveal, medians and the propose transaction are the same, and the contract only checks that the given iteration is elected.
Type of change
How Has This Been Tested?
TestGetIterationTest 7: a staker elected at iterations 12, 45, 62, … must get 12. On the current code it fails intermittently (3 of 50 runs, returning e.g. 45); with this change it passes 50 of 50.TestGetIteration,TestIsElectedProposerandTestProposepass.GetIteration(with the RPC calls mocked): lowest iteration in every epoch.Test Configuration:
Checklist: