Skip to content

Commit 7ac87b8

Browse files
TXIN_BASE_WEIGHT shouldn't include the script len
We would before calculate the TXIN_BASE_WEIGHT as prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes) + script_sig_len (1 bytes), but that's wrong: the script_sig_len shouldn't be included, as miniscript already includes it in the `max_satisfaction_size` calculation. Fixes #160
1 parent ac051d7 commit 7ac87b8

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/wallet/coin_selection.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! # use bdk::database::Database;
3131
//! # use bdk::*;
3232
//! # use bdk::wallet::coin_selection::decide_change;
33-
//! # const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4 + 1) * 4;
33+
//! # const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4) * 4;
3434
//! #[derive(Debug)]
3535
//! struct AlwaysSpendEverything;
3636
//!
@@ -119,8 +119,8 @@ pub type DefaultCoinSelectionAlgorithm = BranchAndBoundCoinSelection;
119119
pub type DefaultCoinSelectionAlgorithm = LargestFirstCoinSelection; // make the tests more predictable
120120

121121
// Base weight of a Txin, not counting the weight needed for satisfying it.
122-
// prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes) + script_len (1 bytes)
123-
pub(crate) const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4 + 1) * 4;
122+
// prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes)
123+
pub(crate) const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4) * 4;
124124

125125
#[derive(Debug)]
126126
/// Remaining amount after performing coin selection
@@ -731,7 +731,7 @@ mod test {
731731
use rand::seq::SliceRandom;
732732
use rand::{Rng, SeedableRng};
733733

734-
const P2WPKH_WITNESS_SIZE: usize = 73 + 33 + 2;
734+
const P2WPKH_SATISFACTION_SIZE: usize = 73 + 33 + 2 + 1;
735735

736736
const FEE_AMOUNT: u64 = 50;
737737

@@ -743,7 +743,7 @@ mod test {
743743
))
744744
.unwrap();
745745
WeightedUtxo {
746-
satisfaction_weight: P2WPKH_WITNESS_SIZE,
746+
satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
747747
utxo: Utxo::Local(LocalUtxo {
748748
outpoint,
749749
txout: TxOut {
@@ -823,7 +823,7 @@ mod test {
823823
let mut res = Vec::new();
824824
for _ in 0..utxos_number {
825825
res.push(WeightedUtxo {
826-
satisfaction_weight: P2WPKH_WITNESS_SIZE,
826+
satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
827827
utxo: Utxo::Local(LocalUtxo {
828828
outpoint: OutPoint::from_str(
829829
"ebd9813ecebc57ff8f30797de7c205e3c7498ca950ea4341ee51a685ff2fa30a:0",
@@ -843,7 +843,7 @@ mod test {
843843

844844
fn generate_same_value_utxos(utxos_value: u64, utxos_number: usize) -> Vec<WeightedUtxo> {
845845
let utxo = WeightedUtxo {
846-
satisfaction_weight: P2WPKH_WITNESS_SIZE,
846+
satisfaction_weight: P2WPKH_SATISFACTION_SIZE,
847847
utxo: Utxo::Local(LocalUtxo {
848848
outpoint: OutPoint::from_str(
849849
"ebd9813ecebc57ff8f30797de7c205e3c7498ca950ea4341ee51a685ff2fa30a:0",
@@ -1313,7 +1313,7 @@ mod test {
13131313

13141314
assert_eq!(result.selected.len(), 1);
13151315
assert_eq!(result.selected_amount(), 100_000);
1316-
let input_size = (TXIN_BASE_WEIGHT + P2WPKH_WITNESS_SIZE).vbytes();
1316+
let input_size = (TXIN_BASE_WEIGHT + P2WPKH_SATISFACTION_SIZE).vbytes();
13171317
let epsilon = 0.5;
13181318
assert!((1.0 - (result.fee_amount as f32 / input_size as f32)).abs() < epsilon);
13191319
}

0 commit comments

Comments
 (0)