Skip to content

Commit 297bd9a

Browse files
committed
test(wallet): refactor helper insert_anchor_from_conf
1 parent 60214fb commit 297bd9a

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed

crates/wallet/tests/common.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,29 @@ pub fn get_funded_wallet_with_change(descriptor: &str, change: &str) -> (Wallet,
8686
.unwrap();
8787

8888
wallet.insert_tx(tx0.clone());
89-
insert_anchor_from_conf(
89+
insert_anchor(
9090
&mut wallet,
9191
tx0.compute_txid(),
92-
ChainPosition::Confirmed(ConfirmationBlockTime {
92+
ConfirmationBlockTime {
9393
block_id: BlockId {
9494
height: 1_000,
9595
hash: BlockHash::all_zeros(),
9696
},
9797
confirmation_time: 100,
98-
}),
98+
},
9999
);
100100

101101
wallet.insert_tx(tx1.clone());
102-
insert_anchor_from_conf(
102+
insert_anchor(
103103
&mut wallet,
104104
tx1.compute_txid(),
105-
ChainPosition::Confirmed(ConfirmationBlockTime {
105+
ConfirmationBlockTime {
106106
block_id: BlockId {
107107
height: 2_000,
108108
hash: BlockHash::all_zeros(),
109109
},
110110
confirmation_time: 200,
111-
}),
111+
},
112112
);
113113

114114
(wallet, tx1.compute_txid())
@@ -208,23 +208,17 @@ pub fn feerate_unchecked(sat_vb: f64) -> FeeRate {
208208
FeeRate::from_sat_per_kwu(sat_kwu)
209209
}
210210

211-
/// Simulates confirming a tx with `txid` at the specified `position` by inserting an anchor
212-
/// at the lowest height in local chain that is greater or equal to `position`'s height,
213-
/// assuming the confirmation time matches `ConfirmationTime::Confirmed`.
214-
pub fn insert_anchor_from_conf(
215-
wallet: &mut Wallet,
216-
txid: Txid,
217-
position: ChainPosition<ConfirmationBlockTime>,
218-
) {
219-
if let ChainPosition::Confirmed(anchor) = position {
220-
wallet
221-
.apply_update(Update {
222-
tx_update: tx_graph::TxUpdate {
223-
anchors: [(anchor, txid)].into(),
224-
..Default::default()
225-
},
211+
/// Simulates confirming a tx with `txid` by applying an update to the wallet containing
212+
/// the given `anchor`. Note: to be considered confirmed the anchor block must exist in
213+
/// the current active chain.
214+
pub fn insert_anchor(wallet: &mut Wallet, txid: Txid, anchor: ConfirmationBlockTime) {
215+
wallet
216+
.apply_update(Update {
217+
tx_update: tx_graph::TxUpdate {
218+
anchors: [(anchor, txid)].into(),
226219
..Default::default()
227-
})
228-
.unwrap();
229-
}
220+
},
221+
..Default::default()
222+
})
223+
.unwrap();
230224
}

crates/wallet/tests/wallet.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ fn receive_output_to_address(
6262
wallet.insert_tx(tx);
6363

6464
match height {
65-
ChainPosition::Confirmed { .. } => {
66-
insert_anchor_from_conf(wallet, txid, height);
65+
ChainPosition::Confirmed(anchor) => {
66+
insert_anchor(wallet, txid, anchor);
6767
}
6868
ChainPosition::Unconfirmed(last_seen) => {
6969
insert_seen_at(wallet, txid, last_seen);
@@ -1237,11 +1237,11 @@ fn test_create_tx_add_utxo() {
12371237
};
12381238
let txid = small_output_tx.compute_txid();
12391239
wallet.insert_tx(small_output_tx);
1240-
let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime {
1241-
block_id: wallet.latest_checkpoint().get(2000).unwrap().block_id(),
1240+
let anchor = ConfirmationBlockTime {
1241+
block_id: wallet.latest_checkpoint().block_id(),
12421242
confirmation_time: 200,
1243-
});
1244-
insert_anchor_from_conf(&mut wallet, txid, chain_position);
1243+
};
1244+
insert_anchor(&mut wallet, txid, anchor);
12451245

12461246
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
12471247
.unwrap()
@@ -1284,11 +1284,11 @@ fn test_create_tx_manually_selected_insufficient() {
12841284
};
12851285
let txid = small_output_tx.compute_txid();
12861286
wallet.insert_tx(small_output_tx.clone());
1287-
let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime {
1288-
block_id: wallet.latest_checkpoint().get(2000).unwrap().block_id(),
1287+
let anchor = ConfirmationBlockTime {
1288+
block_id: wallet.latest_checkpoint().block_id(),
12891289
confirmation_time: 200,
1290-
});
1291-
insert_anchor_from_conf(&mut wallet, txid, chain_position);
1290+
};
1291+
insert_anchor(&mut wallet, txid, anchor);
12921292

12931293
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
12941294
.unwrap()
@@ -1845,11 +1845,11 @@ fn test_bump_fee_confirmed_tx() {
18451845

18461846
wallet.insert_tx(tx);
18471847

1848-
let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime {
1848+
let anchor = ConfirmationBlockTime {
18491849
block_id: wallet.latest_checkpoint().get(42).unwrap().block_id(),
18501850
confirmation_time: 42_000,
1851-
});
1852-
insert_anchor_from_conf(&mut wallet, txid, chain_position);
1851+
};
1852+
insert_anchor(&mut wallet, txid, anchor);
18531853

18541854
wallet.build_fee_bump(txid).unwrap().finish().unwrap();
18551855
}
@@ -2122,11 +2122,11 @@ fn test_bump_fee_drain_wallet() {
21222122
};
21232123
let txid = tx.compute_txid();
21242124
wallet.insert_tx(tx.clone());
2125-
let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime {
2125+
let anchor = ConfirmationBlockTime {
21262126
block_id: wallet.latest_checkpoint().block_id(),
21272127
confirmation_time: 42_000,
2128-
});
2129-
insert_anchor_from_conf(&mut wallet, txid, chain_position);
2128+
};
2129+
insert_anchor(&mut wallet, txid, anchor);
21302130

21312131
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
21322132
.unwrap()
@@ -2182,15 +2182,13 @@ fn test_bump_fee_remove_output_manually_selected_only() {
21822182
value: Amount::from_sat(25_000),
21832183
}],
21842184
};
2185-
let position: ChainPosition<ConfirmationBlockTime> = wallet
2186-
.transactions()
2187-
.last()
2188-
.unwrap()
2189-
.chain_position
2190-
.cloned();
21912185

21922186
wallet.insert_tx(init_tx.clone());
2193-
insert_anchor_from_conf(&mut wallet, init_tx.compute_txid(), position);
2187+
let anchor = ConfirmationBlockTime {
2188+
block_id: wallet.latest_checkpoint().block_id(),
2189+
confirmation_time: 200,
2190+
};
2191+
insert_anchor(&mut wallet, init_tx.compute_txid(), anchor);
21942192

21952193
let outpoint = OutPoint {
21962194
txid: init_tx.compute_txid(),
@@ -2235,14 +2233,12 @@ fn test_bump_fee_add_input() {
22352233
}],
22362234
};
22372235
let txid = init_tx.compute_txid();
2238-
let pos: ChainPosition<ConfirmationBlockTime> = wallet
2239-
.transactions()
2240-
.last()
2241-
.unwrap()
2242-
.chain_position
2243-
.cloned();
22442236
wallet.insert_tx(init_tx);
2245-
insert_anchor_from_conf(&mut wallet, txid, pos);
2237+
let anchor = ConfirmationBlockTime {
2238+
block_id: wallet.latest_checkpoint().block_id(),
2239+
confirmation_time: 200,
2240+
};
2241+
insert_anchor(&mut wallet, txid, anchor);
22462242

22472243
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
22482244
.unwrap()
@@ -3886,11 +3882,11 @@ fn test_spend_coinbase() {
38863882
};
38873883
let txid = coinbase_tx.compute_txid();
38883884
wallet.insert_tx(coinbase_tx);
3889-
let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime {
3885+
let anchor = ConfirmationBlockTime {
38903886
block_id: confirmation_block_id,
38913887
confirmation_time: 30_000,
3892-
});
3893-
insert_anchor_from_conf(&mut wallet, txid, chain_position);
3888+
};
3889+
insert_anchor(&mut wallet, txid, anchor);
38943890

38953891
let not_yet_mature_time = confirmation_height + COINBASE_MATURITY - 1;
38963892
let maturity_time = confirmation_height + COINBASE_MATURITY;
@@ -4134,7 +4130,7 @@ fn test_keychains_with_overlapping_spks() {
41344130
.address;
41354131
let chain_position = ChainPosition::Confirmed(ConfirmationBlockTime {
41364132
block_id: BlockId {
4137-
height: 8000,
4133+
height: 2000,
41384134
hash: BlockHash::all_zeros(),
41394135
},
41404136
confirmation_time: 0,

0 commit comments

Comments
 (0)