Skip to content

Commit

Permalink
feat(mempool): add pending transaction type
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Aug 15, 2024
1 parent fcb733d commit 55308a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::{ResourceBoundsMapping, Tip, TransactionHash};
use starknet_api::transaction::{Resource, ResourceBoundsMapping, Tip, TransactionHash};
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{
Account,
Expand Down Expand Up @@ -235,4 +235,12 @@ impl TransactionReference {
resource_bounds: ResourceBoundsMapping::default(),
}
}

pub fn get_l1_gas_price(&self) -> Option<u128> {
self.resource_bounds.0.get(&Resource::L1Gas).map(|bounds| bounds.max_price_per_unit)
}

pub fn get_l2_gas_price(&self) -> Option<u128> {
self.resource_bounds.0.get(&Resource::L2Gas).map(|bounds| bounds.max_price_per_unit)
}
}
32 changes: 32 additions & 0 deletions crates/mempool/src/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ impl TransactionQueue {
}
}

/// Encapsulates a transaction reference to assess its order (i.e., priority).
/// A transaction with gas price below the threshold. Sorted according to the gas price.
#[derive(Clone, Debug, derive_more::Deref, derive_more::From)]
struct PendingTransaction(pub TransactionReference);

/// Compare transactions based only on their gas price, a uint, using the Eq trait. It ensures that
/// two gas price are either exactly equal or not.
impl PartialEq for PendingTransaction {
fn eq(&self, other: &PendingTransaction) -> bool {
self.get_l2_gas_price() == other.get_l2_gas_price() && self.tx_hash == other.tx_hash
}
}

/// 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 PendingTransaction {}

impl Ord for PendingTransaction {
fn cmp(&self, other: &Self) -> Ordering {
self.get_l2_gas_price()
.cmp(&other.get_l2_gas_price())
.then_with(|| self.tx_hash.cmp(&other.tx_hash))
}
}

impl PartialOrd for PendingTransaction {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

/// Encapsulates a transaction reference to assess its order (i.e., priority).
#[derive(Clone, Debug, derive_more::Deref, derive_more::From)]
struct QueuedTransaction(pub TransactionReference);
Expand Down

0 comments on commit 55308a2

Please sign in to comment.