From c30bc8cd18ac77f64179a8794a26770c68eb4916 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Thu, 16 Nov 2023 15:59:23 +0100 Subject: [PATCH] bitcoin: optimize spend conflict confirmation lookup Make it possible to break immediately if it's found. --- src/bitcoin/mod.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/bitcoin/mod.rs b/src/bitcoin/mod.rs index 0cf345e4..dc0e7426 100644 --- a/src/bitcoin/mod.rs +++ b/src/bitcoin/mod.rs @@ -259,22 +259,25 @@ impl BitcoinInterface for d::BitcoinD { // If a conflicting transaction was confirmed instead, replace the txid of the // spender for this coin with it and mark it as confirmed. - for txid in &res.conflicting_txs { - if let Some(tx) = tx_getter.get_transaction(txid) { + let conflict = res.conflicting_txs.iter().find_map(|txid| { + tx_getter.get_transaction(txid).and_then(|tx| { // FIXME: if a conflict was mined we should somehow wipe the spend_txid of this // coin. - if let Some(block) = tx.block { + tx.block.and_then(|block| { // Being part of our watchonly wallet isn't enough, as it could be a // conflicting transaction which spends a different set of coins. Make sure // it does actually spend this coin. for txin in tx.tx.input { if &txin.previous_output == op { - spent.push((*op, *txid, block)); - break; + return Some((*txid, block)); } } - } - } + None + }) + }) + }); + if let Some((txid, block)) = conflict { + spent.push((*op, txid, block)); } }