@@ -7,9 +7,9 @@ use std::{
77} ;
88
99use bdk_wallet:: {
10- bitcoin:: { Network , Txid } ,
10+ bitcoin:: { hashes :: Hash as _ , BlockHash , Network , Txid } ,
1111 chain:: { BlockId , ConfirmationBlockTime , TxUpdate } ,
12- descriptor :: DescriptorError ,
12+ rusqlite :: Connection ,
1313 KeychainKind , Update , Wallet ,
1414} ;
1515
@@ -52,10 +52,11 @@ impl WalletAction {
5252
5353fuzz_target ! ( |data: & [ u8 ] | {
5454 // creates initial wallet.
55- let wallet: Result <Wallet , DescriptorError > =
56- Wallet :: create( INTERNAL_DESCRIPTOR , EXTERNAL_DESCRIPTOR )
57- . network( NETWORK )
58- . create_wallet_no_persist( ) ;
55+ let mut db_conn = Connection :: open_in_memory( )
56+ . expect( "Should start an in-memory database connection successfully!" ) ;
57+ let wallet = Wallet :: create( EXTERNAL_DESCRIPTOR , INTERNAL_DESCRIPTOR )
58+ . network( NETWORK )
59+ . create_wallet( & mut db_conn) ;
5960
6061 // asserts that the wallet creation did not fail.
6162 let mut wallet = match wallet {
@@ -117,8 +118,40 @@ fuzz_target!(|data: &[u8]| {
117118 continue ;
118119 }
119120 WalletAction :: PersistAndLoad => {
120- // todo!()
121- continue ;
121+ let expected_balance = wallet. balance( ) ;
122+ let expected_internal_index = wallet. next_derivation_index( KeychainKind :: Internal ) ;
123+ let expected_external_index = wallet. next_derivation_index( KeychainKind :: External ) ;
124+ let expected_tip = wallet. latest_checkpoint( ) ;
125+ let expected_genesis_hash =
126+ BlockHash :: from_byte_array( NETWORK . chain_hash( ) . to_bytes( ) ) ;
127+
128+ // generate fuzzed persist
129+ wallet
130+ . persist( & mut db_conn)
131+ . expect( "It should always persist successfully!" ) ;
132+
133+ // generate fuzzed load
134+ wallet = Wallet :: load( )
135+ . descriptor( KeychainKind :: External , Some ( EXTERNAL_DESCRIPTOR ) )
136+ . descriptor( KeychainKind :: Internal , Some ( INTERNAL_DESCRIPTOR ) )
137+ . check_network( NETWORK )
138+ . check_genesis_hash( expected_genesis_hash)
139+ . load_wallet( & mut db_conn)
140+ . expect( "It should always load from persistence successfully!" )
141+ . expect( "It should load the wallet successfully!" ) ;
142+
143+ // verify the persisted data is accurate
144+ assert_eq!( wallet. network( ) , NETWORK ) ;
145+ assert_eq!( wallet. balance( ) , expected_balance) ;
146+ assert_eq!(
147+ wallet. next_derivation_index( KeychainKind :: Internal ) ,
148+ expected_internal_index
149+ ) ;
150+ assert_eq!(
151+ wallet. next_derivation_index( KeychainKind :: External ) ,
152+ expected_external_index
153+ ) ;
154+ assert_eq!( wallet. latest_checkpoint( ) , expected_tip) ;
122155 }
123156 }
124157 }
0 commit comments