@@ -25,12 +25,11 @@ use reth_provider::{
2525 BlockExecutionWriter , BlockNumReader , BlockWriter , CanonStateNotification ,
2626 CanonStateNotificationSender , CanonStateNotifications , ChainSpecProvider , ChainSplit ,
2727 ChainSplitTarget , DBProvider , DisplayBlocksChain , HashedPostStateProvider , HeaderProvider ,
28- ProviderError , StaticFileProviderFactory , StorageLocation ,
28+ LatestStateProviderRef , ProviderError , StateRootProviderExt , StaticFileProviderFactory ,
29+ StorageLocation ,
2930} ;
3031use reth_stages_api:: { MetricEvent , MetricEventsSender } ;
3132use reth_storage_errors:: provider:: { ProviderResult , RootMismatch } ;
32- use reth_trie:: { hashed_cursor:: HashedPostStateCursorFactory , StateRoot } ;
33- use reth_trie_db:: { DatabaseHashedCursorFactory , DatabaseStateRoot } ;
3433use std:: {
3534 collections:: { btree_map:: Entry , BTreeMap , HashSet } ,
3635 sync:: Arc ,
@@ -1216,17 +1215,15 @@ where
12161215 ) -> Result < ( ) , CanonicalError > {
12171216 let ( blocks, state, chain_trie_updates) = chain. into_inner ( ) ;
12181217 let hashed_state = self . externals . provider_factory . hashed_post_state ( state. state ( ) ) ;
1219- let prefix_sets = hashed_state. construct_prefix_sets ( ) . freeze ( ) ;
1220- let hashed_state_sorted = hashed_state. into_sorted ( ) ;
12211218
12221219 // Compute state root or retrieve cached trie updates before opening write transaction.
12231220 let block_hash_numbers =
12241221 blocks. iter ( ) . map ( |( number, b) | ( number, b. hash ( ) ) ) . collect :: < Vec < _ > > ( ) ;
1225- let trie_updates = match chain_trie_updates {
1222+ let ( trie_updates, hashed_state_sorted ) = match chain_trie_updates {
12261223 Some ( updates) => {
12271224 debug ! ( target: "blockchain_tree" , blocks = ?block_hash_numbers, "Using cached trie updates" ) ;
12281225 self . metrics . trie_updates_insert_cached . increment ( 1 ) ;
1229- updates
1226+ ( updates, hashed_state . into_sorted ( ) )
12301227 }
12311228 None => {
12321229 debug ! ( target: "blockchain_tree" , blocks = ?block_hash_numbers, "Recomputing state root for insert" ) ;
@@ -1237,14 +1234,9 @@ where
12371234 // State root calculation can take a while, and we're sure no write transaction
12381235 // will be open in parallel. See https://github.com/paradigmxyz/reth/issues/6168.
12391236 . disable_long_read_transaction_safety ( ) ;
1240- let ( state_root, trie_updates) = StateRoot :: from_tx ( provider. tx_ref ( ) )
1241- . with_hashed_cursor_factory ( HashedPostStateCursorFactory :: new (
1242- DatabaseHashedCursorFactory :: new ( provider. tx_ref ( ) ) ,
1243- & hashed_state_sorted,
1244- ) )
1245- . with_prefix_sets ( prefix_sets)
1246- . root_with_updates ( )
1247- . map_err ( Into :: < BlockValidationError > :: into) ?;
1237+ let ( state_root, trie_updates, hashed_state_sorted) =
1238+ LatestStateProviderRef :: new ( & provider)
1239+ . state_root_from_state_with_updates_and_sorted_state ( hashed_state) ?;
12481240 let tip = blocks. tip ( ) ;
12491241 if state_root != tip. state_root {
12501242 return Err ( ProviderError :: StateRootMismatch ( Box :: new ( RootMismatch {
@@ -1255,7 +1247,7 @@ where
12551247 . into ( ) )
12561248 }
12571249 self . metrics . trie_updates_insert_recomputed . increment ( 1 ) ;
1258- trie_updates
1250+ ( trie_updates, hashed_state_sorted )
12591251 }
12601252 } ;
12611253 recorder. record_relative ( MakeCanonicalAction :: RetrieveStateTrieUpdates ) ;
@@ -1402,6 +1394,7 @@ mod tests {
14021394 } ;
14031395 use reth_stages_api:: StageCheckpoint ;
14041396 use reth_trie:: { root:: state_root_unhashed, StateRoot } ;
1397+ use reth_trie_db:: DatabaseStateRoot ;
14051398 use revm:: AccountInfo ;
14061399 use std:: collections:: HashMap ;
14071400
0 commit comments