From 82775164e8adeb6a910b4c8af9d530aa1248a032 Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Thu, 27 Jun 2019 17:50:10 +0100 Subject: [PATCH] take age of txs into consideraton when bucketing and sorting txs from the pool (#2878) --- pool/src/pool.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pool/src/pool.rs b/pool/src/pool.rs index 015f515fbb..3f7052861c 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -355,7 +355,7 @@ impl Pool { // This is the common case for non 0-conf txs in the txpool. // We assume the tx is valid here as we validated it on the way into the txpool. insert_pos = Some(tx_buckets.len()); - tx_buckets.push(Bucket::new(entry.tx.clone())); + tx_buckets.push(Bucket::new(entry.tx.clone(), tx_buckets.len())); } Some(pos) => { // We found a single parent tx, so aggregate in the bucket @@ -375,7 +375,7 @@ impl Pool { // Otherwise put it in its own bucket at the end. // Note: This bucket will have a lower fee_to_weight // than the bucket it depends on. - tx_buckets.push(Bucket::new(entry.tx.clone())); + tx_buckets.push(Bucket::new(entry.tx.clone(), tx_buckets.len())); } } else { // Aggregation failed so discard this new tx. @@ -397,10 +397,11 @@ impl Pool { } } - // Sort them by fee_to_weight (descending). - // Txs with no dependencies will be toward the start of the vec. - // Txs with a big chain of dependencies will be toward the end of the vec. - tx_buckets.sort_unstable_by_key(|x| Reverse(x.fee_to_weight)); + // Sort buckets by fee_to_weight (descending) and age (oldest first). + // Txs with highest fee_to_weight will be prioritied. + // Aggregation that increases the fee_to_weight of a bucket will prioritize the bucket. + // Oldest (based on pool insertion time) will then be prioritized. + tx_buckets.sort_unstable_by_key(|x| (Reverse(x.fee_to_weight), x.age_idx)); tx_buckets .into_iter() @@ -454,13 +455,19 @@ impl Pool { struct Bucket { raw_txs: Vec, fee_to_weight: u64, + age_idx: usize, } impl Bucket { - fn new(tx: Transaction) -> Bucket { + /// Construct a new bucket with the given tx. + /// also specifies an "age_idx" so we can sort buckets by age + /// as well as fee_to_weight. Txs are maintainedin the pool in insert order + /// so buckets with low age_idx contain oldest txs. + fn new(tx: Transaction, age_idx: usize) -> Bucket { Bucket { fee_to_weight: tx.fee_to_weight(), raw_txs: vec![tx.clone()], + age_idx, } } @@ -477,6 +484,7 @@ impl Bucket { Ok(Bucket { fee_to_weight: agg_tx.fee_to_weight(), raw_txs: raw_txs, + age_idx: self.age_idx, }) } }