Skip to content

Commit af90f1b

Browse files
committed
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<ElectrumClient>`, 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.
1 parent 7188d5b commit af90f1b

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lightning-transaction-sync/src/electrum.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bitcoin::{BlockHash, Script, Transaction, Txid};
2222

2323
use std::collections::HashSet;
2424
use std::ops::Deref;
25-
use std::sync::Mutex;
25+
use std::sync::{Arc, Mutex};
2626
use std::time::Instant;
2727

2828
/// Synchronizes LDK with a given Electrum server.
@@ -43,7 +43,7 @@ where
4343
{
4444
sync_state: Mutex<SyncState>,
4545
queue: Mutex<FilterQueue>,
46-
client: ElectrumClient,
46+
client: Arc<ElectrumClient>,
4747
logger: L,
4848
}
4949

@@ -53,18 +53,18 @@ where
5353
{
5454
/// Returns a new [`ElectrumSyncClient`] object.
5555
pub fn new(server_url: String, logger: L) -> Result<Self, TxSyncError> {
56-
let client = ElectrumClient::new(&server_url).map_err(|e| {
56+
let client = Arc::new(ElectrumClient::new(&server_url).map_err(|e| {
5757
log_error!(logger, "Failed to connect to electrum server '{}': {}", server_url, e);
5858
e
59-
})?;
59+
})?);
6060

6161
Self::from_client(client, logger)
6262
}
6363

6464
/// Returns a new [`ElectrumSyncClient`] object using the given Electrum client.
6565
///
6666
/// This is not exported to bindings users as the underlying client from BDK is not exported.
67-
pub fn from_client(client: ElectrumClient, logger: L) -> Result<Self, TxSyncError> {
67+
pub fn from_client(client: Arc<ElectrumClient>, logger: L) -> Result<Self, TxSyncError> {
6868
let sync_state = Mutex::new(SyncState::new());
6969
let queue = Mutex::new(FilterQueue::new());
7070

@@ -489,8 +489,8 @@ where
489489
/// Returns a reference to the underlying Electrum client.
490490
///
491491
/// This is not exported to bindings users as the underlying client from BDK is not exported.
492-
pub fn client(&self) -> &ElectrumClient {
493-
&self.client
492+
pub fn client(&self) -> Arc<ElectrumClient> {
493+
Arc::clone(&self.client)
494494
}
495495
}
496496

0 commit comments

Comments
 (0)