Skip to content

Commit 798ed8c

Browse files
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 Update crates/bdk/src/wallet/coin_selection.rs Co-authored-by: ValuedMammal <[email protected]>
1 parent b5557dc commit 798ed8c

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};
@@ -1712,10 +1711,9 @@ impl<D> Wallet<D> {
17121711

17131712
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
17141713
Some((keychain, derivation_index)) => {
1715-
#[allow(deprecated)]
17161714
let satisfaction_weight = self
17171715
.get_descriptor_for_keychain(keychain)
1718-
.max_satisfaction_weight()
1716+
.max_weight_to_satisfy()
17191717
.unwrap();
17201718
WeightedUtxo {
17211719
utxo: Utxo::Local(LocalOutput {
@@ -1733,7 +1731,6 @@ impl<D> Wallet<D> {
17331731
let satisfaction_weight =
17341732
serialize(&txin.script_sig).len() * 4 + serialize(&txin.witness).len();
17351733
WeightedUtxo {
1736-
satisfaction_weight,
17371734
utxo: Utxo::Foreign {
17381735
outpoint: txin.previous_output,
17391736
sequence: Some(txin.sequence),
@@ -1743,6 +1740,7 @@ impl<D> Wallet<D> {
17431740
..Default::default()
17441741
}),
17451742
},
1743+
satisfaction_weight,
17461744
}
17471745
}
17481746
};
@@ -2057,13 +2055,11 @@ impl<D> Wallet<D> {
20572055
self.list_unspent()
20582056
.map(|utxo| {
20592057
let keychain = utxo.keychain;
2060-
#[allow(deprecated)]
2061-
(
2062-
utxo,
2058+
(utxo, {
20632059
self.get_descriptor_for_keychain(keychain)
2064-
.max_satisfaction_weight()
2065-
.unwrap(),
2066-
)
2060+
.max_weight_to_satisfy()
2061+
.unwrap()
2062+
})
20672063
})
20682064
.collect()
20692065
}

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
@@ -1171,10 +1171,9 @@ fn test_add_foreign_utxo() {
11711171
.unwrap()
11721172
.assume_checked();
11731173
let utxo = wallet2.list_unspent().next().expect("must take!");
1174-
#[allow(deprecated)]
11751174
let foreign_utxo_satisfaction = wallet2
11761175
.get_descriptor_for_keychain(KeychainKind::External)
1177-
.max_satisfaction_weight()
1176+
.max_weight_to_satisfy()
11781177
.unwrap();
11791178

11801179
let psbt_input = psbt::Input {
@@ -1247,10 +1246,9 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
12471246
.unwrap()
12481247
.assume_checked();
12491248
let utxo = wallet2.list_unspent().next().expect("must take!");
1250-
#[allow(deprecated)]
12511249
let foreign_utxo_satisfaction = wallet2
12521250
.get_descriptor_for_keychain(KeychainKind::External)
1253-
.max_satisfaction_weight()
1251+
.max_weight_to_satisfy()
12541252
.unwrap();
12551253

12561254
let psbt_input = psbt::Input {
@@ -1273,10 +1271,9 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
12731271
fn test_add_foreign_utxo_invalid_psbt_input() {
12741272
let (mut wallet, _) = get_funded_wallet(get_test_wpkh());
12751273
let outpoint = wallet.list_unspent().next().expect("must exist").outpoint;
1276-
#[allow(deprecated)]
12771274
let foreign_utxo_satisfaction = wallet
12781275
.get_descriptor_for_keychain(KeychainKind::External)
1279-
.max_satisfaction_weight()
1276+
.max_weight_to_satisfy()
12801277
.unwrap();
12811278

12821279
let mut builder = wallet.build_tx();
@@ -1295,10 +1292,9 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
12951292
let tx1 = wallet1.get_tx(txid1).unwrap().tx_node.tx.clone();
12961293
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx.clone();
12971294

1298-
#[allow(deprecated)]
12991295
let satisfaction_weight = wallet2
13001296
.get_descriptor_for_keychain(KeychainKind::External)
1301-
.max_satisfaction_weight()
1297+
.max_weight_to_satisfy()
13021298
.unwrap();
13031299

13041300
let mut builder = wallet1.build_tx();
@@ -1340,10 +1336,9 @@ fn test_add_foreign_utxo_only_witness_utxo() {
13401336
.assume_checked();
13411337
let utxo2 = wallet2.list_unspent().next().unwrap();
13421338

1343-
#[allow(deprecated)]
13441339
let satisfaction_weight = wallet2
13451340
.get_descriptor_for_keychain(KeychainKind::External)
1346-
.max_satisfaction_weight()
1341+
.max_weight_to_satisfy()
13471342
.unwrap();
13481343

13491344
let mut builder = wallet1.build_tx();
@@ -3074,10 +3069,9 @@ fn test_taproot_foreign_utxo() {
30743069
.assume_checked();
30753070
let utxo = wallet2.list_unspent().next().unwrap();
30763071
let psbt_input = wallet2.get_psbt_input(utxo.clone(), None, false).unwrap();
3077-
#[allow(deprecated)]
30783072
let foreign_utxo_satisfaction = wallet2
30793073
.get_descriptor_for_keychain(KeychainKind::External)
3080-
.max_satisfaction_weight()
3074+
.max_weight_to_satisfy()
30813075
.unwrap();
30823076

30833077
assert!(

0 commit comments

Comments
 (0)