Skip to content

fix: record a refused lightning paykit payment before its cleanup - #1547

Merged
piotr-iohk merged 4 commits into
masterfrom
fix/paykit-refusal-before-cleanup
Nov 28, 2026
Merged

piotr-iohk merged 4 commits into
masterfrom
fix/paykit-refusal-before-cleanup

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1522
Twin: synonymdev/bitkit-android#2176

This PR records a refused Lightning Paykit payment as unsent before its proof is cleared, so a crash in between can no longer leave the attempt open.

Description

  • Fixes the automatic allowance payment leaving its attempt open after a kill, because a refusal at the recheck before submission cleared the proof and released the payment list while the journal still read sending. The journal now moves back to submitted first, which recovery reads as never sent and records as failed once the node has no payment for the hash.
  • Fixes the same for the manual BOLT11 payment of a request, whose journal read sent during that cleanup.
  • Recovery removes the started Lightning proof of such an attempt before it fails it, so the unpaid request comes back to the pending list. It removes only that request's proof, so another request paying the same invoice keeps its own, and it leaves the attempt open when the removal cannot be saved.
  • Splits the "did anything reach the node" check out of the proof service, so the executor can read it before the cleanup runs.

Out of Scope

Design

N/A — no UI changes.

Preview

N/A — no user-visible changes.

QA Notes

Setup

