Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change inner AddressPrioQueue #240

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions crates/mempool/src/priority_queue.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::collections::{BTreeSet, VecDeque};

use starknet_mempool_types::mempool_types::ThinTransaction;
// Assumption: for the MVP only one transaction from the same contract class can be in the mempool
Expand Down Expand Up @@ -61,22 +61,32 @@ impl PartialOrd for PrioritizedTransaction {

// TODO: remove when is used.
#[allow(dead_code)]
// Assumption: there are no gaps, and the transactions are received in order.
pub struct AddressPriorityQueue(pub Vec<ThinTransaction>);
// Invariant: Transactions have strictly increasing nonces, without gaps.
// Assumption: Transactions are provided in the correct order.
#[derive(Default)]
pub struct AddressPriorityQueue(VecDeque<ThinTransaction>);

// TODO: remove when is used.
#[allow(dead_code)]
impl AddressPriorityQueue {
pub fn push(&mut self, tx: ThinTransaction) {
self.0.push(tx);
if let Some(last_tx) = self.0.back() {
assert_eq!(
tx.nonce,
last_tx.nonce.try_increment().expect("Nonce overflow."),
"Nonces must be strictly increasing without gaps."
);
}

self.0.push_back(tx);
}

pub fn top(&self) -> Option<&ThinTransaction> {
self.0.first()
self.0.front()
}

pub fn pop_front(&mut self) -> ThinTransaction {
self.0.remove(0)
pub fn pop_front(&mut self) -> Option<ThinTransaction> {
self.0.pop_front()
}

pub fn is_empty(&self) -> bool {
Expand Down
Loading