Skip to content

fix: GetIteration may return a non-lowest iteration - #1279

Open
CrewcutSpace wants to merge 1 commit into
razor-network:developfrom
CrewcutSpace:fix/lowest-iteration
Open

CrewcutSpace wants to merge 1 commit into
razor-network:developfrom
CrewcutSpace:fix/lowest-iteration

Conversation

@CrewcutSpace

Copy link
Copy Markdown

Description

GetIteration can 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. getIterationConcurrently runs 10 goroutines over interleaved iterations (routine r checks r, r+10, r+20, … in each batch of 1000). The first goroutine that finds an elected iteration signals done, the others stop at once, and GetIteration returns 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 recomputing isElectedProposer from 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 BatchSize iterations. getIterationConcurrently and the sync import 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

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added TestGetIteration Test 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.
  • Existing TestGetIteration, TestIsElectedProposer and TestPropose pass.
  • Replay of 1235 mainnet epochs through the patched GetIteration (with the RPC calls mocked): lowest iteration in every epoch.

Test Configuration:

  • Contracts version: mainnet (SKALE Europa)
  • Hardware: amd64

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@astronaut-aldrin astronaut-aldrin Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error during code review: Error code: 401 - {'error': {'message': 'User not found.', 'code': 401}}

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.

1 participant