From 9211d849c191ff5d2552bde681edaa7038c8d049 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Tue, 13 Dec 2022 16:04:34 +0100 Subject: [PATCH] bitcoind: be less stupid when loading the watchonly wallet Only load it if isn't there already. --- src/bitcoin/d/mod.rs | 17 ++++++++--------- src/lib.rs | 13 ++++++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/bitcoin/d/mod.rs b/src/bitcoin/d/mod.rs index 2d917fc7..412abaea 100644 --- a/src/bitcoin/d/mod.rs +++ b/src/bitcoin/d/mod.rs @@ -553,16 +553,15 @@ impl BitcoinD { Ok(()) } - /// Try to load the watchonly wallet in bitcoind. It will continue on error (since it's - /// likely the wallet is just already loaded) and log it as info instead. - pub fn try_load_watchonly_wallet(&self) { - // TODO: check if it's not loaded instead of blindly trying to load it. - if let Err(e) = self.make_fallible_node_request( - "loadwallet", - ¶ms!(Json::String(self.watchonly_wallet_path.clone()),), - ) { - log::info!("Got error '{}' while trying to load watchonly on bitcoind. It is possibly already loaded.", e); + /// Load the watchonly wallet on bitcoind, if it isn't already. + pub fn maybe_load_watchonly_wallet(&self) -> Result<(), BitcoindError> { + if !self.list_wallets().contains(&self.watchonly_wallet_path) { + self.make_fallible_node_request( + "loadwallet", + ¶ms!(Json::String(self.watchonly_wallet_path.clone()),), + )?; } + Ok(()) } /// Perform various sanity checks on the bitcoind instance. diff --git a/src/lib.rs b/src/lib.rs index a2cb7319..ec96ad09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,7 +203,7 @@ fn setup_bitcoind( bitcoind.create_watchonly_wallet(&config.main_descriptor)?; log::info!("Created a new watchonly wallet on bitcoind."); } - bitcoind.try_load_watchonly_wallet(); + bitcoind.maybe_load_watchonly_wallet()?; bitcoind.sanity_check(&config.main_descriptor, config.bitcoin_config.network)?; log::info!("Connection to bitcoind established and checked."); @@ -488,12 +488,19 @@ mod tests { // Send them a dummy result to loadwallet. fn complete_wallet_loading(server: &net::TcpListener) { - let net_resp = + let listwallets_resp = + "HTTP/1.1 200\n\r\n{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":[]}\n".as_bytes(); + let (mut stream, _) = server.accept().unwrap(); + read_til_json_end(&mut stream); + stream.write_all(listwallets_resp).unwrap(); + stream.flush().unwrap(); + + let loadwallet_resp = "HTTP/1.1 200\n\r\n{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"name\":\"dummy\"}}\n" .as_bytes(); let (mut stream, _) = server.accept().unwrap(); read_til_json_end(&mut stream); - stream.write_all(net_resp).unwrap(); + stream.write_all(loadwallet_resp).unwrap(); stream.flush().unwrap(); }