Skip to content

Commit

Permalink
fix: FeeRate::checked_mul_by_weight should scale output down by 1000
Browse files Browse the repository at this point in the history
  • Loading branch information
conduition committed Nov 9, 2023
1 parent 966b190 commit 0c56131
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions bitcoin/src/blockdata/fee_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ impl FeeRate {

/// Checked weight multiplication.
///
/// Computes `self * rhs` where rhs is of type Weight. `None` is returned if an overflow
/// occurred.
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate.
///
/// `None` is returned if an overflow occurred.
pub fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> {
self.0.checked_mul(rhs.to_wu()).map(Amount::from_sat)
let sats = self.0.checked_mul(rhs.to_wu())?.checked_add(999)? / 1000;
Some(Amount::from_sat(sats))
}

/// Calculates fee by multiplying this fee rate by weight, in weight units, returning `None`
Expand All @@ -95,13 +97,14 @@ impl FeeRate {
///
/// # Examples
///
/// ```no_run
/// ```
/// # use bitcoin::{absolute, transaction, FeeRate, Transaction};
/// # // Dummy transaction.
/// # let tx = Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, input: vec![], output: vec![] };
///
/// let rate = FeeRate::from_sat_per_vb(1).expect("1 sat/vbyte is valid");
/// let fee = rate.fee_wu(tx.weight());
/// let fee = rate.fee_wu(tx.weight()).unwrap();
/// assert_eq!(fee.to_sat(), tx.vsize() as u64);
/// ```
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.checked_mul_by_weight(weight) }

Expand Down Expand Up @@ -209,8 +212,11 @@ mod tests {

#[test]
fn checked_weight_mul_test() {
let weight = Weight::from_wu(10);
let fee: Amount = FeeRate(10).checked_mul_by_weight(weight).expect("expected Amount");
let weight = Weight::from_vb(10).unwrap();
let fee: Amount = FeeRate::from_sat_per_vb(10)
.unwrap()
.checked_mul_by_weight(weight)
.expect("expected Amount");
assert_eq!(Amount::from_sat(100), fee);

let fee = FeeRate(10).checked_mul_by_weight(Weight::MAX);
Expand Down

0 comments on commit 0c56131

Please sign in to comment.