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(trie): create "updatable" trie representation #11167

Open
5 of 11 tasks
Tracked by #11161
rkrasiuk opened this issue Sep 24, 2024 · 1 comment
Open
5 of 11 tasks
Tracked by #11161

feat(trie): create "updatable" trie representation #11167

rkrasiuk opened this issue Sep 24, 2024 · 1 comment
Labels
A-trie Related to Merkle Patricia Trie implementation C-enhancement New feature or request M-prevent-stale Prevents old inactive issues/PRs from being closed due to inactivity

Comments

@rkrasiuk
Copy link
Member

rkrasiuk commented Sep 24, 2024

Description

Subtrie, sparse trie, multiproof - these are the terms commonly used to describe parts of the trie that have been touched and/or updated following the state transition. Currently, the closest representation for desired feature is the MultiProof struct. However, we do not have a good way to reveal or update the paths inside this struct.

Tasks

  1. A-trie C-enhancement
  2. A-trie C-enhancement
  3. A-trie C-enhancement
    shekhirin
  4. A-trie C-bug
    shekhirin
  5. A-trie C-debt
    shekhirin
  6. A-trie C-perf
    shekhirin
  7. A-trie C-benchmark
    shekhirin
  8. A-trie C-perf
  9. A-trie C-perf
  10. A-trie C-perf
  11. A-trie C-bug

Requirements

Requirements for the implementation:

  • Can be constructed from state proofs
  • Must be able to operate only on revealed paths of the main trie
  • Must be able to retain TrieUpdates equivalent to StateRoot struct

Considerations

Hash-Based Subtrie

The "updatable" trie can be represented similarly to the state field of the ExecutionWitness.

struct Subtrie {
  root: B256,
  nodes: HashMap<B256, TrieNode>,
}

The update function for the subtrie could be implemented using the following algorithm:

  1. Starting from the root node, remove all nodes matching the updated key from self.nodes by their hash (including the previous leaf at key if present) and retain the in-memory as a stack ([root, ..., parent, leaf?])
  2. Insert the leaf node with the updated value
  3. Pop the ancestor from the stack, update with descendant's node hash and reinsert back to self.nodes one by one

Path-Based Subtrie

This implementation can have inner representation similar to the MultiProof.

struct Subtrie {
  nodes: HashMap<Nibbles, TrieNode>
}

Note

We avoid BTreeMap at all costs due to performance implications as the collection grows large.

Then the update function could be implemented using the following algorithm:

  1. Update leaf at key and retain the node hash in memory
  2. Search for first ancestor by popping bytes from the key (path)
  3. Update all ancestors by traversing the subtrie up to the root node

Relevant Links

struct MultiProof

/// The state multiproof of target accounts and multiproofs of their storage tries.
/// Multiproof is effectively a state subtrie that only contains the nodes
/// in the paths of target accounts.
#[derive(Clone, Default, Debug)]
pub struct MultiProof {
/// State trie multiproof for requested accounts.
pub account_subtree: BTreeMap<Nibbles, Bytes>,
/// Storage trie multiproofs.
pub storages: HashMap<B256, StorageMultiProof>,
}

@rkrasiuk rkrasiuk added C-enhancement New feature or request A-trie Related to Merkle Patricia Trie implementation labels Sep 24, 2024
Copy link
Contributor

This issue is stale because it has been open for 21 days with no activity.

@github-actions github-actions bot added the S-stale This issue/PR is stale and will close with no further activity label Oct 16, 2024
@Rjected Rjected added M-prevent-stale Prevents old inactive issues/PRs from being closed due to inactivity and removed S-stale This issue/PR is stale and will close with no further activity labels Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trie Related to Merkle Patricia Trie implementation C-enhancement New feature or request M-prevent-stale Prevents old inactive issues/PRs from being closed due to inactivity
Projects
Status: Todo
Development

No branches or pull requests

2 participants