Skip to content

Commit

Permalink
wallet: delete method insert_anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Jun 30, 2024
1 parent b34790c commit c405729
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 59 deletions.
23 changes: 15 additions & 8 deletions crates/wallet/src/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand All @@ -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
}

Expand Down
41 changes: 12 additions & 29 deletions crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/*)";

Expand All @@ -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
}}
}
12 changes: 10 additions & 2 deletions crates/wallet/tests/common.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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();
}
}
52 changes: 32 additions & 20 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()`
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

0 comments on commit c405729

Please sign in to comment.