@@ -2,18 +2,10 @@ mod branch;
22mod extension;
33mod leaf;
44
5- use std:: {
6- array,
7- sync:: { Arc , OnceLock } ,
8- } ;
5+ use std:: sync:: { Arc , OnceLock } ;
96
107pub use branch:: BranchNode ;
11- use ethrex_rlp:: {
12- decode:: { RLPDecode , decode_bytes} ,
13- encode:: RLPEncode ,
14- error:: RLPDecodeError ,
15- structs:: Decoder ,
16- } ;
8+ use ethrex_rlp:: { decode:: RLPDecode , encode:: RLPEncode } ;
179pub use extension:: ExtensionNode ;
1810pub use leaf:: LeafNode ;
1911
@@ -35,7 +27,7 @@ impl NodeRef {
3527 match * self {
3628 NodeRef :: Node ( ref node, _) => Ok ( Some ( node. as_ref ( ) . clone ( ) ) ) ,
3729 NodeRef :: Hash ( NodeHash :: Inline ( ( data, len) ) ) => {
38- Ok ( Some ( Node :: decode_raw ( & data[ ..len as usize ] ) ?) )
30+ Ok ( Some ( Node :: decode ( & data[ ..len as usize ] ) ?) )
3931 }
4032 NodeRef :: Hash ( hash) => db
4133 . get ( path) ?
@@ -69,11 +61,13 @@ impl NodeRef {
6961 }
7062 Node :: Leaf ( _) => { }
7163 }
72- let hash = * hash. get_or_init ( || node. compute_hash ( ) ) ;
64+ let mut buf = Vec :: new ( ) ;
65+ node. encode ( & mut buf) ;
66+ let hash = * hash. get_or_init ( || NodeHash :: from_encoded ( & buf) ) ;
67+ acc. push ( ( path. clone ( ) , buf) ) ;
7368 if let Node :: Leaf ( leaf) = node. as_ref ( ) {
7469 acc. push ( ( path. concat ( & leaf. partial ) , leaf. value . clone ( ) ) ) ;
7570 }
76- acc. push ( ( path. clone ( ) , node. encode_to_vec ( ) ) ) ;
7771
7872 * self = hash. into ( ) ;
7973
@@ -218,70 +212,6 @@ impl Node {
218212 }
219213 }
220214
221- /// Encodes the node
222- pub fn encode_raw ( & self ) -> Vec < u8 > {
223- match self {
224- Node :: Branch ( n) => n. encode_raw ( ) ,
225- Node :: Extension ( n) => n. encode_raw ( ) ,
226- Node :: Leaf ( n) => n. encode_raw ( ) ,
227- }
228- }
229-
230- /// Decodes the node
231- pub fn decode_raw ( rlp : & [ u8 ] ) -> Result < Self , RLPDecodeError > {
232- let mut rlp_items = vec ! [ ] ;
233- let mut decoder = Decoder :: new ( rlp) ?;
234- let mut item;
235- // Get encoded fields
236- loop {
237- ( item, decoder) = decoder. get_encoded_item ( ) ?;
238- rlp_items. push ( item) ;
239- // Check if we reached the end or if we decoded more items than the ones we need
240- if decoder. is_done ( ) || rlp_items. len ( ) > 17 {
241- break ;
242- }
243- }
244- // Deserialize into node depending on the available fields
245- Ok ( match rlp_items. len ( ) {
246- // Leaf or Extension Node
247- 2 => {
248- let ( path, _) = decode_bytes ( & rlp_items[ 0 ] ) ?;
249- let path = Nibbles :: decode_compact ( path) ;
250- if path. is_leaf ( ) {
251- // Decode as Leaf
252- let ( value, _) = decode_bytes ( & rlp_items[ 1 ] ) ?;
253- LeafNode {
254- partial : path,
255- value : value. to_vec ( ) ,
256- }
257- . into ( )
258- } else {
259- // Decode as Extension
260- ExtensionNode {
261- prefix : path,
262- child : decode_child ( & rlp_items[ 1 ] ) . into ( ) ,
263- }
264- . into ( )
265- }
266- }
267- // Branch Node
268- 17 => {
269- let choices = array:: from_fn ( |i| decode_child ( & rlp_items[ i] ) . into ( ) ) ;
270- let ( value, _) = decode_bytes ( & rlp_items[ 16 ] ) ?;
271- BranchNode {
272- choices,
273- value : value. to_vec ( ) ,
274- }
275- . into ( )
276- }
277- n => {
278- return Err ( RLPDecodeError :: Custom ( format ! (
279- "Invalid arg count for Node, expected 2 or 17, got {n}"
280- ) ) ) ;
281- }
282- } )
283- }
284-
285215 /// Computes the node's hash
286216 pub fn compute_hash ( & self ) -> NodeHash {
287217 match self {
@@ -291,11 +221,3 @@ impl Node {
291221 }
292222 }
293223}
294-
295- fn decode_child ( rlp : & [ u8 ] ) -> NodeHash {
296- match decode_bytes ( rlp) {
297- Ok ( ( hash, & [ ] ) ) if hash. len ( ) == 32 => NodeHash :: from_slice ( hash) ,
298- Ok ( ( & [ ] , & [ ] ) ) => NodeHash :: default ( ) ,
299- _ => NodeHash :: from_slice ( rlp) ,
300- }
301- }
0 commit comments