@@ -19,6 +19,7 @@ use op_alloy_consensus::{OpDepositReceipt, OpTypedTransaction};
1919use reth:: builder:: { components:: PayloadServiceBuilder , node:: FullNodeTypes , BuilderContext } ;
2020use reth:: core:: primitives:: InMemorySize ;
2121use reth:: payload:: PayloadBuilderHandle ;
22+ use reth:: tasks:: { TaskExecutor , TaskSpawner } ;
2223use reth_basic_payload_builder:: {
2324 BasicPayloadJobGeneratorConfig , BuildOutcome , BuildOutcomeKind , PayloadConfig ,
2425} ;
@@ -73,6 +74,7 @@ use revm::{
7374} ;
7475use std:: error:: Error as StdError ;
7576use std:: { fmt:: Display , sync:: Arc , time:: Instant } ;
77+ use tokio:: sync:: oneshot;
7678use tokio_util:: sync:: CancellationToken ;
7779use tracing:: { info, trace, warn} ;
7880
@@ -101,7 +103,8 @@ where
101103 + Unpin
102104 + ' static ,
103105{
104- type PayloadBuilder = OpPayloadBuilderVanilla < Pool , Node :: Provider , OpEvmConfig , OpPrimitives > ;
106+ type PayloadBuilder =
107+ OpPayloadBuilderVanilla < Pool , Node :: Provider , OpEvmConfig , OpPrimitives , TaskExecutor > ;
105108
106109 async fn build_payload_builder (
107110 & self ,
@@ -114,6 +117,7 @@ where
114117 pool,
115118 ctx. provider ( ) . clone ( ) ,
116119 Arc :: new ( BasicOpReceiptBuilder :: default ( ) ) ,
120+ ctx. task_executor ( ) . clone ( ) ,
117121 ) )
118122 }
119123
@@ -127,7 +131,6 @@ where
127131
128132 let payload_generator = BlockPayloadJobGenerator :: with_builder (
129133 ctx. provider ( ) . clone ( ) ,
130- ctx. task_executor ( ) . clone ( ) ,
131134 payload_job_config,
132135 payload_builder,
133136 false ,
@@ -145,14 +148,15 @@ where
145148 }
146149}
147150
148- impl < Pool , Client , EvmConfig , N , Txs > reth_basic_payload_builder:: PayloadBuilder
149- for OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N , Txs >
151+ impl < Pool , Client , EvmConfig , N , Tasks , Txs > reth_basic_payload_builder:: PayloadBuilder
152+ for OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N , Tasks , Txs >
150153where
151154 Pool : Clone + Send + Sync ,
152155 Client : Clone + Send + Sync ,
153156 EvmConfig : Clone + Send + Sync ,
154157 N : NodePrimitives ,
155158 Txs : Clone + Send + Sync ,
159+ Tasks : TaskSpawner + Clone + Unpin + ' static ,
156160{
157161 type Attributes = OpPayloadBuilderAttributes < N :: SignedTx > ;
158162 type BuiltPayload = OpBuiltPayload < N > ;
@@ -177,7 +181,10 @@ where
177181
178182/// Optimism's payload builder
179183#[ derive( Debug , Clone ) ]
180- pub struct OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N : NodePrimitives , Txs = ( ) > {
184+ pub struct OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N : NodePrimitives , Tasks , Txs = ( ) >
185+ where
186+ Tasks : TaskSpawner + Clone + Unpin + ' static ,
187+ {
181188 /// The type responsible for creating the evm.
182189 pub evm_config : EvmConfig ,
183190 /// The builder's signer key to use for an end of block tx
@@ -195,10 +202,14 @@ pub struct OpPayloadBuilderVanilla<Pool, Client, EvmConfig, N: NodePrimitives, T
195202 pub metrics : OpRBuilderMetrics ,
196203 /// Node primitive types.
197204 pub receipt_builder : Arc < dyn OpReceiptBuilder < N :: SignedTx , Receipt = N :: Receipt > > ,
205+ /// Executor to spawn tasks
206+ pub executor : Tasks ,
198207}
199208
200- impl < Pool , Client , EvmConfig , N : NodePrimitives >
201- OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N >
209+ impl < Pool , Client , EvmConfig , N : NodePrimitives , Tasks >
210+ OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N , Tasks >
211+ where
212+ Tasks : TaskSpawner + Clone + Unpin + ' static ,
202213{
203214 /// `OpPayloadBuilder` constructor.
204215 pub fn new (
@@ -207,13 +218,15 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives>
207218 pool : Pool ,
208219 client : Client ,
209220 receipt_builder : Arc < dyn OpReceiptBuilder < N :: SignedTx , Receipt = N :: Receipt > > ,
221+ executor : Tasks ,
210222 ) -> Self {
211223 Self :: with_builder_config (
212224 evm_config,
213225 builder_signer,
214226 pool,
215227 client,
216228 receipt_builder,
229+ executor,
217230 Default :: default ( ) ,
218231 )
219232 }
@@ -224,6 +237,7 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives>
224237 pool : Pool ,
225238 client : Client ,
226239 receipt_builder : Arc < dyn OpReceiptBuilder < N :: SignedTx , Receipt = N :: Receipt > > ,
240+ executor : Tasks ,
227241 config : OpBuilderConfig ,
228242 ) -> Self {
229243 Self {
@@ -235,18 +249,23 @@ impl<Pool, Client, EvmConfig, N: NodePrimitives>
235249 best_transactions : ( ) ,
236250 metrics : Default :: default ( ) ,
237251 builder_signer,
252+ executor,
238253 }
239254 }
240255}
241256
242- impl < EvmConfig , Pool , Client , N , Txs > PayloadBuilder
243- for OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N , Txs >
257+ impl < EvmConfig , Pool , Client , N , Tasks , Txs > PayloadBuilder
258+ for OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N , Tasks , Txs >
244259where
245- Client : StateProviderFactory + ChainSpecProvider < ChainSpec : EthChainSpec + OpHardforks > + Clone ,
260+ Client : StateProviderFactory
261+ + ChainSpecProvider < ChainSpec : EthChainSpec + OpHardforks >
262+ + Clone
263+ + ' static ,
246264 N : OpPayloadPrimitives < _TX = OpTransactionSigned > ,
247- Pool : TransactionPool < Transaction : PoolTransaction < Consensus = N :: SignedTx > > ,
265+ Pool : TransactionPool < Transaction : PoolTransaction < Consensus = N :: SignedTx > > + ' static ,
248266 EvmConfig : ConfigureEvmFor < N > ,
249267 Txs : OpPayloadTransactions < Pool :: Transaction > ,
268+ Tasks : TaskSpawner + Clone + Unpin + ' static ,
250269{
251270 type Attributes = OpPayloadBuilderAttributes < N :: SignedTx > ;
252271 type BuiltPayload = OpBuiltPayload < N > ;
@@ -258,44 +277,56 @@ where
258277 ) -> Result < ( ) , PayloadBuilderError > {
259278 let pool = self . pool . clone ( ) ;
260279 let block_build_start_time = Instant :: now ( ) ;
261-
262- match self . build_payload ( args, |attrs| {
263- #[ allow( clippy:: unit_arg) ]
264- self . best_transactions . best_transactions ( pool, attrs)
265- } ) ? {
266- BuildOutcome :: Better { payload, .. } => {
267- best_payload. set ( payload) ;
268- self . metrics
269- . total_block_built_duration
270- . record ( block_build_start_time. elapsed ( ) ) ;
271- self . metrics . block_built_success . increment ( 1 ) ;
272- Ok ( ( ) )
273- }
274- BuildOutcome :: Freeze ( payload) => {
275- best_payload. set ( payload) ;
276- self . metrics
277- . total_block_built_duration
278- . record ( block_build_start_time. elapsed ( ) ) ;
279- Ok ( ( ) )
280- }
281- BuildOutcome :: Cancelled => {
282- tracing:: warn!( "Payload build cancelled" ) ;
283- Err ( PayloadBuilderError :: MissingPayload )
284- }
285- _ => {
286- tracing:: warn!( "No better payload found" ) ;
287- Err ( PayloadBuilderError :: MissingPayload )
280+ let ( tx, rx) = oneshot:: channel ( ) ;
281+ let ctx = self . clone ( ) ;
282+ self . executor . spawn_blocking ( Box :: pin ( async move {
283+ match ctx. build_payload ( args, |attrs| {
284+ #[ allow( clippy:: unit_arg) ]
285+ ctx. best_transactions . best_transactions ( pool, attrs)
286+ } ) {
287+ Ok ( BuildOutcome :: Better { payload, .. } ) => {
288+ best_payload. set ( payload) ;
289+ ctx. metrics
290+ . total_block_built_duration
291+ . record ( block_build_start_time. elapsed ( ) ) ;
292+ ctx. metrics . block_built_success . increment ( 1 ) ;
293+ let _ = tx. send ( Ok ( ( ) ) ) ;
294+ }
295+ Ok ( BuildOutcome :: Freeze ( payload) ) => {
296+ best_payload. set ( payload) ;
297+ ctx. metrics
298+ . total_block_built_duration
299+ . record ( block_build_start_time. elapsed ( ) ) ;
300+ let _ = tx. send ( Ok ( ( ) ) ) ;
301+ }
302+ Ok ( BuildOutcome :: Cancelled ) => {
303+ tracing:: warn!( "Payload build cancelled" ) ;
304+ let _ = tx. send ( Err ( PayloadBuilderError :: MissingPayload ) ) ;
305+ }
306+ Ok ( _) => {
307+ tracing:: warn!( "No better payload found" ) ;
308+ let _ = tx. send ( Err ( PayloadBuilderError :: MissingPayload ) ) ;
309+ }
310+ Err ( err) => {
311+ tracing:: warn!( "Build payload error {}" , err) ;
312+ let _ = tx. send ( Err ( err) ) ;
313+ }
288314 }
289- }
315+ } ) ) ;
316+ rx. blocking_recv ( )
317+ . map_err ( |err| PayloadBuilderError :: from ( err) ) ?
290318 }
291319}
292320
293- impl < Pool , Client , EvmConfig , N , T > OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N , T >
321+ impl < Pool , Client , EvmConfig , N , Tasks , T >
322+ OpPayloadBuilderVanilla < Pool , Client , EvmConfig , N , Tasks , T >
294323where
295- Pool : TransactionPool < Transaction : PoolTransaction < Consensus = N :: SignedTx > > ,
296- Client : StateProviderFactory + ChainSpecProvider < ChainSpec : EthChainSpec + OpHardforks > ,
324+ Pool : TransactionPool < Transaction : PoolTransaction < Consensus = N :: SignedTx > > + ' static ,
325+ Client :
326+ StateProviderFactory + ChainSpecProvider < ChainSpec : EthChainSpec + OpHardforks > + ' static ,
297327 N : OpPayloadPrimitives < _TX = OpTransactionSigned > ,
298328 EvmConfig : ConfigureEvmFor < N > ,
329+ Tasks : TaskSpawner + Clone + Unpin + ' static ,
299330{
300331 /// Constructs an Optimism payload from the transactions sent via the
301332 /// Payload attributes by the sequencer. If the `no_tx_pool` argument is passed in
0 commit comments