]> Untitled Git - bdk/commitdiff
refactor(bdk)!: Remove trait Vbytes
authorvmammal <valuedmammal@protonmail.com>
Sat, 2 Mar 2024 14:48:16 +0000 (09:48 -0500)
committervmammal <valuedmammal@protonmail.com>
Fri, 22 Mar 2024 03:32:00 +0000 (23:32 -0400)
The only place this is being used is a unit test that is
easily refactored. For size conversions prefer methods
on e.g. `Weight`.

crates/bdk/src/types.rs
crates/bdk/src/wallet/coin_selection.rs

index 3bde290e4b04b6a4c3a80fa4070bff6a65ebde39..4ce961b7ef032bf14ea072dcedbc892be89789e7 100644 (file)
@@ -46,19 +46,6 @@ impl AsRef<[u8]> for KeychainKind {
     }
 }
 
-/// Trait implemented by types that can be used to measure weight units.
-pub trait Vbytes {
-    /// Convert weight units to virtual bytes.
-    fn vbytes(self) -> usize;
-}
-
-impl Vbytes for usize {
-    fn vbytes(self) -> usize {
-        // ref: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#transaction-size-calculations
-        (self as f32 / 4.0).ceil() as usize
-    }
-}
-
 /// An unspent output owned by a [`Wallet`].
 ///
 /// [`Wallet`]: crate::Wallet
index 5383e552553a6ce6c4977610c3d026afb51189db..5122a1493351a96d2c8ee761984df9acd0da3dc8 100644 (file)
@@ -744,12 +744,11 @@ mod test {
     use core::str::FromStr;
 
     use bdk_chain::ConfirmationTime;
-    use bitcoin::{OutPoint, ScriptBuf, TxOut};
+    use bitcoin::{Amount, OutPoint, ScriptBuf, TxOut};
 
     use super::*;
     use crate::types::*;
     use crate::wallet::coin_selection::filter_duplicates;
-    use crate::wallet::Vbytes;
 
     use rand::rngs::StdRng;
     use rand::seq::SliceRandom;
@@ -1233,22 +1232,18 @@ mod test {
         let utxos = get_test_utxos();
         let drain_script = ScriptBuf::default();
         let target_amount = 99932; // first utxo's effective value
+        let feerate = FeeRate::BROADCAST_MIN;
 
         let result = BranchAndBoundCoinSelection::new(0)
-            .coin_select(
-                vec![],
-                utxos,
-                FeeRate::from_sat_per_vb_unchecked(1),
-                target_amount,
-                &drain_script,
-            )
+            .coin_select(vec![], utxos, feerate, target_amount, &drain_script)
             .unwrap();
 
         assert_eq!(result.selected.len(), 1);
         assert_eq!(result.selected_amount(), 100_000);
-        let input_size = (TXIN_BASE_WEIGHT + P2WPKH_SATISFACTION_SIZE).vbytes();
+        let input_weight = (TXIN_BASE_WEIGHT + P2WPKH_SATISFACTION_SIZE) as u64;
         // the final fee rate should be exactly the same as the fee rate given
-        assert!((1.0 - (result.fee_amount as f32 / input_size as f32)).abs() < f32::EPSILON);
+        let result_feerate = Amount::from_sat(result.fee_amount) / Weight::from_wu(input_weight);
+        assert_eq!(result_feerate, feerate);
     }
 
     #[test]