reth_node_builder/components/
pool.rs1use crate::{BuilderContext, FullNodeTypes};
4use alloy_primitives::Address;
5use reth_chain_state::CanonStateSubscriptions;
6use reth_chainspec::EthereumHardforks;
7use reth_node_api::{NodeTypes, TxTy};
8use reth_transaction_pool::{
9 blobstore::DiskFileBlobStore, CoinbaseTipOrdering, PoolConfig, PoolTransaction, SubPoolLimit,
10 TransactionPool, TransactionValidationTaskExecutor, TransactionValidator,
11};
12use std::{collections::HashSet, future::Future};
13
14pub trait PoolBuilder<Node: FullNodeTypes>: Send {
16 type Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>>
18 + Unpin
19 + 'static;
20
21 fn build_pool(
23 self,
24 ctx: &BuilderContext<Node>,
25 ) -> impl Future<Output = eyre::Result<Self::Pool>> + Send;
26}
27
28impl<Node, F, Fut, Pool> PoolBuilder<Node> for F
29where
30 Node: FullNodeTypes,
31 Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>>
32 + Unpin
33 + 'static,
34 F: FnOnce(&BuilderContext<Node>) -> Fut + Send,
35 Fut: Future<Output = eyre::Result<Pool>> + Send,
36{
37 type Pool = Pool;
38
39 fn build_pool(
40 self,
41 ctx: &BuilderContext<Node>,
42 ) -> impl Future<Output = eyre::Result<Self::Pool>> {
43 self(ctx)
44 }
45}
46
47#[derive(Debug, Clone, Default)]
49pub struct PoolBuilderConfigOverrides {
50 pub pending_limit: Option<SubPoolLimit>,
52 pub basefee_limit: Option<SubPoolLimit>,
54 pub queued_limit: Option<SubPoolLimit>,
56 pub blob_limit: Option<SubPoolLimit>,
58 pub max_account_slots: Option<usize>,
60 pub minimal_protocol_basefee: Option<u64>,
62 pub local_addresses: HashSet<Address>,
64 pub additional_validation_tasks: Option<usize>,
66}
67
68impl PoolBuilderConfigOverrides {
69 pub fn apply(self, mut config: PoolConfig) -> PoolConfig {
71 let Self {
72 pending_limit,
73 basefee_limit,
74 queued_limit,
75 blob_limit,
76 max_account_slots,
77 minimal_protocol_basefee,
78 local_addresses,
79 additional_validation_tasks: _,
80 } = self;
81
82 if let Some(pending_limit) = pending_limit {
83 config.pending_limit = pending_limit;
84 }
85 if let Some(basefee_limit) = basefee_limit {
86 config.basefee_limit = basefee_limit;
87 }
88 if let Some(queued_limit) = queued_limit {
89 config.queued_limit = queued_limit;
90 }
91 if let Some(blob_limit) = blob_limit {
92 config.blob_limit = blob_limit;
93 }
94 if let Some(max_account_slots) = max_account_slots {
95 config.max_account_slots = max_account_slots;
96 }
97 if let Some(minimal_protocol_basefee) = minimal_protocol_basefee {
98 config.minimal_protocol_basefee = minimal_protocol_basefee;
99 }
100 config.local_transactions_config.local_addresses.extend(local_addresses);
101
102 config
103 }
104}
105
106pub struct TxPoolBuilder<'a, Node: FullNodeTypes, V = ()> {
111 ctx: &'a BuilderContext<Node>,
112 validator: V,
113}
114
115impl<'a, Node: FullNodeTypes> TxPoolBuilder<'a, Node> {
116 pub const fn new(ctx: &'a BuilderContext<Node>) -> Self {
118 Self { ctx, validator: () }
119 }
120}
121
122impl<'a, Node: FullNodeTypes, V> TxPoolBuilder<'a, Node, V> {
123 pub fn with_validator<NewV>(self, validator: NewV) -> TxPoolBuilder<'a, Node, NewV> {
125 TxPoolBuilder { ctx: self.ctx, validator }
126 }
127}
128
129impl<'a, Node, V> TxPoolBuilder<'a, Node, TransactionValidationTaskExecutor<V>>
130where
131 Node: FullNodeTypes<Types: NodeTypes<ChainSpec: EthereumHardforks>>,
132 V: TransactionValidator + 'static,
133 V::Transaction:
134 PoolTransaction<Consensus = TxTy<Node::Types>> + reth_transaction_pool::EthPoolTransaction,
135{
136 pub fn build_and_spawn_maintenance_task(
139 self,
140 blob_store: DiskFileBlobStore,
141 pool_config: PoolConfig,
142 ) -> eyre::Result<
143 reth_transaction_pool::Pool<
144 TransactionValidationTaskExecutor<V>,
145 CoinbaseTipOrdering<V::Transaction>,
146 DiskFileBlobStore,
147 >,
148 > {
149 let TxPoolBuilder { ctx, validator, .. } = self;
151
152 let transaction_pool = reth_transaction_pool::Pool::new(
153 validator,
154 CoinbaseTipOrdering::default(),
155 blob_store,
156 pool_config.clone(),
157 );
158
159 spawn_maintenance_tasks(ctx, transaction_pool.clone(), &pool_config)?;
161
162 Ok(transaction_pool)
163 }
164}
165
166pub fn create_blob_store<Node: FullNodeTypes>(
168 ctx: &BuilderContext<Node>,
169) -> eyre::Result<DiskFileBlobStore> {
170 let cache_size = Some(ctx.config().txpool.max_cached_entries);
171 create_blob_store_with_cache(ctx, cache_size)
172}
173
174pub fn create_blob_store_with_cache<Node: FullNodeTypes>(
177 ctx: &BuilderContext<Node>,
178 cache_size: Option<u32>,
179) -> eyre::Result<DiskFileBlobStore> {
180 let data_dir = ctx.config().datadir();
181 let config = if let Some(cache_size) = cache_size {
182 reth_transaction_pool::blobstore::DiskFileBlobStoreConfig::default()
183 .with_max_cached_entries(cache_size)
184 } else {
185 Default::default()
186 };
187
188 Ok(reth_transaction_pool::blobstore::DiskFileBlobStore::open(data_dir.blobstore(), config)?)
189}
190
191fn spawn_local_backup_task<Node, Pool>(ctx: &BuilderContext<Node>, pool: Pool) -> eyre::Result<()>
193where
194 Node: FullNodeTypes,
195 Pool: TransactionPool + Clone + 'static,
196{
197 if !ctx.config().txpool.disable_transactions_backup {
198 let data_dir = ctx.config().datadir();
199 let transactions_path = ctx
200 .config()
201 .txpool
202 .transactions_backup_path
203 .clone()
204 .unwrap_or_else(|| data_dir.txpool_transactions());
205
206 let transactions_backup_config =
207 reth_transaction_pool::maintain::LocalTransactionBackupConfig::with_local_txs_backup(
208 transactions_path,
209 );
210
211 ctx.task_executor().spawn_critical_with_graceful_shutdown_signal(
212 "local transactions backup task",
213 |shutdown| {
214 reth_transaction_pool::maintain::backup_local_transactions_task(
215 shutdown,
216 pool,
217 transactions_backup_config,
218 )
219 },
220 );
221 }
222 Ok(())
223}
224
225fn spawn_pool_maintenance_task<Node, Pool>(
227 ctx: &BuilderContext<Node>,
228 pool: Pool,
229 pool_config: &PoolConfig,
230) -> eyre::Result<()>
231where
232 Node: FullNodeTypes<Types: NodeTypes<ChainSpec: EthereumHardforks>>,
233 Pool: reth_transaction_pool::TransactionPoolExt + Clone + 'static,
234 Pool::Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>,
235{
236 let chain_events = ctx.provider().canonical_state_stream();
237 let client = ctx.provider().clone();
238
239 ctx.task_executor().spawn_critical(
240 "txpool maintenance task",
241 reth_transaction_pool::maintain::maintain_transaction_pool_future(
242 client,
243 pool,
244 chain_events,
245 ctx.task_executor().clone(),
246 reth_transaction_pool::maintain::MaintainPoolConfig {
247 max_tx_lifetime: pool_config.max_queued_lifetime,
248 no_local_exemptions: pool_config.local_transactions_config.no_exemptions,
249 ..Default::default()
250 },
251 ),
252 );
253
254 Ok(())
255}
256
257pub fn spawn_maintenance_tasks<Node, Pool>(
259 ctx: &BuilderContext<Node>,
260 pool: Pool,
261 pool_config: &PoolConfig,
262) -> eyre::Result<()>
263where
264 Node: FullNodeTypes<Types: NodeTypes<ChainSpec: EthereumHardforks>>,
265 Pool: reth_transaction_pool::TransactionPoolExt + Clone + 'static,
266 Pool::Transaction: PoolTransaction<Consensus = TxTy<Node::Types>>,
267{
268 spawn_local_backup_task(ctx, pool.clone())?;
269 spawn_pool_maintenance_task(ctx, pool, pool_config)?;
270 Ok(())
271}
272
273impl<Node: FullNodeTypes, V: std::fmt::Debug> std::fmt::Debug for TxPoolBuilder<'_, Node, V> {
274 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
275 f.debug_struct("TxPoolBuilder").field("validator", &self.validator).finish()
276 }
277}
278
279#[cfg(test)]
280mod tests {
281 use super::*;
282 use reth_transaction_pool::PoolConfig;
283
284 #[test]
285 fn test_pool_builder_config_overrides_apply() {
286 let base_config = PoolConfig::default();
287 let overrides = PoolBuilderConfigOverrides {
288 pending_limit: Some(SubPoolLimit::default()),
289 max_account_slots: Some(100),
290 minimal_protocol_basefee: Some(1000),
291 ..Default::default()
292 };
293
294 let updated_config = overrides.apply(base_config);
295 assert_eq!(updated_config.max_account_slots, 100);
296 assert_eq!(updated_config.minimal_protocol_basefee, 1000);
297 }
298
299 #[test]
300 fn test_pool_builder_config_overrides_default() {
301 let overrides = PoolBuilderConfigOverrides::default();
302 assert!(overrides.pending_limit.is_none());
303 assert!(overrides.max_account_slots.is_none());
304 assert!(overrides.local_addresses.is_empty());
305 }
306}