Skip to content

Commit

Permalink
feat(l1): don't store trie nodes if they are already inlined on their…
Browse files Browse the repository at this point in the history
… parent (#1124)

**Motivation**
We are currently storing nodes on the DB even if they are already
inlined in their parent node. This is not only storage-inefficient but
it also needs special handling when verifying proofs or importing trie
nodes from other clients (such as in snap sync) as inlined nodes will
not be sent separately.
This PR aims to solve this issue at `TrieState` level, so that the
change is invisible to the main trie logic.
The state will skip insertion for inlined node hashes and will decode
them instead of fetching from the db upon lookups

<!-- Why does this pull request exist? What are its goals? -->

**Description**
* `TrieState::insert` now skips node hashes that represent inlined nodes
* `TrieState::get` now decoded the node if it is inlined instead of
performing a db lookup

<!-- A clear and concise general description of the changes this PR
introduces -->

<!-- Link to issues: Resolves #111, Resolves #222 -->

Closes None, but is needed in order to validate and integrate output
from snap sync requests #184
  • Loading branch information
fmoletta authored Nov 12, 2024
1 parent 393acb1 commit 4112fdd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/storage/trie/node/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod test {
1 => leaf { vec![0, 16] => vec![0x34, 0x56, 0x78, 0x9A] },
}
};
let path = Nibbles::from_hex(vec![2]);
let path = Nibbles::from_bytes(&[2]);
let value = vec![0x3];

let node = node
Expand Down
9 changes: 8 additions & 1 deletion crates/storage/trie/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl TrieState {

/// Retrieves a node based on its hash
pub fn get_node(&self, hash: NodeHash) -> Result<Option<Node>, TrieError> {
// Decode the node if it is inlined
if let NodeHash::Inline(encoded) = hash {
return Ok(Some(Node::decode_raw(&encoded)?));
}
if let Some(node) = self.cache.get(&hash) {
return Ok(Some(node.clone()));
};
Expand All @@ -36,7 +40,10 @@ impl TrieState {

/// Inserts a node
pub fn insert_node(&mut self, node: Node, hash: NodeHash) {
self.cache.insert(hash, node);
// Don't insert the node if it is already inlined on the parent
if matches!(hash, NodeHash::Hashed(_)) {
self.cache.insert(hash, node);
}
}

/// Commits cache changes to DB and clears it
Expand Down

0 comments on commit 4112fdd

Please sign in to comment.