From 12ff24f79cfa2dc21ef6a826e0d6a4137075d1a2 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 11:25:40 +0100 Subject: [PATCH 01/15] fix: hold the funding ask while a savings send is unresolved Progress no longer shows Amount to pay or the funding address while a savings send of the fees may still land; it says to wait for it. Verified: SparkExitReviewTests, SparkExitFundingTests, OnchainSendAttemptServiceTests on the simulator, swiftformat, validate-translations --- Bitkit/Managers/SparkExitManager.swift | 14 ++++++++++++++ .../Localization/ar.lproj/Localizable.strings | 1 + .../Localization/ca.lproj/Localizable.strings | 1 + .../Localization/cs.lproj/Localizable.strings | 1 + .../Localization/de.lproj/Localizable.strings | 1 + .../Localization/el.lproj/Localizable.strings | 1 + .../Localization/en.lproj/Localizable.strings | 1 + .../es-419.lproj/Localizable.strings | 1 + .../Localization/es.lproj/Localizable.strings | 1 + .../Localization/fr.lproj/Localizable.strings | 1 + .../Localization/it.lproj/Localizable.strings | 1 + .../Localization/nl.lproj/Localizable.strings | 1 + .../Localization/pl.lproj/Localizable.strings | 1 + .../pt-BR.lproj/Localizable.strings | 1 + .../Localization/pt.lproj/Localizable.strings | 1 + .../Localization/ru.lproj/Localizable.strings | 1 + .../Settings/Advanced/SparkExitViews.swift | 19 +++++++++++++++---- BitkitTests/SparkExitReviewTests.swift | 10 ++++++++++ changelog.d/next/1451.fixed.md | 1 + 19 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 changelog.d/next/1451.fixed.md diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 5eeffdb33..fd9eb8a61 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -354,6 +354,20 @@ final class SparkExitManager { record.map { Self.isSavingsPaymentRefused($0, attempt: refusedSavingsPayment) } ?? false } + /// A savings send to the funding address went out and is not known accepted or refused yet: it may still land. + var isSavingsSendLanding: Bool { + record.map { + Self.isSavingsSendLanding($0, unresolvedSendAddress: unresolvedSendAddress, isRefused: isSavingsPaymentRefused) + } ?? false + } + + /// While a savings send to `record`'s funding address may still land, Progress holds the funding ask, as retries + /// and cancel do: paying the same fees from another wallet as well would pay them twice. + nonisolated static func isSavingsSendLanding(_ record: SparkExitRecord, unresolvedSendAddress: String?, isRefused: Bool) -> Bool { + guard !isRefused, record.fundingShortfallSats != nil, let unresolvedSendAddress else { return false } + return record.funding.contains { $0.address == unresolvedSendAddress } + } + /// A node refused the savings payment of `record`'s fee funding, and none was adopted since: only before the exit /// is funded, for the payment the start made. nonisolated static func isSavingsPaymentRefused(_ record: SparkExitRecord, attempt: OnchainSendAttempt?) -> Bool { diff --git a/Bitkit/Resources/Localization/ar.lproj/Localizable.strings b/Bitkit/Resources/Localization/ar.lproj/Localizable.strings index 8843b0b0e..b464c32e9 100644 --- a/Bitkit/Resources/Localization/ar.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/ar.lproj/Localizable.strings @@ -349,6 +349,7 @@ "lightning__spark_exit__fees" = "رسوم الشبكة"; "lightning__spark_exit__receive" = "ستستلم (تقديريًا)"; "lightning__spark_exit__savings_refused" = "لم يكتمل دفعك من المدخرات. ادفع مرة أخرى، أو موّل من محفظة أخرى."; +"lightning__spark_exit__savings_unresolved" = "لم يتأكد دفعك من المدخرات بعد. انتظره قبل أن تدفع من محفظة أخرى."; "lightning__spark_exit__quote_loading" = "جارٍ الحصول على التكاليف…"; "lightning__spark_exit__quote_settling" = "بانتظار أن ينتهي Spark من نقل رصيدك. قد يستغرق ذلك حتى 5 دقائق."; "lightning__spark_exit__quote_error" = "تعذّر الحصول على تكاليف التحويل. حاول مرة أخرى."; diff --git a/Bitkit/Resources/Localization/ca.lproj/Localizable.strings b/Bitkit/Resources/Localization/ca.lproj/Localizable.strings index b57827a2b..3c3d7d2d3 100644 --- a/Bitkit/Resources/Localization/ca.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/ca.lproj/Localizable.strings @@ -386,6 +386,7 @@ "lightning__spark_exit__fees" = "Comissions de xarxa"; "lightning__spark_exit__receive" = "Rebràs (estimació)"; "lightning__spark_exit__savings_refused" = "El teu pagament dels estalvis no s'ha completat. Torna a pagar o finança des d'un altre moneder."; +"lightning__spark_exit__savings_unresolved" = "El teu pagament dels estalvis encara no s'ha confirmat. Espera'l abans de pagar des d'un altre moneder."; "lightning__spark_exit__quote_loading" = "Obtenint els costos…"; "lightning__spark_exit__quote_settling" = "Esperant que Spark acabi de moure el teu saldo. Pot trigar fins a 5 minuts."; "lightning__spark_exit__quote_error" = "No s'han pogut obtenir els costos de la transferència. Torna-ho a intentar."; diff --git a/Bitkit/Resources/Localization/cs.lproj/Localizable.strings b/Bitkit/Resources/Localization/cs.lproj/Localizable.strings index a1d8df37d..2e0f07a64 100644 --- a/Bitkit/Resources/Localization/cs.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/cs.lproj/Localizable.strings @@ -401,6 +401,7 @@ "lightning__spark_exit__fees" = "Síťové poplatky"; "lightning__spark_exit__receive" = "Obdržíte (odhad)"; "lightning__spark_exit__savings_refused" = "Vaše platba z úspor neprošla. Zaplaťte znovu, nebo financujte z jiné peněženky."; +"lightning__spark_exit__savings_unresolved" = "Vaše platba z úspor ještě není potvrzena. Počkejte na ni, než zaplatíte z jiné peněženky."; "lightning__spark_exit__quote_loading" = "Zjišťuji náklady…"; "lightning__spark_exit__quote_settling" = "Čekáme, až Spark dokončí přesun vašeho zůstatku. Může to trvat až 5 minut."; "lightning__spark_exit__quote_error" = "Náklady na převod se nepodařilo zjistit. Zkuste to znovu."; diff --git a/Bitkit/Resources/Localization/de.lproj/Localizable.strings b/Bitkit/Resources/Localization/de.lproj/Localizable.strings index 53e150e75..5f7c2c34b 100644 --- a/Bitkit/Resources/Localization/de.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/de.lproj/Localizable.strings @@ -399,6 +399,7 @@ "lightning__spark_exit__fees" = "Netzwerkgebühren"; "lightning__spark_exit__receive" = "Du erhältst (Schätzung)"; "lightning__spark_exit__savings_refused" = "Deine Zahlung vom Sparkonto ist nicht durchgegangen. Zahle erneut oder finanziere aus einer anderen Wallet."; +"lightning__spark_exit__savings_unresolved" = "Deine Zahlung vom Sparkonto ist noch nicht bestätigt. Warte darauf, bevor du aus einer anderen Wallet zahlst."; "lightning__spark_exit__quote_loading" = "Kosten werden abgerufen…"; "lightning__spark_exit__quote_settling" = "Warte, bis Spark dein Guthaben fertig verschoben hat. Das kann bis zu 5 Minuten dauern."; "lightning__spark_exit__quote_error" = "Die Übertragungskosten konnten nicht abgerufen werden. Versuche es erneut."; diff --git a/Bitkit/Resources/Localization/el.lproj/Localizable.strings b/Bitkit/Resources/Localization/el.lproj/Localizable.strings index 9e30774de..4edc35752 100644 --- a/Bitkit/Resources/Localization/el.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/el.lproj/Localizable.strings @@ -373,6 +373,7 @@ "lightning__spark_exit__fees" = "Τέλη δικτύου"; "lightning__spark_exit__receive" = "Λαμβάνετε (εκτίμηση)"; "lightning__spark_exit__savings_refused" = "Η πληρωμή σας από αποταμιεύσεις δεν ολοκληρώθηκε. Πληρώστε ξανά ή χρηματοδοτήστε από άλλο πορτοφόλι."; +"lightning__spark_exit__savings_unresolved" = "Η πληρωμή σας από αποταμιεύσεις δεν έχει επιβεβαιωθεί ακόμη. Περιμένετέ την πριν πληρώσετε από άλλο πορτοφόλι."; "lightning__spark_exit__quote_loading" = "Λήψη κόστους…"; "lightning__spark_exit__quote_settling" = "Αναμονή για να ολοκληρώσει το Spark τη μεταφορά του υπολοίπου σας. Αυτό μπορεί να διαρκέσει έως 5 λεπτά."; "lightning__spark_exit__quote_error" = "Δεν ήταν δυνατή η λήψη του κόστους μεταφοράς. Δοκιμάστε ξανά."; diff --git a/Bitkit/Resources/Localization/en.lproj/Localizable.strings b/Bitkit/Resources/Localization/en.lproj/Localizable.strings index 9c1eb9e59..76a4374f1 100644 --- a/Bitkit/Resources/Localization/en.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/en.lproj/Localizable.strings @@ -394,6 +394,7 @@ "lightning__spark_exit__fees" = "Network fees"; "lightning__spark_exit__receive" = "You receive (estimate)"; "lightning__spark_exit__savings_refused" = "Your savings payment didn't go through. Pay again, or fund from another wallet."; +"lightning__spark_exit__savings_unresolved" = "Your savings payment hasn't confirmed yet. Wait for it before you pay from another wallet."; "lightning__spark_exit__quote_loading" = "Getting the costs…"; "lightning__spark_exit__quote_settling" = "Waiting for Spark to finish moving your balance. This can take up to 5 minutes."; "lightning__spark_exit__quote_error" = "Could not get the transfer costs. Try again."; diff --git a/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings b/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings index 7fd85218d..afab63824 100644 --- a/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings @@ -402,6 +402,7 @@ "lightning__spark_exit__fees" = "Comisiones de red"; "lightning__spark_exit__receive" = "Recibes (estimado)"; "lightning__spark_exit__savings_refused" = "Tu pago desde ahorros no se completó. Paga de nuevo o financia desde otra billetera."; +"lightning__spark_exit__savings_unresolved" = "Tu pago desde ahorros aún no se confirmó. Espéralo antes de pagar desde otra billetera."; "lightning__spark_exit__quote_loading" = "Obteniendo los costos…"; "lightning__spark_exit__quote_settling" = "Esperando a que Spark termine de mover tu saldo. Esto puede tardar hasta 5 minutos."; "lightning__spark_exit__quote_error" = "No se pudieron obtener los costos de la transferencia. Intenta de nuevo."; diff --git a/Bitkit/Resources/Localization/es.lproj/Localizable.strings b/Bitkit/Resources/Localization/es.lproj/Localizable.strings index b9f3a5c6b..d687027e6 100644 --- a/Bitkit/Resources/Localization/es.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/es.lproj/Localizable.strings @@ -387,6 +387,7 @@ "lightning__spark_exit__fees" = "Comisiones de red"; "lightning__spark_exit__receive" = "Recibes (estimado)"; "lightning__spark_exit__savings_refused" = "Tu pago desde ahorros no se completó. Paga de nuevo o financia desde otro monedero."; +"lightning__spark_exit__savings_unresolved" = "Tu pago desde ahorros aún no se confirmó. Espéralo antes de pagar desde otro monedero."; "lightning__spark_exit__quote_loading" = "Obteniendo los costes…"; "lightning__spark_exit__quote_settling" = "Esperando a que Spark termine de mover tu saldo. Esto puede tardar hasta 5 minutos."; "lightning__spark_exit__quote_error" = "No se pudieron obtener los costes de la transferencia. Inténtalo de nuevo."; diff --git a/Bitkit/Resources/Localization/fr.lproj/Localizable.strings b/Bitkit/Resources/Localization/fr.lproj/Localizable.strings index cd930e621..4b440f362 100644 --- a/Bitkit/Resources/Localization/fr.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/fr.lproj/Localizable.strings @@ -402,6 +402,7 @@ "lightning__spark_exit__fees" = "Frais de réseau"; "lightning__spark_exit__receive" = "Vous recevez (estimation)"; "lightning__spark_exit__savings_refused" = "Votre paiement depuis l'épargne n'a pas abouti. Payez à nouveau ou financez depuis un autre portefeuille."; +"lightning__spark_exit__savings_unresolved" = "Votre paiement depuis l'épargne n'est pas encore confirmé. Attendez-le avant de payer depuis un autre portefeuille."; "lightning__spark_exit__quote_loading" = "Obtention des coûts…"; "lightning__spark_exit__quote_settling" = "En attente que Spark termine le transfert de votre solde. Cela peut prendre jusqu'à 5 minutes."; "lightning__spark_exit__quote_error" = "Impossible d'obtenir les coûts du transfert. Réessayez."; diff --git a/Bitkit/Resources/Localization/it.lproj/Localizable.strings b/Bitkit/Resources/Localization/it.lproj/Localizable.strings index 6cff1d76e..12a4a8cf5 100644 --- a/Bitkit/Resources/Localization/it.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/it.lproj/Localizable.strings @@ -396,6 +396,7 @@ "lightning__spark_exit__fees" = "Commissioni di rete"; "lightning__spark_exit__receive" = "Riceverai (stima)"; "lightning__spark_exit__savings_refused" = "Il tuo pagamento dai risparmi non è andato a buon fine. Paga di nuovo o finanzia da un altro portafoglio."; +"lightning__spark_exit__savings_unresolved" = "Il tuo pagamento dai risparmi non è ancora confermato. Attendilo prima di pagare da un altro portafoglio."; "lightning__spark_exit__quote_loading" = "Calcolo dei costi…"; "lightning__spark_exit__quote_settling" = "In attesa che Spark finisca di spostare il tuo saldo. Può richiedere fino a 5 minuti."; "lightning__spark_exit__quote_error" = "Impossibile ottenere i costi del trasferimento. Riprova."; diff --git a/Bitkit/Resources/Localization/nl.lproj/Localizable.strings b/Bitkit/Resources/Localization/nl.lproj/Localizable.strings index 0389e30ef..6b765316b 100644 --- a/Bitkit/Resources/Localization/nl.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/nl.lproj/Localizable.strings @@ -399,6 +399,7 @@ "lightning__spark_exit__fees" = "Netwerkkosten"; "lightning__spark_exit__receive" = "U ontvangt (schatting)"; "lightning__spark_exit__savings_refused" = "Je betaling uit spaargeld is niet gelukt. Betaal opnieuw of financier vanuit een andere wallet."; +"lightning__spark_exit__savings_unresolved" = "Je betaling uit spaargeld is nog niet bevestigd. Wacht erop voordat je vanuit een andere wallet betaalt."; "lightning__spark_exit__quote_loading" = "Kosten ophalen…"; "lightning__spark_exit__quote_settling" = "Wachten tot Spark klaar is met het verplaatsen van je saldo. Dit kan tot 5 minuten duren."; "lightning__spark_exit__quote_error" = "Kon de kosten van de overboeking niet ophalen. Probeer opnieuw."; diff --git a/Bitkit/Resources/Localization/pl.lproj/Localizable.strings b/Bitkit/Resources/Localization/pl.lproj/Localizable.strings index 89dbd098e..76c483604 100644 --- a/Bitkit/Resources/Localization/pl.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/pl.lproj/Localizable.strings @@ -401,6 +401,7 @@ "lightning__spark_exit__fees" = "Opłaty sieciowe"; "lightning__spark_exit__receive" = "Otrzymasz (szacunkowo)"; "lightning__spark_exit__savings_refused" = "Twoja płatność z Oszczędności nie przeszła. Zapłać ponownie lub sfinansuj z innego portfela."; +"lightning__spark_exit__savings_unresolved" = "Twoja płatność z Oszczędności nie jest jeszcze potwierdzona. Poczekaj na nią, zanim zapłacisz z innego portfela."; "lightning__spark_exit__quote_loading" = "Pobieranie kosztów…"; "lightning__spark_exit__quote_settling" = "Czekamy, aż Spark zakończy przenoszenie Twojego salda. Może to potrwać do 5 minut."; "lightning__spark_exit__quote_error" = "Nie udało się pobrać kosztów transferu. Spróbuj ponownie."; diff --git a/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings b/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings index 62f17f3d7..9c7eb903b 100644 --- a/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings @@ -401,6 +401,7 @@ "lightning__spark_exit__fees" = "Taxas de rede"; "lightning__spark_exit__receive" = "Você recebe (estimativa)"; "lightning__spark_exit__savings_refused" = "Seu pagamento da poupança não foi concluído. Pague novamente ou financie de outra carteira."; +"lightning__spark_exit__savings_unresolved" = "Seu pagamento da poupança ainda não foi confirmado. Aguarde antes de pagar de outra carteira."; "lightning__spark_exit__quote_loading" = "Obtendo os custos…"; "lightning__spark_exit__quote_settling" = "Aguardando a Spark terminar de mover seu saldo. Isso pode levar até 5 minutos."; "lightning__spark_exit__quote_error" = "Não foi possível obter os custos da transferência. Tente novamente."; diff --git a/Bitkit/Resources/Localization/pt.lproj/Localizable.strings b/Bitkit/Resources/Localization/pt.lproj/Localizable.strings index 561a186f1..95660df2d 100644 --- a/Bitkit/Resources/Localization/pt.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/pt.lproj/Localizable.strings @@ -401,6 +401,7 @@ "lightning__spark_exit__fees" = "Taxas de rede"; "lightning__spark_exit__receive" = "Recebe (estimativa)"; "lightning__spark_exit__savings_refused" = "O seu pagamento da poupança não foi concluído. Pague novamente ou financie a partir de outra carteira."; +"lightning__spark_exit__savings_unresolved" = "O seu pagamento da poupança ainda não foi confirmado. Aguarde antes de pagar a partir de outra carteira."; "lightning__spark_exit__quote_loading" = "A obter os custos…"; "lightning__spark_exit__quote_settling" = "A aguardar que a Spark termine de mover o seu saldo. Isto pode demorar até 5 minutos."; "lightning__spark_exit__quote_error" = "Não foi possível obter os custos da transferência. Tente novamente."; diff --git a/Bitkit/Resources/Localization/ru.lproj/Localizable.strings b/Bitkit/Resources/Localization/ru.lproj/Localizable.strings index a19e3a975..728e385e4 100644 --- a/Bitkit/Resources/Localization/ru.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/ru.lproj/Localizable.strings @@ -397,6 +397,7 @@ "lightning__spark_exit__fees" = "Сетевые комиссии"; "lightning__spark_exit__receive" = "Вы получите (оценка)"; "lightning__spark_exit__savings_refused" = "Платёж из сбережений не прошёл. Оплатите снова или пополните с другого кошелька."; +"lightning__spark_exit__savings_unresolved" = "Платёж из сбережений ещё не подтверждён. Дождитесь его, прежде чем платить с другого кошелька."; "lightning__spark_exit__quote_loading" = "Получение стоимости…"; "lightning__spark_exit__quote_settling" = "Ждём, пока Spark завершит перемещение вашего баланса. Это может занять до 5 минут."; "lightning__spark_exit__quote_error" = "Не удалось получить стоимость перевода. Попробуйте ещё раз."; diff --git a/Bitkit/Views/Settings/Advanced/SparkExitViews.swift b/Bitkit/Views/Settings/Advanced/SparkExitViews.swift index 6ff563427..ed72bd3ea 100644 --- a/Bitkit/Views/Settings/Advanced/SparkExitViews.swift +++ b/Bitkit/Views/Settings/Advanced/SparkExitViews.swift @@ -760,9 +760,13 @@ struct SparkExitProgressView: View { ].compactMap { $0 }.joined(separator: " ") } - /// A top-up already paid, such as a higher fee's from savings, or seen at the address: nothing to ask for. + /// A top-up already paid, such as a higher fee's from savings, or seen at the address, or a savings payment that + /// may still land: nothing to ask for. private var isFundingOnItsWay: Bool { - switch fundingState { + if exit.isSavingsSendLanding { + return true + } + return switch fundingState { case .paid, .seen: true default: false } @@ -852,9 +856,16 @@ struct SparkExitProgressView: View { .accessibilityIdentifier("SparkExitSavingsRefused") } + if exit.isSavingsSendLanding { + BodySText(t("lightning__spark_exit__savings_unresolved")) + .fixedSize(horizontal: false, vertical: true) + .padding(.top, 24) + .accessibilityIdentifier("SparkExitSavingsUnresolved") + } + if let address = record.funding.first?.address { switch fundingState { - case let .ask(sats, receivedSats): + case let .ask(sats, receivedSats) where !exit.isSavingsSendLanding: // The ask counts the input fee of each payment, so it is what is missing; the hint // names the full total, not the dust-floored ask added to what arrived. SparkExitFeesRow(label: t("lightning__spark_exit__pay_amount"), sats: sats) @@ -867,7 +878,7 @@ struct SparkExitProgressView: View { targetSats: exit.fundingAskTotal ?? sats + receivedSats ) .padding(.top, 32) - case .paid, .seen, nil: + case .ask, .paid, .seen, nil: EmptyView() } } diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index ca97e2e74..ee666b464 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -637,6 +637,16 @@ final class SparkExitReviewTests: XCTestCase { XCTAssertTrue(record.isAwaitingOwnPayment) } + /// While that send may still land, Progress holds the funding ask; a refused send or one to another address does not. + func testAnUnresolvedSavingsSendHoldsTheFundingAsk() { + let record = SparkExitFixtures.record() + XCTAssertNotNil(record.fundingShortfallSats) + XCTAssertTrue(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: false)) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: true)) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qelsewhere", isRefused: false)) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: nil, isRefused: false)) + } + /// The unknown payment confirming late is adopted like any funding, and the exit builds. func testALateConfirmationOfAnUnknownFundingSendIsAdopted() async throws { var record = SparkExitFixtures.record() diff --git a/changelog.d/next/1451.fixed.md b/changelog.d/next/1451.fixed.md new file mode 100644 index 000000000..a2ea8418d --- /dev/null +++ b/changelog.d/next/1451.fixed.md @@ -0,0 +1 @@ +Fixed force transfer progress asking for the emergency fees while a payment of them from savings could still confirm. From cd40eb2c8704ca765f98b93f02d7ac9ab987b28a Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 11:35:33 +0100 Subject: [PATCH 02/15] fix: hold the ask only for a send that may still land, read before the network A refused send keeps its own retry and never holds the ask. The journal is read on load, before a tick's network calls and after a retry, so Progress never shows the ask first. Verified: SparkExitReviewTests, SparkExitFundingTests, OnchainSendAttemptServiceTests on the simulator, swiftformat --lint --- Bitkit/Managers/SparkExitManager.swift | 33 ++++++++++++++++++-------- BitkitTests/SparkExitReviewTests.swift | 14 ++++++----- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index fd9eb8a61..2bcb9ee7d 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -276,6 +276,8 @@ final class SparkExitManager { } let record = record Task { + // Progress holds the funding ask for a savings send that may still land, before any tick reads the network. + await refreshRefusedSavingsPayment() if let record { await settleIfFinished(record) } @@ -354,18 +356,20 @@ final class SparkExitManager { record.map { Self.isSavingsPaymentRefused($0, attempt: refusedSavingsPayment) } ?? false } + /// The address of a savings send that is pending or of unknown outcome, read with the refused payment: it may + /// still land. A refused send will not, and keeps its own retry. + private(set) var landingSendAddress: String? + /// A savings send to the funding address went out and is not known accepted or refused yet: it may still land. var isSavingsSendLanding: Bool { - record.map { - Self.isSavingsSendLanding($0, unresolvedSendAddress: unresolvedSendAddress, isRefused: isSavingsPaymentRefused) - } ?? false + record.map { Self.isSavingsSendLanding($0, landingSendAddress: landingSendAddress) } ?? false } /// While a savings send to `record`'s funding address may still land, Progress holds the funding ask, as retries /// and cancel do: paying the same fees from another wallet as well would pay them twice. - nonisolated static func isSavingsSendLanding(_ record: SparkExitRecord, unresolvedSendAddress: String?, isRefused: Bool) -> Bool { - guard !isRefused, record.fundingShortfallSats != nil, let unresolvedSendAddress else { return false } - return record.funding.contains { $0.address == unresolvedSendAddress } + nonisolated static func isSavingsSendLanding(_ record: SparkExitRecord, landingSendAddress: String?) -> Bool { + guard record.fundingShortfallSats != nil, let landingSendAddress else { return false } + return record.funding.contains { $0.address == landingSendAddress } } /// A node refused the savings payment of `record`'s fee funding, and none was adopted since: only before the exit @@ -405,10 +409,17 @@ final class SparkExitManager { } guard let current = record, let refused = refusedSavingsPayment else { return } let attempts = OnchainSendAttemptService.shared - let txid = try await Self.payRefusedAgain(refused, record: current) { context in - try await attempts.retrySamePayment(using: LightningService.shared, context: context) { attempt, _ in - guard attempt.id == refused.id, attempt.address == refused.address else { throw OnchainSendAttemptError.unresolved } + let txid: String? + do { + txid = try await Self.payRefusedAgain(refused, record: current) { context in + try await attempts.retrySamePayment(using: LightningService.shared, context: context) { attempt, _ in + guard attempt.id == refused.id, attempt.address == refused.address else { throw OnchainSendAttemptError.unresolved } + } } + } catch { + // A retry left pending shows as one that may still land at once. + await refreshRefusedSavingsPayment() + throw error } await refreshRefusedSavingsPayment() guard let txid else { @@ -428,6 +439,7 @@ final class SparkExitManager { private func refreshRefusedSavingsPayment() async { let attempt = try? await OnchainSendAttemptService.shared.blockingAttempt() refusedSavingsPayment = attempt?.status == .rejected ? attempt : nil + landingSendAddress = attempt.flatMap { $0.status == .pending || $0.status == .unknown ? $0.address : nil } } /// What the exit still brings to savings, for its own transfer's in-transfer amount; nil without one. @@ -592,6 +604,8 @@ final class SparkExitManager { guard !isTicking, let current = record, Self.canDrive(current, startingCreatedAt: startingCreatedAt), let chain else { return } isTicking = true defer { isTicking = false } + // Read before the network, which may be slow or unreachable: the send journal is local. + await refreshRefusedSavingsPayment() let store = store let walletIndex = walletIndex // A reconnect for the exit that failed leaves Spark ready but disconnected: the tick connects it again. @@ -608,7 +622,6 @@ final class SparkExitManager { do { tipHeight = await (try? chain.tipHeight()) ?? tipHeight unresolvedSendAddress = await OnchainSendAttemptService.shared.unresolvedSendAddress() - await refreshRefusedSavingsPayment() await refreshNetworkFees(chain: chain) await refreshFundingSeen(chain: chain, record: current) var updated = try await driver.tick(current) diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index ee666b464..87161ae9f 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -637,14 +637,16 @@ final class SparkExitReviewTests: XCTestCase { XCTAssertTrue(record.isAwaitingOwnPayment) } - /// While that send may still land, Progress holds the funding ask; a refused send or one to another address does not. + /// While that send may still land, Progress holds the funding ask; none to the address, or a funded exit, does not. func testAnUnresolvedSavingsSendHoldsTheFundingAsk() { - let record = SparkExitFixtures.record() + var record = SparkExitFixtures.record() XCTAssertNotNil(record.fundingShortfallSats) - XCTAssertTrue(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: false)) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: true)) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qelsewhere", isRefused: false)) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: nil, isRefused: false)) + XCTAssertTrue(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: "bcrt1qfunding")) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: "bcrt1qelsewhere")) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: nil)) + record.phase = .running + XCTAssertNil(record.fundingShortfallSats) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: "bcrt1qfunding")) } /// The unknown payment confirming late is adopted like any funding, and the exit builds. From 759f3233d02f54f8544cdc493f86138ac758e5c3 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 11:43:08 +0100 Subject: [PATCH 03/15] fix: end a retried savings send only once a node accepted it A retry of unknown outcome may still land, so its send keeps holding the funding ask, as on Android. Verified: SparkExitReviewTests, SparkExitFundingTests, OnchainSendAttemptServiceTests on the simulator, swiftformat --- Bitkit/Managers/SparkExitManager.swift | 15 +++++++++++++-- BitkitTests/SparkExitReviewTests.swift | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 2bcb9ee7d..e37634650 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -336,6 +336,7 @@ final class SparkExitManager { savingsFundingRefusedFeeSats = nil fundingSeenSats = 0 fundingSeenCount = 0 + landingSendAddress = nil Self.clearReminders() } @@ -425,7 +426,11 @@ final class SparkExitManager { guard let txid else { throw AppError(message: t("wallet__send_error_tx_failed"), debugMessage: "Spark exit savings payment refused again") } - _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) + // Only an accepted payment ends its send, as the start's payment: one of unknown outcome may still land, and + // its send keeps holding the funding ask. + if landingSendAddress == nil { + _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) + } guard var paid = record, paid.createdAt == current.createdAt, paid.isActive else { return } paid.fundingPaymentTxid = txid try saveRecord(paid) @@ -439,7 +444,13 @@ final class SparkExitManager { private func refreshRefusedSavingsPayment() async { let attempt = try? await OnchainSendAttemptService.shared.blockingAttempt() refusedSavingsPayment = attempt?.status == .rejected ? attempt : nil - landingSendAddress = attempt.flatMap { $0.status == .pending || $0.status == .unknown ? $0.address : nil } + landingSendAddress = Self.landingSendAddress(attempt) + } + + /// The address of a send that is pending or of unknown outcome. An accepted send has landed, a refused one will not. + nonisolated static func landingSendAddress(_ attempt: OnchainSendAttempt?) -> String? { + guard let attempt, attempt.status == .pending || attempt.status == .unknown else { return nil } + return attempt.address } /// What the exit still brings to savings, for its own transfer's in-transfer amount; nil without one. diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index 87161ae9f..d11412ed4 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -649,6 +649,22 @@ final class SparkExitReviewTests: XCTestCase { XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: "bcrt1qfunding")) } + /// Only a send that is pending or of unknown outcome may still land: an accepted one has, and a refused one keeps its + /// own retry, also once the exit needs more funding. + func testOnlyAPendingOrUnknownSendMayStillLand() { + XCTAssertEqual(SparkExitManager.landingSendAddress(refusedPayment(status: .pending)), "bcrt1qfunding") + XCTAssertEqual(SparkExitManager.landingSendAddress(refusedPayment(status: .unknown)), "bcrt1qfunding") + XCTAssertNil(SparkExitManager.landingSendAddress(refusedPayment(status: .rejected))) + XCTAssertNil(SparkExitManager.landingSendAddress(refusedPayment(status: .accepted))) + XCTAssertNil(SparkExitManager.landingSendAddress(nil)) + let needsFunding = SparkExitFixtures.record(phase: .needsFunding(requiredSats: 20000)) + XCTAssertNotNil(needsFunding.fundingShortfallSats) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding( + needsFunding, + landingSendAddress: SparkExitManager.landingSendAddress(refusedPayment()) + )) + } + /// The unknown payment confirming late is adopted like any funding, and the exit builds. func testALateConfirmationOfAnUnknownFundingSendIsAdopted() async throws { var record = SparkExitFixtures.record() From 5cef1d816adb4efc3fc9fe90d997c4caf1a1d961 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 11:51:08 +0100 Subject: [PATCH 04/15] fix: read the savings send after any start payment A start payment of unknown outcome holds the funding ask at once, as on Android. Verified: SparkExitReviewTests, SparkExitFundingTests, OnchainSendAttemptServiceTests, SparkExitTests on the simulator, swiftformat --lint --- Bitkit/Managers/SparkExitManager.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index e37634650..2556777e7 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -1093,6 +1093,8 @@ final class SparkExitManager { await refreshRefusedSavingsPayment() throw error } + // One of unknown outcome holds the funding ask at once. + await refreshRefusedSavingsPayment() // Saved before the lock is released, so a cancel after the payment finds it and refuses. started.fundingPaymentTxid = txid do { From 628ccb56709a1ba1f938dfea76f4def771bfc88d Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 12:48:37 +0100 Subject: [PATCH 05/15] fix: say a savings payment may still land only while the exit waits for it An unknown savings payment already keeps its txid on the record, so Progress shows it paid and the own-payment wait brings the ask back if it never lands. The journal-keyed hold could outlive that wait and hide the ask for good; it is gone. The note now shows while the exit waits for its own payment and the send journal has not seen it accepted. Verified: SparkExitReviewTests, SparkExitFundingTests, SparkExitTests on the simulator, swiftformat --lint, validate-translations --- Bitkit/Managers/SparkExitManager.swift | 54 +++++-------------- .../Settings/Advanced/SparkExitViews.swift | 12 ++--- BitkitTests/SparkExitReviewTests.swift | 37 +++++-------- changelog.d/next/1451.changed.md | 1 + changelog.d/next/1451.fixed.md | 1 - 5 files changed, 31 insertions(+), 74 deletions(-) create mode 100644 changelog.d/next/1451.changed.md delete mode 100644 changelog.d/next/1451.fixed.md diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 2556777e7..dbe473ff3 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -276,8 +276,6 @@ final class SparkExitManager { } let record = record Task { - // Progress holds the funding ask for a savings send that may still land, before any tick reads the network. - await refreshRefusedSavingsPayment() if let record { await settleIfFinished(record) } @@ -336,7 +334,6 @@ final class SparkExitManager { savingsFundingRefusedFeeSats = nil fundingSeenSats = 0 fundingSeenCount = 0 - landingSendAddress = nil Self.clearReminders() } @@ -357,20 +354,18 @@ final class SparkExitManager { record.map { Self.isSavingsPaymentRefused($0, attempt: refusedSavingsPayment) } ?? false } - /// The address of a savings send that is pending or of unknown outcome, read with the refused payment: it may - /// still land. A refused send will not, and keeps its own retry. - private(set) var landingSendAddress: String? - - /// A savings send to the funding address went out and is not known accepted or refused yet: it may still land. + /// The exit waits for its own savings payment, which the send journal has not seen accepted yet: it may still land, + /// so Progress says to wait for it before paying from another wallet. var isSavingsSendLanding: Bool { - record.map { Self.isSavingsSendLanding($0, landingSendAddress: landingSendAddress) } ?? false + record.map { + Self.isSavingsSendLanding($0, unresolvedSendAddress: unresolvedSendAddress, isRefused: isSavingsPaymentRefused) + } ?? false } - /// While a savings send to `record`'s funding address may still land, Progress holds the funding ask, as retries - /// and cancel do: paying the same fees from another wallet as well would pay them twice. - nonisolated static func isSavingsSendLanding(_ record: SparkExitRecord, landingSendAddress: String?) -> Bool { - guard record.fundingShortfallSats != nil, let landingSendAddress else { return false } - return record.funding.contains { $0.address == landingSendAddress } + /// Only while `record` still waits for that payment: once the own-payment wait forgets it, the ask comes back. + nonisolated static func isSavingsSendLanding(_ record: SparkExitRecord, unresolvedSendAddress: String?, isRefused: Bool) -> Bool { + guard record.isAwaitingOwnPayment, !isRefused, let unresolvedSendAddress else { return false } + return record.funding.contains { $0.address == unresolvedSendAddress } } /// A node refused the savings payment of `record`'s fee funding, and none was adopted since: only before the exit @@ -410,27 +405,16 @@ final class SparkExitManager { } guard let current = record, let refused = refusedSavingsPayment else { return } let attempts = OnchainSendAttemptService.shared - let txid: String? - do { - txid = try await Self.payRefusedAgain(refused, record: current) { context in - try await attempts.retrySamePayment(using: LightningService.shared, context: context) { attempt, _ in - guard attempt.id == refused.id, attempt.address == refused.address else { throw OnchainSendAttemptError.unresolved } - } + let txid = try await Self.payRefusedAgain(refused, record: current) { context in + try await attempts.retrySamePayment(using: LightningService.shared, context: context) { attempt, _ in + guard attempt.id == refused.id, attempt.address == refused.address else { throw OnchainSendAttemptError.unresolved } } - } catch { - // A retry left pending shows as one that may still land at once. - await refreshRefusedSavingsPayment() - throw error } await refreshRefusedSavingsPayment() guard let txid else { throw AppError(message: t("wallet__send_error_tx_failed"), debugMessage: "Spark exit savings payment refused again") } - // Only an accepted payment ends its send, as the start's payment: one of unknown outcome may still land, and - // its send keeps holding the funding ask. - if landingSendAddress == nil { - _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) - } + _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) guard var paid = record, paid.createdAt == current.createdAt, paid.isActive else { return } paid.fundingPaymentTxid = txid try saveRecord(paid) @@ -444,13 +428,6 @@ final class SparkExitManager { private func refreshRefusedSavingsPayment() async { let attempt = try? await OnchainSendAttemptService.shared.blockingAttempt() refusedSavingsPayment = attempt?.status == .rejected ? attempt : nil - landingSendAddress = Self.landingSendAddress(attempt) - } - - /// The address of a send that is pending or of unknown outcome. An accepted send has landed, a refused one will not. - nonisolated static func landingSendAddress(_ attempt: OnchainSendAttempt?) -> String? { - guard let attempt, attempt.status == .pending || attempt.status == .unknown else { return nil } - return attempt.address } /// What the exit still brings to savings, for its own transfer's in-transfer amount; nil without one. @@ -615,8 +592,6 @@ final class SparkExitManager { guard !isTicking, let current = record, Self.canDrive(current, startingCreatedAt: startingCreatedAt), let chain else { return } isTicking = true defer { isTicking = false } - // Read before the network, which may be slow or unreachable: the send journal is local. - await refreshRefusedSavingsPayment() let store = store let walletIndex = walletIndex // A reconnect for the exit that failed leaves Spark ready but disconnected: the tick connects it again. @@ -633,6 +608,7 @@ final class SparkExitManager { do { tipHeight = await (try? chain.tipHeight()) ?? tipHeight unresolvedSendAddress = await OnchainSendAttemptService.shared.unresolvedSendAddress() + await refreshRefusedSavingsPayment() await refreshNetworkFees(chain: chain) await refreshFundingSeen(chain: chain, record: current) var updated = try await driver.tick(current) @@ -1093,8 +1069,6 @@ final class SparkExitManager { await refreshRefusedSavingsPayment() throw error } - // One of unknown outcome holds the funding ask at once. - await refreshRefusedSavingsPayment() // Saved before the lock is released, so a cancel after the payment finds it and refuses. started.fundingPaymentTxid = txid do { diff --git a/Bitkit/Views/Settings/Advanced/SparkExitViews.swift b/Bitkit/Views/Settings/Advanced/SparkExitViews.swift index ed72bd3ea..718ab537f 100644 --- a/Bitkit/Views/Settings/Advanced/SparkExitViews.swift +++ b/Bitkit/Views/Settings/Advanced/SparkExitViews.swift @@ -760,13 +760,9 @@ struct SparkExitProgressView: View { ].compactMap { $0 }.joined(separator: " ") } - /// A top-up already paid, such as a higher fee's from savings, or seen at the address, or a savings payment that - /// may still land: nothing to ask for. + /// A top-up already paid, such as a higher fee's from savings, or seen at the address: nothing to ask for. private var isFundingOnItsWay: Bool { - if exit.isSavingsSendLanding { - return true - } - return switch fundingState { + switch fundingState { case .paid, .seen: true default: false } @@ -865,7 +861,7 @@ struct SparkExitProgressView: View { if let address = record.funding.first?.address { switch fundingState { - case let .ask(sats, receivedSats) where !exit.isSavingsSendLanding: + case let .ask(sats, receivedSats): // The ask counts the input fee of each payment, so it is what is missing; the hint // names the full total, not the dust-floored ask added to what arrived. SparkExitFeesRow(label: t("lightning__spark_exit__pay_amount"), sats: sats) @@ -878,7 +874,7 @@ struct SparkExitProgressView: View { targetSats: exit.fundingAskTotal ?? sats + receivedSats ) .padding(.top, 32) - case .ask, .paid, .seen, nil: + case .paid, .seen, nil: EmptyView() } } diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index d11412ed4..4a89722f0 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -637,32 +637,19 @@ final class SparkExitReviewTests: XCTestCase { XCTAssertTrue(record.isAwaitingOwnPayment) } - /// While that send may still land, Progress holds the funding ask; none to the address, or a funded exit, does not. - func testAnUnresolvedSavingsSendHoldsTheFundingAsk() { + /// Progress says that payment may still land only while the exit waits for it: once the own-payment wait forgets + /// it, the ask comes back, even with the send journal still unresolved. + func testASavingsPaymentMayStillLandOnlyWhileTheExitWaitsForIt() { var record = SparkExitFixtures.record() - XCTAssertNotNil(record.fundingShortfallSats) - XCTAssertTrue(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: "bcrt1qfunding")) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: "bcrt1qelsewhere")) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: nil)) - record.phase = .running - XCTAssertNil(record.fundingShortfallSats) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, landingSendAddress: "bcrt1qfunding")) - } - - /// Only a send that is pending or of unknown outcome may still land: an accepted one has, and a refused one keeps its - /// own retry, also once the exit needs more funding. - func testOnlyAPendingOrUnknownSendMayStillLand() { - XCTAssertEqual(SparkExitManager.landingSendAddress(refusedPayment(status: .pending)), "bcrt1qfunding") - XCTAssertEqual(SparkExitManager.landingSendAddress(refusedPayment(status: .unknown)), "bcrt1qfunding") - XCTAssertNil(SparkExitManager.landingSendAddress(refusedPayment(status: .rejected))) - XCTAssertNil(SparkExitManager.landingSendAddress(refusedPayment(status: .accepted))) - XCTAssertNil(SparkExitManager.landingSendAddress(nil)) - let needsFunding = SparkExitFixtures.record(phase: .needsFunding(requiredSats: 20000)) - XCTAssertNotNil(needsFunding.fundingShortfallSats) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding( - needsFunding, - landingSendAddress: SparkExitManager.landingSendAddress(refusedPayment()) - )) + record.fundingPaymentTxid = "unk" + XCTAssertTrue(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: false)) + XCTAssertEqual(SparkExitMath.fundingState(record, utxos: nil), .paid(txid: "unk")) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: nil, isRefused: false), "accepted") + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: true)) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qelsewhere", isRefused: false)) + record.fundingPaymentTxid = nil + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: false)) + guard case .ask = SparkExitMath.fundingState(record, utxos: nil) else { return XCTFail("the ask comes back") } } /// The unknown payment confirming late is adopted like any funding, and the exit builds. diff --git a/changelog.d/next/1451.changed.md b/changelog.d/next/1451.changed.md new file mode 100644 index 000000000..8e9c03f6c --- /dev/null +++ b/changelog.d/next/1451.changed.md @@ -0,0 +1 @@ +Force transfer progress now says when a savings payment of the emergency fees has not confirmed yet, before you pay from another wallet. diff --git a/changelog.d/next/1451.fixed.md b/changelog.d/next/1451.fixed.md deleted file mode 100644 index a2ea8418d..000000000 --- a/changelog.d/next/1451.fixed.md +++ /dev/null @@ -1 +0,0 @@ -Fixed force transfer progress asking for the emergency fees while a payment of them from savings could still confirm. From 80bde21a43788c28d6c65522a5629a37b38bd708 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 13:35:58 +0100 Subject: [PATCH 06/15] fix: end a retried savings send only once a node accepted it A retry of unknown outcome may still land: its send stays unresolved, so Progress keeps saying so. A refused send is read from the journal, so its note never shows as one that may still land. Verified: SparkExitReviewTests, SparkExitTests on the simulator, swiftformat --lint --- Bitkit/Managers/SparkExitManager.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index dbe473ff3..08dec2868 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -358,7 +358,7 @@ final class SparkExitManager { /// so Progress says to wait for it before paying from another wallet. var isSavingsSendLanding: Bool { record.map { - Self.isSavingsSendLanding($0, unresolvedSendAddress: unresolvedSendAddress, isRefused: isSavingsPaymentRefused) + Self.isSavingsSendLanding($0, unresolvedSendAddress: unresolvedSendAddress, isRefused: refusedSavingsPayment != nil) } ?? false } @@ -414,7 +414,10 @@ final class SparkExitManager { guard let txid else { throw AppError(message: t("wallet__send_error_tx_failed"), debugMessage: "Spark exit savings payment refused again") } - _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) + // Only an accepted payment ends its send: one of unknown outcome may still land, and Progress says so. + if await (try? attempts.blockingAttempt())?.status == .accepted { + _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) + } guard var paid = record, paid.createdAt == current.createdAt, paid.isActive else { return } paid.fundingPaymentTxid = txid try saveRecord(paid) From 7e8f98b033d51c1f309442675a209e0394eedb2c Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 13:46:28 +0100 Subject: [PATCH 07/15] fix: refresh the savings send after a retry A retry now refreshes whether a savings send may still land, and ends the send only when its own result was accepted. Progress shows the note only while the funding is still paid from savings. Verified: SparkExitReviewTests and SparkExitDriverTests on the simulator, swiftformat --- Bitkit/Managers/SparkExitManager.swift | 15 ++++++++++----- .../Views/Settings/Advanced/SparkExitViews.swift | 2 +- BitkitTests/SparkExitReviewTests.swift | 9 ++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 08dec2868..88d927703 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -384,12 +384,16 @@ final class SparkExitManager { _ refused: OnchainSendAttempt, record: SparkExitRecord, retrySamePayment: (OnchainSendPendingContext) async throws -> OnchainSendResult - ) async throws -> String? { + ) async throws -> (txid: String, isAccepted: Bool)? { guard isSavingsPaymentRefused(record, attempt: refused), refused.canRetrySamePayment else { throw OnchainSendAttemptError.unresolved } let result = try await retrySamePayment(OnchainSendPendingContext(attemptId: refused.id, walletId: refused.walletId, txid: refused.txid)) - return fundingTxid(result) + guard let txid = fundingTxid(result) else { return nil } + if case .accepted = result { + return (txid, true) + } + return (txid, false) } func retrySavingsPayment() async throws { @@ -405,19 +409,20 @@ final class SparkExitManager { } guard let current = record, let refused = refusedSavingsPayment else { return } let attempts = OnchainSendAttemptService.shared - let txid = try await Self.payRefusedAgain(refused, record: current) { context in + let paidAgain = try await Self.payRefusedAgain(refused, record: current) { context in try await attempts.retrySamePayment(using: LightningService.shared, context: context) { attempt, _ in guard attempt.id == refused.id, attempt.address == refused.address else { throw OnchainSendAttemptError.unresolved } } } await refreshRefusedSavingsPayment() - guard let txid else { + guard let (txid, isAccepted) = paidAgain else { throw AppError(message: t("wallet__send_error_tx_failed"), debugMessage: "Spark exit savings payment refused again") } // Only an accepted payment ends its send: one of unknown outcome may still land, and Progress says so. - if await (try? attempts.blockingAttempt())?.status == .accepted { + if isAccepted { _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) } + unresolvedSendAddress = await attempts.unresolvedSendAddress() guard var paid = record, paid.createdAt == current.createdAt, paid.isActive else { return } paid.fundingPaymentTxid = txid try saveRecord(paid) diff --git a/Bitkit/Views/Settings/Advanced/SparkExitViews.swift b/Bitkit/Views/Settings/Advanced/SparkExitViews.swift index 718ab537f..5029cf1bb 100644 --- a/Bitkit/Views/Settings/Advanced/SparkExitViews.swift +++ b/Bitkit/Views/Settings/Advanced/SparkExitViews.swift @@ -852,7 +852,7 @@ struct SparkExitProgressView: View { .accessibilityIdentifier("SparkExitSavingsRefused") } - if exit.isSavingsSendLanding { + if exit.isSavingsSendLanding, case .paid = fundingState { BodySText(t("lightning__spark_exit__savings_unresolved")) .fixedSize(horizontal: false, vertical: true) .padding(.top, 24) diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index 4a89722f0..dbc17ee26 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -1134,9 +1134,16 @@ final class SparkExitReviewTests: XCTestCase { contexts.append(context) return .accepted(txid: "again") } - XCTAssertEqual(txid, "again") + XCTAssertEqual(txid?.txid, "again") + XCTAssertEqual(txid?.isAccepted, true, "an accepted payment ends its send") XCTAssertEqual(contexts, [OnchainSendPendingContext(attemptId: refused.id, walletId: "wallet", txid: "refused")]) + let unknown = try await SparkExitManager.payRefusedAgain(refused, record: SparkExitFixtures.record()) { _ in + .unknown(txid: "maybe") + } + XCTAssertEqual(unknown?.txid, "maybe", "a payment that may still land is kept") + XCTAssertEqual(unknown?.isAccepted, false, "and its send stays open, so Progress says it may still land") + do { _ = try await SparkExitManager.payRefusedAgain(refusedPayment(status: .unknown), record: SparkExitFixtures.record()) { context in contexts.append(context) From 3f8bbfc328016a2a152e08eefd5c2a1b86df5634 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 13:59:52 +0100 Subject: [PATCH 08/15] fix: show the savings note as soon as the payment state changes A tick that forgets the exit's own payment brings the ask back at once, and a savings payment of the funding updates the note right away. A refused later attempt no longer hides the note while the kept payment may still land. Verified: none (tests run after this commit) --- Bitkit/Managers/SparkExitManager.swift | 16 +++++++++------- BitkitTests/SparkExitReviewTests.swift | 9 ++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 88d927703..da8535539 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -357,14 +357,13 @@ final class SparkExitManager { /// The exit waits for its own savings payment, which the send journal has not seen accepted yet: it may still land, /// so Progress says to wait for it before paying from another wallet. var isSavingsSendLanding: Bool { - record.map { - Self.isSavingsSendLanding($0, unresolvedSendAddress: unresolvedSendAddress, isRefused: refusedSavingsPayment != nil) - } ?? false + record.map { Self.isSavingsSendLanding($0, unresolvedSendAddress: unresolvedSendAddress) } ?? false } /// Only while `record` still waits for that payment: once the own-payment wait forgets it, the ask comes back. - nonisolated static func isSavingsSendLanding(_ record: SparkExitRecord, unresolvedSendAddress: String?, isRefused: Bool) -> Bool { - guard record.isAwaitingOwnPayment, !isRefused, let unresolvedSendAddress else { return false } + /// A refused payment is never awaited (its txid is not kept), so a refusal needs no check of its own. + nonisolated static func isSavingsSendLanding(_ record: SparkExitRecord, unresolvedSendAddress: String?) -> Bool { + guard record.isAwaitingOwnPayment, let unresolvedSendAddress else { return false } return record.funding.contains { $0.address == unresolvedSendAddress } } @@ -620,8 +619,9 @@ final class SparkExitManager { await refreshNetworkFees(chain: chain) await refreshFundingSeen(chain: chain, record: current) var updated = try await driver.tick(current) - // A tick that adopted the funding and came up short would otherwise keep showing that payment as seen. - if updated.phase != current.phase { + // A tick that adopted the funding and came up short would otherwise keep showing that payment as seen, and + // one that forgot the exit's own payment would keep hiding the ask. + if updated.phase != current.phase || updated.fundingPaymentTxid != current.fundingPaymentTxid { await refreshFundingSeen(chain: chain, record: updated) } // The result may have been opened while this tick ran; a reset the driver made, such as for change kept @@ -1344,6 +1344,8 @@ final class SparkExitManager { } else { Logger.warn("Spark exit funding \(txid) has an unknown outcome; kept as pending", context: "SparkExitManager") } + // Progress says at once whether the payment may still land. + unresolvedSendAddress = await attempts.unresolvedSendAddress() return txid } diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index dbc17ee26..c861ab4b0 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -642,13 +642,12 @@ final class SparkExitReviewTests: XCTestCase { func testASavingsPaymentMayStillLandOnlyWhileTheExitWaitsForIt() { var record = SparkExitFixtures.record() record.fundingPaymentTxid = "unk" - XCTAssertTrue(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: false)) + XCTAssertTrue(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding")) XCTAssertEqual(SparkExitMath.fundingState(record, utxos: nil), .paid(txid: "unk")) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: nil, isRefused: false), "accepted") - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: true)) - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qelsewhere", isRefused: false)) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: nil), "accepted") + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qelsewhere")) record.fundingPaymentTxid = nil - XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding", isRefused: false)) + XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding")) guard case .ask = SparkExitMath.fundingState(record, utxos: nil) else { return XCTFail("the ask comes back") } } From bfff3d431743691e6f0f0788be04206e4426ada6 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 14:12:04 +0100 Subject: [PATCH 09/15] fix: bring the ask back once the exit forgot its payment A funding state read while the exit still awaited its own payment no longer holds the ask after a background run or tick forgot it, and the savings note reads the send journal right at load. Verified: SparkExitReviewTests and SparkExitDriverTests on the simulator, swiftformat --- Bitkit/Managers/SparkExitManager.swift | 14 +++++++++++++- BitkitTests/SparkExitReviewTests.swift | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index da8535539..11878824a 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -52,7 +52,17 @@ final class SparkExitManager { private(set) var isFeeBumpLeavingLeaves = false /// What the running exit shows about its fee funding while it lacks some. var fundingState: SparkExitFundingState? { - Self.isFundingOfExit(fundingExitCreatedAt, record: record) ? storedFundingState : nil + guard Self.isFundingOfExit(fundingExitCreatedAt, record: record) else { return nil } + return Self.currentFundingState(storedFundingState, record: record) + } + + /// A payment the exit forgot since the funding was read, as a background run or a tick may, no longer holds the + /// ask: Progress reads the record's own state until the next read. + nonisolated static func currentFundingState(_ stored: SparkExitFundingState?, record: SparkExitRecord?) -> SparkExitFundingState? { + if case let .paid(txid) = stored, record?.fundingPaymentTxid != txid { + return nil + } + return stored } /// The full funding the Progress top-up asks for, every extra payment's input fee included. @@ -276,6 +286,8 @@ final class SparkExitManager { } let record = record Task { + // Progress says at once whether a savings payment may still land, before the first tick's network reads. + unresolvedSendAddress = await OnchainSendAttemptService.shared.unresolvedSendAddress() if let record { await settleIfFinished(record) } diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index c861ab4b0..ddc5fb884 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -646,8 +646,11 @@ final class SparkExitReviewTests: XCTestCase { XCTAssertEqual(SparkExitMath.fundingState(record, utxos: nil), .paid(txid: "unk")) XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: nil), "accepted") XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qelsewhere")) + XCTAssertEqual(SparkExitManager.currentFundingState(.paid(txid: "unk"), record: record), .paid(txid: "unk")) record.fundingPaymentTxid = nil XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding")) + // A paid state read before the payment was forgotten, as by a background run, no longer holds the ask. + XCTAssertNil(SparkExitManager.currentFundingState(.paid(txid: "unk"), record: record)) guard case .ask = SparkExitMath.fundingState(record, utxos: nil) else { return XCTFail("the ask comes back") } } From eaa1799e7e48304bc5ed5a127a95f495afdbc484 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 14:20:44 +0100 Subject: [PATCH 10/15] fix: drop a cached paid state once the exit adopted the payment Verified: none (tests run after this commit) --- Bitkit/Managers/SparkExitManager.swift | 6 +++--- BitkitTests/SparkExitReviewTests.swift | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 11878824a..3e883fea9 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -56,10 +56,10 @@ final class SparkExitManager { return Self.currentFundingState(storedFundingState, record: record) } - /// A payment the exit forgot since the funding was read, as a background run or a tick may, no longer holds the - /// ask: Progress reads the record's own state until the next read. + /// A payment the exit forgot or adopted since the funding was read, as a background run or a tick may, no longer + /// holds the ask: Progress reads the record's own state until the next read. nonisolated static func currentFundingState(_ stored: SparkExitFundingState?, record: SparkExitRecord?) -> SparkExitFundingState? { - if case let .paid(txid) = stored, record?.fundingPaymentTxid != txid { + if case let .paid(txid) = stored, record?.isAwaitingOwnPayment != true || record?.fundingPaymentTxid != txid { return nil } return stored diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index ddc5fb884..e16c4ef97 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -647,6 +647,10 @@ final class SparkExitReviewTests: XCTestCase { XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: nil), "accepted") XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qelsewhere")) XCTAssertEqual(SparkExitManager.currentFundingState(.paid(txid: "unk"), record: record), .paid(txid: "unk")) + var adopted = record + adopted.funding = [.init(index: 0, address: "bcrt1qfunding", pubkey: "02", txid: "unk", vout: 0, valueSats: 20000)] + XCTAssertFalse(adopted.isAwaitingOwnPayment) + XCTAssertNil(SparkExitManager.currentFundingState(.paid(txid: "unk"), record: adopted), "adopted: the record says what is left") record.fundingPaymentTxid = nil XCTAssertFalse(SparkExitManager.isSavingsSendLanding(record, unresolvedSendAddress: "bcrt1qfunding")) // A paid state read before the payment was forgotten, as by a background run, no longer holds the ask. From 1910f790207e60ca1662823800a11c73c56bcc6e Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 16:05:02 +0100 Subject: [PATCH 11/15] fix: read the savings sends through master's journal read Verified: none (tests run after this commit) --- Bitkit/Managers/SparkExitManager.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 6d8af52b5..19f008df0 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -287,7 +287,7 @@ final class SparkExitManager { let record = record Task { // Progress says at once whether a savings payment may still land, before the first tick's network reads. - unresolvedSendAddress = await OnchainSendAttemptService.shared.unresolvedSendAddress() + await refreshSends() if let record { await settleIfFinished(record) } @@ -438,7 +438,7 @@ final class SparkExitManager { if isAccepted { _ = try? await attempts.resumeAcceptedOrdinarySend(walletId: refused.walletId, observedTxid: txid) } - unresolvedSendAddress = await attempts.unresolvedSendAddress() + await refreshSends() guard var paid = record, paid.createdAt == current.createdAt, paid.isActive else { return } paid.fundingPaymentTxid = txid paid.savingsRefusal = nil @@ -1554,7 +1554,7 @@ final class SparkExitManager { Logger.warn("Spark exit funding \(txid) has an unknown outcome; kept as pending", context: "SparkExitManager") } // Progress says at once whether the payment may still land. - unresolvedSendAddress = await attempts.unresolvedSendAddress() + await refreshSends() return txid } From ef18c55adada336ff6b01b9ece9c9205087c51b9 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 16:16:16 +0100 Subject: [PATCH 12/15] fix: keep the remainder asked when the funding re-read fails A tick that forgets the exit's own payment reads the funding address again; when that read fails it now goes by the tick's first read, so a short savings payment still counts and only the rest is asked for. Twin of synonymdev/bitkit-android#2123. Verified: SparkExitReviewTests, SparkExitDriverTests, SparkExitTests on the iOS simulator --- Bitkit/Managers/SparkExitManager.swift | 24 ++++++++++++++++++------ BitkitTests/SparkExitReviewTests.swift | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 19f008df0..76ac13828 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -821,12 +821,12 @@ final class SparkExitManager { } await refreshRefusedSavingsPayment() await refreshNetworkFees(chain: chain) - await refreshFundingSeen(chain: chain, record: current) + let seen = await refreshFundingSeen(chain: chain, record: current) var updated = try await driver.tick(current) // A tick that adopted the funding and came up short would otherwise keep showing that payment as seen, and // one that forgot the exit's own payment would keep hiding the ask. if updated.phase != current.phase || updated.fundingPaymentTxid != current.fundingPaymentTxid { - await refreshFundingSeen(chain: chain, record: updated) + await refreshFundingSeen(chain: chain, record: updated, earlier: seen) } // The result may have been opened while this tick ran; a reset the driver made, such as for change kept // after the result was opened, stays. @@ -907,17 +907,29 @@ final class SparkExitManager { } /// Only new funding counts: the adopted inputs and the CPFP change at the address are already part of the exit. - private func refreshFundingSeen(chain: SparkExitChain, record: SparkExitRecord) async { - guard let address = record.funding.first?.address else { return } + /// Reads the funding address for `record`, falling back to `earlier` when the read fails; the UTXOs it went by. + @discardableResult + private func refreshFundingSeen( + chain: SparkExitChain, record: SparkExitRecord, earlier: [EsploraUtxo]? = nil + ) async -> [EsploraUtxo]? { + guard let address = record.funding.first?.address else { return nil } fundingAddress = address guard record.fundingShortfallSats != nil else { // Adopted, or nothing to fund: what was seen is part of the exit now, never funding still to count. fundingSeenSats = 0 fundingSeenCount = 0 setFunding(nil, for: record) - return + return nil } - await noteFundingSeen(record, utxos: try? chain.utxos(address: address)) + let utxos = await Self.fundingReread(try? chain.utxos(address: address), earlier: earlier) + noteFundingSeen(record, utxos: utxos) + return utxos + } + + /// The funding address read again after a tick changed the exit, or the tick's first read when that one failed: a + /// short savings payment the first read saw still counts, so the ask is for the rest, never the whole shortfall. + nonisolated static func fundingReread(_ read: [EsploraUtxo]?, earlier: [EsploraUtxo]?) -> [EsploraUtxo]? { + read ?? earlier } /// What `utxos` at the funding address, nil when unread, says the exit still needs. diff --git a/BitkitTests/SparkExitReviewTests.swift b/BitkitTests/SparkExitReviewTests.swift index d9a6eccf4..8135b139e 100644 --- a/BitkitTests/SparkExitReviewTests.swift +++ b/BitkitTests/SparkExitReviewTests.swift @@ -792,6 +792,20 @@ final class SparkExitReviewTests: XCTestCase { guard case .ask = SparkExitMath.fundingState(record, utxos: nil) else { return XCTFail("the ask comes back") } } + /// A tick that forgot a short savings payment and failed to read the address again still asks only for the rest. + func testAShortSavingsPaymentKeepsItsRemainderAskedWhenTheRereadFails() { + var record = SparkExitFixtures.record() + let short = [SparkExitFixtures.utxo("short", value: 8000)] + let utxos = SparkExitManager.fundingReread(nil, earlier: short) + XCTAssertEqual(utxos?.map(\.txid), ["short"]) + record.fundingPaymentTxid = nil + guard case let .ask(_, receivedSats) = SparkExitMath.fundingState(record, utxos: utxos) else { + return XCTFail("the rest is asked for") + } + XCTAssertEqual(receivedSats, 8000, "the payment the first read found still counts") + XCTAssertEqual(SparkExitManager.fundingReread([], earlier: short)?.count, 0, "a read that worked wins") + } + /// The unknown payment confirming late is adopted like any funding, and the exit builds. func testALateConfirmationOfAnUnknownFundingSendIsAdopted() async throws { var record = SparkExitFixtures.record() From 97bb885b091377ff249238e23b1df0341198a009 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 16:17:34 +0100 Subject: [PATCH 13/15] fix: hold the ask after a failed start or an unsaved retry payment A start whose savings payment fails reads the send journal at once, so cancel reflects a send that may still land; a paid-again retry stays paid in memory when its save fails. Verified: none (tests run after this commit) --- Bitkit/Managers/SparkExitManager.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 76ac13828..1803d82ca 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -442,7 +442,12 @@ final class SparkExitManager { guard var paid = record, paid.createdAt == current.createdAt, paid.isActive else { return } paid.fundingPaymentTxid = txid paid.savingsRefusal = nil - try saveRecord(paid) + // Kept as paid even when the save fails, so progress never offers to pay it again. + do { + try saveRecord(paid) + } catch { + Logger.error("Failed to save the Spark exit funding payment: \(error)", context: "SparkExitManager") + } record = paid setFunding(SparkExitMath.fundingState(paid, utxos: nil), for: paid) refusedSavingsPayment = nil @@ -1295,6 +1300,7 @@ final class SparkExitManager { } catch { // A refused payment shows on progress at once, with the offer to pay again. noteSavingsRefusal(started, sats: savingsSats, after: error) + await refreshSends() await refreshRefusedSavingsPayment() throw error } From cd847b35ae76da3935450e678b677f2c03f5619b Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 16:25:03 +0100 Subject: [PATCH 14/15] fix: refresh the funding after a background run forgets the exit's payment Verified: none (tests run after this commit) --- Bitkit/Managers/SparkExitManager.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 1803d82ca..764b1c540 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -1688,6 +1688,11 @@ final class SparkExitManager { ) if let updated = outcome.record { record = updated + // As after a tick: a run that forgot the exit's own payment (it confirmed short) or changed the phase reads + // the address, so Progress asks only for the rest. + if updated.phase != previous?.phase || updated.fundingPaymentTxid != previous?.fundingPaymentTxid { + await refreshFundingSeen(chain: chain, record: updated) + } if let previous, Self.isFundingAskWithdrawn(updated, previous: previous) { Self.dismissDeliveredFundingNotifications(record: updated) } From c8fd11e9cefccc1a7899e6ed086937091f5e9e11 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Thu, 26 Nov 2026 17:34:36 +0100 Subject: [PATCH 15/15] fix: pay a refused savings payment again only while it is still refused Progress re-reads the refused send under the lock before paying it again, and authorizes the retry only while the journal still holds it refused, so a retry from Send Pending since the last tick is never paid a second time. Verified: none (tests run after this commit) --- Bitkit/Managers/SparkExitManager.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Bitkit/Managers/SparkExitManager.swift b/Bitkit/Managers/SparkExitManager.swift index 764b1c540..9b8598719 100644 --- a/Bitkit/Managers/SparkExitManager.swift +++ b/Bitkit/Managers/SparkExitManager.swift @@ -423,12 +423,22 @@ final class SparkExitManager { if let stored = try? Self.loadRecord(store: store, lostLeafStore: lostLeafStore) { record = stored } + // Read again under the lock: a retry from Send Pending may have sent it since the last tick. + await refreshRefusedSavingsPayment() guard let current = record, let refused = refusedSavingsPayment else { return } let attempts = OnchainSendAttemptService.shared - let paidAgain = try await Self.payRefusedAgain(refused, record: current) { context in - try await attempts.retrySamePayment(using: LightningService.shared, context: context) { attempt, _ in - guard attempt.id == refused.id, attempt.address == refused.address else { throw OnchainSendAttemptError.unresolved } + let paidAgain: (txid: String, isAccepted: Bool)? + do { + paidAgain = try await Self.payRefusedAgain(refused, record: current) { context in + try await attempts.retrySamePayment(using: LightningService.shared, context: context) { attempt, _ in + guard attempt.id == refused.id, attempt.address == refused.address, attempt.status == .rejected + else { throw OnchainSendAttemptError.unresolved } + } } + } catch { + await refreshSends() + await refreshRefusedSavingsPayment() + throw error } await refreshRefusedSavingsPayment() guard let (txid, isAccepted) = paidAgain else {