@@ -3,7 +3,10 @@ use ethrex_rlp::structs::Encoder;
33use crate :: ValueRLP ;
44use crate :: nibbles:: Nibbles ;
55use crate :: node_hash:: NodeHash ;
6- use crate :: { TrieDB , error:: TrieError } ;
6+ use crate :: {
7+ TrieDB ,
8+ error:: { ExtensionNodeErrorData , TrieError } ,
9+ } ;
710
811use super :: { BranchNode , Node , NodeRef , ValueOrHash } ;
912
@@ -26,10 +29,18 @@ impl ExtensionNode {
2629 // If the path is prefixed by this node's prefix, delegate to its child.
2730 // Otherwise, no value is present.
2831 if path. skip_prefix ( & self . prefix ) {
29- let child_node = self
30- . child
31- . get_node ( db, path. current ( ) ) ?
32- . ok_or ( TrieError :: InconsistentTree ) ?;
32+ let child_node = self . child . get_node ( db, path. current ( ) ) ?. ok_or_else ( || {
33+ TrieError :: InconsistentTree (
34+ crate :: InconsistentTreeError :: NodeNotFoundOnExtensionNode ( Box :: new (
35+ ExtensionNodeErrorData {
36+ node_hash : self . child . compute_hash ( ) . finalize ( ) ,
37+ extension_node_hash : self . compute_hash ( ) . finalize ( ) ,
38+ extension_node_prefix : self . prefix . clone ( ) ,
39+ node_path : path. current ( ) ,
40+ } ,
41+ ) ) ,
42+ )
43+ } ) ?;
3344
3445 child_node. get ( db, path)
3546 } else {
@@ -58,14 +69,23 @@ impl ExtensionNode {
5869 if match_index == self . prefix . len ( ) {
5970 let path = path. offset ( match_index) ;
6071 // Insert into child node
61- let child_node = self
62- . child
63- . get_node ( db, path. current ( ) ) ?
64- . ok_or ( TrieError :: InconsistentTree ) ?;
72+ let child_node = self . child . get_node ( db, path. current ( ) ) ?. ok_or_else ( || {
73+ TrieError :: InconsistentTree (
74+ crate :: InconsistentTreeError :: NodeNotFoundOnExtensionNode ( Box :: new (
75+ ExtensionNodeErrorData {
76+ node_hash : self . child . compute_hash ( ) . finalize ( ) ,
77+ extension_node_hash : self . compute_hash ( ) . finalize ( ) ,
78+ extension_node_prefix : self . prefix . clone ( ) ,
79+ node_path : path. current ( ) ,
80+ } ,
81+ ) ) ,
82+ )
83+ } ) ?;
6584 let new_child_node = child_node. insert ( db, path, value) ?;
6685 self . child = new_child_node. into ( ) ;
6786 Ok ( self . into ( ) )
6887 } else if match_index == 0 {
88+ let current_node_hash = self . compute_hash ( ) . finalize ( ) ;
6989 let new_node = if self . prefix . len ( ) == 1 {
7090 self . child
7191 } else {
@@ -75,7 +95,18 @@ impl ExtensionNode {
7595 let branch_node = if self . prefix . at ( 0 ) == 16 {
7696 match new_node. get_node ( db, path. current ( ) ) ? {
7797 Some ( Node :: Leaf ( leaf) ) => BranchNode :: new_with_value ( choices, leaf. value ) ,
78- _ => return Err ( TrieError :: InconsistentTree ) ,
98+ _ => {
99+ return Err ( TrieError :: InconsistentTree (
100+ crate :: InconsistentTreeError :: ExtensionNodeInsertionError ( Box :: new (
101+ ExtensionNodeErrorData {
102+ node_hash : new_node. compute_hash ( ) . finalize ( ) ,
103+ extension_node_hash : current_node_hash,
104+ extension_node_prefix : self . prefix ,
105+ node_path : path. current ( ) ,
106+ } ,
107+ ) ) ,
108+ ) ) ;
109+ }
79110 }
80111 } else {
81112 choices[ self . prefix . at ( 0 ) ] = new_node;
@@ -106,10 +137,18 @@ impl ExtensionNode {
106137
107138 // Check if the value is part of the child subtrie according to the prefix
108139 if path. skip_prefix ( & self . prefix ) {
109- let child_node = self
110- . child
111- . get_node ( db, path. current ( ) ) ?
112- . ok_or ( TrieError :: InconsistentTree ) ?;
140+ let child_node = self . child . get_node ( db, path. current ( ) ) ?. ok_or_else ( || {
141+ TrieError :: InconsistentTree (
142+ crate :: InconsistentTreeError :: NodeNotFoundOnExtensionNode ( Box :: new (
143+ ExtensionNodeErrorData {
144+ node_hash : self . child . compute_hash ( ) . finalize ( ) ,
145+ extension_node_hash : self . compute_hash ( ) . finalize ( ) ,
146+ extension_node_prefix : self . prefix . clone ( ) ,
147+ node_path : path. current ( ) ,
148+ } ,
149+ ) ) ,
150+ )
151+ } ) ?;
113152 // Remove value from child subtrie
114153 let ( child_node, old_value) = child_node. remove ( db, path) ?;
115154 // Restructure node based on removal
@@ -173,10 +212,18 @@ impl ExtensionNode {
173212 } ;
174213 // Continue to child
175214 if path. skip_prefix ( & self . prefix ) {
176- let child_node = self
177- . child
178- . get_node ( db, path. current ( ) ) ?
179- . ok_or ( TrieError :: InconsistentTree ) ?;
215+ let child_node = self . child . get_node ( db, path. current ( ) ) ?. ok_or_else ( || {
216+ TrieError :: InconsistentTree (
217+ crate :: InconsistentTreeError :: NodeNotFoundOnExtensionNode ( Box :: new (
218+ ExtensionNodeErrorData {
219+ node_hash : self . child . clone ( ) . compute_hash ( ) . finalize ( ) ,
220+ extension_node_hash : self . compute_hash ( ) . finalize ( ) ,
221+ extension_node_prefix : self . prefix . clone ( ) ,
222+ node_path : path. current ( ) ,
223+ } ,
224+ ) ) ,
225+ )
226+ } ) ?;
180227 child_node. get_path ( db, path, node_path) ?;
181228 }
182229 Ok ( ( ) )
0 commit comments