diff --git a/crates/transaction-pool/src/config.rs b/crates/transaction-pool/src/config.rs index 9671dcd41c..5472647e0e 100644 --- a/crates/transaction-pool/src/config.rs +++ b/crates/transaction-pool/src/config.rs @@ -1,13 +1,26 @@ +/// Guarantees max transactions for one sender, compatible with geth/erigon +pub(crate) const MAX_ACCOUNT_SLOTS_PER_SENDER: usize = 16; + ///! Configuration options for the Transaction pool. #[derive(Debug, Clone)] pub struct PoolConfig { - // TODO add limits for subpools - // TODO limits for per peer - // TODO config whether to check if transactions are banned + /// Max number of transaction in the pending sub-pool + pub pending_limit: usize, + /// Max number of transaction in the basefee sub-pool + pub basefee_limit: usize, + /// Max number of transaction in the queued sub-pool + pub queued_limit: usize, + /// Max number of executable transaction slots guaranteed per account + pub max_account_slots: usize, } impl Default for PoolConfig { fn default() -> Self { - todo!() + Self { + pending_limit: 10_000, + basefee_limit: 10_000, + queued_limit: 10_000, + max_account_slots: MAX_ACCOUNT_SLOTS_PER_SENDER, + } } } diff --git a/crates/transaction-pool/src/pool/mod.rs b/crates/transaction-pool/src/pool/mod.rs index 84cdad681f..20c8697cf9 100644 --- a/crates/transaction-pool/src/pool/mod.rs +++ b/crates/transaction-pool/src/pool/mod.rs @@ -114,15 +114,15 @@ where T: TransactionOrdering::Transaction>, { /// Create a new transaction pool instance. - pub fn new(client: Arc, ordering: Arc, config: PoolConfig) -> Self { + pub fn new(validator: Arc, ordering: Arc, config: PoolConfig) -> Self { Self { identifiers: Default::default(), - validator: client, - config, + validator, event_listener: Default::default(), - pool: RwLock::new(TxPool::new(ordering)), + pool: RwLock::new(TxPool::new(ordering, config.clone())), pending_transaction_listener: Default::default(), transaction_listener: Default::default(), + config, } } diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index 9fba8da705..11a1a0ce72 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -1,5 +1,6 @@ //! The internal transaction pool implementation. use crate::{ + config::MAX_ACCOUNT_SLOTS_PER_SENDER, error::PoolError, identifier::{SenderId, TransactionId}, pool::{ @@ -9,7 +10,7 @@ use crate::{ state::{SubPool, TxState}, AddedPendingTransaction, AddedTransaction, }, - PoolResult, PoolTransaction, TransactionOrdering, ValidPoolTransaction, U256, + PoolConfig, PoolResult, PoolTransaction, TransactionOrdering, ValidPoolTransaction, U256, }; use fnv::FnvHashMap; use reth_primitives::TxHash; @@ -64,6 +65,8 @@ pub struct TxPool { sender_info: FnvHashMap, /// pending subpool pending_pool: PendingPool, + /// Pool settings to enforce limits etc. + config: PoolConfig, /// queued subpool /// /// Holds all parked transactions that depend on external changes from the sender: @@ -84,13 +87,14 @@ pub struct TxPool { impl TxPool { /// Create a new graph pool instance. - pub fn new(ordering: Arc) -> Self { + pub fn new(ordering: Arc, config: PoolConfig) -> Self { Self { sender_info: Default::default(), pending_pool: PendingPool::new(ordering), queued_pool: Default::default(), basefee_pool: Default::default(), - all_transactions: Default::default(), + all_transactions: AllTransactions::new(config.max_account_slots), + config, } } /// Updates the pool based on the changed base fee. @@ -313,6 +317,8 @@ pub struct AllTransactions { minimal_protocol_basefee: U256, /// The max gas limit of the block block_gas_limit: u64, + /// Max number of executable transaction slots guaranteed per account + max_account_slots: usize, /// _All_ transactions identified by their hash. by_hash: HashMap>>, /// _All_ transaction in the pool sorted by their sender and nonce pair. @@ -322,6 +328,11 @@ pub struct AllTransactions { } impl AllTransactions { + /// Create a new instance + fn new(max_account_slots: usize) -> Self { + Self { max_account_slots, ..Default::default() } + } + /// Returns if the transaction for the given hash is already included in this pool pub(crate) fn contains(&self, tx_hash: &TxHash) -> bool { self.by_hash.contains_key(tx_hash) @@ -588,6 +599,7 @@ impl AllTransactions { impl Default for AllTransactions { fn default() -> Self { Self { + max_account_slots: MAX_ACCOUNT_SLOTS_PER_SENDER, pending_basefee: Default::default(), minimal_protocol_basefee: MIN_PROTOCOL_BASE_FEE, block_gas_limit: 30_000_000, diff --git a/crates/transaction-pool/src/test_util/mock.rs b/crates/transaction-pool/src/test_util/mock.rs index 1284e7e329..c8b9392179 100644 --- a/crates/transaction-pool/src/test_util/mock.rs +++ b/crates/transaction-pool/src/test_util/mock.rs @@ -19,7 +19,7 @@ pub type MockValidTx = ValidPoolTransaction; /// Create an empty `TxPool` pub fn mock_tx_pool() -> MockTxPool { - MockTxPool::new(Arc::new(Default::default())) + MockTxPool::new(Arc::new(Default::default()), Default::default()) } /// Sets the value for the field diff --git a/crates/transaction-pool/src/test_util/pool.rs b/crates/transaction-pool/src/test_util/pool.rs index 5de45b3a52..a42e1987f9 100644 --- a/crates/transaction-pool/src/test_util/pool.rs +++ b/crates/transaction-pool/src/test_util/pool.rs @@ -41,7 +41,7 @@ impl MockPool { impl Default for MockPool { fn default() -> Self { - Self { pool: TxPool::new(Arc::new(MockOrdering::default())) } + Self { pool: TxPool::new(Arc::new(MockOrdering::default()), Default::default()) } } }