Skip to content

Commit

Permalink
Wait for wallet to load (#4095)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Dec 12, 2024
1 parent 080fcef commit 894c41d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
26 changes: 22 additions & 4 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,29 @@ impl Wallet {
});
}

settings
match settings
.bitcoin_rpc_client(Some(name.clone()))?
.call::<serde_json::Value>("importdescriptors", &[serde_json::to_value(descriptors)?])?;

Ok(())
.call::<serde_json::Value>(
"importdescriptors",
&[serde_json::to_value(descriptors.clone())?],
) {
Ok(_) => Ok(()),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err)))
if err.code == -4 && err.message == "Wallet already loading." =>
{
// wallet loading
Ok(())
}
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::Error::Rpc(err)))
if err.code == -35 =>
{
// wallet already loaded
Ok(())
}
Err(err) => {
bail!("Failed to import descriptors for wallet {}: {err}", name)
}
}
}

pub(crate) fn check_version(client: Client) -> Result<Client> {
Expand Down
25 changes: 24 additions & 1 deletion src/wallet/wallet_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,30 @@ 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 && err.message == "Wallet already loading." =>
{
// wallet loading
eprint!(".");
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 {
Expand Down

0 comments on commit 894c41d

Please sign in to comment.