Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Bitkit/Managers/SparkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Comment thread
ovitrif marked this conversation as resolved.
Resolved
outputs = []
}
let isPayoutMined: Bool = if let output = outputs.first(where: { $0.txid == payout.txId && $0.vout == payout.vout }) {
output.confirmations > 0
} else {
Expand Down
15 changes: 15 additions & 0 deletions BitkitTests/SparkWithdrawalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading