Skip to content

Commit 7abec98

Browse files
committed
fix(wallet): remove TxBuilder allow_shrinking function and unneeded context param
1 parent 80e190b commit 7abec98

File tree

3 files changed

+34
-88
lines changed

3 files changed

+34
-88
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub use utils::IsDust;
5757
#[allow(deprecated)]
5858
use coin_selection::DefaultCoinSelectionAlgorithm;
5959
use signer::{SignOptions, SignerOrdering, SignersContainer, TransactionSigner};
60-
use tx_builder::{BumpFee, CreateTx, FeePolicy, TxBuilder, TxParams};
60+
use tx_builder::{FeePolicy, TxBuilder, TxParams};
6161
use utils::{check_nsequence_rbf, After, Older, SecpCtx};
6262

6363
use crate::descriptor::policy::BuildSatisfaction;
@@ -1252,12 +1252,11 @@ impl<D> Wallet<D> {
12521252
/// ```
12531253
///
12541254
/// [`TxBuilder`]: crate::TxBuilder
1255-
pub fn build_tx(&mut self) -> TxBuilder<'_, D, DefaultCoinSelectionAlgorithm, CreateTx> {
1255+
pub fn build_tx(&mut self) -> TxBuilder<'_, D, DefaultCoinSelectionAlgorithm> {
12561256
TxBuilder {
12571257
wallet: alloc::rc::Rc::new(core::cell::RefCell::new(self)),
12581258
params: TxParams::default(),
12591259
coin_selection: DefaultCoinSelectionAlgorithm::default(),
1260-
phantom: core::marker::PhantomData,
12611260
}
12621261
}
12631262

@@ -1662,7 +1661,7 @@ impl<D> Wallet<D> {
16621661
pub fn build_fee_bump(
16631662
&mut self,
16641663
txid: Txid,
1665-
) -> Result<TxBuilder<'_, D, DefaultCoinSelectionAlgorithm, BumpFee>, BuildFeeBumpError> {
1664+
) -> Result<TxBuilder<'_, D, DefaultCoinSelectionAlgorithm>, BuildFeeBumpError> {
16661665
let graph = self.indexed_graph.graph();
16671666
let txout_index = &self.indexed_graph.index;
16681667
let chain_tip = self.chain.tip().block_id();
@@ -1786,7 +1785,6 @@ impl<D> Wallet<D> {
17861785
wallet: alloc::rc::Rc::new(core::cell::RefCell::new(self)),
17871786
params,
17881787
coin_selection: DefaultCoinSelectionAlgorithm::default(),
1789-
phantom: core::marker::PhantomData,
17901788
})
17911789
}
17921790

crates/bdk/src/wallet/tx_builder.rs

Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@
4343
use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec};
4444
use core::cell::RefCell;
4545
use core::fmt;
46-
use core::marker::PhantomData;
4746

4847
use bdk_chain::PersistBackend;
4948
use bitcoin::psbt::{self, PartiallySignedTransaction as Psbt};
5049
use bitcoin::script::PushBytes;
5150
use bitcoin::{absolute, FeeRate, OutPoint, ScriptBuf, Sequence, Transaction, Txid};
5251

53-
use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
52+
use super::coin_selection::CoinSelectionAlgorithm;
5453
use super::{ChangeSet, CreateTxError, Wallet};
5554
use crate::collections::{BTreeMap, HashSet};
5655
use crate::{KeychainKind, LocalOutput, Utxo, WeightedUtxo};
@@ -124,11 +123,10 @@ impl TxBuilderContext for BumpFee {}
124123
/// [`finish`]: Self::finish
125124
/// [`coin_selection`]: Self::coin_selection
126125
#[derive(Debug)]
127-
pub struct TxBuilder<'a, D, Cs, Ctx> {
126+
pub struct TxBuilder<'a, D, Cs> {
128127
pub(crate) wallet: Rc<RefCell<&'a mut Wallet<D>>>,
129128
pub(crate) params: TxParams,
130129
pub(crate) coin_selection: Cs,
131-
pub(crate) phantom: PhantomData<Ctx>,
132130
}
133131

134132
/// The parameters for transaction creation sans coin selection algorithm.
@@ -176,19 +174,18 @@ impl Default for FeePolicy {
176174
}
177175
}
178176

