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::Args;
7use reth_cli_util::parse_duration_from_secs_or_ms;
8use reth_transaction_pool::{
9    blobstore::disk::DEFAULT_MAX_CACHED_BLOBS,
10    maintain::MAX_QUEUED_TRANSACTION_LIFETIME,
11    pool::{NEW_TX_LISTENER_BUFFER_SIZE, PENDING_TX_LISTENER_BUFFER_SIZE},
12    validate::DEFAULT_MAX_TX_INPUT_BYTES,
13    LocalTransactionConfig, PoolConfig, PriceBumpConfig, SubPoolLimit, DEFAULT_PRICE_BUMP,
14    DEFAULT_TXPOOL_ADDITIONAL_VALIDATION_TASKS, MAX_NEW_PENDING_TXS_NOTIFICATIONS,
15    REPLACE_BLOB_PRICE_BUMP, TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
16    TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT, TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
17};
18use std::time::Duration;
19
20/// Parameters for debugging purposes
21#[derive(Debug, Clone, Args, PartialEq, Eq)]
22#[command(next_help_heading = "TxPool")]
23pub struct TxPoolArgs {
24    /// Max number of transaction in the pending sub-pool.
25    #[arg(long = "txpool.pending-max-count", alias = "txpool.pending_max_count", default_value_t = TXPOOL_SUBPOOL_MAX_TXS_DEFAULT)]
26    pub pending_max_count: usize,
27    /// Max size of the pending sub-pool in megabytes.
28    #[arg(long = "txpool.pending-max-size", alias = "txpool.pending_max_size", default_value_t = TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT)]
29    pub pending_max_size: usize,
30
31    /// Max number of transaction in the basefee sub-pool
32    #[arg(long = "txpool.basefee-max-count", alias = "txpool.basefee_max_count", default_value_t = TXPOOL_SUBPOOL_MAX_TXS_DEFAULT)]
33    pub basefee_max_count: usize,
34    /// Max size of the basefee sub-pool in megabytes.
35    #[arg(long = "txpool.basefee-max-size", alias = "txpool.basefee_max_size", default_value_t = TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT)]
36    pub basefee_max_size: usize,
37
38    /// Max number of transaction in the queued sub-pool
39    #[arg(long = "txpool.queued-max-count", alias = "txpool.queued_max_count", default_value_t = TXPOOL_SUBPOOL_MAX_TXS_DEFAULT)]
40    pub queued_max_count: usize,
41    /// Max size of the queued sub-pool in megabytes.
42    #[arg(long = "txpool.queued-max-size", alias = "txpool.queued_max_size", default_value_t = TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT)]
43    pub queued_max_size: usize,
44
45    /// Max number of transaction in the blobpool
46    #[arg(long = "txpool.blobpool-max-count", alias = "txpool.blobpool_max_count", default_value_t = TXPOOL_SUBPOOL_MAX_TXS_DEFAULT)]
47    pub blobpool_max_count: usize,
48    /// Max size of the blobpool in megabytes.
49    #[arg(long = "txpool.blobpool-max-size", alias = "txpool.blobpool_max_size", default_value_t = TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT)]
50    pub blobpool_max_size: usize,
51
52    /// Max number of entries for the in memory cache of the blob store.
53    #[arg(long = "txpool.blob-cache-size", alias = "txpool.blob_cache_size")]
54    pub blob_cache_size: Option<u32>,
55
56    /// Max number of executable transaction slots guaranteed per account
57    #[arg(long = "txpool.max-account-slots", alias = "txpool.max_account_slots", default_value_t = TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER)]
58    pub max_account_slots: usize,
59
60    /// Price bump (in %) for the transaction pool underpriced check.
61    #[arg(long = "txpool.pricebump", default_value_t = DEFAULT_PRICE_BUMP)]
62    pub price_bump: u128,
63
64    /// Minimum base fee required by the protocol.
65    #[arg(long = "txpool.minimal-protocol-fee", default_value_t = MIN_PROTOCOL_BASE_FEE)]
66    pub minimal_protocol_basefee: u64,
67
68    /// The default enforced gas limit for transactions entering the pool
69    #[arg(long = "txpool.gas-limit", default_value_t = ETHEREUM_BLOCK_GAS_LIMIT_30M)]
70    pub enforced_gas_limit: u64,
71
72    /// Price bump percentage to replace an already existing blob transaction
73    #[arg(long = "blobpool.pricebump", default_value_t = REPLACE_BLOB_PRICE_BUMP)]
74    pub blob_transaction_price_bump: u128,
75
76    /// Max size in bytes of a single transaction allowed to enter the pool
77    #[arg(long = "txpool.max-tx-input-bytes", alias = "txpool.max_tx_input_bytes", default_value_t = DEFAULT_MAX_TX_INPUT_BYTES)]
78    pub max_tx_input_bytes: usize,
79
80    /// The maximum number of blobs to keep in the in memory blob cache.
81    #[arg(long = "txpool.max-cached-entries", alias = "txpool.max_cached_entries", default_value_t = DEFAULT_MAX_CACHED_BLOBS)]
82    pub max_cached_entries: u32,
83
84    /// Flag to disable local transaction exemptions.
85    #[arg(long = "txpool.nolocals")]
86    pub no_locals: bool,
87    /// Flag to allow certain addresses as local.
88    #[arg(long = "txpool.locals")]
89    pub locals: Vec<Address>,
90    /// Flag to toggle local transaction propagation.
91    #[arg(long = "txpool.no-local-transactions-propagation")]
92    pub no_local_transactions_propagation: bool,
93    /// Number of additional transaction validation tasks to spawn.
94    #[arg(long = "txpool.additional-validation-tasks", alias = "txpool.additional_validation_tasks", default_value_t = DEFAULT_TXPOOL_ADDITIONAL_VALIDATION_TASKS)]
95    pub additional_validation_tasks: usize,
96
97    /// Maximum number of pending transactions from the network to buffer
98    #[arg(long = "txpool.max-pending-txns", alias = "txpool.max_pending_txns", default_value_t = PENDING_TX_LISTENER_BUFFER_SIZE)]
99    pub pending_tx_listener_buffer_size: usize,
100
101    /// Maximum number of new transactions to buffer
102    #[arg(long = "txpool.max-new-txns", alias = "txpool.max_new_txns", default_value_t = NEW_TX_LISTENER_BUFFER_SIZE)]
103    pub new_tx_listener_buffer_size: usize,
104
105    /// How many new pending transactions to buffer and send to in progress pending transaction
106    /// iterators.
107    #[arg(long = "txpool.max-new-pending-txs-notifications", alias = "txpool.max-new-pending-txs-notifications", default_value_t = MAX_NEW_PENDING_TXS_NOTIFICATIONS)]
108    pub max_new_pending_txs_notifications: usize,
109
110    /// Maximum amount of time non-executable transaction are queued.
111    #[arg(long = "txpool.lifetime", value_parser = parse_duration_from_secs_or_ms, default_value = "10800", value_name = "DURATION")]
112    pub max_queued_lifetime: Duration,
113
114    /// Path to store the local transaction backup at, to survive node restarts.
115    #[arg(long = "txpool.transactions-backup", alias = "txpool.journal", value_name = "PATH")]
116    pub transactions_backup_path: Option<std::path::PathBuf>,
117
118    /// Disables transaction backup to disk on node shutdown.
119    #[arg(
120        long = "txpool.disable-transactions-backup",
121        alias = "txpool.disable-journal",
122        conflicts_with = "transactions_backup_path"
123    )]
124    pub disable_transactions_backup: bool,
125}
126
127impl Default for TxPoolArgs {
128    fn default() -> Self {
129        Self {
130            pending_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
131            pending_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
132            basefee_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
133            basefee_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
134            queued_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
135            queued_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
136            blobpool_max_count: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
137            blobpool_max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT,
138            blob_cache_size: None,
139            max_account_slots: TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
140            price_bump: DEFAULT_PRICE_BUMP,
141            minimal_protocol_basefee: MIN_PROTOCOL_BASE_FEE,
142            enforced_gas_limit: ETHEREUM_BLOCK_GAS_LIMIT_30M,
143            blob_transaction_price_bump: REPLACE_BLOB_PRICE_BUMP,
144            max_tx_input_bytes: DEFAULT_MAX_TX_INPUT_BYTES,
145            max_cached_entries: DEFAULT_MAX_CACHED_BLOBS,
146            no_locals: false,
147            locals: Default::default(),
148            no_local_transactions_propagation: false,
149            additional_validation_tasks: DEFAULT_TXPOOL_ADDITIONAL_VALIDATION_TASKS,
150            pending_tx_listener_buffer_size: PENDING_TX_LISTENER_BUFFER_SIZE,
151            new_tx_listener_buffer_size: NEW_TX_LISTENER_BUFFER_SIZE,
152            max_new_pending_txs_notifications: MAX_NEW_PENDING_TXS_NOTIFICATIONS,
153            max_queued_lifetime: MAX_QUEUED_TRANSACTION_LIFETIME,
154            transactions_backup_path: None,
155            disable_transactions_backup: false,
156        }
157    }
158}
159
160impl RethTransactionPoolConfig for TxPoolArgs {
161    /// Returns transaction pool configuration.
162    fn pool_config(&self) -> PoolConfig {
163        PoolConfig {
164            local_transactions_config: LocalTransactionConfig {
165                no_exemptions: self.no_locals,
166                local_addresses: self.locals.clone().into_iter().collect(),
167                propagate_local_transactions: !self.no_local_transactions_propagation,
168            },
169            pending_limit: SubPoolLimit {
170                max_txs: self.pending_max_count,
171                max_size: self.pending_max_size.saturating_mul(1024 * 1024),
172            },
173            basefee_limit: SubPoolLimit {
174                max_txs: self.basefee_max_count,
175                max_size: self.basefee_max_size.saturating_mul(1024 * 1024),
176            },
177            queued_limit: SubPoolLimit {
178                max_txs: self.queued_max_count,
179                max_size: self.queued_max_size.saturating_mul(1024 * 1024),
180            },
181            blob_limit: SubPoolLimit {
182                max_txs: self.blobpool_max_count,
183                max_size: self.blobpool_max_size.saturating_mul(1024 * 1024),
184            },
185            blob_cache_size: self.blob_cache_size,
186            max_account_slots: self.max_account_slots,
187            price_bumps: PriceBumpConfig {
188                default_price_bump: self.price_bump,
189                replace_blob_tx_price_bump: self.blob_transaction_price_bump,
190            },
191            minimal_protocol_basefee: self.minimal_protocol_basefee,
192            gas_limit: self.enforced_gas_limit,
193            pending_tx_listener_buffer_size: self.pending_tx_listener_buffer_size,
194            new_tx_listener_buffer_size: self.new_tx_listener_buffer_size,
195            max_new_pending_txs_notifications: self.max_new_pending_txs_notifications,
196            max_queued_lifetime: self.max_queued_lifetime,
197        }
198    }
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204    use clap::Parser;
205
206    /// A helper type to parse Args more easily
207    #[derive(Parser)]
208    struct CommandParser<T: Args> {
209        #[command(flatten)]
210        args: T,
211    }
212
213    #[test]
214    fn txpool_args_default_sanity_test() {
215        let default_args = TxPoolArgs::default();
216        let args = CommandParser::<TxPoolArgs>::parse_from(["reth"]).args;
217        assert_eq!(args, default_args);
218    }
219
220    #[test]
221    fn txpool_parse_locals() {
222        let args = CommandParser::<TxPoolArgs>::parse_from([
223            "reth",
224            "--txpool.locals",
225            "0x0000000000000000000000000000000000000000",
226        ])
227        .args;
228        assert_eq!(args.locals, vec![Address::ZERO]);
229    }
230
231    #[test]
232    fn txpool_parse_max_tx_lifetime() {
233        // Test with a custom duration
234        let args =
235            CommandParser::<TxPoolArgs>::parse_from(["reth", "--txpool.lifetime", "300"]).args;
236        assert_eq!(args.max_queued_lifetime, Duration::from_secs(300));
237
238        // Test with the default value
239        let args = CommandParser::<TxPoolArgs>::parse_from(["reth"]).args;
240        assert_eq!(args.max_queued_lifetime, Duration::from_secs(3 * 60 * 60)); // Default is 3h
241    }
242
243    #[test]
244    fn txpool_parse_max_tx_lifetime_invalid() {
245        let result =
246            CommandParser::<TxPoolArgs>::try_parse_from(["reth", "--txpool.lifetime", "invalid"]);
247
248        assert!(result.is_err(), "Expected an error for invalid duration");
249    }
250}