Skip to content

Allow ElectrumSyncClient to reuse a preexisting client #3646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lightning-transaction-sync/src/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -43,7 +43,7 @@ where
{
sync_state: Mutex<SyncState>,
queue: Mutex<FilterQueue>,
client: ElectrumClient,
client: Arc<ElectrumClient>,
logger: L,
}

Expand All @@ -53,18 +53,18 @@ where
{
/// Returns a new [`ElectrumSyncClient`] object.
pub fn new(server_url: String, logger: L) -> Result<Self, TxSyncError> {
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)
}

/// 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<Self, TxSyncError> {
pub fn from_client(client: Arc<ElectrumClient>, logger: L) -> Result<Self, TxSyncError> {
let sync_state = Mutex::new(SyncState::new());
let queue = Mutex::new(FilterQueue::new());

Expand Down Expand Up @@ -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<ElectrumClient> {
Arc::clone(&self.client)
}
}

Expand Down
Loading