179-
impl<'a, D, Cs: Clone, Ctx> Clone for TxBuilder<'a, D, Cs, Ctx> {
177+
impl<'a, D, Cs: Clone> Clone for TxBuilder<'a, D, Cs> {
180178
fn clone(&self) -> Self {
181179
TxBuilder {
182180
wallet: self.wallet.clone(),
183181
params: self.params.clone(),
184182
coin_selection: self.coin_selection.clone(),
185-
phantom: PhantomData,
186183
}
187184
}
188185
}
189186

190187
// methods supported by both contexts, for any CoinSelectionAlgorithm
191-
impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
188+
impl<'a, D, Cs> TxBuilder<'a, D, Cs> {
192189
/// Set a custom fee rate.
193190
///
194191
/// This method sets the mining fee paid by the transaction as a rate on its size.
@@ -561,12 +558,11 @@ impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
561558
pub fn coin_selection<P: CoinSelectionAlgorithm>(
562559
self,
563560
coin_selection: P,
564-
) -> TxBuilder<'a, D, P, Ctx> {
561+
) -> TxBuilder<'a, D, P> {
565562
TxBuilder {
566563
wallet: self.wallet,
567564
params: self.params,
568565
coin_selection,
569-
phantom: PhantomData,
570566
}
571567
}
572568

@@ -616,7 +612,7 @@ impl<'a, D, Cs, Ctx> TxBuilder<'a, D, Cs, Ctx> {
616612
}
617613
}
618614

