Skip to content

Commit 2bb6540

Browse files
committed
Merge #1345: fix: remove deprecated max_satisfaction_weight
798ed8c fix: remove deprecated `max_satisfaction_weight (Jose Storopoli) Pull request description: ### Description Continuation of #1115. Closes #1036. * Change deprecated `max_satisfaction_weight` to `max_weight_to_satisfy` * Remove `#[allow(deprecated)]` flags ### Notes to the reviewers I've changed all `max_satisfaction_weight()` to `max_weight_to_satisfy()` in `Wallet.get_available_utxo()` and `Wallet.build_fee_bump()`. Checking the docs on the `miniscript` crate for `max_weight_to_satisfy` has the following note: We are testing if the underlying descriptor `is.segwit()` or `.is_taproot`, then adding 4WU if true or leaving as it is otherwise. Another thing, we are not testing in BDK tests for legacy (pre-segwit) descriptors. Should I also add them to this PR? ### Changelog notice ### Fixed Replace the deprecated `max_satisfaction_weight` from `rust-miniscript` to `max_weight_to_satisfy`. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [ ] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [x] I'm linking the issue being fixed by this PR ACKs for top commit: evanlinjin: ACK 798ed8c Tree-SHA512: 60babecee13c24915348ddb64894127a76a59d9421d52ea37acc714913685d57cc2be1904f9d0508078dd1db1f7d7dad83a734af5ee981801ca87de2e9984429
2 parents 19304c1 + 798ed8c commit 2bb6540

File tree

4 files changed

+29
-42
lines changed

4 files changed

+29
-42
lines changed

crates/bdk/src/wallet/coin_selection.rs

