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: stub for instant resharding state root creation #12083

Merged
merged 9 commits into from
Sep 20, 2024

Conversation

Longarithm
Copy link
Member

@Longarithm Longarithm commented Sep 11, 2024

Create the code flow intended for resharding state root creation.
The ultimate goal will be the temporary memtries created, which will be used until new memtries are created from flat storage.

This will be a multi-step work. To complete it, all mentioned TODOs must be resolved.

Overview

The core is a MemTrieUpdate::retain_multi_range function and a simple test_retain_multi_range test. It follows the logic in #12074. I plan to enhance logic sequentially until it will be fit for the actual resharding.
Drawback is that code is not used on chain, but setting up full resharding test requires separate work. I suggest to start from the the incomplete flow; hopefully I will need ~5 PRs to set up the full test.

It follows the "top-down" logic as in insert/delete functions: first we pull root node to in-memory update list, then we descend into children. In the end, we call to_trie_changes which calls post_order_traverse_updated_nodes to create full set of changes to be applied to trie.

Alternative

For the multi-range deletion, better logic is "bottom-up": we first understand the result of children subtrees, and then mark root node as updated if needed.
It's not clear whether everything must be "bottom-up". It probably has its own challenges. But here it is strictly better because it automatically gives the post traversal order of updated nodes.
We could compute hashes on the fly but it's less LoC to postpone it to reuse existing logic, so I just call compute_hashes_and_serialized_nodes in the end.

Copy link

codecov bot commented Sep 11, 2024

Codecov Report

Attention: Patch coverage is 67.52137% with 76 lines in your changes missing coverage. Please review.

Project coverage is 71.52%. Comparing base (773aae7) to head (8f0cc63).
Report is 9 commits behind head on master.

Files with missing lines Patch % Lines
chain/chain/src/chain.rs 19.69% 50 Missing and 3 partials ⚠️
core/store/src/trie/mem/resharding.rs 83.80% 19 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12083      +/-   ##
==========================================
- Coverage   71.59%   71.52%   -0.07%     
==========================================
  Files         818      819       +1     
  Lines      164532   164694     +162     
  Branches   164532   164694     +162     
==========================================
+ Hits       117792   117796       +4     
- Misses      41597    41753     +156     
- Partials     5143     5145       +2     
Flag Coverage Δ
backward-compatibility 0.17% <0.00%> (-0.01%) ⬇️
db-migration 0.17% <0.00%> (-0.01%) ⬇️
genesis-check 1.26% <0.00%> (-0.01%) ⬇️
integration-tests 38.60% <16.66%> (-0.09%) ⬇️
linux 71.29% <67.52%> (+0.10%) ⬆️
linux-nightly 71.09% <67.52%> (-0.04%) ⬇️
macos 54.14% <67.52%> (+0.69%) ⬆️
pytests 1.52% <0.00%> (-0.01%) ⬇️
sanity-checks 1.33% <0.00%> (-0.01%) ⬇️
unittests 65.27% <67.52%> (-0.12%) ⬇️
upgradability 0.21% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Longarithm Longarithm changed the title draft: prototype instant split state root creation feat: stub for instant resharding state root creation Sep 17, 2024
@Longarithm Longarithm marked this pull request as ready for review September 17, 2024 21:09
@Longarithm Longarithm requested a review from a team as a code owner September 17, 2024 21:09
Copy link
Contributor

@wacban wacban left a comment

Choose a reason for hiding this comment

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

LGTM, left a few comments but the high level structure makes sense. I didn't have a full proper look so I'll let others approve.

chain/chain/src/chain.rs Outdated Show resolved Hide resolved
chain/chain/src/chain.rs Outdated Show resolved Hide resolved
chain/chain/src/chain.rs Outdated Show resolved Hide resolved
chain/chain/src/chain.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
/// Returns id of the node after deletion applied.
fn delete_multi_range_recursive<'a, M: ArenaMemory>(
root: MemTrieNodePtr<'a, M>,
key_nibbles: Vec<u8>,
Copy link
Contributor

Choose a reason for hiding this comment

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

this feels like it could be some more efficient representation like &[u8] but it's hard to say if it matters and how exactly to do it

Copy link
Member Author

Choose a reason for hiding this comment

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

Another option is to cut nibbles from the front of intervals_nibbles. But as the keys are short and this happens only once, I want to be simple wherever it's possible.

core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved

match node_view {
MemTrieNodeView::Leaf { extension, .. } => {
let extension = NibbleSlice::from_encoded(extension).0;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Would be nice to implement a getter method instead of using .0 (I know it's outside of this PR but it's a small change so might as well do it).

Copy link
Member Author

Choose a reason for hiding this comment

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

There are a lot of places using NibbleSlice::from_encoded(...).0 and I wouldn't touch them for now, seems better to fix them all at once.
Also not sure if a getter method is applicable because from_encoded returns (Self, bool).

core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
@Longarithm
Copy link
Member Author

@wacban thanks for the great feedback! applied a lot of changes

Copy link
Contributor

@shreyan-gupta shreyan-gupta left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks a lot Alex! 😄

core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
@Trisfald
Copy link
Contributor

nice start! 🚀

@Longarithm
Copy link
Member Author

Longarithm commented Sep 19, 2024

@shreyan-gupta answering globally - yeah, reusing even more from MemTrieUpdate works more nicely.
I didn't want to call post_order_traverse_updated_nodes because I don't really need that and existing methods could avoid it as well, but reusing it means even less lines of code, so I like it more now.

Copy link
Contributor

@shreyan-gupta shreyan-gupta 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!

core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
core/store/src/trie/mem/resharding.rs Outdated Show resolved Hide resolved
}

/// Based on the key and the intervals, makes decision on the subtree exploration.
fn retain_decision(key: &[u8], intervals: &[Range<Vec<u8>>]) -> RetainDecision {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Thanks! :)

@Longarithm Longarithm added this pull request to the merge queue Sep 20, 2024
Merged via the queue into near:master with commit 4fe8380 Sep 20, 2024
25 of 29 checks passed
@Longarithm Longarithm deleted the r-new-state-root branch September 20, 2024 10:42
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.

4 participants