Skip to content

Commit

Permalink
if equal, consider as lesser so that its not considered as duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Aug 30, 2024
1 parent e8ba3dc commit 37c0847
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ katana-codecs = { path = "crates/katana/storage/codecs" }
katana-codecs-derive = { path = "crates/katana/storage/codecs/derive" }
katana-core = { path = "crates/katana/core", default-features = false }
katana-db = { path = "crates/katana/storage/db" }
katana-executor = { path = "crates/katana/executor", default-features = false }
katana-executor = { path = "crates/katana/executor" }
katana-node = { path = "crates/katana/node", default-features = false }
katana-pool = { path = "crates/katana/pool" }
katana-primitives = { path = "crates/katana/primitives" }
Expand Down
34 changes: 30 additions & 4 deletions crates/katana/pool/src/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct TxSubmissionNonce(u64);
impl Ord for TxSubmissionNonce {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// Reverse the ordering so lower values have higher priority
other.0.cmp(&self.0)
self.0.cmp(&other.0)
}
}

Expand Down Expand Up @@ -84,12 +84,35 @@ impl<T> TipOrdering<T> {
}
}

#[derive(Debug, Clone)]
pub struct Tip(u64);

impl Ord for Tip {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
other.0.cmp(&self.0)
}
}

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

impl PartialEq for Tip {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl Eq for Tip {}

impl<T: PoolTransaction> PoolOrd for TipOrdering<T> {
type Transaction = T;
type PriorityValue = u64;
type PriorityValue = Tip;

fn priority(&self, tx: &Self::Transaction) -> Self::PriorityValue {
tx.tip()
Tip(tx.tip())
}
}

Expand Down Expand Up @@ -138,6 +161,7 @@ mod tests {
PoolTx::new().with_tip(6),
PoolTx::new().with_tip(3),
PoolTx::new().with_tip(2),
PoolTx::new().with_tip(2),
PoolTx::new().with_tip(5),
PoolTx::new().with_tip(4),
PoolTx::new().with_tip(7),
Expand All @@ -153,6 +177,7 @@ mod tests {

// Get pending transactions
let pending = pool.take_transactions().collect::<Vec<_>>();
assert_eq!(pending.len(), txs.len());

// Assert that the transactions are ordered by tip (highest to lowest)
assert_eq!(pending[0].tx.tip(), 7);
Expand All @@ -161,6 +186,7 @@ mod tests {
assert_eq!(pending[3].tx.tip(), 4);
assert_eq!(pending[4].tx.tip(), 3);
assert_eq!(pending[5].tx.tip(), 2);
assert_eq!(pending[6].tx.tip(), 1);
assert_eq!(pending[6].tx.tip(), 2);
assert_eq!(pending[7].tx.tip(), 1);
}
}
34 changes: 2 additions & 32 deletions crates/katana/pool/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::fmt;
use std::collections::{btree_set::IntoIter, BTreeSet};
use std::collections::btree_set::IntoIter;
use std::collections::BTreeSet;
use std::sync::Arc;

use futures::channel::mpsc::{channel, Receiver, Sender};
Expand Down Expand Up @@ -215,7 +216,6 @@ pub(crate) mod test_utils {

use super::*;
use crate::tx::PoolTransaction;
use crate::validation::{ValidationOutcome, ValidationResult, Validator};

fn random_bytes<const SIZE: usize>() -> [u8; SIZE] {
let mut bytes = [0u8; SIZE];
Expand Down Expand Up @@ -284,36 +284,6 @@ pub(crate) mod test_utils {
self.tip
}
}

/// A tip-based validator that flags transactions as invalid if they have less than 10 tip.
pub struct TipValidator<T> {
threshold: u64,
t: std::marker::PhantomData<T>,
}

impl<T> TipValidator<T> {
pub fn new(threshold: u64) -> Self {
Self { threshold, t: std::marker::PhantomData }
}
}

impl<T: PoolTransaction> Validator for TipValidator<T> {
type Transaction = T;

fn validate(&self, tx: Self::Transaction) -> ValidationResult<Self::Transaction> {
if tx.tip() < self.threshold {
return ValidationResult::Ok(ValidationOutcome::Invalid {
error: InvalidTransactionError::InsufficientFunds {
balance: FieldElement::ONE,
max_fee: tx.max_fee(),
},
tx,
});
}

ValidationResult::Ok(ValidationOutcome::Valid(tx))
}
}
}

#[cfg(test)]
Expand Down
5 changes: 4 additions & 1 deletion crates/katana/pool/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ impl<T, O: PoolOrd> PartialOrd for PendingTx<T, O> {

impl<T, O: PoolOrd> Ord for PendingTx<T, O> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.priority.cmp(&other.priority)
match self.priority.cmp(&other.priority) {
std::cmp::Ordering::Equal => std::cmp::Ordering::Less,
other => other,
}
}
}

Expand Down

0 comments on commit 37c0847

Please sign in to comment.