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
11pub const TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER: usize = 16;
13
14pub const TXPOOL_SUBPOOL_MAX_TXS_DEFAULT: usize = 10_000;
16
17pub const TXPOOL_SUBPOOL_MAX_SIZE_MB_DEFAULT: usize = 20;
19
20pub const DEFAULT_TXPOOL_ADDITIONAL_VALIDATION_TASKS: usize = 1;
22
23pub const DEFAULT_PRICE_BUMP: u128 = 10;
25
26pub const REPLACE_BLOB_PRICE_BUMP: u128 = 100;
30
31pub const MAX_NEW_PENDING_TXS_NOTIFICATIONS: usize = 200;
33
34pub const DEFAULT_MAX_INFLIGHT_DELEGATED_SLOTS: usize = 1;
36
37#[derive(Debug, Clone)]
39pub struct PoolConfig {
40 pub pending_limit: SubPoolLimit,
42 pub basefee_limit: SubPoolLimit,
44 pub queued_limit: SubPoolLimit,
46 pub blob_limit: SubPoolLimit,
48 pub blob_cache_size: Option<u32>,
50 pub max_account_slots: usize,
52 pub price_bumps: PriceBumpConfig,
54 pub minimal_protocol_basefee: u64,
56 pub minimum_priority_fee: Option<u128>,
58 pub gas_limit: u64,
60 pub local_transactions_config: LocalTransactionConfig,
63 pub pending_tx_listener_buffer_size: usize,
65 pub new_tx_listener_buffer_size: usize,
67 pub max_new_pending_txs_notifications: usize,
69 pub max_queued_lifetime: Duration,
71 pub max_inflight_delegated_slot_limit: usize,
75}
76
77impl PoolConfig {
78 pub const fn with_disabled_protocol_base_fee(self) -> Self {
82 self.with_protocol_base_fee(0)
83 }
84
85 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 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 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
138pub struct SubPoolLimit {
139 pub max_txs: usize,
141 pub max_size: usize,
143}
144
145impl SubPoolLimit {
146 pub const fn new(max_txs: usize, max_size: usize) -> Self {
148 Self { max_txs, max_size }
149 }
150
151 pub const fn max() -> Self {
153 Self::new(usize::MAX, usize::MAX)
154 }
155
156 #[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 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#[derive(Debug, Clone, Copy, Eq, PartialEq)]
184pub struct PriceBumpConfig {
185 pub default_price_bump: u128,
187 pub replace_blob_tx_price_bump: u128,
189}
190
191impl PriceBumpConfig {
192 #[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#[derive(Debug, Clone, Eq, PartialEq)]
214pub struct LocalTransactionConfig {
215 pub no_exemptions: bool,
222 pub local_addresses: HashSet<Address>,
224 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 #[inline]
241 pub const fn no_local_exemptions(&self) -> bool {
242 self.no_exemptions
243 }
244
245 #[inline]
247 pub fn contains_local_address(&self, address: &Address) -> bool {
248 self.local_addresses.contains(address)
249 }
250
251 #[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 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 let config = PoolConfig::default();
294 assert!(!config.is_exceeded(pool_size));
295
296 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 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 assert!(config.contains_local_address(&address));
338
339 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 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 assert!(config.is_local(TransactionOrigin::Local, &Address::new([2; 20])));
367 assert!(config.is_local(TransactionOrigin::Local, &address));
368
369 assert!(config.is_local(TransactionOrigin::External, &address));
371 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}