]> Untitled Git - bdk/commitdiff
fix(wallet)!: delete method `insert_tx`
authorvalued mammal <valuedmammal@protonmail.com>
Tue, 5 Nov 2024 18:49:50 +0000 (13:49 -0500)
committervalued mammal <valuedmammal@protonmail.com>
Tue, 5 Nov 2024 19:14:35 +0000 (14:14 -0500)
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.

crates/wallet/src/wallet/mod.rs
crates/wallet/tests/wallet.rs

index 2059438d532c0c671ef39b2b0d11d81006bf3a39..a5178eb9e4822f84635db32598c5a5d68ab95dd3 100644 (file)
@@ -24,9 +24,7 @@ use core::{cmp::Ordering, fmt, mem, ops::Deref};
 use bdk_chain::{
     indexed_tx_graph,
     indexer::keychain_txout::KeychainTxOutIndex,
-    local_chain::{
-        self, ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain,
-    },
+    local_chain::{ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain},
     spk_client::{
         FullScanRequest, FullScanRequestBuilder, FullScanResult, SyncRequest, SyncRequestBuilder,
         SyncResult,
@@ -120,7 +118,7 @@ pub struct Wallet {
 
 /// An update to [`Wallet`].
 ///
-/// It updates [`KeychainTxOutIndex`], [`bdk_chain::TxGraph`] and [`local_chain::LocalChain`] atomically.
+/// It updates [`KeychainTxOutIndex`], [`bdk_chain::TxGraph`] and [`LocalChain`] atomically.
 #[derive(Debug, Clone, Default)]
 pub struct Update {
     /// Contains the last active derivation indices per keychain (`K`), which is used to update the
@@ -131,8 +129,6 @@ pub struct Update {
     pub tx_update: TxUpdate<ConfirmationBlockTime>,
 
     /// Update for the wallet's internal [`LocalChain`].
-    ///
-    /// [`LocalChain`]: local_chain::LocalChain
     pub chain: Option<CheckPoint>,
 }
 
@@ -1068,44 +1064,6 @@ impl Wallet {
         })
     }
 
-    /// Add a new checkpoint to the wallet's internal view of the chain.
-    ///
-    /// Returns whether anything changed with the insertion (e.g. `false` if checkpoint was already
-    /// there).
-    ///
-    /// **WARNING**: You must persist the changes resulting from one or more calls to this method
-    /// if you need the inserted checkpoint data to be reloaded after closing the wallet.
-    /// See [`Wallet::reveal_next_address`].
-    pub fn insert_checkpoint(
-        &mut self,
-        block_id: BlockId,
-    ) -> Result<bool, local_chain::AlterCheckPointError> {
-        let changeset = self.chain.insert_block(block_id)?;
-        let changed = !changeset.is_empty();
-        self.stage.merge(changeset.into());
-        Ok(changed)
-    }
-
-    /// Add a transaction to the wallet's internal view of the chain. This stages the change,
-    /// you must persist it later.
-    ///
-    /// This method inserts the given `tx` and returns whether anything changed after insertion,
-    /// which will be false if the same transaction already exists in the wallet's transaction
-    /// graph. Any changes are staged but not committed.
-    ///
-    /// # Note
-    ///
-    /// By default the inserted `tx` won't be considered "canonical" because it's not known
-    /// whether the transaction exists in the best chain. To know whether it exists, the tx
-    /// must be broadcast to the network and the wallet synced via a chain source.
-    pub fn insert_tx<T: Into<Arc<Transaction>>>(&mut self, tx: T) -> bool {
-        let mut changeset = ChangeSet::default();
-        changeset.merge(self.indexed_graph.insert_tx(tx).into());
-        let ret = !changeset.is_empty();
-        self.stage.merge(changeset);
-        ret
-    }
-
     /// Iterate over the transactions in the wallet.
     pub fn transactions(&self) -> impl Iterator<Item = WalletTx> + '_ {
         self.indexed_graph
index 9cf4177964bd4031210a52519540e8ccc49dddef..6bfae2ec739a2191fd6af0cf6f9bbaed3f7405a9 100644 (file)
@@ -4124,48 +4124,6 @@ fn test_thread_safety() {
     thread_safe::<Wallet>(); // compiles only if true
 }
 
-#[test]
-fn test_insert_tx_balance_and_utxos() {
-    // creating many txs has no effect on the wallet's available utxos
-    let (mut wallet, _) = get_funded_wallet_single(get_test_tr_single_sig_xprv());
-    let addr = Address::from_str("bcrt1qc6fweuf4xjvz4x3gx3t9e0fh4hvqyu2qw4wvxm")
-        .unwrap()
-        .assume_checked();
-
-    let unspent: Vec<_> = wallet.list_unspent().collect();
-    assert!(!unspent.is_empty());
-
-    let balance = wallet.balance().total();
-    let fee = Amount::from_sat(143);
-    let amt = balance - fee;
-
-    for _ in 0..3 {
-        let mut builder = wallet.build_tx();
-        builder.add_recipient(addr.script_pubkey(), amt);
-        let mut psbt = builder.finish().unwrap();
-        assert!(wallet.sign(&mut psbt, SignOptions::default()).unwrap());
-        let tx = psbt.extract_tx().unwrap();
-        let _ = wallet.insert_tx(tx);
-    }
-    assert_eq!(wallet.list_unspent().collect::<Vec<_>>(), unspent);
-    assert_eq!(wallet.balance().confirmed, balance);
-
-    // manually setting a tx last_seen will consume the wallet's available utxos
-    let addr = Address::from_str("bcrt1qfjg5lv3dvc9az8patec8fjddrs4aqtauadnagr")
-        .unwrap()
-        .assume_checked();
-    let mut builder = wallet.build_tx();
-    builder.add_recipient(addr.script_pubkey(), amt);
-    let mut psbt = builder.finish().unwrap();
-    assert!(wallet.sign(&mut psbt, SignOptions::default()).unwrap());
-    let tx = psbt.extract_tx().unwrap();
-    let txid = tx.compute_txid();
-    let _ = wallet.insert_tx(tx);
-    insert_seen_at(&mut wallet, txid, 2);
-    assert!(wallet.list_unspent().next().is_none());
-    assert_eq!(wallet.balance().total().to_sat(), 0);
-}
-
 #[test]
 fn single_descriptor_wallet_can_create_tx_and_receive_change() {
     // create single descriptor wallet and fund it