Skip to content

Commit

Permalink
Fix behavior to handle persistent db
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rajarshimaitra committed May 12, 2022
1 parent 8d8e3ae commit 2d5661d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
with:
toolchain: stable
override: true
- run: echo "TEMPDIR_ROOT=/dev/shm" >> $GITHUB_ENV
#- run: echo "TEMPDIR_ROOT=/dev/shm" >> $GITHUB_ENV # conflicts with test `test_data_persistence`
if: ${{ matrix.os != 'macos-10.15' }}
- uses: actions-rs/cargo@v1
with:
Expand Down
49 changes: 44 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,15 @@ impl BitcoinD {
// to be compatible with different version, in the end we are only interested if
// the call is succesfull not in the returned value.
if client_base.call::<Value>("getblockchaininfo", &[]).is_ok() {
client_base
// Try creating new wallet, if fails due to already existing wallet file
// try loading the same. Return if still errors.
if client_base
.create_wallet("default", None, None, None, None)
.unwrap();
break Client::new(&node_url_default, Auth::CookieFile(cookie_file.clone()))
.unwrap();
.is_err()
{
client_base.load_wallet("default")?;
}
break Client::new(&node_url_default, Auth::CookieFile(cookie_file.clone()))?;
}
}
};
Expand Down Expand Up @@ -375,7 +379,7 @@ impl BitcoinD {
impl Drop for BitcoinD {
fn drop(&mut self) {
if let DataDir::Persistent(_) = self.work_dir {
let _ = self.client.stop();
let _ = self.stop();
}
let _ = self.process.kill();
}
Expand Down Expand Up @@ -436,6 +440,7 @@ mod test {
use crate::{get_available_port, BitcoinD, Conf, LOCAL_IP, P2P};
use bitcoincore_rpc::RpcApi;
use std::net::SocketAddrV4;
use tempfile::TempDir;

#[test]
fn test_local_ip() {
Expand Down Expand Up @@ -490,6 +495,40 @@ mod test {
assert_eq!(peers_connected(&other_bitcoind.client), 1);
}

#[test]
fn test_data_persistence() {
// Create a Conf with staticdir type
let mut conf = Conf::default();
let datadir = TempDir::new().unwrap();
conf.staticdir = Some(datadir.path().to_path_buf());

// Start BitcoinD with persistent db config
// Generate 101 blocks
// Wallet balance should be 50
let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap();
let core_addrs = bitcoind.client.get_new_address(None, None).unwrap();
bitcoind
.client
.generate_to_address(101, &core_addrs)
.unwrap();
let wallet_balance_1 = bitcoind.client.get_balance(None, None).unwrap();
let best_block_1 = bitcoind.client.get_best_block_hash().unwrap();

drop(bitcoind);

// Start a new BitcoinD with the same datadir
let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap();

let wallet_balance_2 = bitcoind.client.get_balance(None, None).unwrap();
let best_block_2 = bitcoind.client.get_best_block_hash().unwrap();

// Check node chain data persists
assert_eq!(best_block_1, best_block_2);

// Check the node wallet balance persists
assert_eq!(wallet_balance_1, wallet_balance_2);
}

#[test]
fn test_multi_p2p() {
let _ = env_logger::try_init();
Expand Down

0 comments on commit 2d5661d

Please sign in to comment.