From afc1d5a9f95c3c0f7decf69eb948c4955675f8fc Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 1 Aug 2022 14:44:18 -0400 Subject: [PATCH 1/6] Don't create wallet by default --- src/lib.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ee6c740..3b12ead 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,7 +290,6 @@ impl BitcoinD { .stdout(stdout) .spawn()?; - let node_url_default = format!("{}/wallet/default", rpc_url); // wait bitcoind is ready, use default wallet let client = loop { if let Some(status) = process.try_wait()? { @@ -304,7 +303,6 @@ impl BitcoinD { return Err(Error::EarlyExit(status)); } } - thread::sleep(Duration::from_millis(500)); assert!(process.stderr.is_none()); let client_result = Client::new(&rpc_url, Auth::CookieFile(cookie_file.clone())); if let Ok(client_base) = client_result { @@ -312,17 +310,10 @@ 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::("getblockchaininfo", &[]).is_ok() { - // 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) - .is_err() - { - client_base.load_wallet("default")?; - } - break Client::new(&node_url_default, Auth::CookieFile(cookie_file.clone()))?; + break Client::new(&rpc_url, Auth::CookieFile(cookie_file.clone()))?; } } + thread::sleep(Duration::from_millis(500)); }; Ok(BitcoinD { From 2f7bb652f5d4dfbfad41478c84563aff64f7eae3 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 1 Aug 2022 14:47:41 -0400 Subject: [PATCH 2/6] Reduce sleep time --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3b12ead..3b619e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -313,7 +313,7 @@ impl BitcoinD { break Client::new(&rpc_url, Auth::CookieFile(cookie_file.clone()))?; } } - thread::sleep(Duration::from_millis(500)); + thread::sleep(Duration::from_millis(100)); }; Ok(BitcoinD { From 479fc66c34eb51e2f0ea3ef21fc2e266874f76b6 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 1 Aug 2022 15:11:52 -0400 Subject: [PATCH 3/6] Fix tests --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 3b619e8..4f3c3d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -473,6 +473,7 @@ mod test { fn test_bitcoind() { let exe = init(); let bitcoind = BitcoinD::new(exe).unwrap(); + bitcoind.client.create_wallet("default", None, None, None, None).unwrap(); let info = bitcoind.client.get_blockchain_info().unwrap(); assert_eq!(0, info.blocks); let address = bitcoind.client.get_new_address(None, None).unwrap(); @@ -525,6 +526,7 @@ mod test { // Generate 101 blocks // Wallet balance should be 50 let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); + bitcoind.client.create_wallet("default", None, None, None, None).unwrap(); let core_addrs = bitcoind.client.get_new_address(None, None).unwrap(); bitcoind .client @@ -537,6 +539,7 @@ mod test { // Start a new BitcoinD with the same datadir let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); + bitcoind.client.load_wallet("default").unwrap(); let wallet_balance_2 = bitcoind.client.get_balance(None, None).unwrap(); let best_block_2 = bitcoind.client.get_best_block_hash().unwrap(); @@ -664,6 +667,7 @@ mod test { conf.args.push("-rpcauth=bitcoind:cccd5d7fd36e55c1b8576b8077dc1b83$60b5676a09f8518dcb4574838fb86f37700cd690d99bd2fdc2ea2bf2ab80ead6"); let bitcoind = BitcoinD::with_conf(exe, &conf).unwrap(); + bitcoind.client.create_wallet("default", None, None, None, None).unwrap(); let client = Client::new( format!("{}/wallet/default", bitcoind.rpc_url().as_str()).as_str(), From 44db514218c638b6e356fb6f5ebeb6b5813bd161 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 1 Aug 2022 15:12:58 -0400 Subject: [PATCH 4/6] Format --- src/lib.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4f3c3d2..0c59467 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -473,7 +473,10 @@ mod test { fn test_bitcoind() { let exe = init(); let bitcoind = BitcoinD::new(exe).unwrap(); - bitcoind.client.create_wallet("default", None, None, None, None).unwrap(); + bitcoind + .client + .create_wallet("default", None, None, None, None) + .unwrap(); let info = bitcoind.client.get_blockchain_info().unwrap(); assert_eq!(0, info.blocks); let address = bitcoind.client.get_new_address(None, None).unwrap(); @@ -526,7 +529,10 @@ mod test { // Generate 101 blocks // Wallet balance should be 50 let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); - bitcoind.client.create_wallet("default", None, None, None, None).unwrap(); + bitcoind + .client + .create_wallet("default", None, None, None, None) + .unwrap(); let core_addrs = bitcoind.client.get_new_address(None, None).unwrap(); bitcoind .client @@ -667,7 +673,10 @@ mod test { conf.args.push("-rpcauth=bitcoind:cccd5d7fd36e55c1b8576b8077dc1b83$60b5676a09f8518dcb4574838fb86f37700cd690d99bd2fdc2ea2bf2ab80ead6"); let bitcoind = BitcoinD::with_conf(exe, &conf).unwrap(); - bitcoind.client.create_wallet("default", None, None, None, None).unwrap(); + bitcoind + .client + .create_wallet("default", None, None, None, None) + .unwrap(); let client = Client::new( format!("{}/wallet/default", bitcoind.rpc_url().as_str()).as_str(), From 5af90c39a2175374313bec23921350eec716e1d3 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 1 Aug 2022 15:28:29 -0400 Subject: [PATCH 5/6] Load wallet if not created --- src/lib.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0c59467..13b20f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -473,10 +473,13 @@ mod test { fn test_bitcoind() { let exe = init(); let bitcoind = BitcoinD::new(exe).unwrap(); - bitcoind + if bitcoind .client .create_wallet("default", None, None, None, None) - .unwrap(); + .is_err() + { + bitcoind.client.load_wallet("default").unwrap(); + } let info = bitcoind.client.get_blockchain_info().unwrap(); assert_eq!(0, info.blocks); let address = bitcoind.client.get_new_address(None, None).unwrap(); @@ -529,10 +532,13 @@ mod test { // Generate 101 blocks // Wallet balance should be 50 let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); - bitcoind + if bitcoind .client .create_wallet("default", None, None, None, None) - .unwrap(); + .is_err() + { + bitcoind.client.load_wallet("default").unwrap(); + } let core_addrs = bitcoind.client.get_new_address(None, None).unwrap(); bitcoind .client @@ -545,7 +551,13 @@ mod test { // Start a new BitcoinD with the same datadir let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); - bitcoind.client.load_wallet("default").unwrap(); + if bitcoind + .client + .create_wallet("default", None, None, None, None) + .is_err() + { + bitcoind.client.load_wallet("default").unwrap(); + } let wallet_balance_2 = bitcoind.client.get_balance(None, None).unwrap(); let best_block_2 = bitcoind.client.get_best_block_hash().unwrap(); @@ -673,10 +685,13 @@ mod test { conf.args.push("-rpcauth=bitcoind:cccd5d7fd36e55c1b8576b8077dc1b83$60b5676a09f8518dcb4574838fb86f37700cd690d99bd2fdc2ea2bf2ab80ead6"); let bitcoind = BitcoinD::with_conf(exe, &conf).unwrap(); - bitcoind + if bitcoind .client .create_wallet("default", None, None, None, None) - .unwrap(); + .is_err() + { + bitcoind.client.load_wallet("default").unwrap(); + } let client = Client::new( format!("{}/wallet/default", bitcoind.rpc_url().as_str()).as_str(), From 68502585f056884f99417c3e32ca8518744ae0c9 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 12 Aug 2022 23:57:52 -0400 Subject: [PATCH 6/6] Add flag to skip wallet creation --- src/lib.rs | 67 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 13b20f7..c63fa9c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -198,6 +198,9 @@ pub struct Conf<'a> { /// happen they are used at the time the process is spawn. When retrying other available ports /// are returned reducing the probability of conflicts to negligible. pub attempts: u8, + + /// If set to `true` a default wallet will not be created upon startup. + pub skip_default_wallet_creation: bool, } impl Default for Conf<'_> { @@ -210,6 +213,7 @@ impl Default for Conf<'_> { tmpdir: None, staticdir: None, attempts: 3, + skip_default_wallet_creation: false, } } } @@ -290,6 +294,7 @@ impl BitcoinD { .stdout(stdout) .spawn()?; + let node_url_default = format!("{}/wallet/default", rpc_url); // wait bitcoind is ready, use default wallet let client = loop { if let Some(status) = process.try_wait()? { @@ -310,7 +315,22 @@ 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::("getblockchaininfo", &[]).is_ok() { - break Client::new(&rpc_url, Auth::CookieFile(cookie_file.clone()))?; + if !conf.skip_default_wallet_creation { + // 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) + .is_err() + { + client_base.load_wallet("default")?; + } + break Client::new( + &node_url_default, + Auth::CookieFile(cookie_file.clone()), + )?; + } else { + break client_base; + } } } thread::sleep(Duration::from_millis(100)); @@ -473,13 +493,6 @@ mod test { fn test_bitcoind() { let exe = init(); let bitcoind = BitcoinD::new(exe).unwrap(); - if bitcoind - .client - .create_wallet("default", None, None, None, None) - .is_err() - { - bitcoind.client.load_wallet("default").unwrap(); - } let info = bitcoind.client.get_blockchain_info().unwrap(); assert_eq!(0, info.blocks); let address = bitcoind.client.get_new_address(None, None).unwrap(); @@ -532,13 +545,6 @@ mod test { // Generate 101 blocks // Wallet balance should be 50 let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); - if bitcoind - .client - .create_wallet("default", None, None, None, None) - .is_err() - { - bitcoind.client.load_wallet("default").unwrap(); - } let core_addrs = bitcoind.client.get_new_address(None, None).unwrap(); bitcoind .client @@ -551,13 +557,6 @@ mod test { // Start a new BitcoinD with the same datadir let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); - if bitcoind - .client - .create_wallet("default", None, None, None, None) - .is_err() - { - bitcoind.client.load_wallet("default").unwrap(); - } let wallet_balance_2 = bitcoind.client.get_balance(None, None).unwrap(); let best_block_2 = bitcoind.client.get_best_block_hash().unwrap(); @@ -685,13 +684,6 @@ mod test { conf.args.push("-rpcauth=bitcoind:cccd5d7fd36e55c1b8576b8077dc1b83$60b5676a09f8518dcb4574838fb86f37700cd690d99bd2fdc2ea2bf2ab80ead6"); let bitcoind = BitcoinD::with_conf(exe, &conf).unwrap(); - if bitcoind - .client - .create_wallet("default", None, None, None, None) - .is_err() - { - bitcoind.client.load_wallet("default").unwrap(); - } let client = Client::new( format!("{}/wallet/default", bitcoind.rpc_url().as_str()).as_str(), @@ -708,6 +700,23 @@ mod test { assert_eq!(1, info.blocks); } + #[test] + fn test_skip_wallet_creation() { + let mut conf = Conf::default(); + + conf.skip_default_wallet_creation = false; + + let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); + + assert!(bitcoind.client.get_new_address(None, None).is_ok()); + + conf.skip_default_wallet_creation = true; + + let bitcoind = BitcoinD::with_conf(exe_path().unwrap(), &conf).unwrap(); + + assert!(bitcoind.client.get_new_address(None, None).is_err()); + } + fn peers_connected(client: &Client) -> usize { let result: Vec = client.call("getpeerinfo", &[]).unwrap(); result.len()