From 3b938f5842ddb5e5be281499879b16d39bf4fd31 Mon Sep 17 00:00:00 2001 From: Mohammad Nassar Date: Thu, 18 Jul 2024 15:24:38 +0300 Subject: [PATCH] feat(mempool): remove addresses of reverted transactions from queue in commit block method --- crates/mempool/src/mempool.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs index bf2546d2..44c04802 100644 --- a/crates/mempool/src/mempool.rs +++ b/crates/mempool/src/mempool.rs @@ -83,7 +83,7 @@ impl Mempool { &mut self, state_changes: HashMap, ) -> MempoolResult<()> { - for (address, AccountState { nonce }) in state_changes { + for (&address, AccountState { nonce }) in &state_changes { let next_nonce = nonce.try_increment().map_err(|_| MempoolError::FeltOutOfRange)?; // Align the queue with the committed nonces. @@ -96,13 +96,23 @@ impl Mempool { } if self.tx_queue.get_nonce(address).is_none() { - if let Some(tx) = self.tx_pool.get_by_address_and_nonce(address, nonce) { + 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. + let addresses_not_included_in_block = + self.mempool_state.keys().filter(|&key| !state_changes.contains_key(key)); + for address in addresses_not_included_in_block { + self.tx_queue.remove(*address); + } + + self.mempool_state.clear(); + Ok(()) }