Skip to content

Commit 2d5661d

Browse files
Fix behavior to handle persistent db
In case of persistent db, check if the "default" wallet file already exists. Load it if it does. Or else create new wallet. Always create new wallet for temp db. Remove global temp env variable in CI.
1 parent 8d8e3ae commit 2d5661d

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
with:
3232
toolchain: stable
3333
override: true
34-
- run: echo "TEMPDIR_ROOT=/dev/shm" >> $GITHUB_ENV
34+
#- run: echo "TEMPDIR_ROOT=/dev/shm" >> $GITHUB_ENV # conflicts with test `test_data_persistence`
3535
if: ${{ matrix.os != 'macos-10.15' }}
3636
- uses: actions-rs/cargo@v1
3737
with:

src/lib.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
375379
impl 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

Comments
 (0)