@@ -304,11 +304,15 @@ impl BitcoinD {
304
304
// to be compatible with different version, in the end we are only interested if
305
305
// the call is succesfull not in the returned value.
306
306
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
308
310
. 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 ( ) ) ) ?;
312
316
}
313
317
}
314
318
} ;
@@ -375,7 +379,7 @@ impl BitcoinD {
375
379
impl Drop for BitcoinD {
376
380
fn drop ( & mut self ) {
377
381
if let DataDir :: Persistent ( _) = self . work_dir {
378
- let _ = self . client . stop ( ) ;
382
+ let _ = self . stop ( ) ;
379
383
}
380
384
let _ = self . process . kill ( ) ;
381
385
}
@@ -436,6 +440,7 @@ mod test {
436
440
use crate :: { get_available_port, BitcoinD , Conf , LOCAL_IP , P2P } ;
437
441
use bitcoincore_rpc:: RpcApi ;
438
442
use std:: net:: SocketAddrV4 ;
443
+ use tempfile:: TempDir ;
439
444
440
445
#[ test]
441
446
fn test_local_ip ( ) {
@@ -490,6 +495,40 @@ mod test {
490
495
assert_eq ! ( peers_connected( & other_bitcoind. client) , 1 ) ;
491
496
}
492
497
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
+
493
532
#[ test]
494
533
fn test_multi_p2p ( ) {
495
534
let _ = env_logger:: try_init ( ) ;
0 commit comments