From 9bfe85fb3a6eb866f5b3874dad768f35435bdf8f Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 10:57:22 +0100 Subject: [PATCH] fix: confirm a spark payout from its transaction when electrum fails --- Bitkit/Managers/SparkManager.swift | 11 +++++++++-- BitkitTests/SparkWithdrawalTests.swift | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Bitkit/Managers/SparkManager.swift b/Bitkit/Managers/SparkManager.swift index 7671fd555..bb0aeff7a 100644 --- a/Bitkit/Managers/SparkManager.swift +++ b/Bitkit/Managers/SparkManager.swift @@ -1625,7 +1625,7 @@ final class SparkManager: ChannelFreeLightningRail { /// Spark lists a withdrawal completed once its provider took the payout on, which can be before the payout transaction /// is broadcast or mined, so a payout's activity is confirmed only once the payout is in a block. The payout address's /// unspent outputs show that first; an output missing there, such as one the contact spent before Bitkit saw it mined, - /// is looked up by its transaction (`isMined`) instead. + /// or a failed read of them, is looked up by its transaction (`isMined`) instead. nonisolated static func confirmMinedPayouts( _ payouts: [ContactPayout], needsConfirmation: (String) async -> Bool, @@ -1636,7 +1636,14 @@ final class SparkManager: ChannelFreeLightningRail { for payout in payouts { guard await needsConfirmation(payout.txId) else { continue } do { - let outputs = try await unspentOutputs(payout.address) + let outputs: [AccountUtxo] + do { + outputs = try await unspentOutputs(payout.address) + } catch { + // A failed Electrum lookup falls through to the transaction's status, as a missing output does. + Logger.warn("Failed to read the payout address of Spark withdrawal '\(payout.txId)': \(error)", context: "SparkManager") + outputs = [] + } let isPayoutMined: Bool = if let output = outputs.first(where: { $0.txid == payout.txId && $0.vout == payout.vout }) { output.confirmations > 0 } else { diff --git a/BitkitTests/SparkWithdrawalTests.swift b/BitkitTests/SparkWithdrawalTests.swift index a7766cec9..74a7ed0b3 100644 --- a/BitkitTests/SparkWithdrawalTests.swift +++ b/BitkitTests/SparkWithdrawalTests.swift @@ -404,6 +404,21 @@ final class SparkWithdrawalTests: XCTestCase { XCTAssertEqual(confirmed, ["spent"]) } + func testAPayoutIsConfirmedFromItsTransactionWhenElectrumFails() async { + let marked = TxIds() + + await SparkManager.confirmMinedPayouts( + [SparkManager.ContactPayout(txId: "mined", vout: 1, address: "bcrt1qcontact")], + needsConfirmation: { _ in true }, + unspentOutputs: { _ in throw URLError(.timedOut) }, + isMined: { $0 == "mined" }, + markConfirmed: { await marked.insert($0) } + ) + + let confirmed = await marked.ids + XCTAssertEqual(confirmed, ["mined"], "a failed Electrum read should fall back to the payout transaction's status") + } + func testAPayoutListedUnspentIsNotAskedAboutByItsTransaction() async { let asked = TxIds()