Skip to content

Commit 52d6283

Browse files
committed
chore: remove segwit_add in favor of TXIN_BASE_WEIGHT
1 parent 42106ec commit 52d6283

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

crates/bdk/src/wallet/coin_selection.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
//! # use bdk::*;
3333
//! # use bdk::wallet::coin_selection::decide_change;
3434
//! # use anyhow::Error;
35-
//! # const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4) * 4;
3635
//! #[derive(Debug)]
3736
//! struct AlwaysSpendEverything;
3837
//!
@@ -55,7 +54,8 @@
5554
//! |(selected_amount, additional_weight), weighted_utxo| {
5655
//! **selected_amount += weighted_utxo.utxo.txout().value;
5756
//! **additional_weight += Weight::from_wu(
58-
//! (TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as u64,
57+
//! (TxIn::default().segwit_weight() + weighted_utxo.satisfaction_weight)
58+
//! as u64,
5959
//! );
6060
//! Some(weighted_utxo.utxo)
6161
//! },
@@ -109,6 +109,7 @@ use crate::WeightedUtxo;
109109
use alloc::vec::Vec;
110110
use bitcoin::consensus::encode::serialize;
111111
use bitcoin::OutPoint;
112+
use bitcoin::TxIn;
112113
use bitcoin::{Script, Weight};
113114

114115
use core::convert::TryInto;
@@ -119,10 +120,6 @@ use rand::seq::SliceRandom;
119120
/// overridden
120121
pub type DefaultCoinSelectionAlgorithm = BranchAndBoundCoinSelection;
121122

122-
// Base weight of a Txin, not counting the weight needed for satisfying it.
123-
// prev_txid (32 bytes) + prev_vout (4 bytes) + sequence (4 bytes)
124-
pub(crate) const TXIN_BASE_WEIGHT: usize = (32 + 4 + 4) * 4;
125-
126123
/// Errors that can be thrown by the [`coin_selection`](crate::wallet::coin_selection) module
127124
#[derive(Debug)]
128125
pub enum Error {
@@ -345,7 +342,8 @@ fn select_sorted_utxos(
345342
|(selected_amount, fee_amount), (must_use, weighted_utxo)| {
346343
if must_use || **selected_amount < target_amount + **fee_amount {
347344
**fee_amount += fee_rate.fee_wu(Weight::from_wu(
348-
(TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as u64,
345+
(TxIn::default().segwit_weight() + weighted_utxo.satisfaction_weight)
346+
as u64,
349347
));
350348
**selected_amount += weighted_utxo.utxo.txout().value;
351349
Some(weighted_utxo.utxo)
@@ -388,7 +386,7 @@ struct OutputGroup {
388386
impl OutputGroup {
389387
fn new(weighted_utxo: WeightedUtxo, fee_rate: FeeRate) -> Self {
390388
let fee = fee_rate.fee_wu(Weight::from_wu(
391-
(TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as u64,
389+
(TxIn::default().segwit_weight() + weighted_utxo.satisfaction_weight) as u64,
392390
));
393391
let effective_value = weighted_utxo.utxo.txout().value as i64 - fee as i64;
394392
OutputGroup {
@@ -738,7 +736,7 @@ mod test {
738736
use core::str::FromStr;
739737

740738
use bdk_chain::ConfirmationTime;
741-
use bitcoin::{OutPoint, ScriptBuf, TxOut};
739+
use bitcoin::{OutPoint, ScriptBuf, TxIn, TxOut};
742740

743741
use super::*;
744742
use crate::types::*;
@@ -749,9 +747,9 @@ mod test {
749747
use rand::seq::SliceRandom;
750748
use rand::{Rng, RngCore, SeedableRng};
751749

752-
// n. of items on witness (1WU) + signature len (1WU) + signature and sighash (72WU)
753-
// + pubkey len (1WU) + pubkey (33WU) + script sig len (1 byte, 4WU)
754-
const P2WPKH_SATISFACTION_SIZE: usize = 1 + 1 + 72 + 1 + 33 + 4;
750+
// n. of items on witness (1WU) signature and sighash (72WU)
751+
// + pubkey len (1WU) + pubkey (33WU)
752+
const P2WPKH_SATISFACTION_SIZE: usize = 1 + 72 + 1 + 33;
755753

756754
const FEE_AMOUNT: u64 = 50;
757755

@@ -1240,7 +1238,7 @@ mod test {
12401238

12411239
assert_eq!(result.selected.len(), 1);
12421240
assert_eq!(result.selected_amount(), 100_000);
1243-
let input_size = (TXIN_BASE_WEIGHT + P2WPKH_SATISFACTION_SIZE).vbytes();
1241+
let input_size = (TxIn::default().segwit_weight() + P2WPKH_SATISFACTION_SIZE).vbytes();
12441242
// the final fee rate should be exactly the same as the fee rate given
12451243
assert!((1.0 - (result.fee_amount as f32 / input_size as f32)).abs() < f32::EPSILON);
12461244
}

crates/bdk/src/wallet/mod.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,17 +1714,10 @@ impl<D> Wallet<D> {
17141714

17151715
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
17161716
Some((keychain, derivation_index)) => {
1717-
let is_segwit = self.get_descriptor_for_keychain(keychain).is_witness()
1718-
|| self.get_descriptor_for_keychain(keychain).is_taproot();
1719-
let segwit_add = match is_segwit {
1720-
true => 4,
1721-
false => 0,
1722-
};
17231717
let satisfaction_weight = self
17241718
.get_descriptor_for_keychain(keychain)
17251719
.max_weight_to_satisfy()
1726-
.unwrap()
1727-
+ segwit_add;
1720+
.unwrap();
17281721
WeightedUtxo {
17291722
utxo: Utxo::Local(LocalOutput {
17301723
outpoint: txin.previous_output,
@@ -2051,16 +2044,9 @@ impl<D> Wallet<D> {
20512044
.map(|utxo| {
20522045
let keychain = utxo.keychain;
20532046
(utxo, {
2054-
let is_segwit = self.get_descriptor_for_keychain(keychain).is_witness()
2055-
|| self.get_descriptor_for_keychain(keychain).is_taproot();
2056-
let segwit_add = match is_segwit {
2057-
true => 4,
2058-
false => 0,
2059-
};
20602047
self.get_descriptor_for_keychain(keychain)
20612048
.max_weight_to_satisfy()
20622049
.unwrap()
2063-
+ segwit_add
20642050
})
20652051
})
20662052
.collect()

0 commit comments

Comments
 (0)