@@ -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,44 @@ 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+ if let Err ( e) = wallet. persist( & mut db_conn) {
130+ assert!(
131+ matches!( e, bdk_wallet:: rusqlite:: Error :: ToSqlConversionFailure ( ..) ) ,
132+ "It should always persist successfully!"
133+ ) ;
134+ return ;
135+ } ;
136+
137+ // generate fuzzed load
138+ wallet = Wallet :: load( )
139+ . descriptor( KeychainKind :: External , Some ( EXTERNAL_DESCRIPTOR ) )
140+ . descriptor( KeychainKind :: Internal , Some ( INTERNAL_DESCRIPTOR ) )
141+ . check_network( NETWORK )
142+ . check_genesis_hash( expected_genesis_hash)
143+ . load_wallet( & mut db_conn)
144+ . expect( "It should always load from persistence successfully!" )
145+ . expect( "It should load the wallet successfully!" ) ;
146+
147+ // verify the persisted data is accurate
148+ assert_eq!( wallet. network( ) , NETWORK ) ;
149+ assert_eq!( wallet. balance( ) , expected_balance) ;
150+ assert_eq!(
151+ wallet. next_derivation_index( KeychainKind :: Internal ) ,
152+ expected_internal_index
153+ ) ;
154+ assert_eq!(
155+ wallet. next_derivation_index( KeychainKind :: External ) ,
156+ expected_external_index
157+ ) ;
158+ assert_eq!( wallet. latest_checkpoint( ) , expected_tip) ;
122159 }
123160 }
124161 }
0 commit comments