Skip to content

Commit

Permalink
bucket_transactions is now fee_to_weight aware
Browse files Browse the repository at this point in the history
and will not aggregate txs if this reduces fee_to_weight for the bucket
  • Loading branch information
antiochp committed Apr 17, 2019
1 parent b403ccb commit e37acd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
6 changes: 6 additions & 0 deletions core/src/core/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,12 @@ impl Transaction {
Ok(())
}

/// Can be used to compare txs by their fee/weight ratio.
/// Don't use these values for anything else though due to precision multiplier.
pub fn fee_to_weight(&self) -> u64 {
self.fee() * 1_000 / self.tx_weight() as u64
}

/// Calculate transaction weight
pub fn tx_weight(&self) -> usize {
self.body.body_weight()
Expand Down
16 changes: 12 additions & 4 deletions pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Pool {
// Sort them by fees over weight, multiplying by 1000 to keep some precision
// don't think we'll ever see a >max_u64/1000 fee transaction.
// We want to select the txs with highest fee per unit of weight first.
tx_buckets.sort_unstable_by_key(|tx| tx.fee() * 1000 / tx.tx_weight() as u64);
tx_buckets.sort_unstable_by_key(|tx| tx.fee_to_weight());

// Iteratively apply the txs to the current chain state,
// rejecting any that do not result in a valid state.
Expand Down Expand Up @@ -364,16 +364,24 @@ impl Pool {
// We found a single parent tx, so aggregate in the bucket
// if the aggregate tx is a valid tx.
// Otherwise discard and let the next block pick this tx up.
let current = tx_buckets[pos].clone();
if let Ok(agg_tx) = transaction::aggregate(vec![current, entry.tx.clone()]) {
let ref current = tx_buckets[pos];

if let Ok(agg_tx) =
transaction::aggregate(vec![current.clone(), entry.tx.clone()])
{
if agg_tx
.validate(
Weighting::AsLimitedTransaction { max_weight },
self.verifier_cache.clone(),
)
.is_ok()
{
tx_buckets[pos] = agg_tx;
// Only aggregate if it does not reduce the fee_to_weight ratio.
if agg_tx.fee_to_weight() >= current.fee_to_weight() {
tx_buckets[pos] = agg_tx;
} else {
tx_buckets.push(entry.tx.clone());
}
} else {
// Aggregated tx is not valid so discard this new tx.
is_rejected = true;
Expand Down

0 comments on commit e37acd0

Please sign in to comment.