Skip to content

Commit

Permalink
refactor(bdk)!: Remove trait Vbytes
Browse files Browse the repository at this point in the history
The only place this is being used is a unit test that is
easily refactored. For size conversions prefer methods
on e.g. `Weight`.
  • Loading branch information
ValuedMammal committed Mar 21, 2024
1 parent 95374a8 commit 4ec57c4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
13 changes: 0 additions & 13 deletions crates/bdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 6 additions & 11 deletions crates/bdk/src/wallet/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 4ec57c4

Please sign in to comment.