@@ -11,7 +11,7 @@ use std::{
1111 time:: Duration ,
1212} ;
1313
14- use alloy_consensus:: BlockHeader ;
14+ use alloy_consensus:: { BlockHeader , transaction :: TxHashRef } ;
1515use alloy_eips:: eip2935:: { HISTORY_SERVE_WINDOW , HISTORY_STORAGE_ADDRESS } ;
1616use alloy_network:: { ReceiptResponse , TransactionBuilder , TransactionResponse } ;
1717use alloy_primitives:: { Address , B256 , Bloom , Bytes , U64 , U256 , keccak256} ;
@@ -37,7 +37,7 @@ use reth_rpc_eth_api::{
3737} ;
3838use reth_rpc_eth_types:: { EthApiError , logs_utils} ;
3939use reth_storage_api:: { BlockNumReader , StateProviderFactory } ;
40- use reth_trie_common:: { ExecutionWitnessMode , HashedStorage } ;
40+ use reth_trie_common:: { ExecutionWitnessMode , HashedPostState } ;
4141use tempo_alloy:: {
4242 TempoNetwork ,
4343 provider:: ext:: TempoProviderExt as _,
@@ -286,17 +286,27 @@ where
286286 let ( evm_config, recorder) = eth_api. evm_config ( ) . with_l1_storage_recorder ( ) ;
287287 let block_executor = evm_config. executor ( & mut db) ;
288288 let mode = ExecutionWitnessMode :: default ( ) ;
289- let mut witness_record = ExecutionWitnessRecord :: default ( ) ;
289+ let mut witness = None ;
290290
291291 let _ = block_executor
292292 . execute_with_state_closure ( & block, |statedb : & State < _ > | {
293- witness_record. record_executed_state ( statedb, mode) ;
294- record_block_hash_storage_proofs ( & mut witness_record, statedb) ;
293+ let mut additional_state = HashedPostState :: default ( ) ;
294+ record_block_hash_storage_proofs ( & mut additional_state, statedb) ;
295+ witness = Some (
296+ ExecutionWitnessRecord :: new ( statedb)
297+ . with_additional_state ( additional_state)
298+ . into_execution_witness (
299+ & statedb. database . database . 0 ,
300+ eth_api. provider ( ) ,
301+ block_number,
302+ mode,
303+ ) ,
304+ ) ;
295305 } )
296306 . map_err ( |error| EthApiError :: Internal ( error. into ( ) ) ) ?;
297307
298- let witness = witness_record
299- . into_execution_witness ( & db . database . 0 , eth_api . provider ( ) , block_number , mode )
308+ let witness = witness
309+ . expect ( "state closure is called after successful execution" )
300310 . map_err ( EthApiError :: from) ?;
301311 Ok ( ZoneExecutionWitness {
302312 execution_witness : witness,
@@ -320,17 +330,16 @@ where
320330/// Reth records these reads in REVM's block-hash cache and normally proves them with ancestor
321331/// headers. Zones already commit the EIP-2935 history contract in state, so adding the matching
322332/// storage targets lets the SPF authenticate the same values against the parent state root.
323- fn record_block_hash_storage_proofs < DB > ( witness : & mut ExecutionWitnessRecord , state : & State < DB > ) {
333+ fn record_block_hash_storage_proofs < DB > ( additional_state : & mut HashedPostState , state : & State < DB > ) {
324334 let block_hashes = state. block_hashes . iter ( ) . collect :: < Vec < _ > > ( ) ;
325335 if block_hashes. is_empty ( ) {
326336 return ;
327337 }
328338
329- let history_storage = witness
330- . hashed_state
339+ let history_storage = additional_state
331340 . storages
332341 . entry ( keccak256 ( HISTORY_STORAGE_ADDRESS ) )
333- . or_insert_with ( || HashedStorage :: new ( false ) ) ;
342+ . or_default ( ) ;
334343 for ( number, hash) in block_hashes {
335344 let slot = U256 :: from ( number % HISTORY_SERVE_WINDOW as u64 ) ;
336345 history_storage. storage . insert (
@@ -1187,6 +1196,7 @@ where
11871196 fn ws_subscribe_logs ( & self , mut filter : Filter , auth : AuthContext ) -> BoxWsSubscriptionFut < ' _ > {
11881197 Box :: pin ( async move {
11891198 let provider = self . eth . api . provider ( ) . clone ( ) ;
1199+ let api = self . eth . api . clone ( ) ;
11901200 let caller = auth. caller ;
11911201
11921202 let zone_tokens = self . zone_tokens ( ) ;
@@ -1195,18 +1205,36 @@ where
11951205
11961206 let stream = provider
11971207 . canonical_state_stream ( )
1198- . flat_map ( |canon_state| futures:: stream:: iter ( canon_state. block_receipts ( ) ) )
1199- . flat_map ( move |( block_receipts, removed) | {
1200- let all_logs = logs_utils:: matching_block_logs_with_tx_hashes (
1201- & filter,
1202- block_receipts. block ,
1203- block_receipts. timestamp ,
1204- block_receipts
1205- . tx_receipts
1206- . iter ( )
1207- . map ( |( tx, receipt) | ( * tx, receipt) ) ,
1208- removed,
1209- ) ;
1208+ . flat_map ( move |canon_state| {
1209+ let reverted_chains = canon_state. reverted ( ) ;
1210+ let committed_chain = canon_state. committed ( ) ;
1211+ let reverted = reverted_chains. iter ( ) . flat_map ( |chain| {
1212+ chain
1213+ . blocks_and_receipts ( )
1214+ . map ( |( block, receipts) | ( block, receipts, true ) )
1215+ } ) ;
1216+ let committed = committed_chain
1217+ . blocks_and_receipts ( )
1218+ . map ( |( block, receipts) | ( block, receipts, false ) ) ;
1219+ let mut all_logs = Vec :: new ( ) ;
1220+
1221+ for ( block, receipts, removed) in reverted. chain ( committed) {
1222+ match logs_utils:: matching_block_logs_with_tx_hashes (
1223+ api. converter ( ) ,
1224+ & filter,
1225+ block. sealed_header ( ) ,
1226+ block
1227+ . transactions_recovered ( )
1228+ . zip ( receipts. iter ( ) )
1229+ . map ( |( tx, receipt) | ( * tx. tx_hash ( ) , receipt) ) ,
1230+ removed,
1231+ ) {
1232+ Ok ( logs) => all_logs. extend ( logs) ,
1233+ Err ( error) => {
1234+ tracing:: error!( target: "rpc" , %error, "Failed to convert logs" ) ;
1235+ }
1236+ }
1237+ }
12101238 futures:: stream:: iter ( all_logs)
12111239 } ) ;
12121240
@@ -1484,12 +1512,11 @@ mod tests {
14841512 . with_database ( revm:: database:: EmptyDB :: default ( ) )
14851513 . build ( ) ;
14861514 state. block_hashes . insert ( number, hash) ;
1487- let mut witness = ExecutionWitnessRecord :: default ( ) ;
1515+ let mut additional_state = HashedPostState :: default ( ) ;
14881516
1489- record_block_hash_storage_proofs ( & mut witness , & state) ;
1517+ record_block_hash_storage_proofs ( & mut additional_state , & state) ;
14901518
1491- let storage = witness
1492- . hashed_state
1519+ let storage = additional_state
14931520 . storages
14941521 . get ( & keccak256 ( HISTORY_STORAGE_ADDRESS ) )
14951522 . unwrap ( ) ;
0 commit comments