Skip to content

Commit

Permalink
separate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Dec 4, 2024
1 parent adad7a6 commit 9641986
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/hash_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl HashBuilder {
let root = self.current_root();
if root == EMPTY_ROOT_HASH {
if let Some(proof_retainer) = self.proof_retainer.as_mut() {
proof_retainer.retain(&Nibbles::default(), &[EMPTY_STRING_CODE], None)
proof_retainer.retain(&Nibbles::default(), &[EMPTY_STRING_CODE])
}
}
root
Expand Down Expand Up @@ -267,7 +267,7 @@ impl HashBuilder {
"pushing leaf node",
);
self.stack.push(rlp);
self.retain_proof_from_buf(&current.slice(..len_from), None);
self.retain_proof_from_buf(&current.slice(..len_from));
}
HashBuilderValueRef::Hash(hash) => {
trace!(target: "trie::hash_builder", ?hash, "pushing branch node hash");
Expand Down Expand Up @@ -299,7 +299,7 @@ impl HashBuilder {
"pushing extension node",
);
self.stack.push(rlp);
self.retain_proof_from_buf(&current.slice(..len_from), None);
self.retain_proof_from_buf(&current.slice(..len_from));
self.resize_masks(len_from);
}

Expand Down Expand Up @@ -357,7 +357,7 @@ impl HashBuilder {

self.rlp_buf.clear();
let rlp = branch_node.rlp(&mut self.rlp_buf);
self.retain_proof_from_buf(&current.slice(..len), Some(hash_mask));
self.retain_proof_from_buf_with_hash_mask(&current.slice(..len), hash_mask);

// Clears the stack from the branch node elements
let first_child_idx = self.stack.len() - state_mask.count_ones() as usize;
Expand Down Expand Up @@ -406,9 +406,15 @@ impl HashBuilder {
}
}

fn retain_proof_from_buf(&mut self, prefix: &Nibbles, hash_mask: Option<TrieMask>) {
fn retain_proof_from_buf(&mut self, prefix: &Nibbles) {
if let Some(proof_retainer) = self.proof_retainer.as_mut() {
proof_retainer.retain(prefix, &self.rlp_buf, hash_mask)
proof_retainer.retain(prefix, &self.rlp_buf)
}
}

fn retain_proof_from_buf_with_hash_mask(&mut self, prefix: &Nibbles, hash_mask: TrieMask) {
if let Some(proof_retainer) = self.proof_retainer.as_mut() {
proof_retainer.retain_with_hash_mask(prefix, &self.rlp_buf, hash_mask);
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/proof/retainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ impl ProofRetainer {
}

/// Retain the proof if the key matches any of the targets.
pub fn retain(&mut self, prefix: &Nibbles, proof: &[u8], hash_mask: Option<TrieMask>) {
pub fn retain(&mut self, prefix: &Nibbles, proof: &[u8]) {
if prefix.is_empty() || self.matches(prefix) {
self.proof_nodes.insert(prefix.clone(), Bytes::from(proof.to_vec()));
if let Some(hash_mask) = hash_mask {
self.hash_masks.insert(prefix.clone(), hash_mask);
}
}
}

/// Retain the proof and the branch node hash mask if the key matches any of the targets.
pub fn retain_with_hash_mask(&mut self, prefix: &Nibbles, proof: &[u8], hash_mask: TrieMask) {
if prefix.is_empty() || self.matches(prefix) {
self.proof_nodes.insert(prefix.clone(), Bytes::from(proof.to_vec()));
self.hash_masks.insert(prefix.clone(), hash_mask);
}
}
}

0 comments on commit 9641986

Please sign in to comment.