619-
impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx> TxBuilder<'a, D, Cs, Ctx> {
615+
impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs> {
620616
/// Finish building the transaction.
621617
///
622618
/// Returns a new [`Psbt`] per [`BIP174`].
@@ -694,29 +690,7 @@ impl fmt::Display for AddForeignUtxoError {
694690
#[cfg(feature = "std")]
695691
impl std::error::Error for AddForeignUtxoError {}
696692

697-
#[derive(Debug)]
698-
/// Error returned from [`TxBuilder::allow_shrinking`]
699-
pub enum AllowShrinkingError {
700-
/// Script/PubKey was not in the original transaction
701-
MissingScriptPubKey(ScriptBuf),
702-
}
703-
704-
impl fmt::Display for AllowShrinkingError {
705-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
706-
match self {
707-
Self::MissingScriptPubKey(script_buf) => write!(
708-
f,
709-
"Script/PubKey was not in the original transaction: {}",
710-
script_buf,
711-
),
712-
}
713-
}
714-
}
715-
716-
#[cfg(feature = "std")]
717-
impl std::error::Error for AllowShrinkingError {}
718-
719-
impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
693+
impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs> {
720694
/// Replace the recipients already added with a new list
721695
pub fn set_recipients(&mut self, recipients: Vec<(ScriptBuf, u64)>) -> &mut Self {
722696
self.params.recipients = recipients;
@@ -745,11 +719,8 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
745719
/// difference is that it is valid to use `drain_to` without setting any ordinary recipients
746720
/// with [`add_recipient`] (but it is perfectly fine to add recipients as well).
747721
///
748-
/// If you choose not to set any recipients, you should either provide the utxos that the
749-
/// transaction should spend via [`add_utxos`], or set [`drain_wallet`] to spend all of them.
750-
///
751-
/// When bumping the fees of a transaction made with this option, you probably want to
752-
/// use [`allow_shrinking`] to allow this output to be reduced to pay for the extra fees.
722+
/// If you choose not to set any recipients, you should provide the utxos that the
723+
/// transaction should spend via [`add_utxos`].
753724
///
754725
/// # Example
755726
///
@@ -783,7 +754,6 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
783754
/// # Ok::<(), anyhow::Error>(())
784755
/// ```
785756
///
786-
/// [`allow_shrinking`]: Self::allow_shrinking
787757
/// [`add_recipient`]: Self::add_recipient
788758
/// [`add_utxos`]: Self::add_utxos
789759
/// [`drain_wallet`]: Self::drain_wallet
@@ -793,38 +763,6 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
793763
}
794764
}
795765

796-
// methods supported only by bump_fee
797-
impl<'a, D> TxBuilder<'a, D, DefaultCoinSelectionAlgorithm, BumpFee> {
798-
/// Explicitly tells the wallet that it is allowed to reduce the amount of the output matching this
799-
/// `script_pubkey` in order to bump the transaction fee. Without specifying this the wallet
800-
/// will attempt to find a change output to shrink instead.
801-
///
802-
/// **Note** that the output may shrink to below the dust limit and therefore be removed. If it is
803-
/// preserved then it is currently not guaranteed to be in the same position as it was
804-
/// originally.
805-
///
806-
/// Returns an `Err` if `script_pubkey` can't be found among the recipients of the
807-
/// transaction we are bumping.
808-
pub fn allow_shrinking(
809-
&mut self,
810-
script_pubkey: ScriptBuf,
811-
) -> Result<&mut Self, AllowShrinkingError> {
812-
match self
813-
.params
814-
.recipients
815-
.iter()
816-
.position(|(recipient_script, _)| *recipient_script == script_pubkey)
817-
{
818-
Some(position) => {
819-
self.params.recipients.remove(position);
820-
self.params.drain_to = Some(script_pubkey);
821-
Ok(self)
822-
}
823-
None => Err(AllowShrinkingError::MissingScriptPubKey(script_pubkey)),
824-
}
825-
}
826-
}
827-
828766
/// Ordering of the transaction's inputs and outputs
829767
#[derive(Default, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)]
830768
pub enum TxOrdering {

crates/bdk/tests/wallet.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,8 +1684,12 @@ fn test_bump_fee_reduce_single_recipient() {
16841684
let mut builder = wallet.build_fee_bump(txid).unwrap();
16851685
builder
16861686
.fee_rate(feerate)
1687-
.allow_shrinking(addr.script_pubkey())
1688-
.unwrap();
1687+
// remove original tx drain_to address and amount
1688+
.set_recipients(Vec::new())
1689+
// set back original drain_to address
1690+
.drain_to(addr.script_pubkey())
1691+
// drain wallet output amount will be re-calculated with new fee rate
1692+
.drain_wallet();
16891693
let psbt = builder.finish().unwrap();
16901694
let sent_received = wallet.sent_and_received(&psbt.clone().extract_tx());
16911695
let fee = check_fee!(wallet, psbt);
@@ -1722,9 +1726,13 @@ fn test_bump_fee_absolute_reduce_single_recipient() {
17221726

17231727
let mut builder = wallet.build_fee_bump(txid).unwrap();
17241728
builder
1725-
.allow_shrinking(addr.script_pubkey())
1726-
.unwrap()
1727-
.fee_absolute(300);
1729+
.fee_absolute(300)
1730+
// remove original tx drain_to address and amount
1731+
.set_recipients(Vec::new())
1732+
// set back original drain_to address
1733+
.drain_to(addr.script_pubkey())
1734+
// drain wallet output amount will be re-calculated with new fee rate
1735+
.drain_wallet();
17281736
let psbt = builder.finish().unwrap();
17291737
let tx = &psbt.unsigned_tx;
17301738
let sent_received = wallet.sent_and_received(tx);
@@ -1790,8 +1798,6 @@ fn test_bump_fee_drain_wallet() {
17901798
let mut builder = wallet.build_fee_bump(txid).unwrap();
17911799
builder
17921800
.drain_wallet()
1793-
.allow_shrinking(addr.script_pubkey())
1794-
.unwrap()
17951801
.fee_rate(FeeRate::from_sat_per_vb_unchecked(5));
17961802
let psbt = builder.finish().unwrap();
17971803
let sent_received = wallet.sent_and_received(&psbt.extract_tx());
@@ -1807,7 +1813,7 @@ fn test_bump_fee_remove_output_manually_selected_only() {
18071813
// them, and make sure that `bump_fee` doesn't try to add more. This fails because we've
18081814
// told the wallet it's not allowed to add more inputs AND it can't reduce the value of the
18091815
// existing output. In other words, bump_fee + manually_selected_only is always an error
1810-
// unless you've also set "allow_shrinking" OR there is a change output.
1816+
// unless there is a change output.
18111817
let init_tx = Transaction {
18121818
version: 1,
18131819
lock_time: absolute::LockTime::ZERO,
@@ -2003,8 +2009,8 @@ fn test_bump_fee_no_change_add_input_and_change() {
20032009
.insert_tx(tx, ConfirmationTime::Unconfirmed { last_seen: 0 })
20042010
.unwrap();
20052011

2006-
// now bump the fees without using `allow_shrinking`. the wallet should add an
2007-
// extra input and a change output, and leave the original output untouched
2012+
// Now bump the fees, the wallet should add an extra input and a change output, and leave
2013+
// the original output untouched.
20082014
let mut builder = wallet.build_fee_bump(txid).unwrap();
20092015
builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(50));
20102016
let psbt = builder.finish().unwrap();
@@ -2297,8 +2303,12 @@ fn test_bump_fee_unconfirmed_input() {
22972303
let mut builder = wallet.build_fee_bump(txid).unwrap();
22982304
builder
22992305
.fee_rate(FeeRate::from_sat_per_vb_unchecked(15))
2300-
.allow_shrinking(addr.script_pubkey())
2301-
.unwrap();
2306+
// remove original tx drain_to address and amount
2307+
.set_recipients(Vec::new())
2308+
// set back original drain_to address
2309+
.drain_to(addr.script_pubkey())
2310+
// drain wallet output amount will be re-calculated with new fee rate
2311+
.drain_wallet();
23022312
builder.finish().unwrap();
23032313
}
23042314

0 commit comments

Comments
 (0)