Skip to content

Commit 68c75dd

Browse files
committed
f Cache Txid in ConfirmedTx to avoid superfluous hashing operations
1 parent afdb559 commit 68c75dd

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

lightning-transaction-sync/src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl FilterQueue {
128128
#[derive(Debug)]
129129
pub(crate) struct ConfirmedTx {
130130
pub tx: Transaction,
131+
pub txid: Txid,
131132
pub block_header: Header,
132133
pub block_height: u32,
133134
pub pos: usize,

lightning-transaction-sync/src/electrum.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ 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) {
308+
if confirmed_txs.iter().any(|ctx| ctx.txid == **txid) {
309309
continue;
310310
}
311311
let mut filtered_history = script_history.iter().filter(|h| h.tx_hash == **txid);
@@ -327,7 +327,7 @@ where
327327
}
328328

329329
let txid = possible_output_spend.tx_hash;
330-
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == txid) {
330+
if confirmed_txs.iter().any(|ctx| ctx.txid == txid) {
331331
continue;
332332
}
333333

@@ -426,6 +426,7 @@ where
426426
}
427427
let confirmed_tx = ConfirmedTx {
428428
tx: tx.clone(),
429+
txid,
429430
block_header, block_height: prob_conf_height,
430431
pos,
431432
};

lightning-transaction-sync/src/esplora.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ where
270270
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) {
273+
if confirmed_txs.iter().any(|ctx| ctx.txid == *txid) {
274274
continue;
275275
}
276276
if let Some(confirmed_tx) = maybe_await!(self.get_confirmed_tx(&txid, None, None))? {
@@ -284,7 +284,7 @@ where
284284
{
285285
if let Some(spending_txid) = output_status.txid {
286286
if let Some(spending_tx_status) = output_status.status {
287-
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == spending_txid) {
287+
if confirmed_txs.iter().any(|ctx| ctx.txid == spending_txid) {
288288
if spending_tx_status.confirmed {
289289
// Skip inserting duplicate ConfirmedTx entry
290290
continue;
@@ -342,14 +342,15 @@ where
342342
// unwrap() safety: len() > 0 is checked above
343343
let pos = *indexes.first().unwrap() as usize;
344344
if let Some(tx) = maybe_await!(self.client.get_tx(&txid))? {
345+
let txid = tx.txid();
345346
if let Some(block_height) = known_block_height {
346347
// We can take a shortcut here if a previous call already gave us the height.
347-
return Ok(Some(ConfirmedTx { tx, block_header, pos, block_height }));
348+
return Ok(Some(ConfirmedTx { tx, txid, block_header, pos, block_height }));
348349
}
349350

350351
let block_status = maybe_await!(self.client.get_block_status(&block_hash))?;
351352
if let Some(block_height) = block_status.height {
352-
return Ok(Some(ConfirmedTx { tx, block_header, pos, block_height }));
353+
return Ok(Some(ConfirmedTx { tx, txid, block_header, pos, block_height }));
353354
} else {
354355
// If any previously-confirmed block suddenly is no longer confirmed, we found
355356
// an inconsistency and should start over.

0 commit comments

Comments
 (0)