Skip to content

Commit 9e5eb92

Browse files
committed
test: update tests/* to use KeyRing
The tests related to signers are yet to be fixed completely.
1 parent ccbb880 commit 9e5eb92

8 files changed

Lines changed: 1289 additions & 521 deletions

File tree

‎src/persist_test_utils.rs‎

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ use std::path::Path;
3434
use std::str::FromStr;
3535
use std::sync::Arc;
3636

37+
use crate::KeychainKind;
38+
3739
const DESCRIPTORS: [&str; 4] = [
3840
"tr([5940b9b9/86'/0'/0']tpubDDVNqmq75GNPWQ9UNKfP43UwjaHU4GYfoPavojQbfpyfZp2KetWgjGBRRAy4tYCrAA6SB11mhQAkqxjh1VtQHyKwT4oYxpwLaGHvoKmtxZf/0/*)#44aqnlam",
3941
"tr([5940b9b9/86'/0'/0']tpubDDVNqmq75GNPWQ9UNKfP43UwjaHU4GYfoPavojQbfpyfZp2KetWgjGBRRAy4tYCrAA6SB11mhQAkqxjh1VtQHyKwT4oYxpwLaGHvoKmtxZf/1/*)#ypcpw2dr",
@@ -66,6 +68,16 @@ fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> Scr
6668
.script_pubkey()
6769
}
6870

71+
// Check equalit of changesets.
72+
fn check_changesets(changeset1: &ChangeSet<KeychainKind>, changeset2: &ChangeSet<KeychainKind>) {
73+
assert_eq!(changeset1.network, changeset2.network);
74+
assert_eq!(changeset1.local_chain, changeset2.local_chain);
75+
assert_eq!(changeset1.tx_graph, changeset2.tx_graph);
76+
assert_eq!(changeset1.indexer, changeset2.indexer);
77+
assert_eq!(changeset1.locked_outpoints, changeset2.locked_outpoints);
78+
assert_eq!(changeset1.descriptors, changeset2.descriptors);
79+
}
80+
6981
/// tests if [`Wallet`] is being persisted correctly
7082
///
7183
/// [`Wallet`]: <https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html>
@@ -77,7 +89,7 @@ fn spk_at_index(descriptor: &Descriptor<DescriptorPublicKey>, index: u32) -> Scr
7789
pub fn persist_wallet_changeset<Store, CreateStore>(filename: &str, create_store: CreateStore)
7890
where
7991
CreateStore: Fn(&Path) -> anyhow::Result<Store>,
80-
Store: WalletPersister,
92+
Store: WalletPersister<KeychainKind>,
8193
Store::Error: Debug,
8294
{
8395
// create store
@@ -172,6 +184,11 @@ where
172184
tx_graph: tx_graph_changeset,
173185
indexer: keychain_txout_changeset,
174186
locked_outpoints: locked_outpoints_changeset,
187+
descriptors: [
188+
(KeychainKind::External, descriptor.clone()),
189+
(KeychainKind::Internal, change_descriptor),
190+
]
191+
.into(),
175192
};
176193

177194
// persist and load
@@ -180,7 +197,7 @@ where
180197
let changeset_read =
181198
WalletPersister::initialize(&mut store).expect("changeset should get loaded");
182199

183-
assert_eq!(changeset, changeset_read);
200+
check_changesets(&changeset, &changeset_read);
184201

185202
// create another changeset
186203
let local_chain_changeset = local_chain::ChangeSet {
@@ -231,6 +248,7 @@ where
231248
tx_graph: tx_graph_changeset,
232249
indexer: keychain_txout_changeset,
233250
locked_outpoints: locked_outpoints_changeset,
251+
descriptors: [].into(),
234252
};
235253

236254
// persist, load and check if same as merged
@@ -239,7 +257,7 @@ where
239257

240258
changeset.merge(changeset_new);
241259

242-
assert_eq!(changeset, changeset_read_new);
260+
check_changesets(&changeset, &changeset_read_new);
243261
}
244262

245263
/// tests if multiple [`Wallet`]s can be persisted in a single file correctly
@@ -255,7 +273,7 @@ pub fn persist_multiple_wallet_changesets<Store, CreateStores>(
255273
create_dbs: CreateStores,
256274
) where
257275
CreateStores: Fn(&Path) -> anyhow::Result<(Store, Store)>,
258-
Store: WalletPersister,
276+
Store: WalletPersister<KeychainKind>,
259277
Store::Error: Debug,
260278
{
261279
// create stores
@@ -279,7 +297,8 @@ pub fn persist_multiple_wallet_changesets<Store, CreateStores>(
279297
change_descriptor: Some(change_descriptor.clone()),
280298
network: Some(Network::Testnet),
281299
..ChangeSet::default()
282-
};
300+
}
301+
.from_v3();
283302

284303
// persist first changeset
285304
WalletPersister::persist(&mut store_first, &changeset1).expect("should persist changeset");
@@ -298,20 +317,24 @@ pub fn persist_multiple_wallet_changesets<Store, CreateStores>(
298317
change_descriptor: Some(change_descriptor.clone()),
299318
network: Some(Network::Testnet),
300319
..ChangeSet::default()
301-
};
320+
}
321+
.from_v3();
302322

303323
// persist second changeset
304324
WalletPersister::persist(&mut store_sec, &changeset2).expect("should persist changeset");
305325

306326
// load first changeset
307327
let changeset_read =
308328
WalletPersister::initialize(&mut store_first).expect("should load persisted changeset1");
309-
assert_eq!(changeset_read, changeset1);
329+
330+
check_changesets(&changeset_read, &changeset1);
310331

311332
// load second changeset
312333
let changeset_read =
313334
WalletPersister::initialize(&mut store_sec).expect("should load persisted changeset2");
314335
assert_eq!(changeset_read, changeset2);
336+
337+
check_changesets(&changeset_read, &changeset2);
315338
}
316339

317340
/// tests if [`Network`] is being persisted correctly
@@ -324,7 +347,7 @@ pub fn persist_multiple_wallet_changesets<Store, CreateStores>(
324347
pub fn persist_network<Store, CreateStore>(filename: &str, create_store: CreateStore)
325348
where
326349
CreateStore: Fn(&Path) -> anyhow::Result<Store>,
327-
Store: WalletPersister,
350+
Store: WalletPersister<KeychainKind>,
328351
Store::Error: Debug,
329352
{
330353
// create store
@@ -360,7 +383,7 @@ where
360383
pub fn persist_keychains<Store, CreateStore>(filename: &str, create_store: CreateStore)
361384
where
362385
CreateStore: Fn(&Path) -> anyhow::Result<Store>,
363-
Store: WalletPersister,
386+
Store: WalletPersister<KeychainKind>,
364387
Store::Error: Debug,
365388
{
366389
// create store
@@ -381,16 +404,23 @@ where
381404
descriptor: Some(descriptor.clone()),
382405
change_descriptor: Some(change_descriptor.clone()),
383406
..ChangeSet::default()
384-
};
407+
}
408+
.from_v3();
385409

386410
WalletPersister::persist(&mut store, &changeset).expect("should persist descriptors");
387411

388412
// load the descriptors
389413
let changeset_read =
390414
WalletPersister::initialize(&mut store).expect("should read persisted changeset");
391415

392-
assert_eq!(changeset_read.descriptor.unwrap(), descriptor);
393-
assert_eq!(changeset_read.change_descriptor.unwrap(), change_descriptor);
416+
assert_eq!(
417+
changeset_read.descriptors,
418+
[
419+
(KeychainKind::External, descriptor),
420+
(KeychainKind::Internal, change_descriptor)
421+
]
422+
.into()
423+
);
394424
}
395425

396426
/// tests if descriptor(in a single keychain wallet) is being persisted correctly
@@ -402,7 +432,7 @@ where
402432
pub fn persist_single_keychain<Store, CreateStore>(filename: &str, create_store: CreateStore)
403433
where
404434
CreateStore: Fn(&Path) -> anyhow::Result<Store>,
405-
Store: WalletPersister,
435+
Store: WalletPersister<KeychainKind>,
406436
Store::Error: Debug,
407437
{
408438
// create store
@@ -421,14 +451,17 @@ where
421451
let changeset = ChangeSet {
422452
descriptor: Some(descriptor.clone()),
423453
..ChangeSet::default()
424-
};
454+
}
455+
.from_v3();
425456

426457
WalletPersister::persist(&mut store, &changeset).expect("should persist descriptors");
427458

428459
// load the descriptor
429460
let changeset_read =
430461
WalletPersister::initialize(&mut store).expect("should read persisted changeset");
431462

432-
assert_eq!(changeset_read.descriptor.unwrap(), descriptor);
433-
assert_eq!(changeset_read.change_descriptor, None);
463+
assert_eq!(
464+
changeset_read.descriptors,
465+
[(KeychainKind::External, descriptor)].into()
466+
);
434467
}

‎src/test_utils.rs‎

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! `bdk_wallet` test utilities
22
3-
use alloc::string::ToString;
43
use alloc::sync::Arc;
54
use core::str::FromStr;
65

@@ -10,18 +9,24 @@ use bitcoin::{
109
Transaction, TxIn, TxOut, Txid,
1110
};
1211

13-
use crate::{KeychainKind, Update, Wallet};
12+
use crate::{KeyRing, KeychainKind, Update, Wallet};
1413

1514
/// Return a fake wallet that appears to be funded for testing.
1615
///
1716
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
1817
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
1918
/// sats are the transaction fee.
20-
pub fn get_funded_wallet(descriptor: &str, change_descriptor: &str) -> (Wallet, Txid) {
19+
pub fn get_funded_wallet(
20+
descriptor: &str,
21+
change_descriptor: &str,
22+
) -> (Wallet<KeychainKind>, Txid) {
2123
new_funded_wallet(descriptor, Some(change_descriptor))
2224
}
2325

24-
fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wallet, Txid) {
26+
fn new_funded_wallet(
27+
descriptor: &str,
28+
change_descriptor: Option<&str>,
29+
) -> (Wallet<KeychainKind>, Txid) {
2530
let (mut wallet, txid, update) = new_wallet_and_funding_update(descriptor, change_descriptor);
2631
wallet.apply_update(update).unwrap();
2732
(wallet, txid)
@@ -32,12 +37,12 @@ fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wall
3237
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
3338
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
3439
/// sats are the transaction fee.
35-
pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet, Txid) {
40+
pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet<KeychainKind>, Txid) {
3641
new_funded_wallet(descriptor, None)
3742
}
3843

3944
/// Get funded segwit wallet
40-
pub fn get_funded_wallet_wpkh() -> (Wallet, Txid) {
45+
pub fn get_funded_wallet_wpkh() -> (Wallet<KeychainKind>, Txid) {
4146
let (desc, change_desc) = get_test_wpkh_and_change_desc();
4247
get_funded_wallet(desc, change_desc)
4348
}
@@ -50,19 +55,28 @@ pub fn get_funded_wallet_wpkh() -> (Wallet, Txid) {
5055
pub fn new_wallet_and_funding_update(
5156
descriptor: &str,
5257
change_descriptor: Option<&str>,
53-
) -> (Wallet, Txid, Update) {
54-
let params = if let Some(change_desc) = change_descriptor {
55-
Wallet::create(descriptor.to_string(), change_desc.to_string())
56-
} else {
57-
Wallet::create_single(descriptor.to_string())
58-
};
58+
) -> (Wallet<KeychainKind>, Txid, Update<KeychainKind>) {
59+
let mut keyring = KeyRing::new(Network::Regtest);
60+
keyring
61+
.add_descriptor(KeychainKind::External, descriptor)
62+
.expect("should add keychain");
63+
if let Some(change_desc) = change_descriptor {
64+
keyring
65+
.add_descriptor(KeychainKind::Internal, change_desc)
66+
.expect("should add keychain");
67+
}
5968

60-
let wallet = params
61-
.network(Network::Regtest)
69+
let wallet = keyring
70+
.into_params()
71+
.expect("should be a valid keyring")
6272
.create_wallet_no_persist()
6373
.expect("descriptors must be valid");
6474

65-
let receive_address = wallet.peek_address(KeychainKind::External, 0).address;
75+
let receive_address = wallet
76+
.peek_address(KeychainKind::External, 0)
77+
.expect("keychain must exist")
78+
.expect("index is valid")
79+
.address;
6680
let sendto_address = Address::from_str("bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5")
6781
.expect("address")
6882
.require_network(Network::Regtest)
@@ -253,7 +267,10 @@ impl From<ConfirmationBlockTime> for ReceiveTo {
253267
}
254268

255269
/// Receive a tx output with the given value in the latest block
256-
pub fn receive_output_in_latest_block(wallet: &mut Wallet, value: Amount) -> OutPoint {
270+
pub fn receive_output_in_latest_block(
271+
wallet: &mut Wallet<KeychainKind>,
272+
value: Amount,
273+
) -> OutPoint {
257274
let latest_cp = wallet.latest_checkpoint();
258275
let height = latest_cp.height();
259276
assert!(height > 0, "cannot receive tx into genesis block");
@@ -269,17 +286,20 @@ pub fn receive_output_in_latest_block(wallet: &mut Wallet, value: Amount) -> Out
269286

270287
/// Receive a tx output with the given value and chain position
271288
pub fn receive_output(
272-
wallet: &mut Wallet,
289+
wallet: &mut Wallet<KeychainKind>,
273290
value: Amount,
274291
receive_to: impl Into<ReceiveTo>,
275292
) -> OutPoint {
276-
let addr = wallet.next_unused_address(KeychainKind::External).address;
293+
let addr = wallet
294+
.next_unused_address(KeychainKind::External)
295+
.expect("keychain must exist")
296+
.address;
277297
receive_output_to_address(wallet, addr, value, receive_to)
278298
}
279299

280300
/// Receive a tx output to an address with the given value and chain position
281301
pub fn receive_output_to_address(
282-
wallet: &mut Wallet,
302+
wallet: &mut Wallet<KeychainKind>,
283303
addr: Address,
284304
value: Amount,
285305
receive_to: impl Into<ReceiveTo>,
@@ -308,7 +328,7 @@ pub fn receive_output_to_address(
308328
/// Insert a checkpoint into the wallet. This can be used to extend the wallet's local chain
309329
/// or to insert a block that did not exist previously. Note that if replacing a block with
310330
/// a different one at the same height, then all later blocks are evicted as well.
311-
pub fn insert_checkpoint(wallet: &mut Wallet, block: BlockId) {
331+
pub fn insert_checkpoint(wallet: &mut Wallet<KeychainKind>, block: BlockId) {
312332
let mut cp = wallet.latest_checkpoint();
313333
cp = cp.insert(block);
314334
wallet
@@ -322,7 +342,7 @@ pub fn insert_checkpoint(wallet: &mut Wallet, block: BlockId) {
322342
/// Inserts a transaction into the local view, assuming it is currently present in the mempool.
323343
///
324344
/// This can be used, for example, to track a transaction immediately after it is broadcast.
325-
pub fn insert_tx(wallet: &mut Wallet, tx: Transaction) {
345+
pub fn insert_tx(wallet: &mut Wallet<KeychainKind>, tx: Transaction) {
326346
let txid = tx.compute_txid();
327347
let seen_at = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
328348
let mut tx_update = TxUpdate::default();
@@ -339,7 +359,7 @@ pub fn insert_tx(wallet: &mut Wallet, tx: Transaction) {
339359
/// Simulates confirming a tx with `txid` by applying an update to the wallet containing
340360
/// the given `anchor`. Note: to be considered confirmed the anchor block must exist in
341361
/// the current active chain.
342-
pub fn insert_anchor(wallet: &mut Wallet, txid: Txid, anchor: ConfirmationBlockTime) {
362+
pub fn insert_anchor(wallet: &mut Wallet<KeychainKind>, txid: Txid, anchor: ConfirmationBlockTime) {
343363
let mut tx_update = TxUpdate::default();
344364
tx_update.anchors = [(anchor, txid)].into();
345365
wallet
@@ -351,7 +371,7 @@ pub fn insert_anchor(wallet: &mut Wallet, txid: Txid, anchor: ConfirmationBlockT
351371
}
352372

353373
/// Marks the given `txid` seen as unconfirmed at `seen_at`
354-
pub fn insert_seen_at(wallet: &mut Wallet, txid: Txid, seen_at: u64) {
374+
pub fn insert_seen_at(wallet: &mut Wallet<KeychainKind>, txid: Txid, seen_at: u64) {
355375
let mut tx_update = TxUpdate::default();
356376
tx_update.seen_ats = [(txid, seen_at)].into();
357377
wallet

0 commit comments

Comments
 (0)