Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove stale expiries in AdvanceTimeTo #3415

Merged
merged 9 commits into from
Sep 25, 2024

Conversation

StephenButtolph
Copy link
Contributor

Why this should be merged

In ACP-77, RegisterValidator warp messages use time expiry to prevent replay attacks. This is implemented using an expiry set. This PR introduces the removal of stale expiry entries during AdvanceTimeTo.

How this works

The timestamp in an expiry is when that value is no longer valid to be added to the chain. Ref

expiry is the time at which this message becomes invalid. As of a P-Chain timestamp >= expiry, this Avalanche Warp Message can no longer be used to add the nodeID to the validator set of subnetID

Note: This change doesn't need an IsEtnaActivated flag because Expiries are only added after Etna is activated. And if there are no expiries, the code is a noop (as tested)

How this was tested

  • Added new unit test

@StephenButtolph StephenButtolph added this to the v1.11.12 milestone Sep 25, 2024
@StephenButtolph StephenButtolph self-assigned this Sep 25, 2024
newChainTimeUnix := uint64(newChainTime.Unix())
for expiryIterator.Next() {
expiry := expiryIterator.Value()
if expiry.Timestamp > newChainTimeUnix {
Copy link
Contributor

Choose a reason for hiding this comment

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

(No action required) Maybe add a comment indicating that expiryIterator is ordered ascending by Timestamp such that the first unexpired Expiry implies that all subsequent Expiries are also unexpired?

vms/platformvm/txs/executor/state_changes_test.go Outdated Show resolved Hide resolved
Comment on lines +117 to 126
// Promote any pending stakers to current if [StartTime] <= [newChainTime].
//
// Invariant: It is not safe to modify the state while iterating over it,
// so we use the parentState's iterator rather than the changes iterator.
// ParentState must not be modified before this iterator is released.
pendingStakerIterator, err := parentState.GetPendingStakerIterator()
if err != nil {
return false, err
return nil, false, err
}
defer pendingStakerIterator.Release()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was perilously close to being an existing race.

The iterator is released in a defer, but the changes are applied on the return line. The defer is executed after the return line is evaluated. Which previously broke the invariant (that is now documented). However, because we only ever passed in a state.Diff into this function and the state.Diff lazily allocates the btree's that are iterated over, the returned iterator wasn't a tree iterator, but just explicitly empty... Which does support writes during iteration.

The expiry tree is allocated during diff initialization, which is why this function showed a race when adding the expiry iterator.

By moving the state application to the parent caller, the defer executes before Apply and there is no chance of a race.

@StephenButtolph StephenButtolph added this pull request to the merge queue Sep 25, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 25, 2024
@StephenButtolph StephenButtolph added this pull request to the merge queue Sep 25, 2024
Merged via the queue into master with commit 4680312 Sep 25, 2024
22 checks passed
@StephenButtolph StephenButtolph deleted the implement-acp-77-expiry-removal branch September 25, 2024 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants