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

fix(trie): check branch node masks if store_in_db_trie is None #13828

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,10 @@ impl<P> RevealedSparseTrie<P> {
if let Some((hash, store_in_db_trie)) =
hash.zip(*store_in_db_trie).filter(|_| !prefix_set_contains(&path))
{
(RlpNode::word_rlp(&hash), SparseNodeType::Extension { store_in_db_trie })
(
RlpNode::word_rlp(&hash),
SparseNodeType::Extension { store_in_db_trie: Some(store_in_db_trie) },
)
} else if buffers.rlp_node_stack.last().is_some_and(|e| e.0 == child_path) {
let (_, child, child_node_type) = buffers.rlp_node_stack.pop().unwrap();
self.rlp_buf.clear();
Expand All @@ -697,7 +700,7 @@ impl<P> RevealedSparseTrie<P> {
"Extension node"
);

*store_in_db_trie = Some(store_in_db_trie_value);
*store_in_db_trie = store_in_db_trie_value;

(
rlp_node,
Expand All @@ -720,7 +723,7 @@ impl<P> RevealedSparseTrie<P> {
buffers.rlp_node_stack.push((
path,
RlpNode::word_rlp(&hash),
SparseNodeType::Branch { store_in_db_trie },
SparseNodeType::Branch { store_in_db_trie: Some(store_in_db_trie) },
));
continue
}
Expand Down Expand Up @@ -755,17 +758,19 @@ impl<P> RevealedSparseTrie<P> {
let last_child_nibble = child_path.last().unwrap();

// Determine whether we need to set trie mask bit.
let should_set_tree_mask_bit =
// A blinded node has the tree mask bit set
(
child_node_type.is_hash() &&
self.branch_node_tree_masks
.get(&path)
.is_some_and(|mask| mask.is_bit_set(last_child_nibble))
) ||
let should_set_tree_mask_bit = if let Some(store_in_db_trie) =
child_node_type.store_in_db_trie()
{
// A branch or an extension node explicitly set the
// `store_in_db_trie` flag
child_node_type.store_in_db_trie();
store_in_db_trie
} else {
// A blinded node has the tree mask bit set
child_node_type.is_hash() &&
self.branch_node_tree_masks.get(&path).is_some_and(
|mask| mask.is_bit_set(last_child_nibble),
)
};
if should_set_tree_mask_bit {
tree_mask.set_bit(last_child_nibble);
}
Expand Down Expand Up @@ -868,7 +873,10 @@ impl<P> RevealedSparseTrie<P> {
};
*store_in_db_trie = Some(store_in_db_trie_value);

(rlp_node, SparseNodeType::Branch { store_in_db_trie: store_in_db_trie_value })
(
rlp_node,
SparseNodeType::Branch { store_in_db_trie: Some(store_in_db_trie_value) },
)
}
};
buffers.rlp_node_stack.push((path, rlp_node, node_type));
Expand Down Expand Up @@ -1191,12 +1199,12 @@ enum SparseNodeType {
/// Sparse extension node.
Extension {
/// A flag indicating whether the extension node should be stored in the database.
store_in_db_trie: bool,
store_in_db_trie: Option<bool>,
},
/// Sparse branch node.
Branch {
/// A flag indicating whether the branch node should be stored in the database.
store_in_db_trie: bool,
store_in_db_trie: Option<bool>,
},
}

Expand All @@ -1209,12 +1217,12 @@ impl SparseNodeType {
matches!(self, Self::Branch { .. })
}

const fn store_in_db_trie(&self) -> bool {
const fn store_in_db_trie(&self) -> Option<bool> {
match *self {
Self::Extension { store_in_db_trie } | Self::Branch { store_in_db_trie } => {
store_in_db_trie
}
_ => false,
_ => None,
}
}
}
Expand Down
Loading