fix: FeeRate::checked_mul_by_weight should scale output down by 1000
This commit is contained in:
parent
966b190f23
commit
0c56131819
|
@ -82,10 +82,12 @@ impl FeeRate {
|
||||||
|
|
||||||
/// Checked weight multiplication.
|
/// Checked weight multiplication.
|
||||||
///
|
///
|
||||||
/// Computes `self * rhs` where rhs is of type Weight. `None` is returned if an overflow
|
/// Computes the absolute fee amount for a given [`Weight`] at this fee rate.
|
||||||
/// occurred.
|
///
|
||||||
|
/// `None` is returned if an overflow occurred.
|
||||||
pub fn checked_mul_by_weight(self, rhs: Weight) -> Option<Amount> {
|
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`
|
/// Calculates fee by multiplying this fee rate by weight, in weight units, returning `None`
|
||||||
|
@ -95,13 +97,14 @@ impl FeeRate {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```
|
||||||
/// # use bitcoin::{absolute, transaction, FeeRate, Transaction};
|
/// # use bitcoin::{absolute, transaction, FeeRate, Transaction};
|
||||||
/// # // Dummy transaction.
|
/// # // Dummy transaction.
|
||||||
/// # let tx = Transaction { version: transaction::Version::ONE, lock_time: absolute::LockTime::ZERO, input: vec![], output: vec![] };
|
/// # 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 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) }
|
pub fn fee_wu(self, weight: Weight) -> Option<Amount> { self.checked_mul_by_weight(weight) }
|
||||||
|
|
||||||
|
@ -209,8 +212,11 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn checked_weight_mul_test() {
|
fn checked_weight_mul_test() {
|
||||||
let weight = Weight::from_wu(10);
|
let weight = Weight::from_vb(10).unwrap();
|
||||||
let fee: Amount = FeeRate(10).checked_mul_by_weight(weight).expect("expected Amount");
|
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);
|
assert_eq!(Amount::from_sat(100), fee);
|
||||||
|
|
||||||
let fee = FeeRate(10).checked_mul_by_weight(Weight::MAX);
|
let fee = FeeRate(10).checked_mul_by_weight(Weight::MAX);
|
||||||
|
|
Loading…
Reference in New Issue