From 7f5a38b2b5706caf0cf02f389d9aabf5891f79a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Est=C3=A9fano=20Bargas?= Date: Wed, 24 Sep 2025 19:15:04 -0300 Subject: [PATCH 1/5] opt --- crates/common/trie/node.rs | 8 ++++++++ crates/common/trie/trie.rs | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/common/trie/node.rs b/crates/common/trie/node.rs index 8818c54f8a..759af0ae55 100644 --- a/crates/common/trie/node.rs +++ b/crates/common/trie/node.rs @@ -84,6 +84,14 @@ impl NodeRef { NodeRef::Hash(hash) => *hash, } } + + // TODO: could be "with_hash" + // WARN: could lead to errors if setting the wrong hash + pub fn set_hash(&self, hash: NodeHash) { + if let NodeRef::Node(_, node_hash) = self { + let _ = node_hash.set(hash); + } + } } impl Default for NodeRef { diff --git a/crates/common/trie/trie.rs b/crates/common/trie/trie.rs index cda570fcad..5787f248cf 100644 --- a/crates/common/trie/trie.rs +++ b/crates/common/trie/trie.rs @@ -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.set_hash(hash); + node_ref + }, None => hash.into(), }; } @@ -293,7 +297,11 @@ 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.set_hash(hash); + node_ref + } None => hash.into(), }; @@ -303,8 +311,9 @@ 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(); + root.set_hash(root_hash.into()); + Ok(root) } /// Builds a trie from a set of nodes with an empty InMemoryTrieDB as a backend because the nodes are embedded in the root. From 414309436f69d037ed5bced0592d432d4cf89172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Est=C3=A9fano=20Bargas?= Date: Thu, 25 Sep 2025 12:52:10 -0300 Subject: [PATCH 2/5] clean --- crates/common/trie/node.rs | 10 ++++++---- crates/common/trie/trie.rs | 14 ++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/common/trie/node.rs b/crates/common/trie/node.rs index 759af0ae55..cbbfb4e15e 100644 --- a/crates/common/trie/node.rs +++ b/crates/common/trie/node.rs @@ -85,12 +85,14 @@ impl NodeRef { } } - // TODO: could be "with_hash" - // WARN: could lead to errors if setting the wrong hash - pub fn set_hash(&self, hash: NodeHash) { - if let NodeRef::Node(_, node_hash) = self { + /// # Warning + /// will lead to errors if setting the wrong hash, use only + /// when the hash was correctly precalculated + pub fn with_hash(self, hash: NodeHash) -> Self { + if let NodeRef::Node(_, node_hash) = &self { let _ = node_hash.set(hash); } + self } } diff --git a/crates/common/trie/trie.rs b/crates/common/trie/trie.rs index 5787f248cf..9c762e6ef8 100644 --- a/crates/common/trie/trie.rs +++ b/crates/common/trie/trie.rs @@ -280,10 +280,10 @@ impl Trie { if hash.is_valid() { *choice = match all_nodes.get(&hash.finalize()) { Some(rlp) => { - let node_ref: NodeRef = get_embedded_node(all_nodes, rlp)?.into(); - node_ref.set_hash(hash); - node_ref - }, + let node_ref: NodeRef = + get_embedded_node(all_nodes, rlp)?.into(); + node_ref.with_hash(hash) + } None => hash.into(), }; } @@ -299,8 +299,7 @@ impl Trie { node.child = match all_nodes.get(&hash.finalize()) { Some(rlp) => { let node_ref: NodeRef = get_embedded_node(all_nodes, rlp)?.into(); - node_ref.set_hash(hash); - node_ref + node_ref.with_hash(hash) } None => hash.into(), }; @@ -312,8 +311,7 @@ impl Trie { } let root: NodeRef = get_embedded_node(all_nodes, root_rlp)?.into(); - root.set_hash(root_hash.into()); - Ok(root) + Ok(root.with_hash(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. From 33d3c8b59529a0b467a8923fc81e97fbc48c0eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Est=C3=A9fano=20Bargas?= Date: Mon, 29 Sep 2025 17:00:58 -0300 Subject: [PATCH 3/5] Rename with_hash to with_hash_unchecked with safety note Renamed the method to with_hash_unchecked and updated the documentation to include safety warnings. --- crates/common/trie/node.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/common/trie/node.rs b/crates/common/trie/node.rs index cbbfb4e15e..623830a20c 100644 --- a/crates/common/trie/node.rs +++ b/crates/common/trie/node.rs @@ -84,11 +84,11 @@ impl NodeRef { NodeRef::Hash(hash) => *hash, } } - - /// # Warning - /// will lead to errors if setting the wrong hash, use only - /// when the hash was correctly precalculated - pub fn with_hash(self, hash: NodeHash) -> Self { + + /// # 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); } From 7f513eaf8b7b9fd940dc9e58593590e4eafa8f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Est=C3=A9fano=20Bargas?= Date: Mon, 29 Sep 2025 17:01:27 -0300 Subject: [PATCH 4/5] Replace with_hash with with_hash_unchecked in trie.rs --- crates/common/trie/trie.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/common/trie/trie.rs b/crates/common/trie/trie.rs index 9c762e6ef8..2710ecc0e3 100644 --- a/crates/common/trie/trie.rs +++ b/crates/common/trie/trie.rs @@ -282,7 +282,7 @@ impl Trie { Some(rlp) => { let node_ref: NodeRef = get_embedded_node(all_nodes, rlp)?.into(); - node_ref.with_hash(hash) + node_ref.with_hash_unchecked(hash) } None => hash.into(), }; @@ -299,7 +299,7 @@ impl Trie { node.child = match all_nodes.get(&hash.finalize()) { Some(rlp) => { let node_ref: NodeRef = get_embedded_node(all_nodes, rlp)?.into(); - node_ref.with_hash(hash) + node_ref.with_hash_unchecked(hash) } None => hash.into(), }; @@ -311,7 +311,7 @@ impl Trie { } let root: NodeRef = get_embedded_node(all_nodes, root_rlp)?.into(); - Ok(root.with_hash(root_hash.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. From 46207b67726cb1349c932a5104a5ac34b9ed09eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Est=C3=A9fano=20Bargas?= Date: Tue, 30 Sep 2025 13:33:41 -0300 Subject: [PATCH 5/5] fmt --- crates/common/trie/node.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/common/trie/node.rs b/crates/common/trie/node.rs index 623830a20c..bfd3f3738f 100644 --- a/crates/common/trie/node.rs +++ b/crates/common/trie/node.rs @@ -84,7 +84,7 @@ 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`.