Skip to content

Commit

Permalink
refactor(mempool): rearrange insert_tx before refactoring code (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeletstarkware authored Aug 20, 2024
1 parent 7ba1089 commit 0a81470
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,24 @@ impl Mempool {
for (&address, AccountState { nonce }) in &state_changes {
let next_nonce = nonce.try_increment().map_err(|_| MempoolError::FeltOutOfRange)?;

// Align the queue with the committed nonces.
// Maybe remove out-of-date transactions.
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);

// Maybe close nonce gap.
if self.tx_queue.get_nonce(address).is_none() {
if let Some(tx_reference) =
self.tx_pool.get_by_address_and_nonce(address, next_nonce)
{
self.tx_queue.insert(tx_reference.clone());
}
}

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

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

self.tx_pool.insert(tx)?;

// Maybe remove out-of-date transactions.
// 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)?;

// 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 0a81470

Please sign in to comment.