1+ use std:: sync:: Arc ;
2+
3+ use futures:: FutureExt ;
4+ use reth_chain_state:: CanonStateSubscriptions ;
15use reth_evm:: ConfigureEvm ;
26use reth_node_api:: { FullNodeTypes , NodeTypes , PrimitivesTy , TxTy } ;
37use reth_node_builder:: { BuilderContext , components:: PoolBuilder } ;
8+ use reth_optimism_chainspec:: OpChainSpec ;
9+ use reth_optimism_evm:: OpEvmConfig ;
410use reth_optimism_forks:: OpHardforks ;
511use reth_optimism_node:: OpPoolBuilder ;
6- use reth_optimism_txpool:: { OpPooledTx , OpTransactionPool } ;
7- use reth_transaction_pool:: { EthPoolTransaction , blobstore:: DiskFileBlobStore } ;
12+ use reth_optimism_txpool:: { OpPooledTx , OpTransactionPool , OpTransactionValidator } ;
13+ use reth_primitives_traits:: { Block , NodePrimitives } ;
14+ use reth_provider:: { BlockReaderIdExt , ChainSpecProvider , NodePrimitivesProvider } ;
15+ use reth_transaction_pool:: {
16+ EthPoolTransaction , TransactionPool , TransactionValidationTaskExecutor ,
17+ blobstore:: DiskFileBlobStore ,
18+ } ;
819
9- use crate :: { args:: OpRbuilderArgs , pool:: Flashpool , tx:: FBPooledTransaction } ;
20+ use crate :: {
21+ args:: OpRbuilderArgs ,
22+ pool:: {
23+ Flashpool ,
24+ metrics:: PoolMetrics ,
25+ presim:: { TopOfBlockSimulator , maintain_pending_simulations, maintain_tip_state} ,
26+ } ,
27+ tx:: FBPooledTransaction ,
28+ } ;
1029
1130pub struct FlashpoolBuilder {
1231 op_pool_builder : OpPoolBuilder < FBPooledTransaction > ,
32+ pre_simulate_bundles : bool ,
33+ block_time_secs : u64 ,
1334}
1435
1536impl FlashpoolBuilder {
@@ -22,26 +43,79 @@ impl FlashpoolBuilder {
2243 rollup_args. enable_tx_conditional || builder_args. enable_revert_protection ,
2344 ) ;
2445
25- Self { op_pool_builder }
46+ Self {
47+ op_pool_builder,
48+ pre_simulate_bundles : builder_args. pre_simulate_bundles ,
49+ block_time_secs : builder_args. chain_block_time / 1000 ,
50+ }
2651 }
2752}
2853
2954impl < Node , Evm > PoolBuilder < Node , Evm > for FlashpoolBuilder
3055where
3156 Node : FullNodeTypes < Types : NodeTypes < ChainSpec : OpHardforks > > ,
57+ Node :: Provider : ChainSpecProvider < ChainSpec = OpChainSpec >
58+ + BlockReaderIdExt < Header = alloy_consensus:: Header > ,
59+ <Node :: Provider as NodePrimitivesProvider >:: Primitives :
60+ NodePrimitives < Block : Block < Header = alloy_consensus:: Header > > ,
3261 FBPooledTransaction : EthPoolTransaction < Consensus = TxTy < Node :: Types > > + OpPooledTx ,
3362 Evm : ConfigureEvm < Primitives = PrimitivesTy < Node :: Types > > + Clone + ' static ,
3463{
35- type Pool =
36- Flashpool < OpTransactionPool < Node :: Provider , DiskFileBlobStore , Evm , FBPooledTransaction > > ;
64+ type Pool = Flashpool <
65+ OpTransactionPool < Node :: Provider , DiskFileBlobStore , Evm , FBPooledTransaction > ,
66+ TransactionValidationTaskExecutor <
67+ OpTransactionValidator < Node :: Provider , FBPooledTransaction , Evm > ,
68+ > ,
69+ > ;
3770
3871 async fn build_pool (
3972 self ,
4073 ctx : & BuilderContext < Node > ,
4174 evm_config : Evm ,
4275 ) -> eyre:: Result < Self :: Pool > {
76+ let inner_pool = self . op_pool_builder . build_pool ( ctx, evm_config) . await ?;
77+ let validator = inner_pool. validator ( ) . clone ( ) ;
78+ let metrics = Arc :: new ( PoolMetrics :: default ( ) ) ;
79+
80+ let simulator = if self . pre_simulate_bundles {
81+ let simulator = Arc :: new ( TopOfBlockSimulator :: new ( ) ) ;
82+
83+ let chain_events = ctx. provider ( ) . canonical_state_stream ( ) ;
84+ let op_evm_config = OpEvmConfig :: optimism ( ctx. provider ( ) . chain_spec ( ) ) ;
85+ ctx. task_executor ( ) . spawn_task (
86+ maintain_tip_state (
87+ simulator. clone ( ) ,
88+ ctx. provider ( ) . clone ( ) ,
89+ op_evm_config,
90+ self . block_time_secs ,
91+ metrics. clone ( ) ,
92+ chain_events,
93+ )
94+ . boxed ( ) ,
95+ ) ;
96+
97+ let pending_events = inner_pool. all_transactions_event_listener ( ) ;
98+ ctx. task_executor ( ) . spawn_task (
99+ maintain_pending_simulations (
100+ simulator. clone ( ) ,
101+ inner_pool. clone ( ) ,
102+ metrics. clone ( ) ,
103+ pending_events,
104+ )
105+ . boxed ( ) ,
106+ ) ;
107+
108+ Some ( simulator)
109+ } else {
110+ None
111+ } ;
112+
43113 Ok ( Flashpool {
44- inner : self . op_pool_builder . build_pool ( ctx, evm_config) . await ?,
114+ inner : inner_pool,
115+ validator,
116+ simulator,
117+ task_executor : ctx. task_executor ( ) . clone ( ) ,
118+ metrics,
45119 } )
46120 }
47121}
0 commit comments