@@ -404,119 +404,74 @@ impl LEVM {
404
404
. map_err ( Box :: new) ?
405
405
. account_updates ;
406
406
407
- // index read and touched account addresses and storage keys
408
- let account_accessed = logger_ref
409
- . accounts_accessed
407
+ // index accessed account addresses and storage keys
408
+ let state_accessed = logger_ref
409
+ . state_accessed
410
410
. lock ( )
411
411
. map_err ( |_| {
412
412
ExecutionDBError :: Store ( StoreError :: Custom ( "Could not lock mutex" . to_string ( ) ) )
413
413
} ) ?
414
414
. clone ( ) ;
415
415
416
- let mut index = Vec :: with_capacity ( account_accessed. len ( ) ) ;
417
- for address in account_accessed. iter ( ) {
418
- let storage_keys = logger_ref
419
- . storage_accessed
420
- . lock ( )
421
- . map_err ( |_| {
422
- ExecutionDBError :: Store ( StoreError :: Custom ( "Could not lock mutex" . to_string ( ) ) )
423
- } ) ?
424
- . iter ( )
425
- . filter_map (
426
- |( ( addr, key) , _) | {
427
- if * addr == * address {
428
- Some ( key)
429
- } else {
430
- None
431
- }
432
- } ,
433
- )
434
- . map ( |key| H256 :: from_slice ( & key. to_fixed_bytes ( ) ) )
435
- . collect :: < Vec < _ > > ( ) ;
436
- index. push ( ( address, storage_keys) ) ;
437
- }
416
+ // fetch all read/written accounts from store
417
+ let accounts = state_accessed
418
+ . keys ( )
419
+ . chain ( execution_updates. iter ( ) . map ( |update| & update. address ) )
420
+ . filter_map ( |address| {
421
+ store
422
+ . get_account_info_by_hash ( parent_hash, * address)
423
+ . transpose ( )
424
+ . map ( |account| Ok ( ( * address, account?) ) )
425
+ } )
426
+ . collect :: < Result < HashMap < _ , _ > , ExecutionDBError > > ( ) ?;
438
427
439
- // fetch all read/written values from store
440
- let cache_accounts = account_accessed. iter ( ) . filter_map ( |address| {
441
- // filter new accounts (accounts that didn't exist before) assuming our store is
442
- // correct (based on the success of the pre-execution).
443
- if let Ok ( Some ( info) ) = db. store . get_account_info_by_hash ( parent_hash, * address) {
444
- Some ( ( address, info) )
428
+ // fetch all read/written code from store
429
+ let code_accessed = logger_ref
430
+ . code_accessed
431
+ . lock ( )
432
+ . map_err ( |_| {
433
+ ExecutionDBError :: Store ( StoreError :: Custom ( "Could not lock mutex" . to_string ( ) ) )
434
+ } ) ?
435
+ . clone ( ) ;
436
+ let code = accounts
437
+ . values ( )
438
+ . map ( |account| account. code_hash )
439
+ . chain ( code_accessed. into_iter ( ) )
440
+ . filter_map ( |hash| {
441
+ store
442
+ . get_account_code ( hash)
443
+ . transpose ( )
444
+ . map ( |account| Ok ( ( hash, account?) ) )
445
+ } )
446
+ . collect :: < Result < HashMap < _ , _ > , ExecutionDBError > > ( ) ?;
447
+
448
+ // fetch all read/written storage from store
449
+ let added_storage = execution_updates. iter ( ) . filter_map ( |update| {
450
+ if !update. added_storage . is_empty ( ) {
451
+ let keys = update. added_storage . keys ( ) . cloned ( ) . collect :: < Vec < _ > > ( ) ;
452
+ Some ( ( update. address , keys) )
445
453
} else {
446
454
None
447
455
}
448
456
} ) ;
449
- let accounts = cache_accounts
457
+ let storage = state_accessed
450
458
. clone ( )
451
- . map ( |( address, _) | {
452
- // return error if account is missing
453
- let account = match db. store . get_account_info_by_hash ( parent_hash, * address) {
454
- Ok ( Some ( some) ) => Ok ( some) ,
455
- Err ( err) => Err ( ExecutionDBError :: Database ( err) ) ,
456
- Ok ( None ) => unreachable ! ( ) , // we are filtering out accounts that are not present
457
- // in the store
458
- } ;
459
- Ok ( ( * address, account?) )
459
+ . into_iter ( )
460
+ . chain ( added_storage)
461
+ . map ( |( address, keys) | {
462
+ let keys: Result < HashMap < _ , _ > , ExecutionDBError > = keys
463
+ . iter ( )
464
+ . filter_map ( |key| {
465
+ store
466
+ . get_storage_at_hash ( parent_hash, address, * key)
467
+ . transpose ( )
468
+ . map ( |value| Ok ( ( * key, value?) ) )
469
+ } )
470
+ . collect ( ) ;
471
+ Ok ( ( address, keys?) )
460
472
} )
461
473
. collect :: < Result < HashMap < _ , _ > , ExecutionDBError > > ( ) ?;
462
- let mut code: HashMap < H256 , Bytes > = execution_updates
463
- . clone ( )
464
- . iter ( )
465
- . map ( |update| {
466
- // return error if code is missing
467
- let hash = update. info . clone ( ) . unwrap_or_default ( ) . code_hash ;
468
- Ok ( (
469
- hash,
470
- db. store
471
- . get_account_code ( hash) ?
472
- . ok_or ( ExecutionDBError :: NewMissingCode ( hash) ) ?,
473
- ) )
474
- } )
475
- . collect :: < Result < _ , ExecutionDBError > > ( ) ?;
476
-
477
- let mut code_accessed = HashMap :: new ( ) ;
478
-
479
- {
480
- let all_code_accessed = logger_ref. code_accessed . lock ( ) . map_err ( |_| {
481
- ExecutionDBError :: Store ( StoreError :: Custom ( "Could not lock mutex" . to_string ( ) ) )
482
- } ) ?;
483
- for code_hash in all_code_accessed. iter ( ) {
484
- code_accessed. insert (
485
- * code_hash,
486
- store
487
- . get_account_code ( * code_hash) ?
488
- . ok_or ( ExecutionDBError :: CodeNotFound ( code_hash. 0 . into ( ) ) ) ?
489
- . clone ( ) ,
490
- ) ;
491
- }
492
- code. extend ( code_accessed) ;
493
- }
494
474
495
- let storage = execution_updates
496
- . iter ( )
497
- . map ( |update| {
498
- // return error if storage is missing
499
- Ok ( (
500
- update. address ,
501
- update
502
- . added_storage
503
- . keys ( )
504
- . map ( |key| {
505
- let key = H256 :: from ( key. to_fixed_bytes ( ) ) ;
506
- let value = store
507
- . get_storage_at_hash (
508
- block. header . compute_block_hash ( ) ,
509
- update. address ,
510
- key,
511
- )
512
- . map_err ( ExecutionDBError :: Store ) ?
513
- . ok_or ( ExecutionDBError :: NewMissingStorage ( update. address , key) ) ?;
514
- Ok ( ( key, value) )
515
- } )
516
- . collect :: < Result < HashMap < _ , _ > , ExecutionDBError > > ( ) ?,
517
- ) )
518
- } )
519
- . collect :: < Result < HashMap < _ , _ > , ExecutionDBError > > ( ) ?;
520
475
let block_hashes = logger_ref
521
476
. block_hashes_accessed
522
477
. lock ( )
@@ -528,23 +483,14 @@ impl LEVM {
528
483
. map ( |( num, hash) | ( num, H256 :: from ( hash. 0 ) ) )
529
484
. collect ( ) ;
530
485
531
- let new_store = store. clone ( ) ;
532
- new_store
533
- . apply_account_updates ( block. hash ( ) , & execution_updates)
534
- . await ?;
535
-
536
486
// get account proofs
537
- let state_trie = new_store
487
+ let state_trie = store
538
488
. state_trie ( block. hash ( ) ) ?
539
489
. ok_or ( ExecutionDBError :: NewMissingStateTrie ( parent_hash) ) ?;
540
490
let parent_state_trie = store
541
491
. state_trie ( parent_hash) ?
542
492
. ok_or ( ExecutionDBError :: NewMissingStateTrie ( parent_hash) ) ?;
543
- let hashed_addresses: Vec < _ > = index
544
- . clone ( )
545
- . into_iter ( )
546
- . map ( |( address, _) | hash_address ( address) )
547
- . collect ( ) ;
493
+ let hashed_addresses: Vec < _ > = state_accessed. keys ( ) . map ( hash_address) . collect ( ) ;
548
494
let initial_state_proofs = parent_state_trie. get_proofs ( & hashed_addresses) ?;
549
495
let final_state_proofs: Vec < _ > = hashed_addresses
550
496
. iter ( )
@@ -563,14 +509,14 @@ impl LEVM {
563
509
// get storage proofs
564
510
let mut storage_proofs = HashMap :: new ( ) ;
565
511
let mut final_storage_proofs = HashMap :: new ( ) ;
566
- for ( address, storage_keys) in index {
567
- let Some ( parent_storage_trie) = store. storage_trie ( parent_hash, * address) ? else {
512
+ for ( address, storage_keys) in state_accessed {
513
+ let Some ( parent_storage_trie) = store. storage_trie ( parent_hash, address) ? else {
568
514
// the storage of this account was empty or the account is newly created, either
569
515
// way the storage trie was initially empty so there aren't any proofs to add.
570
516
continue ;
571
517
} ;
572
- let storage_trie = new_store . storage_trie ( block. hash ( ) , * address) ?. ok_or (
573
- ExecutionDBError :: NewMissingStorageTrie ( block. hash ( ) , * address) ,
518
+ let storage_trie = store . storage_trie ( block. hash ( ) , address) ?. ok_or (
519
+ ExecutionDBError :: NewMissingStorageTrie ( block. hash ( ) , address) ,
574
520
) ?;
575
521
let paths = storage_keys. iter ( ) . map ( hash_key) . collect :: < Vec < _ > > ( ) ;
576
522
@@ -594,7 +540,7 @@ impl LEVM {
594
540
[ initial_proofs. 1 , potential_child_nodes] . concat ( ) ,
595
541
) ;
596
542
597
- storage_proofs. insert ( * address, proofs) ;
543
+ storage_proofs. insert ( address, proofs) ;
598
544
final_storage_proofs. insert ( address, final_proofs) ;
599
545
}
600
546
0 commit comments