11use alloy_primitives:: { utils:: format_ether, Address , TxHash , I256 , U256 } ;
2- use reth_provider:: StateProvider ;
32use std:: {
43 cmp:: max,
5- sync:: Arc ,
64 time:: { Duration , Instant } ,
75} ;
86use time:: OffsetDateTime ;
@@ -20,6 +18,7 @@ use crate::{
2018 ThreadBlockBuildingContext ,
2119 } ,
2220 live_builder:: block_output:: bidding_service_interface:: CompetitionBidContext ,
21+ provider:: StateProviderSource ,
2322 telemetry:: { self , add_block_fill_time, add_order_simulation_time} ,
2423 utils:: { check_block_hash_reader_health, elapsed_ms, HistoricalBlockError } ,
2524} ;
@@ -37,8 +36,9 @@ use super::Block;
3736/// 2 - Call lots of commit_order.
3837/// 3 - Call set_trace_fill_time when you are done calling commit_order (we still have to review this step).
3938/// 4 - Call finalize_block.
40- pub trait BlockBuildingHelper : Send + Sync {
41- fn box_clone ( & self ) -> Box < dyn BlockBuildingHelper > ;
39+ pub trait BlockBuildingHelper : Send {
40+ /// Clones the helper, reopening its state provider. Fallible because reopening can fail.
41+ fn try_clone ( & self ) -> Result < Box < dyn BlockBuildingHelper > , BlockBuildingHelperError > ;
4242
4343 /// Tries to add an order to the end of the block.
4444 /// Block state changes only on Ok(Ok)
@@ -112,16 +112,6 @@ pub struct BiddableUnfinishedBlock {
112112 pub chosen_as_best_at : OffsetDateTime ,
113113}
114114
115- impl Clone for BiddableUnfinishedBlock {
116- fn clone ( & self ) -> Self {
117- Self {
118- block : self . block . box_clone ( ) ,
119- true_block_value : self . true_block_value ,
120- chosen_as_best_at : self . chosen_as_best_at ,
121- }
122- }
123- }
124-
125115impl BiddableUnfinishedBlock {
126116 pub fn new ( block : Box < dyn BlockBuildingHelper > ) -> Result < Self , BlockBuildingHelperError > {
127117 let true_block_value = block. true_block_value ( ) ?;
@@ -131,6 +121,16 @@ impl BiddableUnfinishedBlock {
131121 chosen_as_best_at : OffsetDateTime :: now_utc ( ) ,
132122 } )
133123 }
124+
125+ /// Clones the block by reopening the underlying state provider. Fallible because reopening the
126+ /// provider can fail.
127+ pub fn try_clone ( & self ) -> Result < Self , BlockBuildingHelperError > {
128+ Ok ( Self {
129+ block : self . block . try_clone ( ) ?,
130+ true_block_value : self . true_block_value ,
131+ chosen_as_best_at : self . chosen_as_best_at ,
132+ } )
133+ }
134134 pub fn id ( & self ) -> BuiltBlockId {
135135 self . block . id ( )
136136 }
@@ -145,12 +145,13 @@ impl BiddableUnfinishedBlock {
145145}
146146
147147/// Implementation of BlockBuildingHelper based on a generic Provider
148- #[ derive( Clone ) ]
149148pub struct BlockBuildingHelperFromProvider <
150149 PartialBlockExecutionTracerType : PartialBlockExecutionTracer + Clone + Send + Sync + ' static ,
151150> {
152151 /// Balance of fee recipient before we stared building.
153152 _fee_recipient_balance_start : U256 ,
153+ /// Reopens a fresh state provider when the helper is cloned for bidding/finalization.
154+ source : StateProviderSource ,
154155 /// Accumulated changes for the block (due to commit_order calls).
155156 block_state : BlockState < CachedDB > ,
156157 partial_block : PartialBlock < GasUsedSimulationTracer , PartialBlockExecutionTracerType > ,
@@ -215,7 +216,7 @@ impl BlockBuildingHelperFromProvider<NullPartialBlockExecutionTracer> {
215216 pub fn new (
216217 built_block_id : BuiltBlockId ,
217218 next_journal_sequence_number : JournalSequenceNumber ,
218- state_provider : Arc < dyn StateProvider + Send + Sync > ,
219+ state_provider_source : StateProviderSource ,
219220 building_ctx : BlockBuildingContext ,
220221 builder_name : String ,
221222 discard_txs : bool ,
@@ -226,7 +227,7 @@ impl BlockBuildingHelperFromProvider<NullPartialBlockExecutionTracer> {
226227 BlockBuildingHelperFromProvider :: new_with_execution_tracer (
227228 built_block_id,
228229 next_journal_sequence_number,
229- state_provider ,
230+ state_provider_source ,
230231 building_ctx,
231232 builder_name,
232233 discard_txs,
@@ -251,7 +252,7 @@ impl<
251252 pub fn new_with_execution_tracer (
252253 built_block_id : BuiltBlockId ,
253254 next_journal_sequence_number : JournalSequenceNumber ,
254- state_provider : Arc < dyn StateProvider + Send + Sync > ,
255+ state_provider_source : StateProviderSource ,
255256 building_ctx : BlockBuildingContext ,
256257 builder_name : String ,
257258 discard_txs : bool ,
@@ -260,6 +261,7 @@ impl<
260261 partial_block_execution_tracer : PartialBlockExecutionTracerType ,
261262 max_order_execution_duration_warning : Option < Duration > ,
262263 ) -> Result < Self , BlockBuildingHelperError > {
264+ let state_provider = state_provider_source. state_provider ( ) ?;
263265 let last_committed_block = building_ctx. block ( ) - 1 ;
264266 check_block_hash_reader_health ( last_committed_block, & state_provider) ?;
265267
@@ -288,6 +290,7 @@ impl<
288290 built_block_trace. available_orders_statistics = available_orders_statistics;
289291 Ok ( Self {
290292 _fee_recipient_balance_start : fee_recipient_balance_start,
293+ source : state_provider_source,
291294 block_state,
292295 partial_block,
293296 payout_tx_gas,
@@ -621,8 +624,27 @@ impl<
621624 & self . building_ctx
622625 }
623626
624- fn box_clone ( & self ) -> Box < dyn BlockBuildingHelper > {
625- Box :: new ( self . clone ( ) )
627+ fn try_clone ( & self ) -> Result < Box < dyn BlockBuildingHelper > , BlockBuildingHelperError > {
628+ let state_provider = self . source . state_provider ( ) ?;
629+ let cached = CachedDB :: new (
630+ state_provider,
631+ self . building_ctx . shared_cached_reads . clone ( ) ,
632+ ) ;
633+ let block_state =
634+ BlockState :: new ( cached) . with_bundle_state ( self . block_state . clone_bundle ( ) ) ;
635+ Ok ( Box :: new ( Self {
636+ _fee_recipient_balance_start : self . _fee_recipient_balance_start ,
637+ source : self . source . clone ( ) ,
638+ block_state,
639+ partial_block : self . partial_block . clone ( ) ,
640+ payout_tx_gas : self . payout_tx_gas ,
641+ builder_name : self . builder_name . clone ( ) ,
642+ building_ctx : self . building_ctx . clone ( ) ,
643+ built_block_trace : self . built_block_trace . clone ( ) ,
644+ cancel_on_fatal_error : self . cancel_on_fatal_error . clone ( ) ,
645+ finalize_adjustment_state : self . finalize_adjustment_state . clone ( ) ,
646+ max_order_execution_duration_warning : self . max_order_execution_duration_warning ,
647+ } ) )
626648 }
627649
628650 fn builder_name ( & self ) -> & str {
0 commit comments