From: valued mammal Date: Thu, 24 Oct 2024 02:09:14 +0000 (-0400) Subject: test(wallet): refactor helper `insert_anchor_from_conf` X-Git-Tag: v1.0.0-beta.6~22^2~8 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/scripts/static/struct.SyncRequest.html?a=commitdiff_plain;h=297bd9a5123f3c901e11eadf33a51351c1e2f6a4;p=bdk test(wallet): refactor helper `insert_anchor_from_conf` --- diff --git a/crates/wallet/tests/common.rs b/crates/wallet/tests/common.rs index a2870c80..59acbb11 100644 --- a/crates/wallet/tests/common.rs +++ b/crates/wallet/tests/common.rs @@ -86,29 +86,29 @@ pub fn get_funded_wallet_with_change(descriptor: &str, change: &str) -> (Wallet, .unwrap(); wallet.insert_tx(tx0.clone()); - insert_anchor_from_conf( + insert_anchor( &mut wallet, tx0.compute_txid(), - ChainPosition::Confirmed(ConfirmationBlockTime { + ConfirmationBlockTime { block_id: BlockId { height: 1_000, hash: BlockHash::all_zeros(), }, confirmation_time: 100, - }), + }, ); wallet.insert_tx(tx1.clone()); - insert_anchor_from_conf( + insert_anchor( &mut wallet, tx1.compute_txid(), - ChainPosition::Confirmed(ConfirmationBlockTime { + ConfirmationBlockTime { block_id: BlockId { height: 2_000, hash: BlockHash::all_zeros(), }, confirmation_time: 200, - }), + }, ); (wallet, tx1.compute_txid()) @@ -208,23 +208,17 @@ pub fn feerate_unchecked(sat_vb: f64) -> FeeRate { FeeRate::from_sat_per_kwu(sat_kwu) } -/// Simulates confirming a tx with `txid` at the specified `position` by inserting an anchor -/// at the lowest height in local chain that is greater or equal to `position`'s height, -/// assuming the confirmation time matches `ConfirmationTime::Confirmed`. -pub fn insert_anchor_from_conf( - wallet: &mut Wallet, - txid: Txid, - position: ChainPosition, -) { - if let ChainPosition::Confirmed(anchor) = position { - wallet - .apply_update(Update { - tx_update: tx_graph::TxUpdate { - anchors: [(anchor, txid)].into(), - ..Default::default() - }, +/// Simulates confirming a tx with `txid` by applying an update to the wallet containing +/// the given `anchor`. Note: to be considered confirmed the anchor block must exist in +/// the current active chain. +pub fn insert_anchor(wallet: &mut Wallet, txid: Txid, anchor: ConfirmationBlockTime) { + wallet + .apply_update(Update { + tx_update: tx_graph::TxUpdate { + anchors: [(anchor, txid)].into(), ..Default::default() - }) - .unwrap(); - } + }, + ..Default::default() + }) + .unwrap(); } diff --git a/crates/wallet/tests/wallet.rs b/crates/wallet/tests/wallet.rs index d51af335..abf9a9c9 100644 --- a/crates/wallet/tests/wallet.rs +++ b/crates/wallet/tests/wallet.rs @@ -62,8 +62,8 @@ fn receive_output_to_address( wallet.insert_tx(tx); match height { - ChainPosition::Confirmed { .. } => { - insert_anchor_from_conf(wallet, txid, height); + ChainPosition::Confirmed(anchor) => { + insert_anchor(wallet, txid, anchor); } ChainPosition::Unconfirmed(last_seen) => { insert_seen_at(wallet, txid, last_seen); @@ -1237,11 +1237,11 @@ fn test_create_tx_add_utxo() { }; let txid = small_output_tx.compute_txid(); wallet.insert_tx(small_output_tx); - let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime { - block_id: wallet.latest_checkpoint().get(2000).unwrap().block_id(), + let anchor = ConfirmationBlockTime { + block_id: wallet.latest_checkpoint().block_id(), confirmation_time: 200, - }); - insert_anchor_from_conf(&mut wallet, txid, chain_position); + }; + insert_anchor(&mut wallet, txid, anchor); let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") .unwrap() @@ -1284,11 +1284,11 @@ fn test_create_tx_manually_selected_insufficient() { }; let txid = small_output_tx.compute_txid(); wallet.insert_tx(small_output_tx.clone()); - let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime { - block_id: wallet.latest_checkpoint().get(2000).unwrap().block_id(), + let anchor = ConfirmationBlockTime { + block_id: wallet.latest_checkpoint().block_id(), confirmation_time: 200, - }); - insert_anchor_from_conf(&mut wallet, txid, chain_position); + }; + insert_anchor(&mut wallet, txid, anchor); let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") .unwrap() @@ -1845,11 +1845,11 @@ fn test_bump_fee_confirmed_tx() { wallet.insert_tx(tx); - let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime { + let anchor = ConfirmationBlockTime { block_id: wallet.latest_checkpoint().get(42).unwrap().block_id(), confirmation_time: 42_000, - }); - insert_anchor_from_conf(&mut wallet, txid, chain_position); + }; + insert_anchor(&mut wallet, txid, anchor); wallet.build_fee_bump(txid).unwrap().finish().unwrap(); } @@ -2122,11 +2122,11 @@ fn test_bump_fee_drain_wallet() { }; let txid = tx.compute_txid(); wallet.insert_tx(tx.clone()); - let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime { + let anchor = ConfirmationBlockTime { block_id: wallet.latest_checkpoint().block_id(), confirmation_time: 42_000, - }); - insert_anchor_from_conf(&mut wallet, txid, chain_position); + }; + insert_anchor(&mut wallet, txid, anchor); let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") .unwrap() @@ -2182,15 +2182,13 @@ fn test_bump_fee_remove_output_manually_selected_only() { value: Amount::from_sat(25_000), }], }; - let position: ChainPosition = wallet - .transactions() - .last() - .unwrap() - .chain_position - .cloned(); wallet.insert_tx(init_tx.clone()); - insert_anchor_from_conf(&mut wallet, init_tx.compute_txid(), position); + let anchor = ConfirmationBlockTime { + block_id: wallet.latest_checkpoint().block_id(), + confirmation_time: 200, + }; + insert_anchor(&mut wallet, init_tx.compute_txid(), anchor); let outpoint = OutPoint { txid: init_tx.compute_txid(), @@ -2235,14 +2233,12 @@ fn test_bump_fee_add_input() { }], }; let txid = init_tx.compute_txid(); - let pos: ChainPosition = wallet - .transactions() - .last() - .unwrap() - .chain_position - .cloned(); wallet.insert_tx(init_tx); - insert_anchor_from_conf(&mut wallet, txid, pos); + let anchor = ConfirmationBlockTime { + block_id: wallet.latest_checkpoint().block_id(), + confirmation_time: 200, + }; + insert_anchor(&mut wallet, txid, anchor); let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") .unwrap() @@ -3886,11 +3882,11 @@ fn test_spend_coinbase() { }; let txid = coinbase_tx.compute_txid(); wallet.insert_tx(coinbase_tx); - let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime { + let anchor = ConfirmationBlockTime { block_id: confirmation_block_id, confirmation_time: 30_000, - }); - insert_anchor_from_conf(&mut wallet, txid, chain_position); + }; + insert_anchor(&mut wallet, txid, anchor); let not_yet_mature_time = confirmation_height + COINBASE_MATURITY - 1; let maturity_time = confirmation_height + COINBASE_MATURITY; @@ -4134,7 +4130,7 @@ fn test_keychains_with_overlapping_spks() { .address; let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime { block_id: BlockId { - height: 8000, + height: 2000, hash: BlockHash::all_zeros(), }, confirmation_time: 0,