@@ -304,11 +304,15 @@ impl BitcoinD {
304304 // to be compatible with different version, in the end we are only interested if
305305 // the call is succesfull not in the returned value.
306306 if client_base. call :: < Value > ( "getblockchaininfo" , & [ ] ) . is_ok ( ) {
307- client_base
307+ // Try creating new wallet, if fails due to already existing wallet file
308+ // try loading the same. Return if still errors.
309+ if client_base
308310 . create_wallet ( "default" , None , None , None , None )
309- . unwrap ( ) ;
310- break Client :: new ( & node_url_default, Auth :: CookieFile ( cookie_file. clone ( ) ) )
311- . unwrap ( ) ;
311+ . is_err ( )
312+ {
313+ client_base. load_wallet ( "default" ) ?;
314+ }
315+ break Client :: new ( & node_url_default, Auth :: CookieFile ( cookie_file. clone ( ) ) ) ?;
312316 }
313317 }
314318 } ;
@@ -375,7 +379,7 @@ impl BitcoinD {
375379impl Drop for BitcoinD {
376380 fn drop ( & mut self ) {
377381 if let DataDir :: Persistent ( _) = self . work_dir {
378- let _ = self . client . stop ( ) ;
382+ let _ = self . stop ( ) ;
379383 }
380384 let _ = self . process . kill ( ) ;
381385 }
@@ -436,6 +440,7 @@ mod test {
436440 use crate :: { get_available_port, BitcoinD , Conf , LOCAL_IP , P2P } ;
437441 use bitcoincore_rpc:: RpcApi ;
438442 use std:: net:: SocketAddrV4 ;
443+ use tempfile:: TempDir ;
439444
440445 #[ test]
441446 fn test_local_ip ( ) {
@@ -490,6 +495,40 @@ mod test {
490495 assert_eq ! ( peers_connected( & other_bitcoind. client) , 1 ) ;
491496 }
492497
498+ #[ test]
499+ fn test_data_persistence ( ) {
500+ // Create a Conf with staticdir type
501+ let mut conf = Conf :: default ( ) ;
502+ let datadir = TempDir :: new ( ) . unwrap ( ) ;
503+ conf. staticdir = Some ( datadir. path ( ) . to_path_buf ( ) ) ;
504+
505+ // Start BitcoinD with persistent db config
506+ // Generate 101 blocks
507+ // Wallet balance should be 50
508+ let bitcoind = BitcoinD :: with_conf ( exe_path ( ) . unwrap ( ) , & conf) . unwrap ( ) ;
509+ let core_addrs = bitcoind. client . get_new_address ( None , None ) . unwrap ( ) ;
510+ bitcoind
511+ . client
512+ . generate_to_address ( 101 , & core_addrs)
513+ . unwrap ( ) ;
514+ let wallet_balance_1 = bitcoind. client . get_balance ( None , None ) . unwrap ( ) ;
515+ let best_block_1 = bitcoind. client . get_best_block_hash ( ) . unwrap ( ) ;
516+
517+ drop ( bitcoind) ;
518+
519+ // Start a new BitcoinD with the same datadir
520+ let bitcoind = BitcoinD :: with_conf ( exe_path ( ) . unwrap ( ) , & conf) . unwrap ( ) ;
521+
522+ let wallet_balance_2 = bitcoind. client . get_balance ( None , None ) . unwrap ( ) ;
523+ let best_block_2 = bitcoind. client . get_best_block_hash ( ) . unwrap ( ) ;
524+
525+ // Check node chain data persists
526+ assert_eq ! ( best_block_1, best_block_2) ;
527+
528+ // Check the node wallet balance persists
529+ assert_eq ! ( wallet_balance_1, wallet_balance_2) ;
530+ }
531+
493532 #[ test]
494533 fn test_multi_p2p ( ) {
495534 let _ = env_logger:: try_init ( ) ;
0 commit comments