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

feat(node): avoid cloning in-memory data structures on apply_block #532

Merged
merged 8 commits into from
Oct 30, 2024

Conversation

polydez
Copy link
Contributor

@polydez polydez commented Oct 29, 2024

Resolves #149

In this PR we utilize new compute_mutations and apply_mutations methods in Smt in order to get rid of cloning in-memory account and nullifier trees. We also don't clone chain MMR anymore, because it wasn't necessary for adding block hash. Also we put additional checks before applying changes and reorder applying sequence due to possible inconsistent state on applying error.

@polydez polydez marked this pull request as ready for review October 29, 2024 09:16
Cargo.lock Outdated Show resolved Hide resolved
crates/store/Cargo.toml Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a few comments and a question inline.

crates/store/src/state.rs Outdated Show resolved Hide resolved
Comment on lines 319 to 324
// We need to check that the account tree root hasn't changed while we were waiting for
// the DB update task to complete. If it has changed, we mustn't proceed with in-memory
// updates, since it might lead to inconsistent state.
if inner.account_tree.root() != account_tree_old_root {
return Err(ApplyBlockError::ConcurrentWrite);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this check here? If the account tree has changed somehow, wouldn't we get an error on line 344 below? Or more specifically: which scenario are we trying to prevent with this?

If we do need this check, why are we checking only the account tree and not nullifier tree as well?

Copy link
Contributor Author

@polydez polydez Oct 30, 2024

Choose a reason for hiding this comment

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

The motivation for adding this check was in possible situation, when nullifier tree was successfully updated, but update of accounts tree raised an error, this could lead to inconsistent in-memory state. Additional check before updating of nullifier tree can prevent this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. Could we move this check closer to the update site (e.g., to line 336 below) - or is there a reason it needs to be here?

Also, I'd expand the comment to explain the motivation more (like you've done in the comment). For example, there seems to be a specific order in which we need to do updates: first nullifier tree, then account tree, and lastly MMR - so, we should add a comment that this order is meaningful and shouldn't be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we move this check closer to the update site (e.g., to line 336 below) - or is there a reason it needs to be here?

In this case we would allow database to update, but fail to update in-memory state. This is also not a good choice. By the way, we should add checking for nullifier tree root here as well in order to prevent this inconsistent between in-memory and db state. I will add it now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

crates/store/src/nullifier_tree.rs Outdated Show resolved Hide resolved
crates/store/Cargo.toml Outdated Show resolved Hide resolved
@polydez polydez requested a review from bobbinth October 30, 2024 05:44
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a couple of minor comments inline. Once these are addressed, we can merge.

crates/store/src/state.rs Outdated Show resolved Hide resolved
crates/store/src/state.rs Outdated Show resolved Hide resolved
@polydez polydez requested a review from bobbinth October 30, 2024 06:25
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

All looks good! Thank you! Once CI is green, we are good to merge.

@polydez polydez merged commit 17a5ca1 into next Oct 30, 2024
8 checks passed
@polydez polydez deleted the polydez-avoid-smt-cloning branch October 30, 2024 07:53
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.

3 participants