From c4057297a96ccbd49d984b7139994b801c2120dc Mon Sep 17 00:00:00 2001 From: valued mammal Date: Tue, 25 Jun 2024 11:00:17 -0400 Subject: [PATCH] wallet: delete method `insert_anchor` --- crates/wallet/src/wallet/export.rs | 23 ++++++++----- crates/wallet/src/wallet/mod.rs | 41 +++++++---------------- crates/wallet/tests/common.rs | 12 +++++-- crates/wallet/tests/wallet.rs | 52 ++++++++++++++++++------------ 4 files changed, 69 insertions(+), 59 deletions(-) diff --git a/crates/wallet/src/wallet/export.rs b/crates/wallet/src/wallet/export.rs index c4a3c0961..bf86f926a 100644 --- a/crates/wallet/src/wallet/export.rs +++ b/crates/wallet/src/wallet/export.rs @@ -222,6 +222,8 @@ mod test { use crate::wallet::Wallet; fn get_test_wallet(descriptor: &str, change_descriptor: &str, network: Network) -> Wallet { + use crate::wallet::Update; + use bdk_chain::TxGraph; let mut wallet = Wallet::new(descriptor, change_descriptor, network).unwrap(); let transaction = Transaction { input: vec![], @@ -236,14 +238,19 @@ mod test { }; wallet.insert_checkpoint(block_id).unwrap(); wallet.insert_tx(transaction); - wallet.insert_anchor( - txid, - ConfirmationTimeHeightAnchor { - confirmation_height: 5000, - confirmation_time: 0, - anchor_block: block_id, - }, - ); + let anchor = ConfirmationTimeHeightAnchor { + confirmation_height: 5000, + confirmation_time: 0, + anchor_block: block_id, + }; + let mut graph = TxGraph::default(); + let _ = graph.insert_anchor(txid, anchor); + wallet + .apply_update(Update { + graph, + ..Default::default() + }) + .unwrap(); wallet } diff --git a/crates/wallet/src/wallet/mod.rs b/crates/wallet/src/wallet/mod.rs index 6f8f8d0db..235f600b6 100644 --- a/crates/wallet/src/wallet/mod.rs +++ b/crates/wallet/src/wallet/mod.rs @@ -877,23 +877,6 @@ impl Wallet { self.stage.append(additions.into()); } - /// Inserts an `anchor` for a transaction with the given `txid`. - /// - /// This stages the changes, you must persist them later. - pub fn insert_anchor(&mut self, txid: Txid, anchor: ConfirmationTimeHeightAnchor) { - let indexed_graph_changeset = self.indexed_graph.insert_anchor(txid, anchor); - self.stage.append(indexed_graph_changeset.into()); - } - - /// Inserts a unix timestamp of when a transaction is seen in the mempool. - /// - /// This is used for transaction conflict resolution where the transaction with the - /// later last-seen is prioritized. This stages the changes, you must persist them later. - pub fn insert_seen_at(&mut self, txid: Txid, seen_at: u64) { - let indexed_graph_changeset = self.indexed_graph.insert_seen_at(txid, seen_at); - self.stage.append(indexed_graph_changeset.into()); - } - /// Calculates the fee of a given transaction. Returns [`Amount::ZERO`] if `tx` is a coinbase transaction. /// /// To calculate the fee for a [`Transaction`] with inputs not owned by this wallet you must @@ -2486,8 +2469,9 @@ macro_rules! floating_rate { macro_rules! doctest_wallet { () => {{ use $crate::bitcoin::{BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash}; - use $crate::chain::{ConfirmationTimeHeightAnchor, BlockId}; - use $crate::{KeychainKind, wallet::Wallet}; + use $crate::chain::{ConfirmationTimeHeightAnchor, BlockId, TxGraph}; + use $crate::wallet::{Update, Wallet}; + use $crate::KeychainKind; let descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/0/*)"; let change_descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/1/*)"; @@ -2511,16 +2495,15 @@ macro_rules! doctest_wallet { let block = BlockId { height: 1_000, hash: BlockHash::all_zeros() }; let _ = wallet.insert_checkpoint(block); let _ = wallet.insert_tx(tx); - wallet - .insert_anchor( - txid, - ConfirmationTimeHeightAnchor { - confirmation_height: 500, - confirmation_time: 50_000, - anchor_block: block, - } - ); - + let anchor = ConfirmationTimeHeightAnchor { + confirmation_height: 500, + confirmation_time: 50_000, + anchor_block: block, + }; + let mut graph = TxGraph::default(); + let _ = graph.insert_anchor(txid, anchor); + let update = Update { graph, ..Default::default() }; + wallet.apply_update(update).unwrap(); wallet }} } diff --git a/crates/wallet/tests/common.rs b/crates/wallet/tests/common.rs index 2d4bb8527..0a3e479b8 100644 --- a/crates/wallet/tests/common.rs +++ b/crates/wallet/tests/common.rs @@ -1,7 +1,8 @@ #![allow(unused)] use bdk_chain::indexed_tx_graph::Indexer; -use bdk_chain::{BlockId, ConfirmationTime, ConfirmationTimeHeightAnchor}; +use bdk_chain::{BlockId, ConfirmationTime, ConfirmationTimeHeightAnchor, TxGraph}; +use bdk_wallet::wallet::Update; use bdk_wallet::{KeychainKind, LocalOutput, Wallet}; use bitcoin::hashes::Hash; use bitcoin::{ @@ -212,6 +213,13 @@ pub fn insert_anchor_from_conf(wallet: &mut Wallet, txid: Txid, position: Confir }) .expect("confirmation height cannot be greater than tip"); - wallet.insert_anchor(txid, anchor); + let mut graph = TxGraph::default(); + let _ = graph.insert_anchor(txid, anchor); + wallet + .apply_update(Update { + graph, + ..Default::default() + }) + .unwrap(); } } diff --git a/crates/wallet/tests/wallet.rs b/crates/wallet/tests/wallet.rs index 5937e05be..452c839e1 100644 --- a/crates/wallet/tests/wallet.rs +++ b/crates/wallet/tests/wallet.rs @@ -50,7 +50,7 @@ fn receive_output(wallet: &mut Wallet, value: u64, height: ConfirmationTime) -> insert_anchor_from_conf(wallet, txid, height); } ConfirmationTime::Unconfirmed { last_seen } => { - wallet.insert_seen_at(txid, last_seen); + insert_seen_at(wallet, txid, last_seen); } } @@ -68,6 +68,18 @@ fn receive_output_in_latest_block(wallet: &mut Wallet, value: u64) -> OutPoint { receive_output(wallet, value, anchor) } +fn insert_seen_at(wallet: &mut Wallet, txid: Txid, seen_at: u64) { + use bdk_wallet::wallet::Update; + let mut graph = bdk_chain::TxGraph::default(); + let _ = graph.insert_seen_at(txid, seen_at); + wallet + .apply_update(Update { + graph, + ..Default::default() + }) + .unwrap(); +} + // The satisfaction size of a P2WPKH is 112 WU = // 1 (elements in witness) + 1 (OP_PUSH) + 33 (pk) + 1 (OP_PUSH) + 72 (signature + sighash) + 1*4 (script len) // On the witness itself, we have to push once for the pk (33WU) and once for signature + sighash (72WU), for @@ -1291,7 +1303,7 @@ fn test_create_tx_policy_path_no_csv() { }; let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let external_policy = wallet.policies(KeychainKind::External).unwrap().unwrap(); let root_id = external_policy.id; @@ -1683,7 +1695,7 @@ fn test_bump_fee_irreplaceable_tx() { let tx = psbt.extract_tx().expect("failed to extract tx"); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); wallet.build_fee_bump(txid).unwrap().finish().unwrap(); } @@ -1727,7 +1739,7 @@ fn test_bump_fee_low_fee_rate() { let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder.fee_rate(FeeRate::BROADCAST_MIN); @@ -1759,7 +1771,7 @@ fn test_bump_fee_low_abs() { let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder.fee_absolute(Amount::from_sat(10)); @@ -1780,7 +1792,7 @@ fn test_bump_fee_zero_abs() { let tx = psbt.extract_tx().expect("failed to extract tx"); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder.fee_absolute(Amount::ZERO); @@ -1805,7 +1817,7 @@ fn test_bump_fee_reduce_change() { let tx = psbt.extract_tx().expect("failed to extract tx"); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb let mut builder = wallet.build_fee_bump(txid).unwrap(); @@ -1902,7 +1914,7 @@ fn test_bump_fee_reduce_single_recipient() { let original_fee = check_fee!(wallet, psbt); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb let mut builder = wallet.build_fee_bump(txid).unwrap(); @@ -1949,7 +1961,7 @@ fn test_bump_fee_absolute_reduce_single_recipient() { let original_sent_received = wallet.sent_and_received(&tx); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder @@ -2024,7 +2036,7 @@ fn test_bump_fee_drain_wallet() { let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); assert_eq!(original_sent_received.0, Amount::from_sat(25_000)); // for the new feerate, it should be enough to reduce the output, but since we specify @@ -2089,7 +2101,7 @@ fn test_bump_fee_remove_output_manually_selected_only() { let original_sent_received = wallet.sent_and_received(&tx); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); assert_eq!(original_sent_received.0, Amount::from_sat(25_000)); let mut builder = wallet.build_fee_bump(txid).unwrap(); @@ -2136,7 +2148,7 @@ fn test_bump_fee_add_input() { let original_details = wallet.sent_and_received(&tx); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(50)); @@ -2192,7 +2204,7 @@ fn test_bump_fee_absolute_add_input() { let original_sent_received = wallet.sent_and_received(&tx); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder.fee_absolute(Amount::from_sat(6_000)); @@ -2257,7 +2269,7 @@ fn test_bump_fee_no_change_add_input_and_change() { let tx = psbt.extract_tx().expect("failed to extract tx"); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); // Now bump the fees, the wallet should add an extra input and a change output, and leave // the original output untouched. @@ -2326,7 +2338,7 @@ fn test_bump_fee_add_input_change_dust() { assert_eq!(tx.output.len(), 2); let txid = tx.compute_txid(); wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); // We set a fee high enough that during rbf we are forced to add @@ -2396,7 +2408,7 @@ fn test_bump_fee_force_add_input() { txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature } wallet.insert_tx(tx.clone()); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); // the new fee_rate is low enough that just reducing the change would be fine, but we force // the addition of an extra input with `add_utxo()` let mut builder = wallet.build_fee_bump(txid).unwrap(); @@ -2462,7 +2474,7 @@ fn test_bump_fee_absolute_force_add_input() { txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature } wallet.insert_tx(tx.clone()); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); // the new fee_rate is low enough that just reducing the change would be fine, but we force // the addition of an extra input with `add_utxo()` @@ -2540,7 +2552,7 @@ fn test_bump_fee_unconfirmed_inputs_only() { txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature } wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(25)); builder.finish().unwrap(); @@ -2572,7 +2584,7 @@ fn test_bump_fee_unconfirmed_input() { txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature } wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 0); + insert_seen_at(&mut wallet, txid, 0); let mut builder = wallet.build_fee_bump(txid).unwrap(); builder @@ -4107,7 +4119,7 @@ fn test_insert_tx_balance_and_utxos() { let tx = psbt.extract_tx().unwrap(); let txid = tx.compute_txid(); let _ = wallet.insert_tx(tx); - wallet.insert_seen_at(txid, 2); + insert_seen_at(&mut wallet, txid, 2); assert!(wallet.list_unspent().next().is_none()); assert_eq!(wallet.balance().total().to_sat(), 0); }