-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update priority queue to use ThinPriorityTransaction
- Loading branch information
1 parent
7a0cf05
commit ac99c1c
Showing
4 changed files
with
67 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
use std::cmp::Ordering; | ||
use std::collections::BTreeSet; | ||
|
||
use starknet_api::core::Nonce; | ||
use starknet_api::transaction::{Tip, TransactionHash}; | ||
use starknet_mempool_types::mempool_types::ThinTransaction; | ||
// Assumption: for the MVP only one transaction from the same contract class can be in the mempool | ||
// at a time. When this changes, saving the transactions themselves on the queu might no longer be | ||
// appropriate, because we'll also need to stores transactions without indexing them. For example, | ||
// transactions with future nonces will need to be stored, and potentially indexed on block commits. | ||
#[derive(Clone, Debug, Default, derive_more::Deref, derive_more::DerefMut)] | ||
pub struct TransactionPriorityQueue(BTreeSet<PrioritizedTransaction>); | ||
pub struct TransactionPriorityQueue(BTreeSet<ThinPriorityTransaction>); | ||
|
||
impl TransactionPriorityQueue { | ||
pub fn push(&mut self, tx: ThinTransaction) { | ||
let mempool_tx = PrioritizedTransaction(tx); | ||
self.insert(mempool_tx); | ||
pub fn push(&mut self, tx: ThinPriorityTransaction) { | ||
self.insert(tx); | ||
} | ||
|
||
// TODO(gilad): remove collect | ||
pub fn pop_last_chunk(&mut self, n_txs: usize) -> Vec<ThinTransaction> { | ||
(0..n_txs).filter_map(|_| self.pop_last().map(|tx| tx.0)).collect() | ||
pub fn pop_last_chunk(&mut self, n_txs: usize) -> Vec<ThinPriorityTransaction> { | ||
(0..n_txs).filter_map(|_| self.pop_last()).collect() | ||
} | ||
} | ||
|
||
impl From<Vec<ThinTransaction>> for TransactionPriorityQueue { | ||
fn from(transactions: Vec<ThinTransaction>) -> Self { | ||
TransactionPriorityQueue(BTreeSet::from_iter( | ||
transactions.into_iter().map(PrioritizedTransaction), | ||
)) | ||
} | ||
#[derive(Clone, Debug, Default)] | ||
pub struct ThinPriorityTransaction { | ||
pub nonce: Nonce, | ||
pub tx_hash: TransactionHash, | ||
pub tip: Tip, | ||
} | ||
|
||
#[derive(Clone, Debug, derive_more::Deref, derive_more::From)] | ||
pub struct PrioritizedTransaction(pub ThinTransaction); | ||
|
||
/// Compare transactions based only on their tip, a uint, using the Eq trait. It ensures that two | ||
/// tips are either exactly equal or not. | ||
impl PartialEq for PrioritizedTransaction { | ||
fn eq(&self, other: &PrioritizedTransaction) -> bool { | ||
impl PartialEq for ThinPriorityTransaction { | ||
fn eq(&self, other: &ThinPriorityTransaction) -> bool { | ||
self.tip == other.tip | ||
} | ||
} | ||
|
||
/// Marks this struct as capable of strict equality comparisons, signaling to the compiler it | ||
/// adheres to equality semantics. | ||
// Note: this depends on the implementation of `PartialEq`, see its docstring. | ||
impl Eq for PrioritizedTransaction {} | ||
impl Eq for ThinPriorityTransaction {} | ||
|
||
impl Ord for PrioritizedTransaction { | ||
impl Ord for ThinPriorityTransaction { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.tip.cmp(&other.tip) | ||
} | ||
} | ||
|
||
impl PartialOrd for PrioritizedTransaction { | ||
impl PartialOrd for ThinPriorityTransaction { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl From<ThinTransaction> for ThinPriorityTransaction { | ||
fn from(tx: ThinTransaction) -> Self { | ||
ThinPriorityTransaction { nonce: tx.nonce, tx_hash: tx.tx_hash, tip: tx.tip } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters