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 20, 2024
1 parent 591f6f6 commit 8ae7bca
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions crates/mempool/src/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ use starknet_api::core::{ContractAddress, Nonce};

use crate::mempool::TransactionReference;

// A queue holding the transaction that with nonces that match account nonces.
// Note: the derived comparison functionality considers the order guaranteed by the data structures
// used.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct TransactionQueue {
// Priority queue of transactions with associated priority.
// Transactions with gas price above base price (sorted by tip).
priority_queue: BTreeSet<PriorityTransaction>,
// Transactions with gas price below base price (sorted by 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 +31,19 @@ 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_reference.into()),
"Keys should be unique; duplicates are checked prior."
);

if tx_reference.get_l2_gas_price() < self.gas_price_threshold {
// self.gas_price_threshold} {
assert!(
self.pending_queue.insert(tx_reference.into()),
"Keys should be unique; duplicates are checked prior."
);
} else {
assert!(
self.priority_queue.insert(tx_reference.into()),
"Keys should be unique; duplicates are checked prior."
);
}
}

// TODO(gilad): remove collect
Expand All @@ -57,14 +70,16 @@ impl TransactionQueue {
/// 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 {
if let Some(tx) = self.address_to_tx.remove(&address) {
return self.priority_queue.remove(&tx.into());
}
false
let Some(tx_reference) = self.address_to_tx.remove(&address) else {
return false;
};

self.priority_queue.remove(&tx_reference.clone().into())
|| self.pending_queue.remove(&tx_reference.into())
}

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 8ae7bca

Please sign in to comment.