Skip to content

Commit

Permalink
refactor(mempool): rearrange insert_tx and commit block before refact…
Browse files Browse the repository at this point in the history
…oring code to share functionality
  • Loading branch information
ayeletstarkware committed Aug 19, 2024
1 parent 369e892 commit 80f1bd5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,22 @@ impl Mempool {
let next_nonce = nonce.try_increment().map_err(|_| MempoolError::FeltOutOfRange)?;

// Align the queue with the committed nonces.

// Remove transactions with lower nonces from the queue and the pool.
if self
.tx_queue
.get_nonce(address)
.is_some_and(|queued_nonce| queued_nonce != next_nonce)
{
self.tx_queue.remove(address);
}
self.tx_pool.remove_up_to_nonce(address, next_nonce);

if self.tx_queue.get_nonce(address).is_none() {
if let Some(tx) = self.tx_pool.get_by_address_and_nonce(address, next_nonce) {
self.tx_queue.insert(*tx);
}
}

self.tx_pool.remove_up_to_nonce(address, next_nonce);
}

// Rewind nonces of addresses that were not included in block.
Expand All @@ -126,15 +127,16 @@ impl Mempool {
let MempoolInput { tx, account: Account { sender_address, state: AccountState { nonce } } } =
input;

self.tx_pool.insert((&tx).into())?;

// Remove transactions with lower nonces from the queue and the pool.
// Note: != is actually equivalent to > here, as lower nonces are rejected in validation.
if self.tx_queue.get_nonce(sender_address).is_some_and(|queued_nonce| queued_nonce != nonce)
{
self.tx_queue.remove(sender_address);
}
self.tx_pool.remove_up_to_nonce(sender_address, nonce);

self.tx_pool.insert((&tx).into())?;

// Maybe close nonce gap.
if self.tx_queue.get_nonce(sender_address).is_none() {
if let Some(tx_reference) = self.tx_pool.get_by_address_and_nonce(sender_address, nonce)
Expand Down

0 comments on commit 80f1bd5

Please sign in to comment.