reth_transaction_pool/
config.rs

1use crate::{
2    maintain::MAX_QUEUED_TRANSACTION_LIFETIME,
3    pool::{NEW_TX_LISTENER_BUFFER_SIZE, PENDING_TX_LISTENER_BUFFER_SIZE},
4    PoolSize, TransactionOrigin,
5};
6use alloy_consensus::constants::EIP4844_TX_TYPE_ID;
7use alloy_eips::eip1559::{ETHEREUM_BLOCK_GAS_LIMIT_30M, MIN_PROTOCOL_BASE_FEE};
8use alloy_primitives::Address;
9use std::{collections::HashSet, ops::Mul, time::Duration};
10
11/// Guarantees max transactions for one sender, compatible with geth/erigon
12pub const TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER: usize = 16;
13
14/// The default maximum allowed number of transactions in the given subpool.
15pub const TXPOOL_SUBPOOL_MAX_TXS_DEFAULT: usize = 10_000;
16
17/// The default maximum allowed size of the given subpool.
18pub const TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT: usize = 20;
19
20/// The default additional validation tasks size.
21pub const DEFAULT_TXPOOL_ADDITIONAL_VALIDATION_TASKS: usize = 1;
22
23/// Default price bump (in %) for the transaction pool underpriced check.
24pub const DEFAULT_PRICE_BUMP: u128 = 10;
25
26/// Replace blob price bump (in %) for the transaction pool underpriced check.
27///
28/// This enforces that a blob transaction requires a 100% price bump to be replaced
29pub const REPLACE_BLOB_PRICE_BUMP: u128 = 100;
30
31/// Default maximum new transactions for broadcasting.
32pub const MAX_NEW_PENDING_TXS_NOTIFICATIONS: usize = 200;
33
34/// Default maximum allowed in flight delegated transactions per account.
35pub const DEFAULT_MAX_INFLIGHT_DELEGATED_SLOTS: usize = 1;
36
37/// Configuration options for the Transaction pool.
38#[derive(Debug, Clone)]
39pub struct PoolConfig {
40    /// Max number of transaction in the pending sub-pool
41    pub pending_limit: SubPoolLimit,
42    /// Max number of transaction in the basefee sub-pool
43    pub basefee_limit: SubPoolLimit,
44    /// Max number of transaction in the queued sub-pool
45    pub queued_limit: SubPoolLimit,
46    /// Max number of transactions in the blob sub-pool
47    pub blob_limit: SubPoolLimit,
48    /// Blob cache size
49    pub blob_cache_size: Option<u32>,
50    /// Max number of executable transaction slots guaranteed per account
51    pub max_account_slots: usize,
52    /// Price bump (in %) for the transaction pool underpriced check.
53    pub price_bumps: PriceBumpConfig,
54    /// Minimum base fee required by the protocol.
55    pub minimal_protocol_basefee: u64,
56    /// Minimum priority fee required for transaction acceptance into the pool.
57    pub minimum_priority_fee: Option<u128>,
58    /// The max gas limit for transactions in the pool
59    pub gas_limit: u64,
60    /// How to handle locally received transactions:
61    /// [`TransactionOrigin::Local`](TransactionOrigin).
62    pub local_transactions_config: LocalTransactionConfig,
63    /// Bound on number of pending transactions from `reth_network::TransactionsManager` to buffer.
64    pub pending_tx_listener_buffer_size: usize,
65    /// Bound on number of new transactions from `reth_network::TransactionsManager` to buffer.
66    pub new_tx_listener_buffer_size: usize,
67    /// How many new pending transactions to buffer and send iterators in progress.
68    pub max_new_pending_txs_notifications: usize,
69    /// Maximum lifetime for transactions in the pool
70    pub max_queued_lifetime: Duration,
71    /// The maximum allowed inflight transactions a delegated sender can have.
72    ///
73    /// This restricts how many executable transaction a delegated sender can stack.
74    pub max_inflight_delegated_slot_limit: usize,
75}
76
77impl PoolConfig {
78    /// Sets the minimal protocol base fee to 0, effectively disabling checks that enforce that a
79    /// transaction's fee must be higher than the [`MIN_PROTOCOL_BASE_FEE`] which is the lowest
80    /// value the ethereum EIP-1559 base fee can reach.
81    pub const fn with_disabled_protocol_base_fee(self) -> Self {
82        self.with_protocol_base_fee(0)
83    }
84
85    /// Configures the minimal protocol base fee that should be enforced.
86    ///
87    /// Ethereum's EIP-1559 base fee can't drop below [`MIN_PROTOCOL_BASE_FEE`] hence this is
88    /// enforced by default in the pool.
89    pub const fn with_protocol_base_fee(mut self, protocol_base_fee: u64) -> Self {
90        self.minimal_protocol_basefee = protocol_base_fee;
91        self
92    }
93
94    /// Configures how many slots are available for a delegated sender.
95    pub const fn with_max_inflight_delegated_slots(
96        mut self,
97        max_inflight_delegation_limit: usize,
98    ) -> Self {
99        self.max_inflight_delegated_slot_limit = max_inflight_delegation_limit;
100        self
101    }
102
103    /// Returns whether the size and amount constraints in any sub-pools are exceeded.
104    #[inline]
105    pub const fn is_exceeded(&self, pool_size: PoolSize) -> bool {
106        self.blob_limit.is_exceeded(pool_size.blob, pool_size.blob_size) ||
107            self.pending_limit.is_exceeded(pool_size.pending, pool_size.pending_size) ||
108            self.basefee_limit.is_exceeded(pool_size.basefee, pool_size.basefee_size) ||
109            self.queued_limit.is_exceeded(pool_size.queued, pool_size.queued_size)
110    }
111}
112
113impl Default for PoolConfig {
114    fn default() -> Self {
115        Self {
116            pending_limit: Default::default(),
117            basefee_limit: Default::default(),
118            queued_limit: Default::default(),
119            blob_limit: Default::default(),
120            blob_cache_size: None,
121            max_account_slots: TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
122            price_bumps: Default::default(),
123            minimal_protocol_basefee: MIN_PROTOCOL_BASE_FEE,
124            minimum_priority_fee: None,
125            gas_limit: ETHEREUM_BLOCK_GAS_LIMIT_30M,
126            local_transactions_config: Default::default(),
127            pending_tx_listener_buffer_size: PENDING_TX_LISTENER_BUFFER_SIZE,
128            new_tx_listener_buffer_size: NEW_TX_LISTENER_BUFFER_SIZE,
129            max_new_pending_txs_notifications: MAX_NEW_PENDING_TXS_NOTIFICATIONS,
130            max_queued_lifetime: MAX_QUEUED_TRANSACTION_LIFETIME,
131            max_inflight_delegated_slot_limit: DEFAULT_MAX_INFLIGHT_DELEGATED_SLOTS,
132        }
133    }
134}
135
136/// Size limits for a sub-pool.
137#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub struct SubPoolLimit {
139    /// Maximum amount of transaction in the pool.
140    pub max_txs: usize,
141    /// Maximum combined size (in bytes) of transactions in the pool.
142    pub max_size: usize,
143}
144
145impl SubPoolLimit {
146    /// Creates a new instance with the given limits.
147    pub const fn new(max_txs: usize, max_size: usize) -> Self {
148        Self { max_txs, max_size }
149    }
150
151    /// Creates an unlimited [`SubPoolLimit`]
152    pub const fn max() -> Self {
153        Self::new(usize::MAX, usize::MAX)
154    }
155
156    /// Returns whether the size or amount constraint is violated.
157    #[inline]
158    pub const fn is_exceeded(&self, txs: usize, size: usize) -> bool {
159        self.max_txs < txs || self.max_size < size
160    }
161}
162
163impl Mul<usize> for SubPoolLimit {
164    type Output = Self;
165
166    fn mul(self, rhs: usize) -> Self::Output {
167        let Self { max_txs, max_size } = self;
168        Self { max_txs: max_txs * rhs, max_size: max_size * rhs }
169    }
170}
171
172impl Default for SubPoolLimit {
173    fn default() -> Self {
174        // either 10k transactions or 20MB
175        Self {
176            max_txs: TXPOOL_SUBPOOL_MAX_TXS_DEFAULT,
177            max_size: TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT * 1024 * 1024,
178        }
179    }
180}
181
182/// Price bump config (in %) for the transaction pool underpriced check.
183#[derive(Debug, Clone, Copy, Eq, PartialEq)]
184pub struct PriceBumpConfig {
185    /// Default price bump (in %) for the transaction pool underpriced check.
186    pub default_price_bump: u128,
187    /// Replace blob price bump (in %) for the transaction pool underpriced check.
188    pub replace_blob_tx_price_bump: u128,
189}
190
191impl PriceBumpConfig {
192    /// Returns the price bump required to replace the given transaction type.
193    #[inline]
194    pub const fn price_bump(&self, tx_type: u8) -> u128 {
195        if tx_type == EIP4844_TX_TYPE_ID {
196            return self.replace_blob_tx_price_bump
197        }
198        self.default_price_bump
199    }
200}
201
202impl Default for PriceBumpConfig {
203    fn default() -> Self {
204        Self {
205            default_price_bump: DEFAULT_PRICE_BUMP,
206            replace_blob_tx_price_bump: REPLACE_BLOB_PRICE_BUMP,
207        }
208    }
209}
210
211/// Configuration options for the locally received transactions:
212/// [`TransactionOrigin::Local`](TransactionOrigin)
213#[derive(Debug, Clone, Eq, PartialEq)]
214pub struct LocalTransactionConfig {
215    /// Apply no exemptions to the locally received transactions.
216    ///
217    /// This includes:
218    ///   - available slots are limited to the configured `max_account_slots` of [`PoolConfig`]
219    ///   - no price exemptions
220    ///   - no eviction exemptions
221    pub no_exemptions: bool,
222    /// Addresses that will be considered as local. Above exemptions apply.
223    pub local_addresses: HashSet<Address>,
224    /// Flag indicating whether local transactions should be propagated.
225    pub propagate_local_transactions: bool,
226}
227
228impl Default for LocalTransactionConfig {
229    fn default() -> Self {
230        Self {
231            no_exemptions: false,
232            local_addresses: HashSet::default(),
233            propagate_local_transactions: true,
234        }
235    }
236}
237
238impl LocalTransactionConfig {
239    /// Returns whether local transactions are not exempt from the configured limits.
240    #[inline]
241    pub const fn no_local_exemptions(&self) -> bool {
242        self.no_exemptions
243    }
244
245    /// Returns whether the local addresses vector contains the given address.
246    #[inline]
247    pub fn contains_local_address(&self, address: &Address) -> bool {
248        self.local_addresses.contains(address)
249    }
250
251    /// Returns whether the particular transaction should be considered local.
252    ///
253    /// This always returns false if the local exemptions are disabled.
254    #[inline]
255    pub fn is_local(&self, origin: TransactionOrigin, sender: &Address) -> bool {
256        if self.no_local_exemptions() {
257            return false
258        }
259        origin.is_local() || self.contains_local_address(sender)
260    }
261
262    /// Sets toggle to propagate transactions received locally by this client (e.g
263    /// transactions from `eth_sendTransaction` to this nodes' RPC server)
264    ///
265    /// If set to false, only transactions received by network peers (via
266    /// p2p) will be marked as propagated in the local transaction pool and returned on a
267    /// `GetPooledTransactions` p2p request
268    pub const fn set_propagate_local_transactions(mut self, propagate_local_txs: bool) -> Self {
269        self.propagate_local_transactions = propagate_local_txs;
270        self
271    }
272}
273
274#[cfg(test)]
275mod tests {
276    use super::*;
277
278    #[test]
279    fn test_pool_size_sanity() {
280        let pool_size = PoolSize {
281            pending: 0,
282            pending_size: 0,
283            basefee: 0,
284            basefee_size: 0,
285            queued: 0,
286            queued_size: 0,
287            blob: 0,
288            blob_size: 0,
289            ..Default::default()
290        };
291
292        // the current size is zero so this should not exceed any limits
293        let config = PoolConfig::default();
294        assert!(!config.is_exceeded(pool_size));
295
296        // set them to be above the limits
297        let pool_size = PoolSize {
298            pending: config.pending_limit.max_txs + 1,
299            pending_size: config.pending_limit.max_size + 1,
300            basefee: config.basefee_limit.max_txs + 1,
301            basefee_size: config.basefee_limit.max_size + 1,
302            queued: config.queued_limit.max_txs + 1,
303            queued_size: config.queued_limit.max_size + 1,
304            blob: config.blob_limit.max_txs + 1,
305            blob_size: config.blob_limit.max_size + 1,
306            ..Default::default()
307        };
308
309        // now this should be above the limits
310        assert!(config.is_exceeded(pool_size));
311    }
312
313    #[test]
314    fn test_default_config() {
315        let config = LocalTransactionConfig::default();
316
317        assert!(!config.no_exemptions);
318        assert!(config.local_addresses.is_empty());
319        assert!(config.propagate_local_transactions);
320    }
321
322    #[test]
323    fn test_no_local_exemptions() {
324        let config = LocalTransactionConfig { no_exemptions: true, ..Default::default() };
325        assert!(config.no_local_exemptions());
326    }
327
328    #[test]
329    fn test_contains_local_address() {
330        let address = Address::new([1; 20]);
331        let mut local_addresses = HashSet::default();
332        local_addresses.insert(address);
333
334        let config = LocalTransactionConfig { local_addresses, ..Default::default() };
335
336        // Should contain the inserted address
337        assert!(config.contains_local_address(&address));
338
339        // Should not contain another random address
340        assert!(!config.contains_local_address(&Address::new([2; 20])));
341    }
342
343    #[test]
344    fn test_is_local_with_no_exemptions() {
345        let address = Address::new([1; 20]);
346        let config = LocalTransactionConfig {
347            no_exemptions: true,
348            local_addresses: HashSet::default(),
349            ..Default::default()
350        };
351
352        // Should return false as no exemptions is set to true
353        assert!(!config.is_local(TransactionOrigin::Local, &address));
354    }
355
356    #[test]
357    fn test_is_local_without_no_exemptions() {
358        let address = Address::new([1; 20]);
359        let mut local_addresses = HashSet::default();
360        local_addresses.insert(address);
361
362        let config =
363            LocalTransactionConfig { no_exemptions: false, local_addresses, ..Default::default() };
364
365        // Should return true as the transaction origin is local
366        assert!(config.is_local(TransactionOrigin::Local, &Address::new([2; 20])));
367        assert!(config.is_local(TransactionOrigin::Local, &address));
368
369        // Should return true as the address is in the local_addresses set
370        assert!(config.is_local(TransactionOrigin::External, &address));
371        // Should return false as the address is not in the local_addresses set
372        assert!(!config.is_local(TransactionOrigin::External, &Address::new([2; 20])));
373    }
374
375    #[test]
376    fn test_set_propagate_local_transactions() {
377        let config = LocalTransactionConfig::default();
378        assert!(config.propagate_local_transactions);
379
380        let new_config = config.set_propagate_local_transactions(false);
381        assert!(!new_config.propagate_local_transactions);
382    }
383
384    #[test]
385    fn scale_pool_limit() {
386        let limit = SubPoolLimit::default();
387        let double = limit * 2;
388        assert_eq!(
389            double,
390            SubPoolLimit { max_txs: limit.max_txs * 2, max_size: limit.max_size * 2 }
391        )
392    }
393}