From 82ec5993808f421c3c4328e8e74c1e40c73bd670 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 11:04:02 +0100 Subject: [PATCH 1/7] chore: bump breez sdk to 0.28.2 --- Bitkit.xcodeproj/project.pbxproj | 2 +- .../project.xcworkspace/xcshareddata/swiftpm/Package.resolved | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Bitkit.xcodeproj/project.pbxproj b/Bitkit.xcodeproj/project.pbxproj index c34b63eb8..91f26a99f 100644 --- a/Bitkit.xcodeproj/project.pbxproj +++ b/Bitkit.xcodeproj/project.pbxproj @@ -1262,7 +1262,7 @@ repositoryURL = "https://github.com/breez/breez-sdk-spark-swift"; requirement = { kind = exactVersion; - version = 0.28.0; + version = 0.28.2; }; }; 968FE13E2DFB016B0053CD7F /* XCRemoteSwiftPackageReference "ldk-node" */ = { diff --git a/Bitkit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Bitkit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index e951aa16e..d0fcdd4a9 100644 --- a/Bitkit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Bitkit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -15,8 +15,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/breez/breez-sdk-spark-swift", "state" : { - "revision" : "09081e9f2862a0359df154aa6813242d36a56275", - "version" : "0.28.0" + "revision" : "063f6ad1184674b77400c3a8f8e83bd9cd35dfd0", + "version" : "0.28.2" } }, { From 8150b14807207aa0ce080a289c531eb109ad9e23 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 12:26:16 +0100 Subject: [PATCH 2/7] fix: wait a check before rebuilding over a step one read left unverified --- .../Services/SparkExit/SparkExitDriver.swift | 24 +++++++++---- BitkitTests/SparkExitTests.swift | 36 +++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/Bitkit/Services/SparkExit/SparkExitDriver.swift b/Bitkit/Services/SparkExit/SparkExitDriver.swift index 3f07dfdea..a15b39c8a 100644 --- a/Bitkit/Services/SparkExit/SparkExitDriver.swift +++ b/Bitkit/Services/SparkExit/SparkExitDriver.swift @@ -625,8 +625,10 @@ struct SparkExitDriver { return } // A step the SDK could not read is settled by a fresh build, once a block, unless a step is still to send: - // a build now would conflict with it at the same fee. - if let tip, isUnverifiedRebuildDue(checked.recovery, record: record, tip: tip) { + // a build now would conflict with it at the same fee. A check marks a step unverified when one chain read + // fails and clears it on the next clean check, so only a step that was already unverified before this check + // rebuilds; one this check just turned unverified waits for the next check, as it may be in flight. + if let tip, isUnverifiedRebuildDue(checked.recovery, stored: stored, record: record, tip: tip) { Logger.warn("Spark exit has a step the SDK could not read, rebuilding over it", context: "SparkExitDriver") record.unverifiedRebuiltAtHeight = tip try save(record) @@ -867,10 +869,18 @@ struct SparkExitDriver { try await rebuild(&record, recovering: recovering) } - /// A unilateral step the SDK could not read, nothing unilateral ready to send, and no such rebuild at this tip yet. - private func isUnverifiedRebuildDue(_ recovery: RecoverFundsResponse, record: SparkExitRecord, tip: UInt32) -> Bool { + /// A unilateral step the SDK could not read, at this check and the one before, nothing unilateral ready to send, + /// and no such rebuild at this tip yet. + private func isUnverifiedRebuildDue( + _ recovery: RecoverFundsResponse, + stored: RecoverFundsResponse, + record: SparkExitRecord, + tip: UInt32 + ) -> Bool { let unilateral = recovery.transactions.filter { $0.kind != .cooperative } - return unilateral.contains { $0.status == .unverified } && !unilateral.contains { $0.status == .ready } + let unreadBefore = Set(stored.transactions.filter { $0.status == .unverified }.map(\.txid)) + return unilateral.contains { $0.status == .unverified && unreadBefore.contains($0.txid) } + && !unilateral.contains { $0.status == .ready } && record.unverifiedRebuiltAtHeight != tip } @@ -1316,8 +1326,8 @@ struct SparkExitDriver { // MARK: Broadcast - /// A unilateral step the SDK could not read is never sent: a rebuild settles it. A cooperative one is, since nodes - /// refuse it if its funds already moved. + /// A unilateral step the SDK could not read is never sent: the next clean check, or a rebuild once it stays unread, + /// settles it. A cooperative one is, since nodes refuse it if its funds already moved. private func broadcastReady(_ record: inout SparkExitRecord, tip: UInt32? = nil) async throws { guard let recovery = record.recovery?.response else { return } let confirmed = Set(recovery.transactions.filter(\.isConfirmed).map(\.txid)) diff --git a/BitkitTests/SparkExitTests.swift b/BitkitTests/SparkExitTests.swift index bff9dcb48..25babe38b 100644 --- a/BitkitTests/SparkExitTests.swift +++ b/BitkitTests/SparkExitTests.swift @@ -1606,6 +1606,42 @@ final class SparkExitDriverTests: XCTestCase { XCTAssertEqual(chain.broadcasts.map(\.txid), ["n2b"], "the unverified step is still not sent") } + /// A ready step that one failed chain read turns unverified may already be sent, so it is not rebuilt over: the + /// exit waits for the next check, which reads it again, and rebuilds only if it stays unread. + func testAStepAFailedReadTurnsUnverifiedWaitsForTheNextCheckInsteadOfRebuilding() async throws { + var record = SparkExitFixtures.record(phase: .running) + let ready = SparkExitFixtures.recovery([ + SparkExitFixtures.tx("n1", status: .confirmed(blockHeight: 10)), + SparkExitFixtures.tx("n2", cpfp: "03c", dependsOn: ["n1"]), + ]) + let unread = SparkExitFixtures.recovery([ + SparkExitFixtures.tx("n1", status: .confirmed(blockHeight: 10)), + SparkExitFixtures.tx("n2", cpfp: "03c", dependsOn: ["n1"], status: .unverified), + ]) + record.adopt(ready, at: clock) + sdk.checks = [CheckRecoverFundsResponse(recovery: unread, verdict: .valid)] + + record = try await driver().tick(record) + + XCTAssertTrue(sdk.recoverRequests.isEmpty, "rebuilt over a step one failed read turned unverified") + XCTAssertNil(record.unverifiedRebuiltAtHeight) + XCTAssertNil(record.isUnverifiedAsk) + XCTAssertEqual(record.generation, 1) + XCTAssertTrue(chain.broadcasts.isEmpty, "the unverified step was sent") + + // Still unread at the next check: now it rebuilds. + sdk.checks = [CheckRecoverFundsResponse(recovery: unread, verdict: .valid)] + sdk.builds = [SparkExitFixtures.recovery([ + SparkExitFixtures.tx("n1", status: .confirmed(blockHeight: 10)), + SparkExitFixtures.tx("n2b", cpfp: "03d", dependsOn: ["n1"]), + ])] + record = try await driver().tick(record) + + XCTAssertEqual(sdk.recoverRequests.count, 1) + XCTAssertEqual(record.unverifiedRebuiltAtHeight, 1000) + XCTAssertEqual(record.generation, 2) + } + /// An unverified rebuild that builds only failed cooperative leaves, beside the leaf it could not read, keeps the /// valid generation: its signed steps are still sent once the chain reads again. func testAnUnverifiedRebuildOfOnlyFailedLeavesKeepsTheValidGeneration() async throws { From bfa6268a2f79ab42f753aacc193eaa2231aeb153 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 12:53:03 +0100 Subject: [PATCH 3/7] fix: hold an unverified exit rebuild while any step just turned unverified --- .../Services/SparkExit/SparkExitDriver.swift | 10 ++++--- BitkitTests/SparkExitTests.swift | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Bitkit/Services/SparkExit/SparkExitDriver.swift b/Bitkit/Services/SparkExit/SparkExitDriver.swift index a15b39c8a..a2b23af77 100644 --- a/Bitkit/Services/SparkExit/SparkExitDriver.swift +++ b/Bitkit/Services/SparkExit/SparkExitDriver.swift @@ -627,7 +627,7 @@ struct SparkExitDriver { // A step the SDK could not read is settled by a fresh build, once a block, unless a step is still to send: // a build now would conflict with it at the same fee. A check marks a step unverified when one chain read // fails and clears it on the next clean check, so only a step that was already unverified before this check - // rebuilds; one this check just turned unverified waits for the next check, as it may be in flight. + // rebuilds, and one this check just turned unverified holds it until the next check, as it may be in flight. if let tip, isUnverifiedRebuildDue(checked.recovery, stored: stored, record: record, tip: tip) { Logger.warn("Spark exit has a step the SDK could not read, rebuilding over it", context: "SparkExitDriver") record.unverifiedRebuiltAtHeight = tip @@ -869,8 +869,9 @@ struct SparkExitDriver { try await rebuild(&record, recovering: recovering) } - /// A unilateral step the SDK could not read, at this check and the one before, nothing unilateral ready to send, - /// and no such rebuild at this tip yet. + /// Unilateral steps the SDK could not read, each one at this check and the one before, nothing unilateral ready to + /// send, and no such rebuild at this tip yet. A step this check just turned unverified may be in flight, so it holds + /// the rebuild as a ready one does. private func isUnverifiedRebuildDue( _ recovery: RecoverFundsResponse, stored: RecoverFundsResponse, @@ -879,7 +880,8 @@ struct SparkExitDriver { ) -> Bool { let unilateral = recovery.transactions.filter { $0.kind != .cooperative } let unreadBefore = Set(stored.transactions.filter { $0.status == .unverified }.map(\.txid)) - return unilateral.contains { $0.status == .unverified && unreadBefore.contains($0.txid) } + let unread = unilateral.filter { $0.status == .unverified } + return !unread.isEmpty && unread.allSatisfy { unreadBefore.contains($0.txid) } && !unilateral.contains { $0.status == .ready } && record.unverifiedRebuiltAtHeight != tip } diff --git a/BitkitTests/SparkExitTests.swift b/BitkitTests/SparkExitTests.swift index 25babe38b..f5e4c262c 100644 --- a/BitkitTests/SparkExitTests.swift +++ b/BitkitTests/SparkExitTests.swift @@ -1642,6 +1642,33 @@ final class SparkExitDriverTests: XCTestCase { XCTAssertEqual(record.generation, 2) } + /// A step one failed read turns unverified holds the rebuild over another leaf's step that was already unread: the + /// rebuild would replace it while it may be in flight. + func testAStepAFailedReadTurnsUnverifiedHoldsTheRebuildOverAnotherUnreadLeaf() async throws { + var record = SparkExitFixtures.record(phase: .running) + record.leafIds = ["leaf-1", "leaf-2"] + let stored = SparkExitFixtures.recovery([ + SparkExitFixtures.tx("n1", status: .confirmed(blockHeight: 900)), + SparkExitFixtures.tx("n2", dependsOn: ["n1"], status: .unverified), + SparkExitFixtures.tx("m1", status: .confirmed(blockHeight: 900)), + SparkExitFixtures.tx("m2", dependsOn: ["m1"]), + ]) + let checked = SparkExitFixtures.recovery([ + SparkExitFixtures.tx("n1", status: .confirmed(blockHeight: 900)), + SparkExitFixtures.tx("n2", dependsOn: ["n1"], status: .unverified), + SparkExitFixtures.tx("m1", status: .confirmed(blockHeight: 900)), + SparkExitFixtures.tx("m2", dependsOn: ["m1"], status: .unverified), + ]) + record.adopt(stored, at: clock) + sdk.checks = [CheckRecoverFundsResponse(recovery: checked, verdict: .valid)] + + record = try await driver().tick(record) + + XCTAssertTrue(sdk.recoverRequests.isEmpty, "rebuilt over m2, which one failed read turned unverified") + XCTAssertNil(record.unverifiedRebuiltAtHeight) + XCTAssertEqual(record.generation, 1) + } + /// An unverified rebuild that builds only failed cooperative leaves, beside the leaf it could not read, keeps the /// valid generation: its signed steps are still sent once the chain reads again. func testAnUnverifiedRebuildOfOnlyFailedLeavesKeepsTheValidGeneration() async throws { From 0869a9e34f655ead317c3d63d8a99c7f3e761150 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 13:25:06 +0100 Subject: [PATCH 4/7] fix: skip a funded unverified exit rebuild once its step reads again --- .../Services/SparkExit/SparkExitDriver.swift | 10 +++++++++- BitkitTests/SparkExitTests.swift | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Bitkit/Services/SparkExit/SparkExitDriver.swift b/Bitkit/Services/SparkExit/SparkExitDriver.swift index a2b23af77..5a409de13 100644 --- a/Bitkit/Services/SparkExit/SparkExitDriver.swift +++ b/Bitkit/Services/SparkExit/SparkExitDriver.swift @@ -149,7 +149,15 @@ struct SparkExitDriver { // generation stays over a failed-only build, running again with the funding adopted. Once a // block, like the check's rebuild: the valid generation's retry clock does not pace it. let tip = try? await chain.tipHeight() - if tip == nil || record.unverifiedRebuiltAtHeight != tip { + let unilateral = (record.recovery?.response.transactions ?? []).filter { $0.kind != .cooperative } + if unilateral.contains(where: { $0.status == .ready }) || !unilateral.contains(where: { $0.status == .unverified }) { + // A clean check while the payment came can read the unread step again, and it is then sent: + // a rebuild now would conflict with it at the same fee. The ask ends, and the funding it + // adopted stays for a later rebuild or the leftover sweep. + Logger.info("Spark exit steps read again: the funded unverified rebuild is not needed", context: "SparkExitDriver") + record.phase = .running + record.isUnverifiedAsk = nil + } else if tip == nil || record.unverifiedRebuiltAtHeight != tip { record.unverifiedRebuiltAtHeight = tip var valid = record valid.phase = .running diff --git a/BitkitTests/SparkExitTests.swift b/BitkitTests/SparkExitTests.swift index f5e4c262c..6d0a1bfa1 100644 --- a/BitkitTests/SparkExitTests.swift +++ b/BitkitTests/SparkExitTests.swift @@ -1829,6 +1829,26 @@ final class SparkExitDriverTests: XCTestCase { return (record, readable, failure) } + /// A savings payment keeps an unverified rebuild's ask open; a clean check while it confirms reads the unread step + /// as ready, and it is sent. The funded rebuild then never runs: it would conflict with that step at the same fee. + func testAFundedUnverifiedAskWhoseStepReadsAgainEndsWithoutARebuild() async throws { + var (record, readable, _) = try await unverifiedAsk() + let paid = String(repeating: "f", count: 64) + record.fundingPaymentTxid = paid + let builds = sdk.recoverRequests.count + sdk.checks = [CheckRecoverFundsResponse(recovery: readable, verdict: .valid)] + chain.utxosByAddress[record.funding[0].address] = [SparkExitFixtures.utxo(paid, value: 50000)] + chain.tip = 1001 + + record = try await driver().tick(record) + + XCTAssertEqual(sdk.recoverRequests.count, builds, "rebuilt over n2, which the clean check made ready and sent") + XCTAssertEqual(chain.broadcasts.map(\.txid), ["n2"]) + XCTAssertEqual(record.phase, .running) + XCTAssertNil(record.isUnverifiedAsk) + XCTAssertEqual(record.generation, 1) + } + /// Confirmed funding for an unverified rebuild's fee ask resumes that rebuild with its guard: a build of only failed /// cooperative leaves beside the waiting leaf is never adopted, and the exit runs on with the valid generation. func testFundingAnUnverifiedRebuildsAskKeepsTheValidGenerationOverAFailedOnlyBuild() async throws { From cf14f5e77d2466f87ae11bec01b33e6bef991146 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 13:28:07 +0100 Subject: [PATCH 5/7] docs: say when an unread exit step's rebuild runs --- Bitkit/Services/SparkExit/SparkExitDriver.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Bitkit/Services/SparkExit/SparkExitDriver.swift b/Bitkit/Services/SparkExit/SparkExitDriver.swift index 5a409de13..3ed9724ea 100644 --- a/Bitkit/Services/SparkExit/SparkExitDriver.swift +++ b/Bitkit/Services/SparkExit/SparkExitDriver.swift @@ -635,7 +635,8 @@ struct SparkExitDriver { // A step the SDK could not read is settled by a fresh build, once a block, unless a step is still to send: // a build now would conflict with it at the same fee. A check marks a step unverified when one chain read // fails and clears it on the next clean check, so only a step that was already unverified before this check - // rebuilds, and one this check just turned unverified holds it until the next check, as it may be in flight. + // rebuilds, and one this check just turned unverified holds it, as it may be in flight. A step that stays + // unread rebuilds at the first check, once a block at most, where no other step just turned unverified. if let tip, isUnverifiedRebuildDue(checked.recovery, stored: stored, record: record, tip: tip) { Logger.warn("Spark exit has a step the SDK could not read, rebuilding over it", context: "SparkExitDriver") record.unverifiedRebuiltAtHeight = tip From d5940383c10f398177582ab4489c4dce669459f5 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 13:55:31 +0100 Subject: [PATCH 6/7] fix: hold a funded spark exit rebuild over a step sent on an earlier check --- .../Services/SparkExit/SparkExitDriver.swift | 4 +++ BitkitTests/SparkExitTests.swift | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/Bitkit/Services/SparkExit/SparkExitDriver.swift b/Bitkit/Services/SparkExit/SparkExitDriver.swift index 3ed9724ea..7273b88f9 100644 --- a/Bitkit/Services/SparkExit/SparkExitDriver.swift +++ b/Bitkit/Services/SparkExit/SparkExitDriver.swift @@ -150,6 +150,7 @@ struct SparkExitDriver { // block, like the check's rebuild: the valid generation's retry clock does not pace it. let tip = try? await chain.tipHeight() let unilateral = (record.recovery?.response.transactions ?? []).filter { $0.kind != .cooperative } + let unreadBefore = Set((before.recovery?.response.transactions ?? []).filter { $0.status == .unverified }.map(\.txid)) if unilateral.contains(where: { $0.status == .ready }) || !unilateral.contains(where: { $0.status == .unverified }) { // A clean check while the payment came can read the unread step again, and it is then sent: // a rebuild now would conflict with it at the same fee. The ask ends, and the funding it @@ -157,6 +158,9 @@ struct SparkExitDriver { Logger.info("Spark exit steps read again: the funded unverified rebuild is not needed", context: "SparkExitDriver") record.phase = .running record.isUnverifiedAsk = nil + } else if unilateral.contains(where: { step in step.status == .unverified && !unreadBefore.contains(step.txid) }) { + // A step this check just turned unverified may have been sent on an earlier clean check: the + // resume waits for the next check, as the check's own rebuild does. } else if tip == nil || record.unverifiedRebuiltAtHeight != tip { record.unverifiedRebuiltAtHeight = tip var valid = record diff --git a/BitkitTests/SparkExitTests.swift b/BitkitTests/SparkExitTests.swift index 6d0a1bfa1..b311a56f9 100644 --- a/BitkitTests/SparkExitTests.swift +++ b/BitkitTests/SparkExitTests.swift @@ -1849,6 +1849,32 @@ final class SparkExitDriverTests: XCTestCase { XCTAssertEqual(record.generation, 1) } + /// A step that a clean check sent while the payment was unconfirmed holds the funded rebuild when the tick that adopts + /// the payment fails to read it again: the resume waits for the next check. + func testAFundedUnverifiedAskHoldsItsRebuildOverAStepSentOnAnEarlierCleanCheck() async throws { + var (record, readable, _) = try await unverifiedAsk() + let unread = try XCTUnwrap(record.recovery?.response) + let paid = String(repeating: "f", count: 64) + record.fundingPaymentTxid = paid + let builds = sdk.recoverRequests.count + sdk.checks = [CheckRecoverFundsResponse(recovery: readable, verdict: .valid)] + chain.utxosByAddress[record.funding[0].address] = [SparkExitFixtures.utxo(paid, value: 50000, confirmed: false)] + + record = try await driver().tick(record) + XCTAssertEqual(chain.broadcasts.map(\.txid), ["n2"]) + XCTAssertEqual(record.isUnverifiedAsk, true) + + sdk.checks = [CheckRecoverFundsResponse(recovery: unread, verdict: .valid)] + chain.utxosByAddress[record.funding[0].address] = [SparkExitFixtures.utxo(paid, value: 50000)] + chain.tip = 1001 + + record = try await driver().tick(record) + + XCTAssertEqual(sdk.recoverRequests.count, builds, "rebuilt over n2, sent on the previous tick") + XCTAssertEqual(record.isUnverifiedAsk, true) + XCTAssertEqual(record.generation, 1) + } + /// Confirmed funding for an unverified rebuild's fee ask resumes that rebuild with its guard: a build of only failed /// cooperative leaves beside the waiting leaf is never adopted, and the exit runs on with the valid generation. func testFundingAnUnverifiedRebuildsAskKeepsTheValidGenerationOverAFailedOnlyBuild() async throws { From 31f88487116070647bde156cf6907a55efdb5d9a Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 12 Nov 2026 14:23:07 +0100 Subject: [PATCH 7/7] test: check the held spark exit resume adopted its payment --- BitkitTests/SparkExitTests.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/BitkitTests/SparkExitTests.swift b/BitkitTests/SparkExitTests.swift index b311a56f9..d21315235 100644 --- a/BitkitTests/SparkExitTests.swift +++ b/BitkitTests/SparkExitTests.swift @@ -1870,6 +1870,7 @@ final class SparkExitDriverTests: XCTestCase { record = try await driver().tick(record) + XCTAssertTrue(record.funding.contains { $0.txid == paid }, "the payment was not adopted, so the hold never ran") XCTAssertEqual(sdk.recoverRequests.count, builds, "rebuilt over n2, sent on the previous tick") XCTAssertEqual(record.isUnverifiedAsk, true) XCTAssertEqual(record.generation, 1)