From af90f1be1bcfb9abacf3fc69d1ecb2fdcbde1e1a Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 5 Mar 2025 12:26:30 +0100 Subject: [PATCH] Allow `ElectrumSyncClient` to reuse a preexisting client Previously, our API would only allow users to have `ElectrumSyncClient` a) construct an entirely `new` `electrum_client::Client` or b) take an owened client via `from_client`. Here, we change `ElectrumSyncClient::from_client` to accept an `Arc`, allowing to reuse a pre-existing client without owning it. This is important as it will allow us to reuse the same connection instead of forcing users to establish multiple connections if they need something beyond syncing LDK. --- lightning-transaction-sync/src/electrum.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lightning-transaction-sync/src/electrum.rs b/lightning-transaction-sync/src/electrum.rs index 78fca1b168f..47489df69bb 100644 --- a/lightning-transaction-sync/src/electrum.rs +++ b/lightning-transaction-sync/src/electrum.rs @@ -22,7 +22,7 @@ use bitcoin::{BlockHash, Script, Transaction, Txid}; use std::collections::HashSet; use std::ops::Deref; -use std::sync::Mutex; +use std::sync::{Arc, Mutex}; use std::time::Instant; /// Synchronizes LDK with a given Electrum server. @@ -43,7 +43,7 @@ where { sync_state: Mutex, queue: Mutex, - client: ElectrumClient, + client: Arc, logger: L, } @@ -53,10 +53,10 @@ where { /// Returns a new [`ElectrumSyncClient`] object. pub fn new(server_url: String, logger: L) -> Result { - let client = ElectrumClient::new(&server_url).map_err(|e| { + let client = Arc::new(ElectrumClient::new(&server_url).map_err(|e| { log_error!(logger, "Failed to connect to electrum server '{}': {}", server_url, e); e - })?; + })?); Self::from_client(client, logger) } @@ -64,7 +64,7 @@ where /// Returns a new [`ElectrumSyncClient`] object using the given Electrum client. /// /// This is not exported to bindings users as the underlying client from BDK is not exported. - pub fn from_client(client: ElectrumClient, logger: L) -> Result { + pub fn from_client(client: Arc, logger: L) -> Result { let sync_state = Mutex::new(SyncState::new()); let queue = Mutex::new(FilterQueue::new()); @@ -489,8 +489,8 @@ where /// Returns a reference to the underlying Electrum client. /// /// This is not exported to bindings users as the underlying client from BDK is not exported. - pub fn client(&self) -> &ElectrumClient { - &self.client + pub fn client(&self) -> Arc { + Arc::clone(&self.client) } }