Skip to content

Commit

Permalink
test(wallet): refactor helper insert_anchor_from_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Oct 24, 2024
1 parent 647d285 commit d33116f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 58 deletions.
42 changes: 18 additions & 24 deletions crates/wallet/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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<ConfirmationBlockTime>,
) {
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();
}
64 changes: 30 additions & 34 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,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);
Expand Down Expand Up @@ -1216,11 +1216,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()
Expand Down Expand Up @@ -1263,11 +1263,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()
Expand Down Expand Up @@ -1824,11 +1824,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();
}
Expand Down Expand Up @@ -2101,11 +2101,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()
Expand Down Expand Up @@ -2161,15 +2161,13 @@ fn test_bump_fee_remove_output_manually_selected_only() {
value: Amount::from_sat(25_000),
}],
};
let position: ChainPosition<ConfirmationBlockTime> = 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(),
Expand Down Expand Up @@ -2214,14 +2212,12 @@ fn test_bump_fee_add_input() {
}],
};
let txid = init_tx.compute_txid();
let pos: ChainPosition<ConfirmationBlockTime> = 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()
Expand Down Expand Up @@ -3865,11 +3861,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;
Expand Down Expand Up @@ -4113,7 +4109,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,
Expand Down

0 comments on commit d33116f

Please sign in to comment.