Lines changed: 13 additions & 15 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 bitcoin::FeeRate;
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 {
@@ -347,10 +344,10 @@ fn select_sorted_utxos(
347344
if must_use || **selected_amount < target_amount + **fee_amount {
348345
**fee_amount += (fee_rate
349346
* Weight::from_wu(
350-
(TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as u64,
347+
(TxIn::default().segwit_weight() + weighted_utxo.satisfaction_weight)
348+
as u64,
351349
))
352350
.to_sat();
353-
354351
**selected_amount += weighted_utxo.utxo.txout().value;
355352
Some(weighted_utxo.utxo)
356353
} else {
@@ -392,9 +389,10 @@ struct OutputGroup {
392389
impl OutputGroup {
393390
fn new(weighted_utxo: WeightedUtxo, fee_rate: FeeRate) -> Self {
394391
let fee = (fee_rate
395-
* Weight::from_wu((TXIN_BASE_WEIGHT + weighted_utxo.satisfaction_weight) as u64))
392+
* Weight::from_wu(
393+
(TxIn::default().segwit_weight() + weighted_utxo.satisfaction_weight) as u64,
394+
))
396395
.to_sat();
397-
398396
let effective_value = weighted_utxo.utxo.txout().value as i64 - fee as i64;
399397
OutputGroup {
400398
weighted_utxo,
@@ -744,7 +742,7 @@ mod test {
744742
use core::str::FromStr;
745743

746744
use bdk_chain::ConfirmationTime;
747-
use bitcoin::{Amount, OutPoint, ScriptBuf, TxOut};
745+
use bitcoin::{Amount, OutPoint, ScriptBuf, TxIn, TxOut};
748746

749747
use super::*;
750748
use crate::types::*;
@@ -754,9 +752,9 @@ mod test {
754752
use rand::seq::SliceRandom;
755753
use rand::{Rng, RngCore, SeedableRng};
756754

757-
// n. of items on witness (1WU) + signature len (1WU) + signature and sighash (72WU)
758-
// + pubkey len (1WU) + pubkey (33WU) + script sig len (1 byte, 4WU)
759-
const P2WPKH_SATISFACTION_SIZE: usize = 1 + 1 + 72 + 1 + 33 + 4;
755+
// signature len (1WU) + signature and sighash (72WU)
756+
// + pubkey len (1WU) + pubkey (33WU)
757+
const P2WPKH_SATISFACTION_SIZE: usize = 1 + 72 + 1 + 33;
760758

761759
const FEE_AMOUNT: u64 = 50;
762760

@@ -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_weight = (TXIN_BASE_WEIGHT + P2WPKH_SATISFACTION_SIZE) as u64;
1241+
let input_weight = (TxIn::default().segwit_weight() + P2WPKH_SATISFACTION_SIZE) as u64;
12441242
// the final fee rate should be exactly the same as the fee rate given
12451243
let result_feerate = Amount::from_sat(result.fee_amount) / Weight::from_wu(input_weight);
12461244
assert_eq!(result_feerate, feerate);

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};
@@ -1714,10 +1713,9 @@ impl<D> Wallet<D> {
17141713

17151714
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
17161715
Some((keychain, derivation_index)) => {
1717-
#[allow(deprecated)]
17181716
let satisfaction_weight = self
17191717
.get_descriptor_for_keychain(keychain)
1720-
.max_satisfaction_weight()
1718+
.max_weight_to_satisfy()
17211719
.unwrap();
17221720
WeightedUtxo {
17231721
utxo: Utxo::Local(LocalOutput {
@@ -1735,7 +1733,6 @@ impl<D> Wallet<D> {
17351733
let satisfaction_weight =
17361734
serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len();
17371735
WeightedUtxo {
1738-
satisfaction_weight,
17391736
utxo: Utxo::Foreign {
17401737
outpoint: txin.previous_output,
17411738
sequence: Some(txin.sequence),
@@ -1745,6 +1742,7 @@ impl<D> Wallet<D> {
17451742
..Default::default()
17461743
}),
17471744
},
1745+
satisfaction_weight,
17481746
}
17491747
}
17501748
};
@@ -2059,13 +2057,11 @@ impl<D> Wallet<D> {
20592057
self.list_unspent()
20602058
.map(|utxo| {
20612059
let keychain = utxo.keychain;
2062-
#[allow(deprecated)]
2063-
(
2064-
utxo,
2060+
(utxo, {
20652061
self.get_descriptor_for_keychain(keychain)
2066-
.max_satisfaction_weight()
2067-
.unwrap(),
2068-
)
2062+
.max_weight_to_satisfy()
2063+
.unwrap()
2064+
})
20692065
})
20702066
.collect()
20712067
}

crates/bdk/src/wallet/tx_builder.rs

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

315315
for utxo in utxos {
316316
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
317-
#[allow(deprecated)]
318-
let satisfaction_weight = descriptor.max_satisfaction_weight().unwrap();
317+
let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
319318
self.params.utxos.push(WeightedUtxo {
320319
satisfaction_weight,
321320
utxo: Utxo::Local(utxo),
@@ -356,9 +355,9 @@ impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
356355
/// causing you to pay a fee that is too high. The party who is broadcasting the transaction can
357356
/// of course check the real input weight matches the expected weight prior to broadcasting.
358357
///
359-
/// To guarantee the `satisfaction_weight` is correct, you can require the party providing the
358+
/// To guarantee the `max_weight_to_satisfy` is correct, you can require the party providing the
360359
/// `psbt_input` provide a miniscript descriptor for the input so you can check it against the
361-
/// `script_pubkey` and then ask it for the [`max_satisfaction_weight`].
360+
/// `script_pubkey` and then ask it for the [`max_weight_to_satisfy`].
362361
///
363362
/// This is an **EXPERIMENTAL** feature, API and other major changes are expected.
364363
///
@@ -379,7 +378,7 @@ impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
379378
///
380379
/// [`only_witness_utxo`]: Self::only_witness_utxo
381380
/// [`finish`]: Self::finish
382-
/// [`max_satisfaction_weight`]: miniscript::Descriptor::max_satisfaction_weight
381+
/// [`max_weight_to_satisfy`]: miniscript::Descriptor::max_weight_to_satisfy
383382
pub fn add_foreign_utxo(
384383
&mut self,
385384
outpoint: OutPoint,

crates/bdk/tests/wallet.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,10 +1173,9 @@ fn test_add_foreign_utxo() {
11731173
.unwrap()
11741174
.assume_checked();
11751175
let utxo = wallet2.list_unspent().next().expect("must take!");
1176-
#[allow(deprecated)]
11771176
let foreign_utxo_satisfaction = wallet2
11781177
.get_descriptor_for_keychain(KeychainKind::External)
1179-
.max_satisfaction_weight()
1178+
.max_weight_to_satisfy()
11801179
.unwrap();
11811180

11821181
let psbt_input = psbt::Input {
@@ -1249,10 +1248,9 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
12491248
.unwrap()
12501249
.assume_checked();
12511250
let utxo = wallet2.list_unspent().next().expect("must take!");
1252-
#[allow(deprecated)]
12531251
let foreign_utxo_satisfaction = wallet2
12541252
.get_descriptor_for_keychain(KeychainKind::External)
1255-
.max_satisfaction_weight()
1253+
.max_weight_to_satisfy()
12561254
.unwrap();
12571255

12581256
let psbt_input = psbt::Input {
@@ -1275,10 +1273,9 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
12751273
fn test_add_foreign_utxo_invalid_psbt_input() {
12761274
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
12771275
let outpoint = wallet.list_unspent().next().expect("must exist").outpoint;
1278-
#[allow(deprecated)]
12791276
let foreign_utxo_satisfaction = wallet
12801277
.get_descriptor_for_keychain(KeychainKind::External)
1281-
.max_satisfaction_weight()
1278+
.max_weight_to_satisfy()
12821279
.unwrap();
12831280

12841281
let mut builder = wallet.build_tx();
@@ -1297,10 +1294,9 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
12971294
let tx1 = wallet1.get_tx(txid1).unwrap().tx_node.tx.clone();
12981295
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx.clone();
12991296

1300-
#[allow(deprecated)]
13011297
let satisfaction_weight = wallet2
13021298
.get_descriptor_for_keychain(KeychainKind::External)
1303-
.max_satisfaction_weight()
1299+
.max_weight_to_satisfy()
13041300
.unwrap();
13051301

13061302
let mut builder = wallet1.build_tx();
@@ -1342,10 +1338,9 @@ fn test_add_foreign_utxo_only_witness_utxo() {
13421338
.assume_checked();
13431339
let utxo2 = wallet2.list_unspent().next().unwrap();
13441340

1345-
#[allow(deprecated)]
13461341
let satisfaction_weight = wallet2
13471342
.get_descriptor_for_keychain(KeychainKind::External)
1348-
.max_satisfaction_weight()
1343+
.max_weight_to_satisfy()
13491344
.unwrap();
13501345

13511346
let mut builder = wallet1.build_tx();
@@ -3077,10 +3072,9 @@ fn test_taproot_foreign_utxo() {
30773072
.assume_checked();
30783073
let utxo = wallet2.list_unspent().next().unwrap();
30793074
let psbt_input = wallet2.get_psbt_input(utxo.clone(), None, false).unwrap();
3080-
#[allow(deprecated)]
30813075
let foreign_utxo_satisfaction = wallet2
30823076
.get_descriptor_for_keychain(KeychainKind::External)
3083-
.max_satisfaction_weight()
3077+
.max_weight_to_satisfy()
30843078
.unwrap();
30853079

30863080
assert!(

0 commit comments

Comments
 (0)