Skip to content

Commit

Permalink
ordered transactions with similar priority
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Aug 30, 2024
1 parent 4f9d8f0 commit b367eb6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 20 additions & 1 deletion crates/katana/pool/src/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ mod tests {
fn tip_based_ordering() {
// Create mock transactions with different tips and in random order
let txs = [
PoolTx::new().with_tip(2),
PoolTx::new().with_tip(1),
PoolTx::new().with_tip(6),
PoolTx::new().with_tip(3),
Expand All @@ -181,12 +182,30 @@ mod tests {

// Assert that the transactions are ordered by tip (highest to lowest)
assert_eq!(pending[0].tx.tip(), 7);
assert_eq!(pending[0].tx.hash(), txs[8].hash());

assert_eq!(pending[1].tx.tip(), 6);
assert_eq!(pending[1].tx.hash(), txs[2].hash());

assert_eq!(pending[2].tx.tip(), 5);
assert_eq!(pending[2].tx.hash(), txs[6].hash());

assert_eq!(pending[3].tx.tip(), 4);
assert_eq!(pending[3].tx.hash(), txs[7].hash());

assert_eq!(pending[4].tx.tip(), 3);
assert_eq!(pending[4].tx.hash(), txs[3].hash());

assert_eq!(pending[5].tx.tip(), 2);
assert_eq!(pending[5].tx.hash(), txs[0].hash());

assert_eq!(pending[6].tx.tip(), 2);
assert_eq!(pending[7].tx.tip(), 1);
assert_eq!(pending[6].tx.hash(), txs[4].hash());

assert_eq!(pending[7].tx.tip(), 2);
assert_eq!(pending[7].tx.hash(), txs[5].hash());

assert_eq!(pending[8].tx.tip(), 1);
assert_eq!(pending[8].tx.hash(), txs[1].hash());
}
}
7 changes: 6 additions & 1 deletion crates/katana/pool/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ impl<T, O: PoolOrd> PartialOrd for PendingTx<T, O> {
}
}

/// When two transactions have the same priority, we want to prioritize the one that was added
/// first. So, when an incoming transaction with similar priority value is added to the
/// [BTreeSet](std::collections::BTreeSet), the transaction is assigned a 'greater'
/// [Ordering](std::cmp::Ordering) so that it will be placed after the existing ones. This is
/// because items in a BTree is ordered from lowest to highest.
impl<T, O: PoolOrd> Ord for PendingTx<T, O> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.priority.cmp(&other.priority) {
std::cmp::Ordering::Equal => std::cmp::Ordering::Less,
std::cmp::Ordering::Equal => std::cmp::Ordering::Greater,
other => other,
}
}
Expand Down

0 comments on commit b367eb6

Please sign in to comment.