N/A — the changed window (a kill between the refusal's cleanup and its ledger write) cannot be staged on a device; unit tests cover it.

Journeys

N/A — no user-visible behaviour change.

Manual Tests

N/A

Automated Checks

  • added PaykitAllowanceExecutorTests.swift — a refused automatic payment is journaled submitted when its proof is cleared; recovery fails a refused manual payment whose outcome was never recorded and removes its proof first; recovery removes the started proof of a refused automatic payment before failing it; recovery leaves a refused payment open when its proof cannot be removed
  • added PaykitPaymentProofServiceTests.swift — a failed Lightning payment of one request keeps another request's and another identity's proof of the same invoice
  • updated PaykitAllowanceExecutorTests.swift — an uncertain send stays sending when the proof would be cleared

Evidence

  • Unit suite (CI selection) on 1908942's production code, iOS 26.5 simulator: 4743 tests, 0 failures; PaykitAllowanceExecutorTests and PaykitPaymentProofServiceTests again on 1908942 after the test-only tightening: 248 tests, 0 failures.
  • Smoke on 1013dbf: fresh install, new wallet, kill and relaunch reach Home with no allowance recovery warnings in the log.

Gaps

  • The refusal itself on a device (a contact blocked between review and submission, with a linked issuer and a funded regtest Lightning wallet) was not run.

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Gemini 4 Neon (reasoning: high)
  • Review rounds: 1

When the recheck before submission refuses a Lightning payment of a
Paykit request, the automatic and manual paths now move the journal
entry back to submitted before the proof is cleared and the payment
list released. Recovery after a kill in between, or after a failed
ledger write, then finds an unsent attempt and records it as failed
instead of leaving it open.

Verified: unit suite (BitkitTests, CI selection) on an iOS 26.5 simulator, 4740 tests, 0 failures; swiftformat --lint on the changed files
The uncertain-send test now reads the journal when the proof would be
cleared, so a guard that journals every failure as unsent fails it, and
the manual recovery test seeds a manual ledger attempt.

Verified: PaykitAllowanceExecutorTests on an iOS 26.5 simulator, 145 tests, 0 failures; swiftformat --lint
@ovitrif ovitrif self-assigned this Nov 27, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T22:01:07.280179Z 1916bb5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ovitrif

ovitrif commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

QA for 1013dbf:

Tested on iPhone 17 simulator, iOS 26.5.

🟢 Unit suite, App starts and recovers on this build
Unit suite

The unit suite passes on this commit, including both new recovery tests; the allowance executor tests pass again on 1916bb5, which only tightens tests.


App starts and recovers on this build

A fresh install creates a wallet, and a kill and relaunch come back to Home with no allowance recovery warnings in the log.

smoke2x.mp4

Device tests skipped ⚠️:

  • Refusal at the recheck before sending, then a kill before the outcome is saved: the kill window cannot be staged on a device, and the refusal needs a linked issuer, a funded regtest Lightning wallet and a contact blocked between review and send; the unit tests cover it.

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Confidence Score: 4/5 · The refusal write must be checked before cleanup for this crash fix to be safe to merge.RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change is limited to Lightning Paykit request payment recovery.

The refusal write must be checked before cleanup for this crash fix to be safe to merge.

Findings

  1. P1 Refused payments can stay blocked ▶

Summary

This PR saves a refused Lightning payment as .submitted before clearing its proof.

  • Lightning Paykit refusals are saved as unsent before proof cleanup.

ovitrif explicitly deferred other manual pre-send failures and automatic proof-association failures to #1545. Those paths are not reported here.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Lightning payment refused] --> B[Save submitted stage]
    B --> C{Save succeeded?}
    C -- Yes --> D[Clear proof]
    C -- No, error hidden --> D
    D --> E{App stops before failed outcome?}
    E -- No --> F[Record failed outcome]
    E -- Yes --> G[Recover from saved stage]
    G --> H[Old sending or sent stage stays open]
Loading

Reviews (1) · Last reviewed commit: "test: check the journal an uncertain lig..." · Reviewed by Greptile

@talosmachina talosmachina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings. Moves a refused Lightning Paykit payment's journal entry back to submitted before its proof is cleared, on the automatic and the manual path, so recovery after a kill fails it instead of leaving it open. Reviewed 1916bb5, full tier.

What I checked, and 5 candidates I ruled out

Read in full: payLightning and lightningPaymentSettled in PaykitAllowanceExecutor.swift, verifiedOutcome and the recovery loop, setStage / setStageDurably, beginManualPayment / manualLightningPaymentSent, PaykitPaymentProofService.failLightningPayment, SendConfirmationView.sendLightningPayment and its manual caller.
Traced: automatic refusal -> journal .submitted -> recovery's .prepared, .submitted branch asks the node by hash, and .failed, .unknown resolves to .failed. Manual refusal (up front and at the recheck) -> journal moves from .sent to .submitted before failLightningPayment(paymentHash:) -> same recovery branch.
CI: SwiftFormat, validate and Greptile green; unit and integration runs still pending at review time.
Build: Not built: on this track the review reasons from the code and CI, and iOS does not build on Linux.

Ruled out

  • The executor and the proof service disagree on "sent nothing": failLightningPayment(paymentHash:submissionError:) now guards on the same static submissionSentNothing, so the stage is moved back exactly when the cleanup runs.
  • A settlement already recorded gets overwritten with .submitted: setStage skips terminal stages, and lightningPaymentSettled still matches .submitted.
  • .submitted makes the wallet offer an already-paid request again: hasSentPayment counts .sending/.sent/.unknown/.succeeded only, and a refused send left nothing at the node.
  • Recovery fails a payment the node does know: a hash the node reports .pending or .succeeded still wins in the .submitted branch.
  • An uncertain error loses its sending stage: the guard only fires when submissionSentNothing is true, and the updated test pins .sending for the uncertain case.

Merge confidence: 4/5, the change is covered by the new PaykitAllowanceExecutorTests cases but CI's unit run had not finished and the branch was not built here.

Reviewer model: Claude Opus 5.5
Reasoning effort: Not exposed (the reviewer's run metadata does not carry a reasoning setting)

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: full PR diff against its merge base, at 1916bb5.

1 actionable finding — resolve or provide an evidence-backed rebuttal.

A kill after the unsent journal save and before the started Lightning proof is deleted closes an automatic refusal and sends its queued acceptance, while the pending list still hides the request. The new tests check the journal stage and the ledger outcome, not a proof left in place.

The resolved comment about a failed journal save was withdrawn. That case keeps the old stage, and retaining the proof would not change it. The finding here is the successful save.

Compared the same ordering on bitkit-android#2176 at 2484af16. That PR also writes submitted before deleting the proof, and its request refresh also omits paymentStarted proofs from the pending list. That was a comparison of this flow only.

Device testing was not performed. The author reports the unit suite and a fresh-install smoke on 1013dbf, and PaykitAllowanceExecutorTests again on 1916bb5. This review did not re-run them.

Suggested additional test cases:

  • iOS unit: automatic refusal whose journal is left at submitted, with the Lightning proof still stored (paymentStarted, payment hash, no preimage) and node lookup unknown. Recover. The proof is removed, the request is not in the in-flight set, and the recorded outcome is failed.

Findings

  • [MEDIUM] Remove the started proof when recovery fails an unsent refusal — inline at Bitkit/Services/PaykitAllowanceExecutor.swift:1364.

Reviewer model: grok-4.7
Reasoning effort: xhigh

Recovery of a refusal killed between its unsent journal save and the
proof cleanup recorded the attempt as failed but kept the proof, which
hid the request from the pending list. Recovery now removes the
Lightning proof of an attempt the node never saw before it fails it,
and leaves the attempt open when the removal cannot be saved.

Verified: unit suite (CI selection) on an iOS 26.5 simulator; swiftformat --lint on the changed files
@ovitrif
ovitrif requested a review from piotr-iohk November 28, 2026 06:56

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: changes since 1916bb56, including affected code paths, at bcef29a3. Baseline: previous review. Inherited coverage is the manual refusal's journal rewind and the submissionSentNothing split. This pass reassessed recovery's new proof removal.

1 actionable finding — resolve or provide an evidence-backed rebuttal.

Recovery of an unsent Lightning attempt deletes every pending proof with that payment hash. Another request that still holds the same invoice loses its started proof, which is what keeps that request off the pending list and what a later success uses to notify the payee. Limit the removal to the attempt's own request, as bitkit-android#2176 does at 249f54e9. The hash-wide helper is tracked in #1510; this commit adds a launch-time caller.

Compared the recovery proof-removal contract on the Android twin at 249f54e9. Device testing: not performed in this review. The new PaykitAllowanceExecutorTests cases check call order on a mock.

Suggested additional test cases

  • iOS unit: two Paykit requests share one Lightning invoice and both have a started proof. One journal entry is submitted, the other is sending, and the node lookup for that hash is unknown. Run allowance recovery. The submitted attempt is recorded failed, and the sending request's proof is still stored.

Earlier findings reconciled at this revision: 1 verified fixed.

Findings

  • [MEDIUM] Scope recovery proof removal to the attempt's request — inline at Bitkit/Services/PaykitAllowanceExecutor.swift:876.

Reviewer model: grok-4.7
Reasoning effort: xhigh

Two Paykit requests can hold started proofs for the same invoice.
Recovery's removal matched the payment hash alone, so failing one
request's unsent attempt also deleted the other request's proof, which
put that request back on the pending list and lost its payee proof.
Recovery now removes the proof of the attempt's request and identity.

Verified: unit suite (CI selection) on an iOS 26.5 simulator before the test-only tightening, then PaykitAllowanceExecutorTests and PaykitPaymentProofServiceTests; swiftformat --lint on the changed files
@ovitrif
ovitrif requested a review from piotr-iohk November 28, 2026 07:34

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: changes since bcef29a3, including the affected recovery and proof-removal paths, at 19089426. Baseline: previous QA review.

No new actionable code findings.

Recovery removes the Lightning proof for the failed attempt's request and payer identity before it saves the outcome. A removal that cannot be saved leaves the attempt open. Another request's proof of the same invoice, and another identity's proof of the same request, remain. An unsent payment is unknown to the node, so later proof reconciliation leaves that other proof in place.

On bitkit-android#2176 at 249f54e9, recovery also discards only that request's proof before saving the outcome. iOS also matches the payer identity. Inherited from the baseline: the manual Lightning refusal rewinds the journal before proof cleanup, and submissionSentNothing stays separate from proof deletion.

Device testing: not performed in this review. The author's manual checks stay N/A; this follow-up is the proof-selection predicate, and the kill window cannot be staged on a device. PaykitAllowanceExecutorTests.swift and PaykitPaymentProofServiceTests.swift were inspected and not executed here.

Earlier findings reconciled at this revision: 1 verified fixed.

Reviewer model: grok-4.7
Reasoning effort: xhigh

@piotr-iohk
piotr-iohk enabled auto-merge November 28, 2026 08:34
@piotr-iohk
piotr-iohk merged commit d808a7c into master Nov 28, 2026
21 checks passed
@piotr-iohk
piotr-iohk deleted the fix/paykit-refusal-before-cleanup branch November 28, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: refused lightning paykit payment can stay open after a crash

3 participants