Skip to content

Commit

Permalink
feat(mempool): add pending queue to tx queue
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Aug 18, 2024
1 parent 3cba78d commit 7502d6e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions crates/mempool/src/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use crate::mempool::TransactionReference;
pub struct TransactionQueue {
// Priority queue of transactions with associated priority.
priority_queue: BTreeSet<QueuedTransaction>,
// queue of transaction with low gas price.
pending_queue: BTreeSet<PendingTransaction>,
gas_price_threshold: u128,
// Set of account addresses for efficient existence checks.
address_to_tx: HashMap<ContractAddress, TransactionReference>,
}
Expand All @@ -27,10 +30,17 @@ impl TransactionQueue {
"Only a single transaction from the same contract class can be in the mempool at a \
time."
);
assert!(
self.priority_queue.insert(tx.into()),
"Keys should be unique; duplicates are checked prior."
);
if tx.get_l2_gas_price() < Some(self.gas_price_threshold) {
assert!(
self.pending_queue.insert(tx.into()),
"Keys should be unique; duplicates are checked prior."
);
} else {
assert!(
self.priority_queue.insert(tx.into()),
"Keys should be unique; duplicates are checked prior."
);
}
}

// TODO(gilad): remove collect
Expand Down Expand Up @@ -58,13 +68,14 @@ impl TransactionQueue {
/// This is well-defined, since there is at most one transaction per address in the queue.
pub fn remove(&mut self, address: ContractAddress) -> bool {
if let Some(tx) = self.address_to_tx.remove(&address) {
return self.priority_queue.remove(&tx.into());
return self.priority_queue.remove(&tx.clone().into())
|| self.pending_queue.remove(&tx.into());
}
false
}

pub fn is_empty(&self) -> bool {
self.priority_queue.is_empty()
self.priority_queue.is_empty() && self.pending_queue.is_empty()
}
}

Expand Down

0 comments on commit 7502d6e

Please sign in to comment.