Skip to content

Commit

Permalink
f Cache Txid in ConfirmedTx to avoid superfluous hashing operations
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Mar 21, 2024
1 parent afdb559 commit 68c75dd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions lightning-transaction-sync/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl FilterQueue {
#[derive(Debug)]
pub(crate) struct ConfirmedTx {
pub tx: Transaction,
pub txid: Txid,
pub block_header: Header,
pub block_height: u32,
pub pos: usize,
Expand Down
5 changes: 3 additions & 2 deletions lightning-transaction-sync/src/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ where

for (i, script_history) in tx_results.iter().enumerate() {
let (txid, tx) = &watched_txs[i];
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == **txid) {
if confirmed_txs.iter().any(|ctx| ctx.txid == **txid) {
continue;
}
let mut filtered_history = script_history.iter().filter(|h| h.tx_hash == **txid);
Expand All @@ -327,7 +327,7 @@ where
}

let txid = possible_output_spend.tx_hash;
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == txid) {
if confirmed_txs.iter().any(|ctx| ctx.txid == txid) {
continue;
}

Expand Down Expand Up @@ -426,6 +426,7 @@ where
}
let confirmed_tx = ConfirmedTx {
tx: tx.clone(),
txid,
block_header, block_height: prob_conf_height,
pos,
};
Expand Down
9 changes: 5 additions & 4 deletions lightning-transaction-sync/src/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ where
let mut confirmed_txs: Vec<ConfirmedTx> = Vec::new();

for txid in &sync_state.watched_transactions {
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == *txid) {
if confirmed_txs.iter().any(|ctx| ctx.txid == *txid) {
continue;
}
if let Some(confirmed_tx) = maybe_await!(self.get_confirmed_tx(&txid, None, None))? {
Expand All @@ -284,7 +284,7 @@ where
{
if let Some(spending_txid) = output_status.txid {
if let Some(spending_tx_status) = output_status.status {
if confirmed_txs.iter().any(|ctx| ctx.tx.txid() == spending_txid) {
if confirmed_txs.iter().any(|ctx| ctx.txid == spending_txid) {
if spending_tx_status.confirmed {
// Skip inserting duplicate ConfirmedTx entry
continue;
Expand Down Expand Up @@ -342,14 +342,15 @@ where
// unwrap() safety: len() > 0 is checked above
let pos = *indexes.first().unwrap() as usize;
if let Some(tx) = maybe_await!(self.client.get_tx(&txid))? {
let txid = tx.txid();
if let Some(block_height) = known_block_height {
// We can take a shortcut here if a previous call already gave us the height.
return Ok(Some(ConfirmedTx { tx, block_header, pos, block_height }));
return Ok(Some(ConfirmedTx { tx, txid, block_header, pos, block_height }));
}

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

0 comments on commit 68c75dd

Please sign in to comment.