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
10 changes: 10 additions & 0 deletions crates/common/trie/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ impl NodeRef {
NodeRef::Hash(hash) => *hash,
}
}

/// # SAFETY: caller must ensure the hash is correct for the node.
/// Otherwise, the `Trie` will silently produce incorrect results and may
/// fail to query other nodes from the `TrieDB`.
pub fn with_hash_unchecked(self, hash: NodeHash) -> Self {
if let NodeRef::Node(_, node_hash) = &self {
let _ = node_hash.set(hash);
}
self
}
}

impl Default for NodeRef {
Expand Down
15 changes: 11 additions & 4 deletions crates/common/trie/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ impl Trie {

if hash.is_valid() {
*choice = match all_nodes.get(&hash.finalize()) {
Some(rlp) => get_embedded_node(all_nodes, rlp)?.into(),
Some(rlp) => {
let node_ref: NodeRef =
get_embedded_node(all_nodes, rlp)?.into();
node_ref.with_hash_unchecked(hash)
}
None => hash.into(),
};
}
Expand All @@ -293,7 +297,10 @@ impl Trie {
};

node.child = match all_nodes.get(&hash.finalize()) {
Some(rlp) => get_embedded_node(all_nodes, rlp)?.into(),
Some(rlp) => {
let node_ref: NodeRef = get_embedded_node(all_nodes, rlp)?.into();
node_ref.with_hash_unchecked(hash)
}
None => hash.into(),
};

Expand All @@ -303,8 +310,8 @@ impl Trie {
})
}

let root = get_embedded_node(all_nodes, root_rlp)?;
Ok(root.into())
let root: NodeRef = get_embedded_node(all_nodes, root_rlp)?.into();
Ok(root.with_hash_unchecked(root_hash.into()))
}

/// Builds a trie from a set of nodes with an empty InMemoryTrieDB as a backend because the nodes are embedded in the root.
Expand Down
Loading