Skip to content

Commit 200a16d

Browse files
committed
fix(wallet)!: delete method insert_tx
Inserting unconfirmed txs can be done using the existing method `apply_unconfirmed_txs`. Also removed `insert_checkpoint`, as the API is unclear regarding where in the local chain the given block should connect. Analogs for these methods are found in `test_utils` module and are mainly used to facilitate testing.
1 parent ab27884 commit 200a16d

File tree

2 files changed

+2
-86
lines changed

2 files changed

+2
-86
lines changed

crates/wallet/src/wallet/mod.rs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ use core::{cmp::Ordering, fmt, mem, ops::Deref};
2424
use bdk_chain::{
2525
indexed_tx_graph,
2626
indexer::keychain_txout::KeychainTxOutIndex,
27-
local_chain::{
28-
self, ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain,
29-
},
27+
local_chain::{ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain},
3028
spk_client::{
3129
FullScanRequest, FullScanRequestBuilder, FullScanResult, SyncRequest, SyncRequestBuilder,
3230
SyncResult,
@@ -120,7 +118,7 @@ pub struct Wallet {
120118

121119
/// An update to [`Wallet`].
122120
///
123-
/// It updates [`KeychainTxOutIndex`], [`bdk_chain::TxGraph`] and [`local_chain::LocalChain`] atomically.
121+
/// It updates [`KeychainTxOutIndex`], [`bdk_chain::TxGraph`] and [`LocalChain`] atomically.
124122
#[derive(Debug, Clone, Default)]
125123
pub struct Update {
126124
/// Contains the last active derivation indices per keychain (`K`), which is used to update the
@@ -131,8 +129,6 @@ pub struct Update {
131129
pub tx_update: TxUpdate<ConfirmationBlockTime>,
132130

133131
/// Update for the wallet's internal [`LocalChain`].
134-
///
135-
/// [`LocalChain`]: local_chain::LocalChain
136132
pub chain: Option<CheckPoint>,
137133
}
138134

@@ -1068,44 +1064,6 @@ impl Wallet {
10681064
})
10691065
}
10701066

1071-
/// Add a new checkpoint to the wallet's internal view of the chain.
1072-
///
1073-
/// Returns whether anything changed with the insertion (e.g. `false` if checkpoint was already
1074-
/// there).
1075-
///
1076-
/// **WARNING**: You must persist the changes resulting from one or more calls to this method
1077-
/// if you need the inserted checkpoint data to be reloaded after closing the wallet.
1078-
/// See [`Wallet::reveal_next_address`].
1079-
pub fn insert_checkpoint(
1080-
&mut self,
1081-
block_id: BlockId,
1082-
) -> Result<bool, local_chain::AlterCheckPointError> {
1083-
let changeset = self.chain.insert_block(block_id)?;
1084-
let changed = !changeset.is_empty();
1085-
self.stage.merge(changeset.into());
1086-
Ok(changed)
1087-
}
1088-
1089-
/// Add a transaction to the wallet's internal view of the chain. This stages the change,
1090-
/// you must persist it later.
1091-
///
1092-
/// This method inserts the given `tx` and returns whether anything changed after insertion,
1093-
/// which will be false if the same transaction already exists in the wallet's transaction
1094-
/// graph. Any changes are staged but not committed.
1095-
///
1096-
/// # Note
1097-
///
1098-
/// By default the inserted `tx` won't be considered "canonical" because it's not known
1099-
/// whether the transaction exists in the best chain. To know whether it exists, the tx
1100-
/// must be broadcast to the network and the wallet synced via a chain source.
1101-
pub fn insert_tx<T: Into<Arc<Transaction>>>(&mut self, tx: T) -> bool {
1102-
let mut changeset = ChangeSet::default();
1103-
changeset.merge(self.indexed_graph.insert_tx(tx).into());
1104-
let ret = !changeset.is_empty();
1105-
self.stage.merge(changeset);
1106-
ret
1107-
}
1108-
11091067
/// Iterate over the transactions in the wallet.
11101068
pub fn transactions(&self) -> impl Iterator<Item = WalletTx> + '_ {
11111069
self.indexed_graph

crates/wallet/tests/wallet.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4124,48 +4124,6 @@ fn test_thread_safety() {
41244124
thread_safe::<Wallet>(); // compiles only if true
41254125
}
41264126

4127-
#[test]
4128-
fn test_insert_tx_balance_and_utxos() {
4129-
// creating many txs has no effect on the wallet's available utxos
4130-
let (mut wallet, _) = get_funded_wallet_single(get_test_tr_single_sig_xprv());
4131-
let addr = Address::from_str("bcrt1qc6fweuf4xjvz4x3gx3t9e0fh4hvqyu2qw4wvxm")
4132-
.unwrap()
4133-
.assume_checked();
4134-
4135-
let unspent: Vec<_> = wallet.list_unspent().collect();
4136-
assert!(!unspent.is_empty());
4137-
4138-
let balance = wallet.balance().total();
4139-
let fee = Amount::from_sat(143);
4140-
let amt = balance - fee;
4141-
4142-
for _ in 0..3 {
4143-
let mut builder = wallet.build_tx();
4144-
builder.add_recipient(addr.script_pubkey(), amt);
4145-
let mut psbt = builder.finish().unwrap();
4146-
assert!(wallet.sign(&mut psbt, SignOptions::default()).unwrap());
4147-
let tx = psbt.extract_tx().unwrap();
4148-
let _ = wallet.insert_tx(tx);
4149-
}
4150-
assert_eq!(wallet.list_unspent().collect::<Vec<_>>(), unspent);
4151-
assert_eq!(wallet.balance().confirmed, balance);
4152-
4153-
// manually setting a tx last_seen will consume the wallet's available utxos
4154-
let addr = Address::from_str("bcrt1qfjg5lv3dvc9az8patec8fjddrs4aqtauadnagr")
4155-
.unwrap()
4156-
.assume_checked();
4157-
let mut builder = wallet.build_tx();
4158-
builder.add_recipient(addr.script_pubkey(), amt);
4159-
let mut psbt = builder.finish().unwrap();
4160-
assert!(wallet.sign(&mut psbt, SignOptions::default()).unwrap());
4161-
let tx = psbt.extract_tx().unwrap();
4162-
let txid = tx.compute_txid();
4163-
let _ = wallet.insert_tx(tx);
4164-
insert_seen_at(&mut wallet, txid, 2);
4165-
assert!(wallet.list_unspent().next().is_none());
4166-
assert_eq!(wallet.balance().total().to_sat(), 0);
4167-
}
4168-
41694127
#[test]
41704128
fn single_descriptor_wallet_can_create_tx_and_receive_change() {
41714129
// create single descriptor wallet and fund it

0 commit comments

Comments
 (0)