Skip to content

Commit

Permalink
feat(mempool): implement commit-block first part that removes txs fro…
Browse files Browse the repository at this point in the history
…m queue
  • Loading branch information
MohammadNassar1 committed Jul 14, 2024
1 parent 8d147b2 commit 9986c59
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,20 @@ impl Mempool {
// push back.
pub fn commit_block(
&mut self,
_state_changes: HashMap<ContractAddress, AccountState>,
state_changes: HashMap<ContractAddress, AccountState>,
) -> MempoolResult<()> {
for (address, AccountState { nonce }) in state_changes {
// Dequeue transactions from the queue in the following cases:
// 1. Remove a transaction from queue with nonce lower than those committed to the
// block, applicable when the block is from the same leader.
// 2. Remove a transaction from queue with nonce greater than those committed to the
// block, applicable when the block is from a different leader.
if self.tx_queue.get_nonce(address).is_some_and(|queued_nonce| queued_nonce != nonce) {
self.tx_queue.remove(address);
}
// TODO: remove the transactions from the tx_pool.
}
// TODO: update the tx_queue with the new state changes.
todo!()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/mempool/src/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ impl TransactionQueue {
self.queue.iter().rev().map(|tx| &tx.0)
}

pub fn _get_nonce(&self, address: ContractAddress) -> Option<Nonce> {
pub fn get_nonce(&self, address: ContractAddress) -> Option<Nonce> {
self.address_to_tx.get(&address).map(|tx| tx.nonce)
}

/// Removes the transaction of the given account address from the queue.
/// This is well-defined, since there is at most one transaction per address in the queue.
pub fn _remove(&mut self, address: ContractAddress) -> bool {
pub fn remove(&mut self, address: ContractAddress) -> bool {
if let Some(tx) = self.address_to_tx.remove(&address) {
return self.queue.remove(&tx.into());
}
Expand Down

0 comments on commit 9986c59

Please sign in to comment.