]> Untitled Git - bdk/commitdiff
Add Vbytes trait
authorTobin Harding <me@tobin.cc>
Thu, 8 Jul 2021 01:33:39 +0000 (11:33 +1000)
committerTobin Harding <me@tobin.cc>
Thu, 8 Jul 2021 01:33:39 +0000 (11:33 +1000)
We convert weight units into vbytes in various places. Lets add a trait
to do it, this makes the code slightly cleaner.

src/wallet/coin_selection.rs
src/wallet/mod.rs

index e3729bbc9bbfbbbf0de6bac62d16d46e47d0e32d..978f3d163b0bad20375fe4686a757462f2316246 100644 (file)
@@ -90,6 +90,7 @@
 //! ```
 
 use crate::types::FeeRate;
+use crate::wallet::Vbytes;
 use crate::{database::Database, WeightedUtxo};
 use crate::{error::Error, Utxo};
 
@@ -257,8 +258,8 @@ struct OutputGroup {
 
 impl OutputGroup {
     fn new(weighted_utxo: WeightedUtxo, fee_rate: FeeRate) -> Self {
-        let fee = (TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as f32 / 4.0
-            * fee_rate.as_sat_vb();
+        let fee =
+            (TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight).vbytes() * fee_rate.as_sat_vb();
         let effective_value = weighted_utxo.utxo.txout().value as i64 - fee.ceil() as i64;
         OutputGroup {
             weighted_utxo,
@@ -862,7 +863,7 @@ mod test {
 
         assert_eq!(result.selected.len(), 1);
         assert_eq!(result.selected_amount(), 100_000);
-        let input_size = (TXIN_BASE_WEIGHT as f32) / 4.0 + P2WPKH_WITNESS_SIZE as f32 / 4.0;
+        let input_size = (TXIN_BASE_WEIGHT + P2WPKH_WITNESS_SIZE).vbytes();
         let epsilon = 0.5;
         assert!((1.0 - (result.fee_amount / input_size)).abs() < epsilon);
     }
index e29b8a626b252c59e34c60b240dcc58d55ca4e7c..55dd711029168956fe80d5b6ceb18234aeb30c18 100644 (file)
@@ -780,7 +780,7 @@ where
             return Err(Error::IrreplaceableTransaction);
         }
 
-        let vbytes = tx.get_weight() as f32 / 4.0;
+        let vbytes = tx.get_weight().vbytes();
         let feerate = details.fee.ok_or(Error::FeeRateUnavailable)? as f32 / vbytes;
 
         // remove the inputs from the tx and process them
@@ -1578,6 +1578,18 @@ where
     }
 }
 
+/// Trait implemented by types that can be used to measure weight units.
+pub trait Vbytes {
+    /// Convert weight units to virtual bytes.
+    fn vbytes(self) -> f32;
+}
+
+impl Vbytes for usize {
+    fn vbytes(self) -> f32 {
+        self as f32 / 4.0
+    }
+}
+
 #[cfg(test)]
 pub(crate) mod test {
     use std::str::FromStr;
@@ -1764,7 +1776,7 @@ pub(crate) mod test {
                 dust_change = true;
             )*
 
-            let tx_fee_rate = $fees as f32 / (tx.get_weight() as f32 / 4.0);
+            let tx_fee_rate = $fees as f32 / (tx.get_weight().vbytes());
             let fee_rate = $fee_rate.as_sat_vb();
 
             if !dust_change {