Skip to content

Commit 9078957

Browse files
committed
fix: remove deprecated `max_satisfaction_weight
- Change deprecated `max_satisfaction_weight` to `max_weight_to_satisfy` - Remove `#[allow(deprecated)]` flags - updates the calculations in TXIN_BASE_WEIGHT and P2WPKH_SATISFACTION_SIZE
1 parent c01983d commit 9078957

File tree

4 files changed

+27
-40
lines changed

4 files changed

+27
-40
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: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub(crate) mod utils;
5454
pub mod error;
5555
pub use utils::IsDust;
5656

57-
#[allow(deprecated)]
5857
use coin_selection::DefaultCoinSelectionAlgorithm;
5958
use signer::{SignOptions, SignerOrdering, SignersContainer, TransactionSigner};
6059
use tx_builder::{BumpFee, CreateTx, FeePolicy, TxBuilder, TxParams};
@@ -1715,10 +1714,9 @@ impl<D> Wallet<D> {
17151714

17161715
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
17171716
Some((keychain, derivation_index)) => {
1718-
#[allow(deprecated)]
17191717
let satisfaction_weight = self
17201718
.get_descriptor_for_keychain(keychain)
1721-
.max_satisfaction_weight()
1719+
.max_weight_to_satisfy()
17221720
.unwrap();
17231721
WeightedUtxo {
17241722
utxo: Utxo::Local(LocalOutput {
@@ -1736,7 +1734,6 @@ impl<D> Wallet<D> {
17361734
let satisfaction_weight =
17371735
serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len();
17381736
WeightedUtxo {
1739-
satisfaction_weight,
17401737
utxo: Utxo::Foreign {
17411738
outpoint: txin.previous_output,
17421739
sequence: Some(txin.sequence),
@@ -1746,6 +1743,7 @@ impl<D> Wallet<D> {
17461743
..Default::default()
17471744
}),
17481745
},
1746+
satisfaction_weight,
17491747
}
17501748
}
17511749
};
@@ -2045,13 +2043,11 @@ impl<D> Wallet<D> {
20452043
self.list_unspent()
20462044
.map(|utxo| {
20472045
let keychain = utxo.keychain;
2048-
#[allow(deprecated)]
2049-
(
2050-
utxo,
2046+
(utxo, {
20512047
self.get_descriptor_for_keychain(keychain)
2052-
.max_satisfaction_weight()
2053-
.unwrap(),
2054-
)
2048+
.max_weight_to_satisfy()
2049+
.unwrap()
2050+
})
20552051
})
20562052
.collect()
20572053
}

crates/bdk/src/wallet/tx_builder.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,7 @@ impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
318318

319319
for utxo in utxos {
320320
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
321-
#[allow(deprecated)]
322-
let satisfaction_weight = descriptor.max_satisfaction_weight().unwrap();
321+
let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
323322
self.params.utxos.push(WeightedUtxo {
324323
satisfaction_weight,
325324
utxo: Utxo::Local(utxo),
@@ -360,9 +359,9 @@ impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
360359
/// causing you to pay a fee that is too high. The party who is broadcasting the transaction can
361360
/// of course check the real input weight matches the expected weight prior to broadcasting.
362361
///
363-
/// To guarantee the `satisfaction_weight` is correct, you can require the party providing the
362+
/// To guarantee the `max_weight_to_satisfy` is correct, you can require the party providing the
364363
/// `psbt_input` provide a miniscript descriptor for the input so you can check it against the
365-
/// `script_pubkey` and then ask it for the [`max_satisfaction_weight`].
364+
/// `script_pubkey` and then ask it for the [`max_weight_to_satisfy`].
366365
///
367366
/// This is an **EXPERIMENTAL** feature, API and other major changes are expected.
368367
///
@@ -383,7 +382,7 @@ impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
383382
///
384383
/// [`only_witness_utxo`]: Self::only_witness_utxo
385384
/// [`finish`]: Self::finish
386-
/// [`max_satisfaction_weight`]: miniscript::Descriptor::max_satisfaction_weight
385+
/// [`max_weight_to_satisfy`]: miniscript::Descriptor::max_weight_to_satisfy
387386
pub fn add_foreign_utxo(
388387
&mut self,
389388
outpoint: OutPoint,

crates/bdk/tests/wallet.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,10 +1165,9 @@ fn test_add_foreign_utxo() {
11651165
.unwrap()
11661166
.assume_checked();
11671167
let utxo = wallet2.list_unspent().next().expect("must take!");
1168-
#[allow(deprecated)]
11691168
let foreign_utxo_satisfaction = wallet2
11701169
.get_descriptor_for_keychain(KeychainKind::External)
1171-
.max_satisfaction_weight()
1170+
.max_weight_to_satisfy()
11721171
.unwrap();
11731172

11741173
let psbt_input = psbt::Input {
@@ -1241,10 +1240,9 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
12411240
.unwrap()
12421241
.assume_checked();
12431242
let utxo = wallet2.list_unspent().next().expect("must take!");
1244-
#[allow(deprecated)]
12451243
let foreign_utxo_satisfaction = wallet2
12461244
.get_descriptor_for_keychain(KeychainKind::External)
1247-
.max_satisfaction_weight()
1245+
.max_weight_to_satisfy()
12481246
.unwrap();
12491247

12501248
let psbt_input = psbt::Input {
@@ -1267,10 +1265,9 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
12671265
fn test_add_foreign_utxo_invalid_psbt_input() {
12681266
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
12691267
let outpoint = wallet.list_unspent().next().expect("must exist").outpoint;
1270-
#[allow(deprecated)]
12711268
let foreign_utxo_satisfaction = wallet
12721269
.get_descriptor_for_keychain(KeychainKind::External)
1273-
.max_satisfaction_weight()
1270+
.max_weight_to_satisfy()
12741271
.unwrap();
12751272

12761273
let mut builder = wallet.build_tx();
@@ -1289,10 +1286,9 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
12891286
let tx1 = wallet1.get_tx(txid1).unwrap().tx_node.tx.clone();
12901287
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx.clone();
12911288

1292-
#[allow(deprecated)]
12931289
let satisfaction_weight = wallet2
12941290
.get_descriptor_for_keychain(KeychainKind::External)
1295-
.max_satisfaction_weight()
1291+
.max_weight_to_satisfy()
12961292
.unwrap();
12971293

12981294
let mut builder = wallet1.build_tx();
@@ -1334,10 +1330,9 @@ fn test_add_foreign_utxo_only_witness_utxo() {
13341330
.assume_checked();
13351331
let utxo2 = wallet2.list_unspent().next().unwrap();
13361332

1337-
#[allow(deprecated)]
13381333
let satisfaction_weight = wallet2
13391334
.get_descriptor_for_keychain(KeychainKind::External)
1340-
.max_satisfaction_weight()
1335+
.max_weight_to_satisfy()
13411336
.unwrap();
13421337

13431338
let mut builder = wallet1.build_tx();
@@ -3030,10 +3025,9 @@ fn test_taproot_foreign_utxo() {
30303025
.assume_checked();
30313026
let utxo = wallet2.list_unspent().next().unwrap();
30323027
let psbt_input = wallet2.get_psbt_input(utxo.clone(), None, false).unwrap();
3033-
#[allow(deprecated)]
30343028
let foreign_utxo_satisfaction = wallet2
30353029
.get_descriptor_for_keychain(KeychainKind::External)
3036-
.max_satisfaction_weight()
3030+
.max_weight_to_satisfy()
30373031
.unwrap();
30383032

30393033
assert!(

0 commit comments

Comments
 (0)