From 702363ea01bbcaaae9b7864312f87e112a1a1649 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 21 Nov 2024 14:19:37 -0800 Subject: [PATCH 1/2] Wait for wallet to load --- src/wallet/wallet_constructor.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/wallet/wallet_constructor.rs b/src/wallet/wallet_constructor.rs index d9d60fce5a..032ecf68f1 100644 --- a/src/wallet/wallet_constructor.rs +++ b/src/wallet/wallet_constructor.rs @@ -52,7 +52,29 @@ impl WalletConstructor { Wallet::check_version(self.settings.bitcoin_rpc_client(Some(self.name.clone()))?)?; if !client.list_wallets()?.contains(&self.name) { - client.load_wallet(&self.name)?; + loop { + match client.load_wallet(&self.name) { + Ok(_) => { + break; + } + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -4 => + { + // wallet loading + thread::sleep(Duration::from_secs(3)); + continue; + } + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -35 => + { + // wallet already loaded + break; + } + Err(err) => { + bail!("Failed to load wallet {}: {err}", self.name); + } + } + } } if client.get_wallet_info()?.private_keys_enabled { From a8a4b8eea950780c04be58614dd8bc6b1575b7a8 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 21 Nov 2024 14:32:09 -0800 Subject: [PATCH 2/2] Same for wallet create --- src/wallet.rs | 31 ++++++++++++++++++++++++++++--- src/wallet/wallet_constructor.rs | 2 +- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/wallet.rs b/src/wallet.rs index 2387d45838..243f786e18 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -555,9 +555,34 @@ impl Wallet { }); } - settings - .bitcoin_rpc_client(Some(name.clone()))? - .call::("importdescriptors", &[serde_json::to_value(descriptors)?])?; + loop { + match settings + .bitcoin_rpc_client(Some(name.clone()))? + .call::( + "importdescriptors", + &[serde_json::to_value(descriptors.clone())?], + ) { + Ok(_) => { + break; + } + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -4 && err.message == "Wallet already loading." => + { + // wallet loading + thread::sleep(Duration::from_secs(3)); + continue; + } + Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) + if err.code == -35 => + { + // wallet already loaded + break; + } + Err(err) => { + bail!("Failed to import descriptors for wallet {}: {err}", name); + } + } + } Ok(()) } diff --git a/src/wallet/wallet_constructor.rs b/src/wallet/wallet_constructor.rs index 032ecf68f1..cdc559cda8 100644 --- a/src/wallet/wallet_constructor.rs +++ b/src/wallet/wallet_constructor.rs @@ -58,7 +58,7 @@ impl WalletConstructor { break; } Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err))) - if err.code == -4 => + if err.code == -4 && err.message == "Wallet already loading." => { // wallet loading thread::sleep(Duration::from_secs(3));