Skip to content

Commit

Permalink
feat(mempool): implement remove from nonce for account transaction index
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Jul 16, 2024
1 parent 7ef1547 commit 50079e8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/mempool/src/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,27 @@ impl AccountTransactionIndex {
fn get(&self, address: ContractAddress, nonce: Nonce) -> Option<&TransactionReference> {
self.0.get(&address)?.get(&nonce)
}

fn _remove_txs_up_to_nonce(
&mut self,
address: ContractAddress,
nonce: Nonce,
) -> Vec<TransactionReference> {
let Some(btree_map) = self.0.get_mut(&address) else {
return Vec::default();
};

let mut txs: Vec<TransactionReference> = Vec::new();
let nonces_to_remove: Vec<Nonce> = btree_map.range(..=nonce).map(|(&n, _)| n).collect();
for n in nonces_to_remove {
txs.push(btree_map.remove(&n).expect("Failed to remove nonce from BTreeMap"));
}

// Remove the entry from the HashMap if the BTreeMap is empty
if btree_map.is_empty() {
self.0.remove(&address);
}

txs
}
}

0 comments on commit 50079e8

Please sign in to comment.