Skip to content

Commit 70014d6

Browse files
committed
Implement (cached) Filter for ElectrumRuntimeClient
Currently, we won't have a `Runtime` available when initializing `ChainSource::Electrum`. We therefore isolate any runtime-specific behavior into the `ElectrumRuntimeStatus`. Here, we implement `Filter` for `ElectrumRuntimeClient`, but we need to cache the registrations as they might happen prior to `ElectrumRuntimeClient` becoming available.
1 parent e793b6f commit 70014d6

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

src/chain/electrum.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
use crate::error::Error;
99
use crate::logger::{log_error, LdkLogger, Logger};
1010

11+
use lightning::chain::{Filter, WatchedOutput};
1112
use lightning_transaction_sync::ElectrumSyncClient;
1213

1314
use bdk_electrum::BdkElectrumClient;
1415

1516
use electrum_client::Client as ElectrumClient;
1617
use electrum_client::ConfigBuilder as ElectrumConfigBuilder;
1718

19+
use bitcoin::{Script, Txid};
20+
1821
use std::sync::Arc;
1922

2023
const ELECTRUM_CLIENT_NUM_RETRIES: u8 = 3;
@@ -58,3 +61,12 @@ impl ElectrumRuntimeClient {
5861
Ok(Self { electrum_client, bdk_electrum_client, tx_sync, runtime, logger })
5962
}
6063
}
64+
65+
impl Filter for ElectrumRuntimeClient {
66+
fn register_tx(&self, txid: &Txid, script_pubkey: &Script) {
67+
self.tx_sync.register_tx(txid, script_pubkey)
68+
}
69+
fn register_output(&self, output: WatchedOutput) {
70+
self.tx_sync.register_output(output)
71+
}
72+
}

src/chain/mod.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::types::{Broadcaster, ChainMonitor, ChannelManager, DynStore, Sweeper,
2828
use crate::{Error, NodeMetrics};
2929

3030
use lightning::chain::chaininterface::ConfirmationTarget as LdkConfirmationTarget;
31-
use lightning::chain::{Confirm, Filter, Listen};
31+
use lightning::chain::{Confirm, Filter, Listen, WatchedOutput};
3232
use lightning::util::ser::Writeable;
3333

3434
use lightning_transaction_sync::EsploraSyncClient;
@@ -42,7 +42,7 @@ use bdk_esplora::EsploraAsyncExt;
4242

4343
use esplora_client::AsyncClient as EsploraAsyncClient;
4444

45-
use bitcoin::{FeeRate, Network};
45+
use bitcoin::{FeeRate, Network, Script, ScriptBuf, Txid};
4646

4747
use std::collections::HashMap;
4848
use std::sync::{Arc, Mutex, RwLock};
@@ -111,22 +111,36 @@ impl WalletSyncStatus {
111111

112112
pub(crate) enum ElectrumRuntimeStatus {
113113
Started(Arc<ElectrumRuntimeClient>),
114-
Stopped,
114+
Stopped {
115+
pending_registered_txs: Vec<(Txid, ScriptBuf)>,
116+
pending_registered_outputs: Vec<WatchedOutput>,
117+
},
115118
}
116119

117120
impl ElectrumRuntimeStatus {
118121
pub(crate) fn new() -> Self {
119-
Self::Stopped
122+
let pending_registered_txs = Vec::new();
123+
let pending_registered_outputs = Vec::new();
124+
Self::Stopped { pending_registered_txs, pending_registered_outputs }
120125
}
121126

122127
pub(crate) fn start(
123128
&mut self, server_url: String, runtime: Arc<tokio::runtime::Runtime>, logger: Arc<Logger>,
124129
) -> Result<(), Error> {
125130
match self {
126-
Self::Stopped => {
131+
Self::Stopped { pending_registered_txs, pending_registered_outputs } => {
127132
let client =
128133
Arc::new(ElectrumRuntimeClient::new(server_url.clone(), runtime, logger)?);
129134

135+
// Apply any pending `Filter` entries
136+
for (txid, script_pubkey) in pending_registered_txs.drain(..) {
137+
client.register_tx(&txid, &script_pubkey);
138+
}
139+
140+
for output in pending_registered_outputs.drain(..) {
141+
client.register_output(output)
142+
}
143+
130144
*self = Self::Started(client);
131145
},
132146
Self::Started(_) => {
@@ -140,12 +154,30 @@ impl ElectrumRuntimeStatus {
140154
*self = Self::new()
141155
}
142156

143-
pub(crate) fn client(&self) -> Option<&Arc<ElectrumRuntimeClient>> {
157+
pub(crate) fn client(&self) -> Option<&ElectrumRuntimeClient> {
144158
match self {
145-
Self::Started(client) => Some(&client),
159+
Self::Started(client) => Some(&*client),
146160
Self::Stopped { .. } => None,
147161
}
148162
}
163+
164+
fn register_tx(&mut self, txid: &Txid, script_pubkey: &Script) {
165+
match self {
166+
Self::Started(client) => client.register_tx(txid, script_pubkey),
167+
Self::Stopped { pending_registered_txs, .. } => {
168+
pending_registered_txs.push((*txid, script_pubkey.to_owned()))
169+
},
170+
}
171+
}
172+
173+
fn register_output(&mut self, output: lightning::chain::WatchedOutput) {
174+
match self {
175+
Self::Started(client) => client.register_output(output),
176+
Self::Stopped { pending_registered_outputs, .. } => {
177+
pending_registered_outputs.push(output)
178+
},
179+
}
180+
}
149181
}
150182

151183
pub(crate) enum ChainSource {
@@ -278,7 +310,7 @@ impl ChainSource {
278310
Self::Electrum { server_url, electrum_runtime_status, logger, .. } => {
279311
electrum_runtime_status.write().unwrap().start(
280312
server_url.clone(),
281-
runtime,
313+
Arc::clone(&runtime),
282314
Arc::clone(&logger),
283315
)?;
284316
},
@@ -1261,17 +1293,21 @@ impl ChainSource {
12611293
}
12621294

12631295
impl Filter for ChainSource {
1264-
fn register_tx(&self, txid: &bitcoin::Txid, script_pubkey: &bitcoin::Script) {
1296+
fn register_tx(&self, txid: &Txid, script_pubkey: &Script) {
12651297
match self {
12661298
Self::Esplora { tx_sync, .. } => tx_sync.register_tx(txid, script_pubkey),
1267-
Self::Electrum { .. } => todo!(),
1299+
Self::Electrum { electrum_runtime_status, .. } => {
1300+
electrum_runtime_status.write().unwrap().register_tx(txid, script_pubkey)
1301+
},
12681302
Self::BitcoindRpc { .. } => (),
12691303
}
12701304
}
12711305
fn register_output(&self, output: lightning::chain::WatchedOutput) {
12721306
match self {
12731307
Self::Esplora { tx_sync, .. } => tx_sync.register_output(output),
1274-
Self::Electrum { .. } => todo!(),
1308+
Self::Electrum { electrum_runtime_status, .. } => {
1309+
electrum_runtime_status.write().unwrap().register_output(output)
1310+
},
12751311
Self::BitcoindRpc { .. } => (),
12761312
}
12771313
}

0 commit comments

Comments
 (0)