@@ -223,6 +223,58 @@ fn wallet_load_checks() -> anyhow::Result<()> {
223
223
Ok ( ( ) )
224
224
}
225
225
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
+
226
278
#[ test]
227
279
fn single_descriptor_wallet_persist_and_recover ( ) {
228
280
use bdk_chain:: miniscript:: Descriptor ;
0 commit comments