Skip to content

Commit ab27884

Browse files
committed
test(wallet): improve usage of test utils
1 parent 9bdf4cb commit ab27884

File tree

4 files changed

+86
-139
lines changed

4 files changed

+86
-139
lines changed

crates/wallet/src/test_utils.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use crate::{KeychainKind, Update, Wallet};
1717
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
1818
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
1919
/// sats are the transaction fee.
20-
pub fn get_funded_wallet(descriptor: &str, change_descriptor: &str) -> (Wallet, bitcoin::Txid) {
20+
pub fn get_funded_wallet(descriptor: &str, change_descriptor: &str) -> (Wallet, Txid) {
2121
new_funded_wallet(descriptor, Some(change_descriptor))
2222
}
2323

24-
fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wallet, bitcoin::Txid) {
24+
fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wallet, Txid) {
2525
let params = if let Some(change_desc) = change_descriptor {
2626
Wallet::create(descriptor.to_string(), change_desc.to_string())
2727
} else {
@@ -40,34 +40,20 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
4040
.unwrap();
4141

4242
let tx0 = Transaction {
43-
version: transaction::Version::ONE,
44-
lock_time: bitcoin::absolute::LockTime::ZERO,
45-
input: vec![TxIn {
46-
previous_output: OutPoint {
47-
txid: Txid::all_zeros(),
48-
vout: 0,
49-
},
50-
script_sig: Default::default(),
51-
sequence: Default::default(),
52-
witness: Default::default(),
53-
}],
5443
output: vec![TxOut {
5544
value: Amount::from_sat(76_000),
5645
script_pubkey: receive_address.script_pubkey(),
5746
}],
47+
..new_tx(0)
5848
};
5949

6050
let tx1 = Transaction {
61-
version: transaction::Version::ONE,
62-
lock_time: bitcoin::absolute::LockTime::ZERO,
6351
input: vec![TxIn {
6452
previous_output: OutPoint {
6553
txid: tx0.compute_txid(),
6654
vout: 0,
6755
},
68-
script_sig: Default::default(),
69-
sequence: Default::default(),
70-
witness: Default::default(),
56+
..Default::default()
7157
}],
7258
output: vec![
7359
TxOut {
@@ -79,28 +65,32 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
7965
script_pubkey: sendto_address.script_pubkey(),
8066
},
8167
],
68+
..new_tx(0)
8269
};
8370

84-
wallet
85-
.insert_checkpoint(BlockId {
71+
insert_checkpoint(
72+
&mut wallet,
73+
BlockId {
8674
height: 42,
8775
hash: BlockHash::all_zeros(),
88-
})
89-
.unwrap();
90-
wallet
91-
.insert_checkpoint(BlockId {
76+
},
77+
);
78+
insert_checkpoint(
79+
&mut wallet,
80+
BlockId {
9281
height: 1_000,
9382
hash: BlockHash::all_zeros(),
94-
})
95-
.unwrap();
96-
wallet
97-
.insert_checkpoint(BlockId {
83+
},
84+
);
85+
insert_checkpoint(
86+
&mut wallet,
87+
BlockId {
9888
height: 2_000,
9989
hash: BlockHash::all_zeros(),
100-
})
101-
.unwrap();
90+
},
91+
);
10292

103-
wallet.insert_tx(tx0.clone());
93+
insert_tx(&mut wallet, tx0.clone());
10494
insert_anchor(
10595
&mut wallet,
10696
tx0.compute_txid(),
@@ -113,7 +103,7 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
113103
},
114104
);
115105

116-
wallet.insert_tx(tx1.clone());
106+
insert_tx(&mut wallet, tx1.clone());
117107
insert_anchor(
118108
&mut wallet,
119109
tx1.compute_txid(),
@@ -134,12 +124,12 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
134124
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
135125
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
136126
/// sats are the transaction fee.
137-
pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet, bitcoin::Txid) {
127+
pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet, Txid) {
138128
new_funded_wallet(descriptor, None)
139129
}
140130

141131
/// Get funded segwit wallet
142-
pub fn get_funded_wallet_wpkh() -> (Wallet, bitcoin::Txid) {
132+
pub fn get_funded_wallet_wpkh() -> (Wallet, Txid) {
143133
let (desc, change_desc) = get_test_wpkh_and_change_desc();
144134
get_funded_wallet(desc, change_desc)
145135
}
@@ -211,6 +201,16 @@ pub fn get_test_tr_dup_keys() -> &'static str {
211201
"tr(cNJmN3fH9DDbDt131fQNkVakkpzawJBSeybCUNmP1BovpmGQ45xG,{pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642),pk(8aee2b8120a5f157f1223f72b5e62b825831a27a9fdf427db7cc697494d4a642)})"
212202
}
213203

204+
/// A new empty transaction with the given locktime
205+
pub fn new_tx(locktime: u32) -> Transaction {
206+
Transaction {
207+
version: transaction::Version::ONE,
208+
lock_time: absolute::LockTime::from_consensus(locktime),
209+
input: vec![],
210+
output: vec![],
211+
}
212+
}
213+
214214
/// Construct a new [`FeeRate`] from the given raw `sat_vb` feerate. This is
215215
/// useful in cases where we want to create a feerate from a `f64`, as the
216216
/// traditional [`FeeRate::from_sat_per_vb`] method will only accept an integer.
@@ -267,7 +267,7 @@ pub fn receive_output_to_address(
267267
};
268268

269269
let txid = tx.compute_txid();
270-
wallet.insert_tx(tx);
270+
insert_tx(wallet, tx);
271271

272272
match pos {
273273
ChainPosition::Confirmed(anchor) => {

crates/wallet/src/wallet/export.rs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -213,55 +213,28 @@ impl FullyNodedExport {
213213

214214
#[cfg(test)]
215215
mod test {
216+
use alloc::string::ToString;
216217
use core::str::FromStr;
217218

218-
use crate::std::string::ToString;
219-
use bdk_chain::{BlockId, ConfirmationBlockTime};
220-
use bitcoin::hashes::Hash;
221-
use bitcoin::{transaction, BlockHash, Network, Transaction};
222-
use chain::tx_graph;
219+
use bdk_chain::BlockId;
220+
use bitcoin::{hashes::Hash, BlockHash, Network};
223221

224222
use super::*;
223+
use crate::test_utils::*;
225224
use crate::Wallet;
226225

227226
fn get_test_wallet(descriptor: &str, change_descriptor: &str, network: Network) -> Wallet {
228-
use crate::wallet::Update;
229227
let mut wallet = Wallet::create(descriptor.to_string(), change_descriptor.to_string())
230228
.network(network)
231229
.create_wallet_no_persist()
232230
.expect("must create wallet");
233-
let transaction = Transaction {
234-
input: vec![],
235-
output: vec![],
236-
version: transaction::Version::non_standard(0),
237-
lock_time: bitcoin::absolute::LockTime::ZERO,
238-
};
239-
let txid = transaction.compute_txid();
240-
let block_id = BlockId {
231+
let block = BlockId {
241232
height: 5000,
242233
hash: BlockHash::all_zeros(),
243234
};
244-
wallet.insert_checkpoint(block_id).unwrap();
245-
wallet
246-
.insert_checkpoint(BlockId {
247-
height: 5001,
248-
hash: BlockHash::all_zeros(),
249-
})
250-
.unwrap();
251-
wallet.insert_tx(transaction);
252-
let anchor = ConfirmationBlockTime {
253-
confirmation_time: 0,
254-
block_id,
255-
};
256-
wallet
257-
.apply_update(Update {
258-
tx_update: tx_graph::TxUpdate {
259-
anchors: [(anchor, txid)].into_iter().collect(),
260-
..Default::default()
261-
},
262-
..Default::default()
263-
})
264-
.unwrap();
235+
insert_checkpoint(&mut wallet, block);
236+
receive_output_in_latest_block(&mut wallet, 10_000);
237+
265238
wallet
266239
}
267240

crates/wallet/src/wallet/mod.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,6 +2609,7 @@ macro_rules! doctest_wallet {
26092609
use $crate::bitcoin::{BlockHash, Transaction, absolute, TxOut, Network, hashes::Hash};
26102610
use $crate::chain::{ConfirmationBlockTime, BlockId, TxGraph, tx_graph};
26112611
use $crate::{Update, KeychainKind, Wallet};
2612+
use $crate::test_utils::*;
26122613
let descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/0/*)";
26132614
let change_descriptor = "tr([73c5da0a/86'/0'/0']tprv8fMn4hSKPRC1oaCPqxDb1JWtgkpeiQvZhsr8W2xuy3GEMkzoArcAWTfJxYb6Wj8XNNDWEjfYKK4wGQXh3ZUXhDF2NcnsALpWTeSwarJt7Vc/1/*)";
26142615

@@ -2628,21 +2629,14 @@ macro_rules! doctest_wallet {
26282629
};
26292630
let txid = tx.compute_txid();
26302631
let block_id = BlockId { height: 500, hash: BlockHash::all_zeros() };
2631-
let _ = wallet.insert_checkpoint(block_id);
2632-
let _ = wallet.insert_checkpoint(BlockId { height: 1_000, hash: BlockHash::all_zeros() });
2633-
let _ = wallet.insert_tx(tx);
2632+
insert_checkpoint(&mut wallet, block_id);
2633+
insert_checkpoint(&mut wallet, BlockId { height: 1_000, hash: BlockHash::all_zeros() });
2634+
insert_tx(&mut wallet, tx);
26342635
let anchor = ConfirmationBlockTime {
26352636
confirmation_time: 50_000,
26362637
block_id,
26372638
};
2638-
let update = Update {
2639-
tx_update: tx_graph::TxUpdate {
2640-
anchors: [(anchor, txid)].into_iter().collect(),
2641-
..Default::default()
2642-
},
2643-
..Default::default()
2644-
};
2645-
wallet.apply_update(update).unwrap();
2639+
insert_anchor(&mut wallet, txid, anchor);
26462640
wallet
26472641
}}
26482642
}

0 commit comments

Comments
 (0)