Skip to content

Commit 0c8d233

Browse files
committed
Dedup confirmed_txs Vec
Previously, we would just push to the `confirmed_txs` `Vec`, leading to redundant `Confirm::transactions_confirmed` calls, especially now that we re-confirm previously disconnected spends. Here, we ensure that we don't push additional `ConfirmTx` entries if already one with matching `Txid` is present. This not only gets rid of the spurious `transactions_confirmed` calls (which are harmless), but more importantly saves us from issuing unnecessary network calls, which improves latency.
1 parent da8a23a commit 0c8d233

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lightning-transaction-sync/src/electrum.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ where
257257

258258
// First, check the confirmation status of registered transactions as well as the
259259
// status of dependent transactions of registered outputs.
260-
let mut confirmed_txs = Vec::new();
260+
let mut confirmed_txs: Vec<ConfirmedTx> = Vec::new();
261261
let mut watched_script_pubkeys = Vec::with_capacity(
262262
sync_state.watched_transactions.len() + sync_state.watched_outputs.len());
263263
let mut watched_txs = Vec::with_capacity(sync_state.watched_transactions.len());
@@ -305,6 +305,9 @@ where
305305

306306
for (i, script_history) in tx_results.iter().enumerate() {
307307
let (txid, tx) = &watched_txs[i];
308+
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == **txid) {
309+
continue;
310+
}
308311
let mut filtered_history = script_history.iter().filter(|h| h.tx_hash == **txid);
309312
if let Some(history) = filtered_history.next()
310313
{
@@ -324,6 +327,10 @@ where
324327
}
325328

326329
let txid = possible_output_spend.tx_hash;
330+
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == txid) {
331+
continue;
332+
}
333+
327334
match self.client.transaction_get(&txid) {
328335
Ok(tx) => {
329336
let mut is_spend = false;

lightning-transaction-sync/src/esplora.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,12 @@ where
267267
// First, check the confirmation status of registered transactions as well as the
268268
// status of dependent transactions of registered outputs.
269269

270-
let mut confirmed_txs = Vec::new();
270+
let mut confirmed_txs: Vec<ConfirmedTx> = Vec::new();
271271

272272
for txid in &sync_state.watched_transactions {
273+
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == *txid) {
274+
continue;
275+
}
273276
if let Some(confirmed_tx) = maybe_await!(self.get_confirmed_tx(&txid, None, None))? {
274277
confirmed_txs.push(confirmed_tx);
275278
}
@@ -280,6 +283,9 @@ where
280283
.get_output_status(&output.outpoint.txid, output.outpoint.index as u64))?
281284
{
282285
if let Some(spending_txid) = output_status.txid {
286+
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == spending_txid) {
287+
continue;
288+
}
283289
if let Some(spending_tx_status) = output_status.status {
284290
if let Some(confirmed_tx) = maybe_await!(self
285291
.get_confirmed_tx(

0 commit comments

Comments
 (0)