Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 17 additions & 8 deletions storage/src/checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ struct SubTrieMetadata {
depth: usize,
path_prefix: Path,
#[cfg(feature = "ethhash")]
has_peers: bool,
num_siblings: usize,
}

/// [`NodeStore`] checker
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
depth: 0,
path_prefix: Path::new(),
#[cfg(feature = "ethhash")]
has_peers: false,
num_siblings: 1,
};
let mut trie_stats = TrieStats::default();
let errors = self
Expand All @@ -268,14 +268,23 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
progress_bar: Option<&ProgressBar>,
hash_check: bool,
) -> Result<(), Vec<CheckerError>> {
#[cfg(not(feature = "ethhash"))]
let SubTrieMetadata {
root_address: subtrie_root_address,
root_hash: subtrie_root_hash,
parent,
depth,
path_prefix,
#[cfg(feature = "ethhash")]
has_peers,
} = subtrie;

#[cfg(feature = "ethhash")]
let SubTrieMetadata {
root_address: subtrie_root_address,
root_hash: subtrie_root_hash,
parent,
depth,
mut path_prefix,
num_siblings,
} = subtrie;

// check that address is aligned
Expand Down Expand Up @@ -323,7 +332,7 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
// compute the hash of the node and check it against the stored hash
if hash_check {
#[cfg(feature = "ethhash")]
let hash = Self::compute_node_ethhash(&node, &path_prefix, has_peers);
let hash = Self::compute_node_ethhash(&node, &mut path_prefix, num_siblings);
#[cfg(not(feature = "ethhash"))]
let hash = hash_node(&node, &path_prefix);
if hash != subtrie_root_hash {
Expand Down Expand Up @@ -419,7 +428,7 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
depth: depth.saturating_add(1),
path_prefix: child_path_prefix,
#[cfg(feature = "ethhash")]
has_peers: num_children != 1,
num_siblings: num_children,
};
if let Err(e) = self.visit_trie_helper(
child_subtrie,
Expand Down Expand Up @@ -1001,8 +1010,8 @@ mod test {
#[cfg(feature = "ethhash")]
let computed_hash = NodeStore::<Committed, MemStore>::compute_node_ethhash(
branch_node,
&Path::from([2, 0]),
false,
&mut Path::from([2, 0]),
1,
);
#[cfg(not(feature = "ethhash"))]
let computed_hash = hash_node(branch_node, &Path::from([2, 0]));
Expand Down
21 changes: 11 additions & 10 deletions storage/src/nodestore/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,25 @@ where
}

#[cfg(feature = "ethhash")]
/// This function computes the ethhash of a single node assuming all its children are hashed.
/// Note that `num_siblings` is the number of children of the parent node, which includes this node.
/// The function appends to `path_prefix` and then truncate it back to the original length - we only reuse the memory space to avoid allocations
pub(crate) fn compute_node_ethhash(
node: &Node,
path_prefix: &Path,
have_peers: bool,
path_prefix: &mut Path,
num_siblings: usize,
) -> HashType {
if path_prefix.0.len() == 65 && !have_peers {
if path_prefix.0.len() == 65 && num_siblings == 1 {
// This is the special case when this node is the only child of an account
// - 64 nibbles for account + 1 nibble for its position in account branch node
let mut fake_root = node.clone();
let extra_nibble = path_prefix.0.pop().expect("path_prefix is not empty");
fake_root.update_partial_path(Path::from_nibbles_iterator(
path_prefix
.0
.last()
.into_iter()
.chain(fake_root.partial_path().0.iter())
.copied(),
std::iter::once(extra_nibble).chain(fake_root.partial_path().0.iter().copied()),
));
hash_node(&fake_root, path_prefix)
let node_hash = hash_node(&fake_root, path_prefix);
path_prefix.0.push(extra_nibble);
node_hash
} else {
hash_node(node, path_prefix)
}
Expand Down
Loading