-
Notifications
You must be signed in to change notification settings - Fork 201
feat(l1): cap mempool pending transactions per sender (default 16) #6603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ilitteri
wants to merge
10
commits into
main
Choose a base branch
from
feat/mempool-account-slots-cap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1dd07d1
feat(l1): cap mempool pending transactions per sender (default 16)
ilitteri b70ce5b
feat(l1): drop peer-client references from --mempool.account-slots help
ilitteri 5838ac8
rename(l1): mempool per-account cap from account_slots to max_pending…
ilitteri f6de3cf
feat(l1): use DEFAULT_MAX_PENDING_TXS_PER_ACCOUNT constant in cli.rs …
ilitteri 00c681e
fix(l1): run chain_id check before per-account cap for better diagnostic
ilitteri 149b829
fix(l1): clarify MaxPendingTxsPerAccountExceeded error wording
ilitteri 3df6bc8
fix(l1): make per-account pending-tx cap atomic with insertion
ilitteri d52d06b
Merge remote-tracking branch 'origin/main' into feat/mempool-account-…
ilitteri ba25e61
Merge remote-tracking branch 'origin/main' into feat/mempool-account-…
ilitteri 887431b
Update three test add_transaction callers for new 4-arg signature
ilitteri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ use std::{ | |
|
|
||
| use clap::{ArgAction, Parser as ClapParser, Subcommand as ClapSubcommand}; | ||
| use ethrex_blockchain::{ | ||
| BlockchainOptions, BlockchainType, L2Config, | ||
| BlockchainOptions, BlockchainType, DEFAULT_MAX_PENDING_TXS_PER_ACCOUNT, L2Config, | ||
| error::{ChainError, InvalidBlockError}, | ||
| }; | ||
| use ethrex_common::types::{Block, DEFAULT_BUILDER_GAS_CEIL, Genesis, validate_block_body}; | ||
|
|
@@ -183,6 +183,15 @@ pub struct Options { | |
| env = "ETHREX_MEMPOOL_MAX_SIZE" | ||
| )] | ||
| pub mempool_max_size: usize, | ||
| #[arg( | ||
| help = "Maximum number of pending transactions a single sender may hold in the mempool. Replacements at an existing (sender, nonce) bypass this cap.", | ||
| long = "mempool.max-pending-txs-per-account", | ||
| default_value_t = DEFAULT_MAX_PENDING_TXS_PER_ACCOUNT, | ||
| value_name = "MAX_PENDING_TXS_PER_ACCOUNT", | ||
| help_heading = "Node options", | ||
| env = "ETHREX_MEMPOOL_MAX_PENDING_TXS_PER_ACCOUNT" | ||
| )] | ||
| pub mempool_max_pending_txs_per_account: usize, | ||
| #[arg( | ||
| long = "http.addr", | ||
| default_value = "127.0.0.1", | ||
|
|
@@ -464,6 +473,7 @@ impl Default for Options { | |
| dev: Default::default(), | ||
| force: false, | ||
| mempool_max_size: Default::default(), | ||
| mempool_max_pending_txs_per_account: DEFAULT_MAX_PENDING_TXS_PER_ACCOUNT, | ||
| tx_broadcasting_time_interval: Default::default(), | ||
|
Comment on lines
473
to
477
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| target_peers: Default::default(), | ||
| lookup_interval: Default::default(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,14 +142,33 @@ impl Mempool { | |
| .map_err(|error| StoreError::MempoolReadLock(error.to_string())) | ||
| } | ||
|
|
||
| /// Add transaction to the pool without doing validity checks | ||
| /// Add transaction to the pool without doing validity checks. | ||
| /// | ||
| /// Enforces the per-sender pending-tx cap atomically: the count is | ||
| /// re-checked under the same write lock that performs the insertion. | ||
| /// Replacement candidates (same `(sender, nonce)`) must have already | ||
| /// been removed via `remove_transaction` so this counter reflects the | ||
| /// post-replacement state. Returns | ||
| /// [`MempoolError::MaxPendingTxsPerAccountExceeded`] if the cap would | ||
| /// be exceeded. | ||
| pub fn add_transaction( | ||
| &self, | ||
| hash: H256, | ||
| sender: Address, | ||
| transaction: MempoolTransaction, | ||
| ) -> Result<(), StoreError> { | ||
| max_pending_txs_per_account: usize, | ||
| ) -> Result<(), MempoolError> { | ||
| let mut inner = self.write()?; | ||
| let count = inner | ||
| .txs_by_sender_nonce | ||
| .range((sender, 0)..=(sender, u64::MAX)) | ||
| .count(); | ||
| if count >= max_pending_txs_per_account { | ||
| return Err(MempoolError::MaxPendingTxsPerAccountExceeded { | ||
| count, | ||
| limit: max_pending_txs_per_account, | ||
| }); | ||
| } | ||
| // Prune the order queue if it has grown too much | ||
| if inner.txs_order.len() > inner.mempool_prune_threshold { | ||
| // NOTE: we do this to avoid borrow checker errors | ||
|
|
@@ -452,6 +471,17 @@ impl Mempool { | |
| Ok(contains) | ||
| } | ||
|
|
||
| /// Returns the number of pending transactions currently held in the | ||
| /// mempool for `sender`. Used by the per-sender slot cap at admission. | ||
| pub fn count_for_sender(&self, sender: Address) -> Result<usize, MempoolError> { | ||
| let inner = self.read()?; | ||
| let count = inner | ||
| .txs_by_sender_nonce | ||
| .range((sender, 0)..=(sender, u64::MAX)) | ||
| .count(); | ||
|
Comment on lines
+479
to
+481
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is bounded, so I think it's OK for now. |
||
| Ok(count) | ||
| } | ||
|
|
||
| pub fn find_tx_to_replace( | ||
| &self, | ||
| sender: Address, | ||
|
|
@@ -599,3 +629,70 @@ pub fn transaction_intrinsic_gas( | |
|
|
||
| Ok(gas) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use ethrex_common::types::EIP1559Transaction; | ||
|
|
||
| fn build_tx(nonce: u64) -> Transaction { | ||
| Transaction::EIP1559Transaction(EIP1559Transaction { | ||
| nonce, | ||
| ..Default::default() | ||
| }) | ||
| } | ||
|
|
||
| fn add_tx(pool: &Mempool, sender: Address, nonce: u64) -> H256 { | ||
| let tx = build_tx(nonce); | ||
| let mtx = MempoolTransaction::new(tx, sender); | ||
| let hash = mtx.hash(); | ||
| pool.add_transaction(hash, sender, mtx, usize::MAX).unwrap(); | ||
| hash | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_for_sender_empty_pool() { | ||
| let pool = Mempool::new(64); | ||
| let sender = Address::from_low_u64_be(1); | ||
| assert_eq!(pool.count_for_sender(sender).unwrap(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_for_sender_one_tx() { | ||
| let pool = Mempool::new(64); | ||
| let sender = Address::from_low_u64_be(1); | ||
| add_tx(&pool, sender, 0); | ||
| assert_eq!(pool.count_for_sender(sender).unwrap(), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_for_sender_many_nonces() { | ||
| let pool = Mempool::new(64); | ||
| let sender = Address::from_low_u64_be(1); | ||
| for nonce in 0..5 { | ||
| add_tx(&pool, sender, nonce); | ||
| } | ||
| assert_eq!(pool.count_for_sender(sender).unwrap(), 5); | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_for_sender_isolates_senders() { | ||
| let pool = Mempool::new(64); | ||
| let a = Address::from_low_u64_be(1); | ||
| let b = Address::from_low_u64_be(2); | ||
| add_tx(&pool, a, 0); | ||
| add_tx(&pool, a, 1); | ||
| add_tx(&pool, b, 0); | ||
| assert_eq!(pool.count_for_sender(a).unwrap(), 2); | ||
| assert_eq!(pool.count_for_sender(b).unwrap(), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn count_for_sender_unknown_returns_zero() { | ||
| let pool = Mempool::new(64); | ||
| let a = Address::from_low_u64_be(1); | ||
| let b = Address::from_low_u64_be(2); | ||
| add_tx(&pool, a, 0); | ||
| assert_eq!(pool.count_for_sender(b).unwrap(), 0); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f6de3cf