Skip to content

Commit c3ea54d

Browse files
committed
test(wallet): check persisted anchors does not lose data
1 parent d41cb62 commit c3ea54d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

crates/wallet/tests/wallet.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,58 @@ fn wallet_load_checks() -> anyhow::Result<()> {
223223
Ok(())
224224
}
225225

226+
#[test]
227+
fn wallet_should_persist_anchors_and_recover() {
228+
use bdk_chain::rusqlite;
229+
let temp_dir = tempfile::tempdir().unwrap();
230+
let db_path = temp_dir.path().join("wallet.db");
231+
let mut db = rusqlite::Connection::open(db_path).unwrap();
232+
233+
let desc = get_test_tr_single_sig_xprv();
234+
let mut wallet = Wallet::create_single(desc)
235+
.network(Network::Testnet)
236+
.create_wallet(&mut db)
237+
.unwrap();
238+
let small_output_tx = Transaction {
239+
input: vec![],
240+
output: vec![TxOut {
241+
script_pubkey: wallet
242+
.next_unused_address(KeychainKind::External)
243+
.script_pubkey(),
244+
value: Amount::from_sat(25_000),
245+
}],
246+
version: transaction::Version::non_standard(0),
247+
lock_time: absolute::LockTime::ZERO,
248+
};
249+
let txid = small_output_tx.compute_txid();
250+
insert_tx(&mut wallet, small_output_tx);
251+
let anchor = ConfirmationBlockTime {
252+
block_id: wallet.latest_checkpoint().block_id(),
253+
confirmation_time: 200,
254+
};
255+
insert_anchor(&mut wallet, txid, anchor);
256+
assert!(wallet.persist(&mut db).unwrap());
257+
258+
// should recover persisted wallet
259+
let secp = wallet.secp_ctx();
260+
let (_, keymap) = <Descriptor<DescriptorPublicKey>>::parse_descriptor(secp, desc).unwrap();
261+
assert!(!keymap.is_empty());
262+
let wallet = Wallet::load()
263+
.descriptor(KeychainKind::External, Some(desc))
264+
.extract_keys()
265+
.load_wallet(&mut db)
266+
.unwrap()
267+
.expect("must have loaded changeset");
268+
// stored anchor should be retrieved in the same condition it was persisted
269+
assert_eq!(
270+
wallet
271+
.get_tx(txid)
272+
.expect("should retrieve stored tx")
273+
.chain_position,
274+
ChainPosition::Confirmed(&anchor),
275+
);
276+
}
277+
226278
#[test]
227279
fn single_descriptor_wallet_persist_and_recover() {
228280
use bdk_chain::miniscript::Descriptor;

0 commit comments

Comments
 (0)