From 582a9895f31f7b119b49d9be4b6379f10a5c5b92 Mon Sep 17 00:00:00 2001 From: Mohammad Nassar Date: Tue, 13 Aug 2024 18:22:19 +0300 Subject: [PATCH] feat(mempool): add pending transaction type --- crates/mempool/src/mempool.rs | 6 +++- crates/mempool/src/transaction_queue.rs | 41 +++++++++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/crates/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs index 8e6f725cf5..aeab67f7e2 100644 --- a/crates/mempool/src/mempool.rs +++ b/crates/mempool/src/mempool.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::executable_transaction::Transaction; -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, AccountState, MempoolInput, MempoolResult}; @@ -227,4 +227,8 @@ impl TransactionReference { resource_bounds: ResourceBoundsMapping::default(), } } + + pub fn get_l2_gas_price(&self) -> Option { + self.resource_bounds.0.get(&Resource::L2Gas).map(|bounds| bounds.max_price_per_unit) + } } diff --git a/crates/mempool/src/transaction_queue.rs b/crates/mempool/src/transaction_queue.rs index c4199166ae..77283fa8bf 100644 --- a/crates/mempool/src/transaction_queue.rs +++ b/crates/mempool/src/transaction_queue.rs @@ -68,21 +68,50 @@ impl TransactionQueue { } } -/// Encapsulates a transaction reference to assess its order (i.e., priority). +/// Encapsulates a transaction reference to assess its order (i.e., gas price). +#[derive(Clone, Debug, derive_more::Deref, derive_more::From)] +struct PendingTransaction(pub TransactionReference); + +/// Compare transactions based only on their gas price, 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 { + Some(self.cmp(other)) + } +} + +/// This struct behaves similarly to `PendingTransaction`, encapsulating a transaction reference +/// to assess its order (i.e., tip). +/// +/// # See also `PendingTransaction` docstring for details. #[derive(Clone, Debug, derive_more::Deref, derive_more::From)] struct PriorityTransaction(pub TransactionReference); -/// 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 PriorityTransaction { fn eq(&self, other: &PriorityTransaction) -> bool { self.tip == other.tip && 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 PriorityTransaction {} impl Ord for PriorityTransaction {