Skip to content

Commit

Permalink
lib: windows: don't unload the wo wallet if not loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
darosior committed Aug 29, 2023
1 parent 26d5750 commit 34f3946
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
24 changes: 14 additions & 10 deletions src/bitcoin/d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ impl BitcoinD {
None
}

pub fn unload_wallet(&self, wallet_path: String) -> Option<String> {
fn unload_wallet(&self, wallet_path: String) -> Option<String> {
let res = self.make_node_request("unloadwallet", &params!(Json::String(wallet_path),));
self.warning_from_res(&res)
}
Expand Down Expand Up @@ -607,23 +607,27 @@ impl BitcoinD {
.collect()
}

/// Create the watchonly wallet on bitcoind, and import it the main descriptor.
pub fn create_watchonly_wallet(
&self,
main_descriptor: &LianaDescriptor,
) -> Result<(), BitcoindError> {
// Remove any leftover. This can happen if we delete the watchonly wallet but don't restart
// bitcoind.
while self.list_wallets().contains(&self.watchonly_wallet_path) {
pub fn maybe_unload_watchonly_wallet(&self, watchonly_wallet_path: String) {
while self.list_wallets().contains(&watchonly_wallet_path) {
log::info!("Found a leftover watchonly wallet loaded on bitcoind. Removing it.");
if let Some(e) = self.unload_wallet(self.watchonly_wallet_path.clone()) {
if let Some(e) = self.unload_wallet(watchonly_wallet_path.clone()) {
log::error!(
"Unloading wallet '{}': '{}'",
&self.watchonly_wallet_path,
e
);
}
}
}

/// Create the watchonly wallet on bitcoind, and import it the main descriptor.
pub fn create_watchonly_wallet(
&self,
main_descriptor: &LianaDescriptor,
) -> Result<(), BitcoindError> {
// Remove any leftover. This can happen if we delete the watchonly wallet but don't restart
// bitcoind.
self.maybe_unload_watchonly_wallet(self.watchonly_wallet_path.clone());

// Now create the wallet and import the main descriptor.
self.create_wallet(self.watchonly_wallet_path.clone())
Expand Down
9 changes: 2 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,11 @@ fn maybe_delete_watchonly_wallet(
};

if wallet_path.exists() {
bitcoind.maybe_unload_watchonly_wallet(wallet_path.to_string_lossy().to_string());
log::info!(
"Found a leftover watchonly wallet at '{}'. Deleting it.",
"Deleting the leftover watchonly wallet at '{}'.",
wallet_path.as_path().to_string_lossy()
);
if let Some(warning) = bitcoind.unload_wallet(wallet_path.to_string_lossy().to_string()) {
log::warn!(
"Warning when unloading watchonly wallet on bitcoind: '{}'",
warning
);
}
fs::remove_dir_all(&wallet_path)
.map_err(|e| StartupError::WindowsBitcoindWatchonlyDeletion(wallet_path, e))?;
} else {
Expand Down

0 comments on commit 34f3946

Please sign in to comment.