@@ -13,7 +13,7 @@ use super::error::{check_node_hash, StateValidationError};
13
13
14
14
/// Validate the trie proof.
15
15
pub fn validate_node_trie_proof (
16
- root_hash : B256 ,
16
+ root_hash : Option < B256 > ,
17
17
node_hash : B256 ,
18
18
path : & Nibbles ,
19
19
proof : & TrieProof ,
@@ -33,7 +33,7 @@ pub fn validate_node_trie_proof(
33
33
34
34
/// Validate the trie proof associated with the account state.
35
35
pub fn validate_account_state (
36
- root_hash : B256 ,
36
+ root_hash : Option < B256 > ,
37
37
address_hash : & B256 ,
38
38
proof : & TrieProof ,
39
39
) -> Result < AccountState , StateValidationError > {
@@ -58,7 +58,7 @@ pub fn validate_account_state(
58
58
/// - last node in the proof (whose presence we are proving)
59
59
/// - remaining (unused) nibbles from the path
60
60
fn validate_trie_proof < ' proof , ' path > (
61
- root_hash : B256 ,
61
+ root_hash : Option < B256 > ,
62
62
path : & ' path [ u8 ] ,
63
63
proof : & ' proof TrieProof ,
64
64
) -> Result < ( & ' proof EncodedTrieNode , & ' path [ u8 ] ) , StateValidationError > {
@@ -68,7 +68,9 @@ fn validate_trie_proof<'proof, 'path>(
68
68
} ;
69
69
70
70
// Check root hash
71
- check_node_hash ( first_node, & root_hash) ?;
71
+ if let Some ( root_hash) = & root_hash {
72
+ check_node_hash ( first_node, root_hash) ?;
73
+ }
72
74
73
75
let mut node = first_node;
74
76
let mut remaining_path = path;
@@ -160,7 +162,7 @@ mod tests {
160
162
let path = Nibbles :: try_from_unpacked_nibbles ( & [ ] ) . unwrap ( ) ;
161
163
let proof = TrieProof :: from ( vec ! [ node. clone( ) ] ) ;
162
164
163
- assert ! ( validate_node_trie_proof( node_hash, node_hash, & path, & proof) . is_ok( ) ) ;
165
+ assert ! ( validate_node_trie_proof( Some ( node_hash) , node_hash, & path, & proof) . is_ok( ) ) ;
164
166
}
165
167
166
168
#[ test]
@@ -175,7 +177,7 @@ mod tests {
175
177
let proof = TrieProof :: from ( vec ! [ root_node. clone( ) , last_node. clone( ) ] ) ;
176
178
177
179
assert ! ( validate_node_trie_proof(
178
- root_node. node_hash( ) ,
180
+ Some ( root_node. node_hash( ) ) ,
179
181
last_node. node_hash( ) ,
180
182
& path,
181
183
& proof,
@@ -208,7 +210,7 @@ mod tests {
208
210
] ) ;
209
211
210
212
assert ! ( validate_node_trie_proof(
211
- root_node. node_hash( ) ,
213
+ Some ( root_node. node_hash( ) ) ,
212
214
last_node. node_hash( ) ,
213
215
& path,
214
216
& proof,
@@ -228,8 +230,13 @@ mod tests {
228
230
let path = Nibbles :: try_from_unpacked_nibbles ( & [ 4 , 3 , 2 , 1 ] ) . unwrap ( ) ;
229
231
let proof = TrieProof :: from ( vec ! [ root_node. clone( ) , last_node. clone( ) ] ) ;
230
232
231
- validate_node_trie_proof ( root_node. node_hash ( ) , last_node. node_hash ( ) , & path, & proof)
232
- . unwrap ( ) ;
233
+ validate_node_trie_proof (
234
+ Some ( root_node. node_hash ( ) ) ,
235
+ last_node. node_hash ( ) ,
236
+ & path,
237
+ & proof,
238
+ )
239
+ . unwrap ( ) ;
233
240
}
234
241
235
242
#[ test]
@@ -246,8 +253,13 @@ mod tests {
246
253
247
254
// This should be the hash of the last_node, and it should fail if it is anything else
248
255
let wrong_last_node_hash = root_node. node_hash ( ) ;
249
- validate_node_trie_proof ( root_node. node_hash ( ) , wrong_last_node_hash, & path, & proof)
250
- . unwrap ( ) ;
256
+ validate_node_trie_proof (
257
+ Some ( root_node. node_hash ( ) ) ,
258
+ wrong_last_node_hash,
259
+ & path,
260
+ & proof,
261
+ )
262
+ . unwrap ( ) ;
251
263
}
252
264
253
265
#[ test]
@@ -280,7 +292,11 @@ mod tests {
280
292
] . map ( |trie_node| EncodedTrieNode :: from ( hex_decode ( trie_node) . unwrap ( ) ) ) ;
281
293
282
294
assert_eq ! (
283
- validate_account_state( state_root, & address_hash, & account_proof. to_vec( ) . into( ) ) ?,
295
+ validate_account_state(
296
+ Some ( state_root) ,
297
+ & address_hash,
298
+ & account_proof. to_vec( ) . into( )
299
+ ) ?,
284
300
account_state
285
301
) ;
286
302
@@ -299,7 +315,8 @@ mod tests {
299
315
} ;
300
316
let node = EncodedTrieNode :: from ( & create_leaf ( & path, & alloy:: rlp:: encode ( & account_state) ) ) ;
301
317
assert_eq ! (
302
- validate_account_state( node. node_hash( ) , & address_hash, & vec![ node] . into( ) ) . unwrap( ) ,
318
+ validate_account_state( Some ( node. node_hash( ) ) , & address_hash, & vec![ node] . into( ) )
319
+ . unwrap( ) ,
303
320
account_state
304
321
) ;
305
322
}
@@ -309,7 +326,7 @@ mod tests {
309
326
fn validate_account_state_last_node_is_not_leaf ( ) {
310
327
let address_hash = B256 :: random ( ) ;
311
328
let node = EncodedTrieNode :: from ( & create_branch ( ) ) ;
312
- validate_account_state ( node. node_hash ( ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
329
+ validate_account_state ( Some ( node. node_hash ( ) ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
313
330
}
314
331
315
332
#[ test]
@@ -327,7 +344,7 @@ mod tests {
327
344
code_hash : B256 :: random ( ) ,
328
345
} ;
329
346
let node = EncodedTrieNode :: from ( & create_leaf ( & path, & alloy:: rlp:: encode ( account_state) ) ) ;
330
- validate_account_state ( node. node_hash ( ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
347
+ validate_account_state ( Some ( node. node_hash ( ) ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
331
348
}
332
349
333
350
#[ test]
@@ -336,15 +353,15 @@ mod tests {
336
353
let address_hash = B256 :: random ( ) ;
337
354
let path = Nibbles :: unpack_nibbles ( address_hash. as_slice ( ) ) ;
338
355
let node = EncodedTrieNode :: from ( & create_leaf ( & path, & [ 0x12 , 0x34 ] ) ) ;
339
- validate_account_state ( node. node_hash ( ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
356
+ validate_account_state ( Some ( node. node_hash ( ) ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
340
357
}
341
358
342
359
#[ test]
343
360
#[ should_panic = "DecodingNode" ]
344
361
fn validate_account_state_non_decodable_leaf ( ) {
345
362
let address_hash = B256 :: random ( ) ;
346
363
let node = EncodedTrieNode :: from ( vec ! [ 0x12 , 0x34 ] ) ;
347
- validate_account_state ( node. node_hash ( ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
364
+ validate_account_state ( Some ( node. node_hash ( ) ) , & address_hash, & vec ! [ node] . into ( ) ) . unwrap ( ) ;
348
365
}
349
366
350
367
#[ test]
@@ -356,7 +373,7 @@ mod tests {
356
373
357
374
let proof = TrieProof :: from ( vec ! [ node. clone( ) ] ) ;
358
375
let path = [ 2 , 3 , 4 ] ;
359
- let validation_info = validate_trie_proof ( node. node_hash ( ) , & path, & proof) . unwrap ( ) ;
376
+ let validation_info = validate_trie_proof ( Some ( node. node_hash ( ) ) , & path, & proof) . unwrap ( ) ;
360
377
361
378
assert_eq ! ( validation_info, ( & node, path. as_slice( ) ) ) ;
362
379
}
@@ -370,7 +387,7 @@ mod tests {
370
387
371
388
let proof = TrieProof :: from ( vec ! [ node. clone( ) ] ) ;
372
389
let path = [ 2 , 3 , 4 ] ;
373
- let validation_info = validate_trie_proof ( node. node_hash ( ) , & path, & proof) . unwrap ( ) ;
390
+ let validation_info = validate_trie_proof ( Some ( node. node_hash ( ) ) , & path, & proof) . unwrap ( ) ;
374
391
375
392
assert_eq ! ( validation_info, ( & node, path. as_slice( ) ) ) ;
376
393
}
@@ -398,7 +415,8 @@ mod tests {
398
415
branch_node,
399
416
last_node. clone( ) ,
400
417
] ) ;
401
- let validation_info = validate_trie_proof ( root_node. node_hash ( ) , & path, & proof) . unwrap ( ) ;
418
+ let validation_info =
419
+ validate_trie_proof ( Some ( root_node. node_hash ( ) ) , & path, & proof) . unwrap ( ) ;
402
420
403
421
assert_eq ! ( validation_info, ( & last_node, & path[ 4 ..] ) ) ;
404
422
}
@@ -416,11 +434,11 @@ mod tests {
416
434
417
435
// First verify that correct order pass the validation
418
436
let proof = TrieProof :: from ( vec ! [ root_node. clone( ) , last_node. clone( ) ] ) ;
419
- assert ! ( validate_trie_proof( root_node. node_hash( ) , & [ 2 , 1 ] , & proof) . is_ok( ) ) ;
437
+ assert ! ( validate_trie_proof( Some ( root_node. node_hash( ) ) , & [ 2 , 1 ] , & proof) . is_ok( ) ) ;
420
438
421
439
// Now verify that wrong order fails because root hash doesn't match
422
440
let proof = TrieProof :: from ( vec ! [ last_node. clone( ) , root_node. clone( ) ] ) ;
423
- let error = validate_trie_proof ( root_node. node_hash ( ) , & [ 2 , 1 ] , & proof) . unwrap_err ( ) ;
441
+ let error = validate_trie_proof ( Some ( root_node. node_hash ( ) ) , & [ 2 , 1 ] , & proof) . unwrap_err ( ) ;
424
442
assert ! ( matches!(
425
443
error,
426
444
StateValidationError :: InvalidNodeHash {
@@ -431,7 +449,7 @@ mod tests {
431
449
432
450
// And that if fails even if root hash that corresponds to the first node is given
433
451
let proof = TrieProof :: from ( vec ! [ last_node. clone( ) , root_node. clone( ) ] ) ;
434
- let error = validate_trie_proof ( last_node. node_hash ( ) , & [ 2 , 1 ] , & proof) . unwrap_err ( ) ;
452
+ let error = validate_trie_proof ( Some ( last_node. node_hash ( ) ) , & [ 2 , 1 ] , & proof) . unwrap_err ( ) ;
435
453
dbg ! ( & error) ;
436
454
dbg ! ( last_node. node_hash( ) ) ;
437
455
assert ! ( matches!(
@@ -443,7 +461,7 @@ mod tests {
443
461
) ) ;
444
462
445
463
// And that it fails even if we reverse order in path
446
- let error = validate_trie_proof ( last_node. node_hash ( ) , & [ 1 , 2 ] , & proof) . unwrap_err ( ) ;
464
+ let error = validate_trie_proof ( Some ( last_node. node_hash ( ) ) , & [ 1 , 2 ] , & proof) . unwrap_err ( ) ;
447
465
assert ! ( matches!(
448
466
error,
449
467
StateValidationError :: InvalidNodeHash {
@@ -456,14 +474,14 @@ mod tests {
456
474
#[ test]
457
475
#[ should_panic = "EmptyTrieProof" ]
458
476
fn validate_trie_proof_empty_proof ( ) {
459
- validate_trie_proof ( B256 :: random ( ) , & [ ] , & TrieProof :: default ( ) ) . unwrap ( ) ;
477
+ validate_trie_proof ( Some ( B256 :: random ( ) ) , & [ ] , & TrieProof :: default ( ) ) . unwrap ( ) ;
460
478
}
461
479
462
480
#[ test]
463
481
#[ should_panic = "InvalidNodeHash" ]
464
482
fn validate_trie_proof_invalid_root_hash ( ) {
465
483
let node = Node :: from_branch ( empty_children ( ) , None ) ;
466
484
let proof = TrieProof :: from ( vec ! [ EncodedTrieNode :: from( & node) ] ) ;
467
- validate_trie_proof ( B256 :: random ( ) , & [ ] , & proof) . unwrap ( ) ;
485
+ validate_trie_proof ( Some ( B256 :: random ( ) ) , & [ ] , & proof) . unwrap ( ) ;
468
486
}
469
487
}
0 commit comments