Skip to content

Commit 663fb13

Browse files
committed
fix(tx_builder)!: make TxBuilder Send safe, remove Clone trait
1 parent 03c7368 commit 663fb13

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

crates/wallet/src/wallet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ impl Wallet {
11951195
/// [`TxBuilder`]: crate::TxBuilder
11961196
pub fn build_tx(&mut self) -> TxBuilder<'_, DefaultCoinSelectionAlgorithm> {
11971197
TxBuilder {
1198-
wallet: alloc::rc::Rc::new(core::cell::RefCell::new(self)),
1198+
wallet: self,
11991199
params: TxParams::default(),
12001200
coin_selection: DefaultCoinSelectionAlgorithm::default(),
12011201
}
@@ -1701,7 +1701,7 @@ impl Wallet {
17011701
};
17021702

17031703
Ok(TxBuilder {
1704-
wallet: alloc::rc::Rc::new(core::cell::RefCell::new(self)),
1704+
wallet: self,
17051705
params,
17061706
coin_selection: DefaultCoinSelectionAlgorithm::default(),
17071707
})

crates/wallet/src/wallet/tx_builder.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
//! # Ok::<(), anyhow::Error>(())
3737
//! ```
3838
39-
use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec};
40-
use core::cell::RefCell;
39+
use alloc::{boxed::Box, string::String, vec::Vec};
4140
use core::fmt;
4241

4342
use alloc::sync::Arc;
@@ -111,7 +110,7 @@ use crate::{KeychainKind, LocalOutput, Utxo, WeightedUtxo};
111110
/// [`coin_selection`]: Self::coin_selection
112111
#[derive(Debug)]
113112
pub struct TxBuilder<'a, Cs> {
114-
pub(crate) wallet: Rc<RefCell<&'a mut Wallet>>,
113+
pub(crate) wallet: &'a mut Wallet,
115114
pub(crate) params: TxParams,
116115
pub(crate) coin_selection: Cs,
117116
}
@@ -161,16 +160,6 @@ impl Default for FeePolicy {
161160
}
162161
}
163162

164-
impl<'a, Cs: Clone> Clone for TxBuilder<'a, Cs> {
165-
fn clone(&self) -> Self {
166-
TxBuilder {
167-
wallet: self.wallet.clone(),
168-
params: self.params.clone(),
169-
coin_selection: self.coin_selection.clone(),
170-
}
171-
}
172-
}
173-
174163
// Methods supported for any CoinSelectionAlgorithm.
175164
impl<'a, Cs> TxBuilder<'a, Cs> {
176165
/// Set a custom fee rate.
@@ -286,7 +275,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
286275
/// the "utxos" and the "unspendable" list, it will be spent.
287276
pub fn add_utxos(&mut self, outpoints: &[OutPoint]) -> Result<&mut Self, AddUtxoError> {
288277
{
289-
let wallet = self.wallet.borrow();
278+
let wallet = &mut self.wallet;
290279
let utxos = outpoints
291280
.iter()
292281
.map(|outpoint| {
@@ -682,9 +671,7 @@ impl<'a, Cs: CoinSelectionAlgorithm> TxBuilder<'a, Cs> {
682671
/// **WARNING**: To avoid change address reuse you must persist the changes resulting from one
683672
/// or more calls to this method before closing the wallet. See [`Wallet::reveal_next_address`].
684673
pub fn finish_with_aux_rand(self, rng: &mut impl RngCore) -> Result<Psbt, CreateTxError> {
685-
self.wallet
686-
.borrow_mut()
687-
.create_tx(self.coin_selection, self.params, rng)
674+
self.wallet.create_tx(self.coin_selection, self.params, rng)
688675
}
689676
}
690677

@@ -750,7 +737,7 @@ impl fmt::Display for AddForeignUtxoError {
750737
#[cfg(feature = "std")]
751738
impl std::error::Error for AddForeignUtxoError {}
752739

753-
type TxSort<T> = dyn Fn(&T, &T) -> core::cmp::Ordering;
740+
type TxSort<T> = dyn (Fn(&T, &T) -> core::cmp::Ordering) + Send + Sync;
754741

755742
/// Ordering of the transaction's inputs and outputs
756743
#[derive(Clone, Default)]

crates/wallet/tests/wallet.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,11 +1645,10 @@ fn test_add_foreign_utxo_only_witness_utxo() {
16451645
.max_weight_to_satisfy()
16461646
.unwrap();
16471647

1648-
let mut builder = wallet1.build_tx();
1649-
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000));
1650-
16511648
{
1652-
let mut builder = builder.clone();
1649+
let mut builder = wallet1.build_tx();
1650+
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000));
1651+
16531652
let psbt_input = psbt::Input {
16541653
witness_utxo: Some(utxo2.txout.clone()),
16551654
..Default::default()
@@ -1664,7 +1663,9 @@ fn test_add_foreign_utxo_only_witness_utxo() {
16641663
}
16651664

16661665
{
1667-
let mut builder = builder.clone();
1666+
let mut builder = wallet1.build_tx();
1667+
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000));
1668+
16681669
let psbt_input = psbt::Input {
16691670
witness_utxo: Some(utxo2.txout.clone()),
16701671
..Default::default()
@@ -1680,7 +1681,9 @@ fn test_add_foreign_utxo_only_witness_utxo() {
16801681
}
16811682

16821683
{
1683-
let mut builder = builder.clone();
1684+
let mut builder = wallet1.build_tx();
1685+
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000));
1686+
16841687
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx;
16851688
let psbt_input = psbt::Input {
16861689
non_witness_utxo: Some(tx2.as_ref().clone()),
@@ -4169,3 +4172,9 @@ fn test_transactions_sort_by() {
41694172
.collect();
41704173
assert_eq!([None, Some(2000), Some(1000)], conf_heights.as_slice());
41714174
}
4175+
4176+
#[test]
4177+
fn test_tx_builder_is_send_safe() {
4178+
let (mut wallet, _txid) = get_funded_wallet_wpkh();
4179+
let _box: Box<dyn Send + Sync> = Box::new(wallet.build_tx());
4180+
}

0 commit comments

Comments
 (0)