Skip to main content

reth_node_core/args/
txpool.rs

1//! Transaction pool arguments
2
3use crate::cli::config::RethTransactionPoolConfig;
4use alloy_eips::eip1559::{ETHEREUM_BLOCK_GAS_LIMIT_30M, MIN_PROTOCOL_BASE_FEE};
5use alloy_primitives::Address;
6use clap::{
7    builder::{RangedU64ValueParser, Resettable},
8    Args,
9};
10use reth_cli_util::{parse_duration_from_secs_or_ms, parsers::format_duration_as_secs_or_ms};
11use reth_transaction_pool::{
12    blobstore::disk::DEFAULT_MAX_CACHED_BLOBS,
13    maintain::MAX_QUEUED_TRANSACTION_LIFETIME,
14    pool::{NEW_TX_LISTENER_BUFFER_SIZE, PENDING_TX_LISTENER_BUFFER_SIZE},
15    validate::DEFAULT_MAX_TX_INPUT_BYTES,
16    LocalTransactionConfig, PoolConfig, PriceBumpConfig, SubPoolLimit, DEFAULT_PRICE_BUMP,
17    DEFAULT_TXPOOL_ADDITIONAL_VALIDATION_TASKS, MAX_NEW_PENDING_TXS_NOTIFICATIONS,
18    REPLACE_BLOB_PRICE_BUMP, TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
19    TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT, TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
20};
21use std::{path::PathBuf, sync::OnceLock, time::Duration};
22
23/// Global static transaction pool defaults
24static TXPOOL_DEFAULTS: OnceLock<DefaultTxPoolValues> = OnceLock::new();
25
26/// Default values for transaction pool that can be customized
27///
28/// Global defaults can be set via [`DefaultTxPoolValues::try_init`].
29#[derive(Debug, Clone)]
30pub struct DefaultTxPoolValues {
31    pending_max_count: usize,
32    pending_max_size: usize,
33    basefee_max_count: usize,
34    basefee_max_size: usize,
35    queued_max_count: usize,
36    queued_max_size: usize,
37    blobpool_max_count: usize,
38    blobpool_max_size: usize,
39    blob_cache_size: Option<u32>,
40    disable_blobs_support: bool,
41    max_account_slots: usize,
42    price_bump: u128,
43    minimal_protocol_basefee: u64,
44    minimum_priority_fee: Option<u128>,
45    enforced_gas_limit: u64,
46    max_tx_gas_limit: Option<u64>,
47    blob_transaction_price_bump: u128,
48    max_tx_input_bytes: usize,
49    max_cached_entries: u32,
50    no_locals: bool,
51    locals: Vec<Address>,
52    no_local_transactions_propagation: bool,
53    additional_validation_tasks: usize,
54    pending_tx_listener_buffer_size: usize,
55    new_tx_listener_buffer_size: usize,
56    max_new_pending_txs_notifications: usize,
57    max_queued_lifetime: Duration,
58    transactions_backup_path: Option<PathBuf>,
59    disable_transactions_backup: bool,
60    max_batch_size: usize,
61    enforce_tracked_nonce: bool,
62}
63
64impl DefaultTxPoolValues {
65    /// Initialize the global transaction pool defaults with this configuration
66    pub fn try_init(self) -> Result<(), Self> {
67        TXPOOL_DEFAULTS.set(self)
68    }
69
70    /// Get a reference to the global transaction pool defaults
71    pub fn get_global() -> &'static Self {
72        TXPOOL_DEFAULTS.get_or_init(Self::default)
73    }
74
75    /// Set the default pending sub-pool max transaction count
76    pub const fn with_pending_max_count(mut self, v: usize) -> Self {
77        self.pending_max_count = v;
78        self
79    }
80
81    /// Set the default pending sub-pool max size in MB
82    pub const fn with_pending_max_size(mut self, v: usize) -> Self {
83        self.pending_max_size = v;
84        self
85    }
86
87    /// Set the default basefee sub-pool max transaction count
88    pub const fn with_basefee_max_count(mut self, v: usize) -> Self {
89        self.basefee_max_count = v;
90        self
91    }
92
93    /// Set the default basefee sub-pool max size in MB
94    pub const fn with_basefee_max_size(mut self, v: usize) -> Self {
95        self.basefee_max_size = v;
96        self
97    }
98
99    /// Set the default queued sub-pool max transaction count
100    pub const fn with_queued_max_count(mut self, v: usize) -> Self {
101        self.queued_max_count = v;
102        self
103    }
104
105    /// Set the default queued sub-pool max size in MB
106    pub const fn with_queued_max_size(mut self, v: usize) -> Self {
107        self.queued_max_size = v;
108        self
109    }
110
111    /// Set the default blobpool max transaction count
112    pub const fn with_blobpool_max_count(mut self, v: usize) -> Self {
113        self.blobpool_max_count = v;
114        self
115    }
116
117    /// Set the default blobpool max size in MB
118    pub const fn with_blobpool_max_size(mut self, v: usize) -> Self {
119        self.blobpool_max_size = v;
120        self
121    }
122
123    /// Set the default blob cache size
124    pub const fn with_blob_cache_size(mut self, v: Option<u32>) -> Self {
125        self.blob_cache_size = v;
126        self
127    }
128
129    /// Set whether to disable blob transaction support by default
130    pub const fn with_disable_blobs_support(mut self, v: bool) -> Self {
131        self.disable_blobs_support = v;
132        self
133    }
134
135    /// Set the default max account slots
136    pub const fn with_max_account_slots(mut self, v: usize) -> Self {
137        self.max_account_slots = v;
138        self
139    }
140
141    /// Set the default price bump percentage
142    pub const fn with_price_bump(mut self, v: u128) -> Self {
143        self.price_bump = v;
144        self
145    }
146
147    /// Set the default minimal protocol base fee
148    pub const fn with_minimal_protocol_basefee(mut self, v: u64) -> Self {
149        self.minimal_protocol_basefee = v;
150        self
151    }
152
153    /// Set the default minimum priority fee
154    pub const fn with_minimum_priority_fee(mut self, v: Option<u128>) -> Self {
155        self.minimum_priority_fee = v;
156        self
157    }
158
159    /// Set the default enforced gas limit
160    pub const fn with_enforced_gas_limit(mut self, v: u64) -> Self {
161        self.enforced_gas_limit = v;
162        self
163    }
164
165    /// Set the default max transaction gas limit
166    pub const fn with_max_tx_gas_limit(mut self, v: Option<u64>) -> Self {
167        self.max_tx_gas_limit = v;
168        self
169    }
170
171    /// Set the default blob transaction price bump
172    pub const fn with_blob_transaction_price_bump(mut self, v: u128) -> Self {
173        self.blob_transaction_price_bump = v;
174        self
175    }
176
177    /// Set the default max transaction input bytes
178    pub const fn with_max_tx_input_bytes(mut self, v: usize) -> Self {
179        self.max_tx_input_bytes = v;
180        self
181    }
182
183    /// Set the default max cached entries
184    pub const fn with_max_cached_entries(mut self, v: u32) -> Self {
185        self.max_cached_entries = v;
186        self
187    }
188
189    /// Set whether to disable local transaction exemptions by default
190    pub const fn with_no_locals(mut self, v: bool) -> Self {
191        self.no_locals = v;
192        self
193    }
194
195    /// Set the default local addresses
196    pub fn with_locals(mut self, v: Vec<Address>) -> Self {
197        self.locals = v;
198        self
199    }
200
201    /// Set whether to disable local transaction propagation by default
202    pub const fn with_no_local_transactions_propagation(mut self, v: bool) -> Self {
203        self.no_local_transactions_propagation = v;
204        self
205    }
206
207    /// Set the default additional validation tasks
208    pub const fn with_additional_validation_tasks(mut self, v: usize) -> Self {
209        self.additional_validation_tasks = v;
210        self
211    }
212
213    /// Set the default pending transaction listener buffer size
214    pub const fn with_pending_tx_listener_buffer_size(mut self, v: usize) -> Self {
215        self.pending_tx_listener_buffer_size = v;
216        self
217    }
218
219    /// Set the default new transaction listener buffer size
220    pub const fn with_new_tx_listener_buffer_size(mut self, v: usize) -> Self {
221        self.new_tx_listener_buffer_size = v;
222        self
223    }
224
225    /// Set the default max new pending transactions notifications
226    pub const fn with_max_new_pending_txs_notifications(mut self, v: usize) -> Self {
227        self.max_new_pending_txs_notifications = v;
228        self
229    }
230
231    /// Set the default max queued lifetime
232    pub const fn with_max_queued_lifetime(mut self, v: Duration) -> Self {
233        self.max_queued_lifetime = v;
234        self
235    }
236
237    /// Set the default transactions backup path
238    pub fn with_transactions_backup_path(mut self, v: Option<PathBuf>) -> Self {
239        self.transactions_backup_path = v;
240        self
241    }
242
243    /// Set whether to disable transaction backup by default
244    pub const fn with_disable_transactions_backup(mut self, v: bool) -> Self {
245        self.disable_transactions_backup = v;
246        self
247    }
248
249    /// Set the default max batch size
250    pub const fn with_max_batch_size(mut self, v: usize) -> Self {
251        self.max_batch_size = v;
252        self
253    }
254
255    /// Set whether to enforce the sender nonce tracked from canonical updates by default
256    pub const fn with_enforce_tracked_nonce(mut self, v: bool) -> Self {
257        self.enforce_tracked_nonce = v;
258        self
259    }
260}
261
262impl Default for DefaultTxPoolValues {
263    fn default() -> Self {
264        Self {
265            pending_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
266            pending_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
267            basefee_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
268            basefee_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
269            queued_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
270            queued_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
271            blobpool_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
272            blobpool_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
273            blob_cache_size: None,
274            disable_blobs_support: false,
275            max_account_slots: TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
276            price_bump: DEFAULT_PRICE_BUMP,
277            minimal_protocol_basefee: MIN_PROTOCOL_BASE_FEE,
278            minimum_priority_fee: None,
279            enforced_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT_30M,
280            max_tx_gas_limit: None,
281            blob_transaction_price_bump: REPLACE_BLOB_PRICE_BUMP,
282            max_tx_input_bytes: DEFAULT_MAX_TX_INPUT_BYTES,
283            max_cached_entries: DEFAULT_MAX_CACHED_BLOBS,
284            no_locals: false,
285            locals: Vec::new(),
286            no_local_transactions_propagation: false,
287            additional_validation_tasks: DEFAULT_TXPOOL_ADDITIONAL_VALIDATION_TASKS,
288            pending_tx_listener_buffer_size: PENDING_TX_LISTENER_BUFFER_SIZE,
289            new_tx_listener_buffer_size: NEW_TX_LISTENER_BUFFER_SIZE,
290            max_new_pending_txs_notifications: MAX_NEW_PENDING_TXS_NOTIFICATIONS,
291            max_queued_lifetime: MAX_QUEUED_TRANSACTION_LIFETIME,
292            transactions_backup_path: None,
293            disable_transactions_backup: false,
294            max_batch_size: 1,
295            enforce_tracked_nonce: false,
296        }
297    }
298}
299
300/// Parameters for debugging purposes
301#[derive(Debug, Clone, Args, PartialEq, Eq)]
302#[command(next_help_heading = "TxPool")]
303pub struct TxPoolArgs {
304    /// Max number of transactions in the pending sub-pool.
305    #[arg(long = "txpool.pending-max-count", alias = "txpool.pending_max_count", default_value_t = DefaultTxPoolValues::get_global().pending_max_count)]
306    pub pending_max_count: usize,
307    /// Max size of the pending sub-pool in megabytes.
308    #[arg(long = "txpool.pending-max-size", alias = "txpool.pending_max_size", default_value_t = DefaultTxPoolValues::get_global().pending_max_size)]
309    pub pending_max_size: usize,
310
311    /// Max number of transactions in the basefee sub-pool
312    #[arg(long = "txpool.basefee-max-count", alias = "txpool.basefee_max_count", default_value_t = DefaultTxPoolValues::get_global().basefee_max_count)]
313    pub basefee_max_count: usize,
314    /// Max size of the basefee sub-pool in megabytes.
315    #[arg(long = "txpool.basefee-max-size", alias = "txpool.basefee_max_size", default_value_t = DefaultTxPoolValues::get_global().basefee_max_size)]
316    pub basefee_max_size: usize,
317
318    /// Max number of transactions in the queued sub-pool
319    #[arg(long = "txpool.queued-max-count", alias = "txpool.queued_max_count", default_value_t = DefaultTxPoolValues::get_global().queued_max_count)]
320    pub queued_max_count: usize,
321    /// Max size of the queued sub-pool in megabytes.
322    #[arg(long = "txpool.queued-max-size", alias = "txpool.queued_max_size", default_value_t = DefaultTxPoolValues::get_global().queued_max_size)]
323    pub queued_max_size: usize,
324
325    /// Max number of transactions in the blobpool
326    #[arg(long = "txpool.blobpool-max-count", alias = "txpool.blobpool_max_count", default_value_t = DefaultTxPoolValues::get_global().blobpool_max_count)]
327    pub blobpool_max_count: usize,
328    /// Max size of the blobpool in megabytes.
329    #[arg(long = "txpool.blobpool-max-size", alias = "txpool.blobpool_max_size", default_value_t = DefaultTxPoolValues::get_global().blobpool_max_size)]
330    pub blobpool_max_size: usize,
331
332    /// Max number of entries for the in memory cache of the blob store.
333    #[arg(long = "txpool.blob-cache-size", alias = "txpool.blob_cache_size", default_value = Resettable::from(DefaultTxPoolValues::get_global().blob_cache_size.map(|v| v.to_string().into())))]
334    pub blob_cache_size: Option<u32>,
335
336    /// Disable EIP-4844 blob transaction support
337    #[arg(long = "txpool.disable-blobs-support", alias = "txpool.disable_blobs_support", default_value_t = DefaultTxPoolValues::get_global().disable_blobs_support, conflicts_with_all = ["blobpool_max_count", "blobpool_max_size", "blob_cache_size", "blob_transaction_price_bump"])]
338    pub disable_blobs_support: bool,
339
340    /// Max number of executable transaction slots guaranteed per account
341    #[arg(long = "txpool.max-account-slots", alias = "txpool.max_account_slots", default_value_t = DefaultTxPoolValues::get_global().max_account_slots)]
342    pub max_account_slots: usize,
343
344    /// Price bump (in %) for the transaction pool underpriced check.
345    #[arg(long = "txpool.pricebump", default_value_t = DefaultTxPoolValues::get_global().price_bump)]
346    pub price_bump: u128,
347
348    /// Minimum base fee required by the protocol.
349    #[arg(long = "txpool.minimal-protocol-fee", default_value_t = DefaultTxPoolValues::get_global().minimal_protocol_basefee)]
350    pub minimal_protocol_basefee: u64,
351
352    /// Minimum priority fee required for transaction acceptance into the pool.
353    /// Transactions with priority fee below this value will be rejected. For legacy and EIP-2930
354    /// transactions the gas price is used as the priority fee.
355    #[arg(long = "txpool.minimum-priority-fee", default_value = Resettable::from(DefaultTxPoolValues::get_global().minimum_priority_fee.map(|v| v.to_string().into())))]
356    pub minimum_priority_fee: Option<u128>,
357
358    /// The default enforced gas limit for transactions entering the pool
359    #[arg(long = "txpool.gas-limit", default_value_t = DefaultTxPoolValues::get_global().enforced_gas_limit)]
360    pub enforced_gas_limit: u64,
361
362    /// Maximum gas limit for individual transactions. Transactions exceeding this limit will be
363    /// rejected by the transaction pool
364    #[arg(long = "txpool.max-tx-gas", default_value = Resettable::from(DefaultTxPoolValues::get_global().max_tx_gas_limit.map(|v| v.to_string().into())))]
365    pub max_tx_gas_limit: Option<u64>,
366
367    /// Price bump percentage to replace an already existing blob transaction
368    #[arg(long = "blobpool.pricebump", default_value_t = DefaultTxPoolValues::get_global().blob_transaction_price_bump)]
369    pub blob_transaction_price_bump: u128,
370
371    /// Max size in bytes of a single transaction allowed to enter the pool
372    #[arg(long = "txpool.max-tx-input-bytes", alias = "txpool.max_tx_input_bytes", default_value_t = DefaultTxPoolValues::get_global().max_tx_input_bytes)]
373    pub max_tx_input_bytes: usize,
374
375    /// The maximum number of blobs to keep in the in memory blob cache.
376    #[arg(long = "txpool.max-cached-entries", alias = "txpool.max_cached_entries", default_value_t = DefaultTxPoolValues::get_global().max_cached_entries)]
377    pub max_cached_entries: u32,
378
379    /// Flag to disable local transaction exemptions.
380    #[arg(long = "txpool.nolocals", default_value_t = DefaultTxPoolValues::get_global().no_locals)]
381    pub no_locals: bool,
382    /// Flag to allow certain addresses as local.
383    #[arg(long = "txpool.locals", default_values = DefaultTxPoolValues::get_global().locals.iter().map(ToString::to_string))]
384    pub locals: Vec<Address>,
385    /// Flag to toggle local transaction propagation.
386    #[arg(long = "txpool.no-local-transactions-propagation", default_value_t = DefaultTxPoolValues::get_global().no_local_transactions_propagation)]
387    pub no_local_transactions_propagation: bool,
388
389    /// Number of additional transaction validation tasks to spawn.
390    #[arg(long = "txpool.additional-validation-tasks", alias = "txpool.additional_validation_tasks", default_value_t = DefaultTxPoolValues::get_global().additional_validation_tasks)]
391    pub additional_validation_tasks: usize,
392
393    /// Maximum number of pending transactions from the network to buffer
394    #[arg(long = "txpool.max-pending-txns", alias = "txpool.max_pending_txns", default_value_t = DefaultTxPoolValues::get_global().pending_tx_listener_buffer_size)]
395    pub pending_tx_listener_buffer_size: usize,
396
397    /// Maximum number of new transactions to buffer
398    #[arg(long = "txpool.max-new-txns", alias = "txpool.max_new_txns", default_value_t = DefaultTxPoolValues::get_global().new_tx_listener_buffer_size)]
399    pub new_tx_listener_buffer_size: usize,
400
401    /// How many new pending transactions to buffer and send to in progress pending transaction
402    /// iterators.
403    #[arg(long = "txpool.max-new-pending-txs-notifications", alias = "txpool.max-new-pending-txs-notifications", default_value_t = DefaultTxPoolValues::get_global().max_new_pending_txs_notifications)]
404    pub max_new_pending_txs_notifications: usize,
405
406    /// Maximum amount of time non-executable transaction are queued.
407    #[arg(long = "txpool.lifetime", value_parser = parse_duration_from_secs_or_ms, value_name = "DURATION", default_value = format_duration_as_secs_or_ms(DefaultTxPoolValues::get_global().max_queued_lifetime))]
408    pub max_queued_lifetime: Duration,
409
410    /// Path to store the local transaction backup at, to survive node restarts.
411    #[arg(long = "txpool.transactions-backup", alias = "txpool.journal", value_name = "PATH", default_value = Resettable::from(DefaultTxPoolValues::get_global().transactions_backup_path.as_ref().map(|v| v.to_string_lossy().into())))]
412    pub transactions_backup_path: Option<PathBuf>,
413
414    /// Disables transaction backup to disk on node shutdown.
415    #[arg(
416        long = "txpool.disable-transactions-backup",
417        alias = "txpool.disable-journal",
418        conflicts_with = "transactions_backup_path",
419        default_value_t = DefaultTxPoolValues::get_global().disable_transactions_backup
420    )]
421    pub disable_transactions_backup: bool,
422
423    /// Max batch size for transaction pool insertions
424    #[arg(long = "txpool.max-batch-size", value_parser = RangedU64ValueParser::<usize>::new().range(1..), default_value_t = DefaultTxPoolValues::get_global().max_batch_size)]
425    pub max_batch_size: usize,
426
427    /// Reject transactions whose nonce is below the sender nonce tracked from canonical updates.
428    ///
429    /// Closes the window in which a validation result that predates a block inserts an already
430    /// mined nonce as pending. Assumes sender nonces only move forward, so this is meant for
431    /// chains without reorgs and very low block times.
432    #[arg(long = "txpool.enforce-tracked-nonce", default_value_t = DefaultTxPoolValues::get_global().enforce_tracked_nonce)]
433    pub enforce_tracked_nonce: bool,
434}
435
436impl TxPoolArgs {
437    /// Sets the minimal protocol base fee to 0, effectively disabling checks that enforce that a
438    /// transaction's fee must be higher than the [`MIN_PROTOCOL_BASE_FEE`] which is the lowest
439    /// value the ethereum EIP-1559 base fee can reach.
440    pub const fn with_disabled_protocol_base_fee(self) -> Self {
441        self.with_protocol_base_fee(0)
442    }
443
444    /// Configures the minimal protocol base fee that should be enforced.
445    ///
446    /// Ethereum's EIP-1559 base fee can't drop below [`MIN_PROTOCOL_BASE_FEE`] hence this is
447    /// enforced by default in the pool.
448    pub const fn with_protocol_base_fee(mut self, protocol_base_fee: u64) -> Self {
449        self.minimal_protocol_basefee = protocol_base_fee;
450        self
451    }
452}
453
454impl Default for TxPoolArgs {
455    fn default() -> Self {
456        let DefaultTxPoolValues {
457            pending_max_count,
458            pending_max_size,
459            basefee_max_count,
460            basefee_max_size,
461            queued_max_count,
462            queued_max_size,
463            blobpool_max_count,
464            blobpool_max_size,
465            blob_cache_size,
466            disable_blobs_support,
467            max_account_slots,
468            price_bump,
469            minimal_protocol_basefee,
470            minimum_priority_fee,
471            enforced_gas_limit,
472            max_tx_gas_limit,
473            blob_transaction_price_bump,
474            max_tx_input_bytes,
475            max_cached_entries,
476            no_locals,
477            locals,
478            no_local_transactions_propagation,
479            additional_validation_tasks,
480            pending_tx_listener_buffer_size,
481            new_tx_listener_buffer_size,
482            max_new_pending_txs_notifications,
483            max_queued_lifetime,
484            transactions_backup_path,
485            disable_transactions_backup,
486            max_batch_size,
487            enforce_tracked_nonce,
488        } = DefaultTxPoolValues::get_global().clone();
489        Self {
490            pending_max_count,
491            pending_max_size,
492            basefee_max_count,
493            basefee_max_size,
494            queued_max_count,
495            queued_max_size,
496            blobpool_max_count,
497            blobpool_max_size,
498            blob_cache_size,
499            disable_blobs_support,
500            max_account_slots,
501            price_bump,
502            minimal_protocol_basefee,
503            minimum_priority_fee,
504            enforced_gas_limit,
505            max_tx_gas_limit,
506            blob_transaction_price_bump,
507            max_tx_input_bytes,
508            max_cached_entries,
509            no_locals,
510            locals,
511            no_local_transactions_propagation,
512            additional_validation_tasks,
513            pending_tx_listener_buffer_size,
514            new_tx_listener_buffer_size,
515            max_new_pending_txs_notifications,
516            max_queued_lifetime,
517            transactions_backup_path,
518            disable_transactions_backup,
519            max_batch_size,
520            enforce_tracked_nonce,
521        }
522    }
523}
524
525impl RethTransactionPoolConfig for TxPoolArgs {
526    /// Returns transaction pool configuration.
527    fn pool_config(&self) -> PoolConfig {
528        let default_config = PoolConfig::default();
529        PoolConfig {
530            local_transactions_config: LocalTransactionConfig {
531                no_exemptions: self.no_locals,
532                local_addresses: self.locals.iter().copied().collect(),
533                propagate_local_transactions: !self.no_local_transactions_propagation,
534            },
535            pending_limit: SubPoolLimit {
536                max_txs: self.pending_max_count,
537                max_size: self.pending_max_size.saturating_mul(1024 * 1024),
538            },
539            basefee_limit: SubPoolLimit {
540                max_txs: self.basefee_max_count,
541                max_size: self.basefee_max_size.saturating_mul(1024 * 1024),
542            },
543            queued_limit: SubPoolLimit {
544                max_txs: self.queued_max_count,
545                max_size: self.queued_max_size.saturating_mul(1024 * 1024),
546            },
547            blob_limit: SubPoolLimit {
548                max_txs: self.blobpool_max_count,
549                max_size: self.blobpool_max_size.saturating_mul(1024 * 1024),
550            },
551            blob_cache_size: self.blob_cache_size,
552            max_account_slots: self.max_account_slots,
553            price_bumps: PriceBumpConfig {
554                default_price_bump: self.price_bump,
555                replace_blob_tx_price_bump: self.blob_transaction_price_bump,
556            },
557            minimal_protocol_basefee: self.minimal_protocol_basefee,
558            minimum_priority_fee: self.minimum_priority_fee,
559            gas_limit: self.enforced_gas_limit,
560            pending_tx_listener_buffer_size: self.pending_tx_listener_buffer_size,
561            new_tx_listener_buffer_size: self.new_tx_listener_buffer_size,
562            max_new_pending_txs_notifications: self.max_new_pending_txs_notifications,
563            max_queued_lifetime: self.max_queued_lifetime,
564            max_inflight_delegated_slot_limit: default_config.max_inflight_delegated_slot_limit,
565            enforce_tracked_nonce: self.enforce_tracked_nonce,
566        }
567    }
568
569    /// Returns max batch size for transaction batch insertion.
570    fn max_batch_size(&self) -> usize {
571        self.max_batch_size
572    }
573}
574
575#[cfg(test)]
576mod tests {
577
578    use super::*;
579    use clap::Parser;
580
581    /// A helper type to parse Args more easily
582    #[derive(Parser)]
583    struct CommandParser<T: Args> {
584        #[command(flatten)]
585        args: T,
586    }
587
588    #[test]
589    fn txpool_args_default_sanity_test() {
590        let default_args = TxPoolArgs::default();
591        let args = CommandParser::<TxPoolArgs>::parse_from(["reth"]).args;
592        assert_eq!(args, default_args);
593    }
594
595    #[test]
596    fn txpool_parse_max_tx_lifetime() {
597        // Test with a custom duration
598        let args =
599            CommandParser::<TxPoolArgs>::parse_from(["reth", "--txpool.lifetime", "300"]).args;
600        assert_eq!(args.max_queued_lifetime, Duration::from_secs(300));
601
602        // Test with the default value
603        let args = CommandParser::<TxPoolArgs>::parse_from(["reth"]).args;
604        assert_eq!(args.max_queued_lifetime, Duration::from_secs(3 * 60 * 60)); // Default is 3h
605    }
606
607    #[test]
608    fn txpool_parse_max_tx_lifetime_invalid() {
609        let result =
610            CommandParser::<TxPoolArgs>::try_parse_from(["reth", "--txpool.lifetime", "invalid"]);
611
612        assert!(result.is_err(), "Expected an error for invalid duration");
613    }
614
615    #[test]
616    fn txpool_max_batch_size_must_be_nonzero() {
617        let zero =
618            CommandParser::<TxPoolArgs>::try_parse_from(["reth", "--txpool.max-batch-size", "0"]);
619        assert!(zero.is_err());
620
621        let valid =
622            CommandParser::<TxPoolArgs>::parse_from(["reth", "--txpool.max-batch-size", "1"]);
623        assert_eq!(valid.args.max_batch_size, 1);
624    }
625
626    #[test]
627    fn txpool_enforce_tracked_nonce_reaches_the_pool_config() {
628        let args = CommandParser::<TxPoolArgs>::parse_from(["reth"]).args;
629        assert!(!args.pool_config().enforce_tracked_nonce);
630
631        let args =
632            CommandParser::<TxPoolArgs>::parse_from(["reth", "--txpool.enforce-tracked-nonce"])
633                .args;
634        assert!(args.pool_config().enforce_tracked_nonce);
635    }
636
637    #[test]
638    fn txpool_args() {
639        let args = TxPoolArgs {
640            pending_max_count: 1000,
641            pending_max_size: 200,
642            basefee_max_count: 2000,
643            basefee_max_size: 300,
644            queued_max_count: 3000,
645            queued_max_size: 400,
646            blobpool_max_count: 4000,
647            blobpool_max_size: 500,
648            blob_cache_size: Some(100),
649            disable_blobs_support: false,
650            max_account_slots: 20,
651            price_bump: 15,
652            minimal_protocol_basefee: 1000000000,
653            minimum_priority_fee: Some(2000000000),
654            enforced_gas_limit: 40000000,
655            max_tx_gas_limit: Some(50000000),
656            blob_transaction_price_bump: 25,
657            max_tx_input_bytes: 131072,
658            max_cached_entries: 200,
659            no_locals: true,
660            locals: vec![Address::with_last_byte(1), Address::with_last_byte(2)],
661            no_local_transactions_propagation: true,
662            additional_validation_tasks: 4,
663            pending_tx_listener_buffer_size: 512,
664            new_tx_listener_buffer_size: 256,
665            max_new_pending_txs_notifications: 128,
666            max_queued_lifetime: Duration::from_secs(7200),
667            transactions_backup_path: Some(PathBuf::from("/tmp/txpool-backup")),
668            disable_transactions_backup: false,
669            max_batch_size: 10,
670            enforce_tracked_nonce: true,
671        };
672
673        let parsed_args = CommandParser::<TxPoolArgs>::parse_from([
674            "reth",
675            "--txpool.pending-max-count",
676            "1000",
677            "--txpool.pending-max-size",
678            "200",
679            "--txpool.basefee-max-count",
680            "2000",
681            "--txpool.basefee-max-size",
682            "300",
683            "--txpool.queued-max-count",
684            "3000",
685            "--txpool.queued-max-size",
686            "400",
687            "--txpool.blobpool-max-count",
688            "4000",
689            "--txpool.blobpool-max-size",
690            "500",
691            "--txpool.blob-cache-size",
692            "100",
693            "--txpool.max-account-slots",
694            "20",
695            "--txpool.pricebump",
696            "15",
697            "--txpool.minimal-protocol-fee",
698            "1000000000",
699            "--txpool.minimum-priority-fee",
700            "2000000000",
701            "--txpool.gas-limit",
702            "40000000",
703            "--txpool.max-tx-gas",
704            "50000000",
705            "--blobpool.pricebump",
706            "25",
707            "--txpool.max-tx-input-bytes",
708            "131072",
709            "--txpool.max-cached-entries",
710            "200",
711            "--txpool.nolocals",
712            "--txpool.locals",
713            "0x0000000000000000000000000000000000000001",
714            "--txpool.locals",
715            "0x0000000000000000000000000000000000000002",
716            "--txpool.no-local-transactions-propagation",
717            "--txpool.additional-validation-tasks",
718            "4",
719            "--txpool.max-pending-txns",
720            "512",
721            "--txpool.max-new-txns",
722            "256",
723            "--txpool.max-new-pending-txs-notifications",
724            "128",
725            "--txpool.lifetime",
726            "7200",
727            "--txpool.transactions-backup",
728            "/tmp/txpool-backup",
729            "--txpool.max-batch-size",
730            "10",
731            "--txpool.enforce-tracked-nonce",
732        ])
733        .args;
734
735        assert_eq!(parsed_args, args);
736    }
737}