take age of txs into consideraton when bucketing and sorting txs from the pool (#2878)

This commit is contained in:
Antioch Peverell 2019-06-27 17:50:10 +01:00 committed by GitHub
parent 5aaf2d058d
commit 82775164e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -355,7 +355,7 @@ impl Pool {
// This is the common case for non 0-conf txs in the txpool. // 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. // We assume the tx is valid here as we validated it on the way into the txpool.
insert_pos = Some(tx_buckets.len()); 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) => { Some(pos) => {
// We found a single parent tx, so aggregate in the bucket // 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. // Otherwise put it in its own bucket at the end.
// Note: This bucket will have a lower fee_to_weight // Note: This bucket will have a lower fee_to_weight
// than the bucket it depends on. // 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 { } else {
// Aggregation failed so discard this new tx. // Aggregation failed so discard this new tx.
@ -397,10 +397,11 @@ impl Pool {
} }
} }
// Sort them by fee_to_weight (descending). // Sort buckets by fee_to_weight (descending) and age (oldest first).
// Txs with no dependencies will be toward the start of the vec. // Txs with highest fee_to_weight will be prioritied.
// Txs with a big chain of dependencies will be toward the end of the vec. // Aggregation that increases the fee_to_weight of a bucket will prioritize the bucket.
tx_buckets.sort_unstable_by_key(|x| Reverse(x.fee_to_weight)); // 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 tx_buckets
.into_iter() .into_iter()
@ -454,13 +455,19 @@ impl Pool {
struct Bucket { struct Bucket {
raw_txs: Vec<Transaction>, raw_txs: Vec<Transaction>,
fee_to_weight: u64, fee_to_weight: u64,
age_idx: usize,
} }
impl Bucket { 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 { Bucket {
fee_to_weight: tx.fee_to_weight(), fee_to_weight: tx.fee_to_weight(),
raw_txs: vec![tx.clone()], raw_txs: vec![tx.clone()],
age_idx,
} }
} }
@ -477,6 +484,7 @@ impl Bucket {
Ok(Bucket { Ok(Bucket {
fee_to_weight: agg_tx.fee_to_weight(), fee_to_weight: agg_tx.fee_to_weight(),
raw_txs: raw_txs, raw_txs: raw_txs,
age_idx: self.age_idx,
}) })
} }
} }