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

fix(sidecar): don't mutate account_state on template refresh #506

Merged
merged 3 commits into from
Dec 2, 2024

Conversation

thedevbirb
Copy link
Contributor

@thedevbirb thedevbirb commented Dec 2, 2024

Closes #505.

The problem wasn't related to missed slot this time but the logic of refreshing templates.

fn refresh_templates(&mut self) {
for (address, (account_state, _)) in self.account_states.iter_mut() {
trace!(%address, ?account_state, "Refreshing template...");
// Iterate over all block templates and apply the state diff
for template in self.block_templates.values_mut() {
// Retain only signed constraints where transactions are still valid based on the
// canonical account states.
template.retain(*address, *account_state);
// Update the account state with the remaining state diff for the next iteration.
if let Some((nonce_diff, balance_diff)) = template.get_diff(address) {
// Nonce will always be increased
account_state.transaction_count += nonce_diff;
// Balance will always be decreased
account_state.balance -= balance_diff;
}
}
}
}

It should not iter_mut over the account_states but rather copying it, which is cheap since it AccountState implements Copy.
By mutating it, if two inclusion requests are made for the same target_slot but during different slots, then the validation fails because the account state has wrong nonce due to adding block templates nonce and balance diffs to it.

A test to cover this case has been introduced.
Tested on devnet as well

@thedevbirb thedevbirb requested a review from mempirate December 2, 2024 14:52
@thedevbirb thedevbirb marked this pull request as draft December 2, 2024 14:53
@thedevbirb thedevbirb marked this pull request as ready for review December 2, 2024 14:55
Copy link
Contributor

@mempirate mempirate left a comment

Choose a reason for hiding this comment

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

Nice catch

@mempirate mempirate merged commit 77cd89e into unstable Dec 2, 2024
3 checks passed
@mempirate mempirate deleted the lore/fix/nonce-issues branch December 2, 2024 18:00
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.

[CHA-101] Nonce accounting issue
3 participants