Skip to content

Commit 3d6a95a

Browse files
thunderbiscuit110CodingPValuedMammal
committed
tests: add tests for KeyRing and Wallet
Co-authored-by: codingp110 <codingp110@gmail.com> Co-authored-by: valued mammal <valuedmammal@protonmail.com>
1 parent c4e1139 commit 3d6a95a

5 files changed

Lines changed: 1058 additions & 796 deletions

File tree

src/persist_test_utils.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ pub fn persist_wallet_changeset<Store, CreateStore, K>(
160160
let keyring_changeset = crate::keyring::ChangeSet {
161161
network: Some(Network::Testnet),
162162
descriptors: [(keychain.clone(), descriptor.clone())].into(),
163-
default_keychain: Some(keychain),
164163
};
165164

166165
let mut changeset = ChangeSet {
@@ -273,7 +272,6 @@ pub fn persist_multiple_wallet_changesets<Store, CreateStores, K>(
273272
let keyring_changeset = crate::keyring::ChangeSet {
274273
network: Some(Network::Testnet),
275274
descriptors: [(keychain.clone(), descriptor.clone())].into(),
276-
default_keychain: Some(keychain.clone()),
277275
};
278276

279277
let changeset1 = ChangeSet {
@@ -295,7 +293,6 @@ pub fn persist_multiple_wallet_changesets<Store, CreateStores, K>(
295293
let keyring_changeset2 = crate::keyring::ChangeSet {
296294
network: Some(Network::Testnet),
297295
descriptors: [(keychain.clone(), descriptor.clone())].into(),
298-
default_keychain: Some(keychain),
299296
};
300297

301298
let changeset2 = ChangeSet {
@@ -392,7 +389,6 @@ pub fn persist_keychain<Store, CreateStore, K>(
392389

393390
let keyring_changeset = crate::keyring::ChangeSet {
394391
descriptors: [(keychain.clone(), descriptor.clone())].into(),
395-
default_keychain: Some(keychain.clone()),
396392
..crate::keyring::ChangeSet::default()
397393
};
398394

@@ -411,8 +407,6 @@ pub fn persist_keychain<Store, CreateStore, K>(
411407
*changeset_read.keyring.descriptors.get(&keychain).unwrap(),
412408
descriptor
413409
);
414-
415-
assert_eq!(changeset_read.keyring.default_keychain, Some(keychain));
416410
}
417411

418412
/// tests if multiple descriptors are being persisted correctly
@@ -454,7 +448,6 @@ pub fn persist_keychains<Store, CreateStore, K>(
454448
(keychain2.clone(), desc2.clone()),
455449
]
456450
.into(),
457-
default_keychain: Some(keychain1.clone()),
458451
..crate::keyring::ChangeSet::default()
459452
};
460453

@@ -478,13 +471,7 @@ pub fn persist_keychains<Store, CreateStore, K>(
478471
desc2
479472
);
480473

481-
assert_eq!(
482-
changeset_read.keyring.default_keychain,
483-
Some(keychain1.clone())
484-
);
485-
486474
let keyring_changeset_new = crate::keyring::ChangeSet {
487-
default_keychain: Some(keychain2.clone()),
488475
..crate::keyring::ChangeSet::default()
489476
};
490477

@@ -497,10 +484,7 @@ pub fn persist_keychains<Store, CreateStore, K>(
497484

498485
let changeset_read_new =
499486
WalletPersister::initialize(&mut store).expect("should read persisted changeset");
500-
assert_eq!(
501-
changeset_read_new.keyring.default_keychain,
502-
Some(keychain2.clone())
503-
);
487+
504488
assert_eq!(
505489
*changeset_read_new
506490
.keyring

src/test_utils.rs

Lines changed: 130 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -5,132 +5,143 @@ use alloc::sync::Arc;
55
use core::fmt;
66
use core::str::FromStr;
77

8+
use crate::keyring::KeyRing;
9+
use crate::{KeychainKind, Update, Wallet};
810
use bdk_chain::{BlockId, CheckPoint, ConfirmationBlockTime, TxUpdate};
911
use bitcoin::{
1012
absolute, hashes::Hash, transaction, Address, Amount, BlockHash, FeeRate, Network, OutPoint,
1113
Transaction, TxIn, TxOut, Txid,
1214
};
1315

14-
use crate::{KeychainKind, Update, Wallet};
16+
/// Return a fake wallet that appears to be funded for testing.
17+
///
18+
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
19+
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
20+
/// sats are the transaction fee.
21+
pub fn get_funded_wallet(
22+
descriptor: &str,
23+
change_descriptor: &str,
24+
) -> (Wallet<KeychainKind>, Txid) {
25+
new_funded_wallet(descriptor, Some(change_descriptor))
26+
}
27+
28+
fn new_funded_wallet(
29+
descriptor: &str,
30+
change_descriptor: Option<&str>,
31+
) -> (Wallet<KeychainKind>, Txid) {
32+
let (mut wallet, txid, update) = new_wallet_and_funding_update(descriptor, change_descriptor);
33+
wallet.apply_update(update).unwrap();
34+
(wallet, txid)
35+
}
36+
37+
/// Return a fake wallet that appears to be funded for testing.
38+
///
39+
/// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
40+
/// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
41+
/// sats are the transaction fee.
42+
pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet<KeychainKind>, Txid) {
43+
new_funded_wallet(descriptor, None)
44+
}
45+
46+
/// Get funded segwit wallet
47+
pub fn get_funded_wallet_wpkh() -> (Wallet<KeychainKind>, Txid) {
48+
let (desc, change_desc) = get_test_wpkh_and_change_desc();
49+
get_funded_wallet(desc, change_desc)
50+
}
51+
52+
// TODO: Perhaps this can be made generic!
53+
/// Get unfunded wallet and wallet update that funds it
54+
///
55+
/// The funding update contains a tx with a 76_000 sats input and two outputs, one spending
56+
/// 25_000 to a foreign address and one returning 50_000 back to the wallet as
57+
/// change. The remaining 1000 sats are the transaction fee.
58+
pub fn new_wallet_and_funding_update(
59+
descriptor: &str,
60+
change_descriptor: Option<&str>,
61+
) -> (Wallet<KeychainKind>, Txid, Update<KeychainKind>) {
62+
let mut keyring = KeyRing::new(Network::Regtest, KeychainKind::External, descriptor)
63+
.expect("descriptor must be valid");
64+
if let Some(change_desc) = change_descriptor {
65+
keyring
66+
.add_descriptor(KeychainKind::Internal, change_desc)
67+
.expect("descriptor must be valid");
68+
};
69+
70+
let wallet = Wallet::create(keyring)
71+
.create_wallet_no_persist()
72+
.expect("descriptors must be valid");
73+
74+
let receive_address = wallet
75+
.peek_address(KeychainKind::External, 0)
76+
.unwrap()
77+
.address;
78+
let sendto_address = Address::from_str("bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5")
79+
.expect("address")
80+
.require_network(Network::Regtest)
81+
.unwrap();
1582

16-
// /// Return a fake wallet that appears to be funded for testing.
17-
// ///
18-
// /// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
19-
// /// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
20-
// /// sats are the transaction fee.
21-
// pub fn get_funded_wallet(descriptor: &str, change_descriptor: &str) -> (Wallet, Txid) {
22-
// new_funded_wallet(descriptor, Some(change_descriptor))
23-
// }
24-
25-
// fn new_funded_wallet(descriptor: &str, change_descriptor: Option<&str>) -> (Wallet, Txid) {
26-
// let (mut wallet, txid, update) = new_wallet_and_funding_update(descriptor,
27-
// change_descriptor); wallet.apply_update(update).unwrap();
28-
// (wallet, txid)
29-
// }
30-
//
31-
// /// Return a fake wallet that appears to be funded for testing.
32-
// ///
33-
// /// The funded wallet contains a tx with a 76_000 sats input and two outputs, one spending 25_000
34-
// /// to a foreign address and one returning 50_000 back to the wallet. The remaining 1000
35-
// /// sats are the transaction fee.
36-
// pub fn get_funded_wallet_single(descriptor: &str) -> (Wallet, Txid) {
37-
// new_funded_wallet(descriptor, None)
38-
// }
39-
//
40-
// /// Get funded segwit wallet
41-
// pub fn get_funded_wallet_wpkh() -> (Wallet, Txid) {
42-
// let (desc, change_desc) = get_test_wpkh_and_change_desc();
43-
// get_funded_wallet(desc, change_desc)
44-
// }
45-
//
46-
// /// Get unfunded wallet and wallet update that funds it
47-
// ///
48-
// /// The funding update contains a tx with a 76_000 sats input and two outputs, one spending
49-
// /// 25_000 to a foreign address and one returning 50_000 back to the wallet as
50-
// /// change. The remaining 1000 sats are the transaction fee.
51-
// pub fn new_wallet_and_funding_update(
52-
// descriptor: &str,
53-
// change_descriptor: Option<&str>,
54-
// ) -> (Wallet, Txid, Update) {
55-
// let params = if let Some(change_desc) = change_descriptor {
56-
// Wallet::create(descriptor.to_string(), change_desc.to_string())
57-
// } else {
58-
// Wallet::create_single(descriptor.to_string())
59-
// };
60-
//
61-
// let wallet = params
62-
// .network(Network::Regtest)
63-
// .create_wallet_no_persist()
64-
// .expect("descriptors must be valid");
65-
//
66-
// let receive_address = wallet.peek_address(KeychainKind::External, 0).address;
67-
// let sendto_address = Address::from_str("bcrt1q3qtze4ys45tgdvguj66zrk4fu6hq3a3v9pfly5")
68-
// .expect("address")
69-
// .require_network(Network::Regtest)
70-
// .unwrap();
71-
//
72-
// let mut update = Update::default();
73-
//
74-
// let tx0 = Transaction {
75-
// output: vec![TxOut {
76-
// value: Amount::from_sat(76_000),
77-
// script_pubkey: receive_address.script_pubkey(),
78-
// }],
79-
// ..new_tx(0)
80-
// };
81-
//
82-
// let tx1 = Transaction {
83-
// input: vec![TxIn {
84-
// previous_output: OutPoint {
85-
// txid: tx0.compute_txid(),
86-
// vout: 0,
87-
// },
88-
// ..Default::default()
89-
// }],
90-
// output: vec![
91-
// TxOut {
92-
// value: Amount::from_sat(50_000),
93-
// script_pubkey: receive_address.script_pubkey(),
94-
// },
95-
// TxOut {
96-
// value: Amount::from_sat(25_000),
97-
// script_pubkey: sendto_address.script_pubkey(),
98-
// },
99-
// ],
100-
// ..new_tx(0)
101-
// };
102-
// let txid1 = tx1.compute_txid();
103-
//
104-
// let b0 = BlockId {
105-
// height: 0,
106-
// hash: BlockHash::from_slice(wallet.network().chain_hash().as_bytes()).unwrap(),
107-
// };
108-
// let b1 = BlockId {
109-
// height: 42,
110-
// hash: BlockHash::all_zeros(),
111-
// };
112-
// let b2 = BlockId {
113-
// height: 1000,
114-
// hash: BlockHash::all_zeros(),
115-
// };
116-
// let a2 = ConfirmationBlockTime {
117-
// block_id: b2,
118-
// confirmation_time: 100,
119-
// };
120-
// let b3 = BlockId {
121-
// height: 2000,
122-
// hash: BlockHash::all_zeros(),
123-
// };
124-
// let a3 = ConfirmationBlockTime {
125-
// block_id: b3,
126-
// confirmation_time: 200,
127-
// };
128-
// update.chain = CheckPoint::from_block_ids([b0, b1, b2, b3]).ok();
129-
// update.tx_update.anchors = [(a2, tx0.compute_txid()), (a3, tx1.compute_txid())].into();
130-
// update.tx_update.txs = [Arc::new(tx0), Arc::new(tx1)].into();
131-
//
132-
// (wallet, txid1, update)
133-
// }
83+
let mut update = Update::default();
84+
85+
let tx0 = Transaction {
86+
output: vec![TxOut {
87+
value: Amount::from_sat(76_000),
88+
script_pubkey: receive_address.script_pubkey(),
89+
}],
90+
..new_tx(0)
91+
};
92+
93+
let tx1 = Transaction {
94+
input: vec![TxIn {
95+
previous_output: OutPoint {
96+
txid: tx0.compute_txid(),
97+
vout: 0,
98+
},
99+
..Default::default()
100+
}],
101+
output: vec![
102+
TxOut {
103+
value: Amount::from_sat(50_000),
104+
script_pubkey: receive_address.script_pubkey(),
105+
},
106+
TxOut {
107+
value: Amount::from_sat(25_000),
108+
script_pubkey: sendto_address.script_pubkey(),
109+
},
110+
],
111+
..new_tx(0)
112+
};
113+
let txid1 = tx1.compute_txid();
114+
115+
let b0 = BlockId {
116+
height: 0,
117+
hash: BlockHash::from_slice(wallet.network().chain_hash().as_bytes()).unwrap(),
118+
};
119+
let b1 = BlockId {
120+
height: 42,
121+
hash: BlockHash::all_zeros(),
122+
};
123+
let b2 = BlockId {
124+
height: 1000,
125+
hash: BlockHash::all_zeros(),
126+
};
127+
let a2 = ConfirmationBlockTime {
128+
block_id: b2,
129+
confirmation_time: 100,
130+
};
131+
let b3 = BlockId {
132+
height: 2000,
133+
hash: BlockHash::all_zeros(),
134+
};
135+
let a3 = ConfirmationBlockTime {
136+
block_id: b3,
137+
confirmation_time: 200,
138+
};
139+
update.chain = CheckPoint::from_block_ids([b0, b1, b2, b3]).ok();
140+
update.tx_update.anchors = [(a2, tx0.compute_txid()), (a3, tx1.compute_txid())].into();
141+
update.tx_update.txs = [Arc::new(tx0), Arc::new(tx1)].into();
142+
143+
(wallet, txid1, update)
144+
}
134145

135146
/// `pkh` single key descriptor
136147
pub fn get_test_pkh() -> &'static str {

0 commit comments

Comments
 (0)