wallet: delete method insert_anchor

This commit is contained in:
valued mammal 2024-06-25 11:00:17 -04:00
parent b34790c6b6
commit c4057297a9
No known key found for this signature in database
4 changed files with 69 additions and 59 deletions

View File

@ -222,6 +222,8 @@ mod test {
use crate::wallet::Wallet; use crate::wallet::Wallet;
fn get_test_wallet(descriptor: &str, change_descriptor: &str, network: Network) -> 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 mut wallet = Wallet::new(descriptor, change_descriptor, network).unwrap();
let transaction = Transaction { let transaction = Transaction {
input: vec![], input: vec![],
@ -236,14 +238,19 @@ mod test {
}; };
wallet.insert_checkpoint(block_id).unwrap(); wallet.insert_checkpoint(block_id).unwrap();
wallet.insert_tx(transaction); wallet.insert_tx(transaction);
wallet.insert_anchor( let anchor = ConfirmationTimeHeightAnchor {
txid, confirmation_height: 5000,
ConfirmationTimeHeightAnchor { confirmation_time: 0,
confirmation_height: 5000, anchor_block: block_id,
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 wallet
} }

View File

@ -877,23 +877,6 @@ impl Wallet {
self.stage.append(additions.into()); 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. /// 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 /// 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 { macro_rules! doctest_wallet {
() => {{ () => {{
use $crate::bitcoin::{BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash}; use $crate::bitcoin::{BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash};
use $crate::chain::{ConfirmationTimeHeightAnchor, BlockId}; use $crate::chain::{ConfirmationTimeHeightAnchor, BlockId, TxGraph};
use $crate::{KeychainKind, wallet::Wallet}; use $crate::wallet::{Update, Wallet};
use $crate::KeychainKind;
let descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/0/*)"; let descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/0/*)";
let change_descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/1/*)"; 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 block = BlockId { height: 1_000, hash: BlockHash::all_zeros() };
let _ = wallet.insert_checkpoint(block); let _ = wallet.insert_checkpoint(block);
let _ = wallet.insert_tx(tx); let _ = wallet.insert_tx(tx);
wallet let anchor = ConfirmationTimeHeightAnchor {
.insert_anchor( confirmation_height: 500,
txid, confirmation_time: 50_000,
ConfirmationTimeHeightAnchor { anchor_block: block,
confirmation_height: 500, };
confirmation_time: 50_000, let mut graph = TxGraph::default();
anchor_block: block, let _ = graph.insert_anchor(txid, anchor);
} let update = Update { graph, ..Default::default() };
); wallet.apply_update(update).unwrap();
wallet wallet
}} }}
} }

View File

@ -1,7 +1,8 @@
#![allow(unused)] #![allow(unused)]
use bdk_chain::indexed_tx_graph::Indexer; 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 bdk_wallet::{KeychainKind, LocalOutput, Wallet};
use bitcoin::hashes::Hash; use bitcoin::hashes::Hash;
use bitcoin::{ 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"); .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();
} }
} }

View File

@ -50,7 +50,7 @@ fn receive_output(wallet: &mut Wallet, value: u64, height: ConfirmationTime) ->
insert_anchor_from_conf(wallet, txid, height); insert_anchor_from_conf(wallet, txid, height);
} }
ConfirmationTime::Unconfirmed { last_seen } => { 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) 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 = // 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) // 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 // 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(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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 external_policy = wallet.policies(KeychainKind::External).unwrap().unwrap();
let root_id = external_policy.id; 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 tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); wallet.build_fee_bump(txid).unwrap().finish().unwrap();
} }
@ -1727,7 +1739,7 @@ fn test_bump_fee_low_fee_rate() {
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::BROADCAST_MIN); builder.fee_rate(FeeRate::BROADCAST_MIN);
@ -1759,7 +1771,7 @@ fn test_bump_fee_low_abs() {
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_absolute(Amount::from_sat(10)); 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 tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_absolute(Amount::ZERO); 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 tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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 feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb
let mut builder = wallet.build_fee_bump(txid).unwrap(); 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 original_fee = check_fee!(wallet, psbt);
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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 feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb
let mut builder = wallet.build_fee_bump(txid).unwrap(); 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 original_sent_received = wallet.sent_and_received(&tx);
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder builder
@ -2024,7 +2036,7 @@ fn test_bump_fee_drain_wallet() {
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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)); 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 // 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 original_sent_received = wallet.sent_and_received(&tx);
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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)); assert_eq!(original_sent_received.0, Amount::from_sat(25_000));
let mut builder = wallet.build_fee_bump(txid).unwrap(); 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 original_details = wallet.sent_and_received(&tx);
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(50)); 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 original_sent_received = wallet.sent_and_received(&tx);
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_absolute(Amount::from_sat(6_000)); 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 tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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 // Now bump the fees, the wallet should add an extra input and a change output, and leave
// the original output untouched. // the original output untouched.
@ -2326,7 +2338,7 @@ fn test_bump_fee_add_input_change_dust() {
assert_eq!(tx.output.len(), 2); assert_eq!(tx.output.len(), 2);
let txid = tx.compute_txid(); let txid = tx.compute_txid();
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
// We set a fee high enough that during rbf we are forced to add // 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 txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
} }
wallet.insert_tx(tx.clone()); 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 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()` // the addition of an extra input with `add_utxo()`
let mut builder = wallet.build_fee_bump(txid).unwrap(); 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 txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
} }
wallet.insert_tx(tx.clone()); 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 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()` // 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 txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
} }
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(25)); builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(25));
builder.finish().unwrap(); builder.finish().unwrap();
@ -2572,7 +2584,7 @@ fn test_bump_fee_unconfirmed_input() {
txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
} }
wallet.insert_tx(tx); 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(); let mut builder = wallet.build_fee_bump(txid).unwrap();
builder builder
@ -4107,7 +4119,7 @@ fn test_insert_tx_balance_and_utxos() {
let tx = psbt.extract_tx().unwrap(); let tx = psbt.extract_tx().unwrap();
let txid = tx.compute_txid(); let txid = tx.compute_txid();
let _ = wallet.insert_tx(tx); 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!(wallet.list_unspent().next().is_none());
assert_eq!(wallet.balance().total().to_sat(), 0); assert_eq!(wallet.balance().total().to_sat(), 0);
} }