Skip to content

Commit

Permalink
calculate fee using rounded-up vbytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jp1ac4 committed Oct 29, 2024
1 parent 7f11393 commit 4eae611
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/coin_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ impl<'a> CoinSelector<'a> {
}

fn implied_fee_from_feerate(&self, target: Target, drain_weights: DrainWeights) -> u64 {
(self.weight(target.outputs, drain_weights) as f32 * target.fee.rate.spwu()).ceil() as u64
target
.fee
.rate
.implied_fee(self.weight(target.outputs, drain_weights))
}

/// The actual fee the selection would pay if it was used in a transaction that had
Expand Down
6 changes: 6 additions & 0 deletions src/feerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl FeeRate {
pub fn spwu(&self) -> f32 {
self.0 .0
}

/// The fee that the transaction with weight `tx_weight` should pay in order to satisfy the fee rate given by `self`.
pub fn implied_fee(&self, tx_weight: u64) -> u64 {
// The fee rate is applied to the rounded-up vbytes.
((tx_weight as f32 / 4.0).ceil() * self.as_sat_vb()).ceil() as u64
}
}

impl Add<FeeRate> for FeeRate {
Expand Down
7 changes: 4 additions & 3 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ impl Replace {
///
/// [RBF rule 4]: https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md#current-replace-by-fee-policy
pub fn min_fee_to_do_replacement(&self, replacing_tx_weight: u64) -> u64 {
let min_fee_increment =
(replacing_tx_weight as f32 * self.incremental_relay_feerate.spwu()).ceil() as u64;
self.fee + min_fee_increment
self.fee
+ self
.incremental_relay_feerate
.implied_fee(replacing_tx_weight)
}
}
4 changes: 2 additions & 2 deletions tests/rbf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ fn run_bitcoin_core_rbf_tests() {
));
assert!(!pays_for_rbf(
low_fee,
low_fee + 22,
low_fee + 29,
8 + 1,
very_high_relay_feerate
));
assert!(pays_for_rbf(
low_fee,
low_fee + 23, // 23 = (10 * (9/4)).ceil()
low_fee + 30, // 30 = (10 * (9/4).ceil())
8 + 1,
very_high_relay_feerate
));
Expand Down

0 comments on commit 4eae611

Please sign in to comment.