Repository navigation
fix: recheck paykit request authorization at submission - #1493
Conversation
A payment request's Lightning send, manual or by an allowance, checks its authorization again after its last await before submission: on Spark right before spends.run, on the node right before its queue. Refused there, nothing is sent and it ends like a refusal up front. Verified: PaykitPaymentRequestServiceTests, ChannelFreeRailTests, PaykitPaymentProofServiceTests (new tests fail with the recheck off), unit test selection on the simulator, swiftformat --lint
Verified: PaykitPaymentRequestServiceTests, SendConfirmationViewTests, ChannelFreeRailTests, PaykitPaymentProofServiceTests, PaykitAllowanceExecutorTests; swiftformat --lint
Verified: PaykitAllowanceExecutorTests, ChannelFreeRailTests, PaykitPaymentProofServiceTests, PaykitPaymentRequestServiceTests, SendConfirmationViewTests; swiftformat --lint
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[Medium impact] The change is limited to Paykit Lightning request payments across manual and allowance workflows. Fix the node's queued-submission race before merging. Findings
SummaryAdds a late authorization check to manual and allowance Lightning payments.
Acknowledged scope: Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Authorize request] --> B[Prepare payment]
B --> C[Recheck authorization]
C --> D{Payment path}
D --> E[Spark spending check]
E --> F[Send through Spark]
D --> G[Wait for node queue]
G --> H[Check deadline and admit attempt]
H --> I[Send through node]
J[Contact blocked during queue wait] -. Not rechecked .-> H
Reviews (1) · Last reviewed commit: "test: cover the allowance recheck refusa..." · Reviewed by Greptile |
talosmachina
left a comment
There was a problem hiding this comment.
Adds a recheckBeforeSubmission closure to the Lightning send so a Paykit request's authorization (ensurePaymentAllowed) runs again after the send's last awaits: on Spark before spends.run, on the node before submitPayment, for manual sends (SendConfirmationView, LnurlPayConfirm) and allowance payments (PaykitAllowanceLivePayer.payLightning).
No findings. Reviewed head 8d42b27, full tier, static only: an iOS branch does not build on this Linux host, so nothing was built or run here. Run Tests and Run Integration Tests were still pending when this was posted.
Scope note: the PR says Closes #1455, but two of that issue's four bullets (on-chain via Spark in PaykitRouteService.payReportingAllowance, and on-chain via the node through beforeBroadcastAttempt then the LDK queue) are untouched here. If those are meant for a follow-up, say so in the body or change Closes to Refs so the issue stays open.
Merge confidence: 4/5, no findings, held below 5 by tests still pending on CI and no build or device run on this head.
What was checked
- Spark path:
SparkManager.sendnow rechecks after the quote and balance awaits, outsidespends.run. A refusal there is aPaykitPaymentRequestError, notChannelFreeRailSendRefused, soLightningService.sendtakes its genericcatchwithdidRecordAttempt == false. That marks nothing uncertain and releases nothing, which is correct because nothing was recorded. - Node path: the recheck runs after the
settledEarlierAttemptawaits and beforeServiceQueue.background(.ldk). The queue wait after it is a window the recheck cannot cover, sincebeforeSubmissionis sync on a dispatch queue. The code comment names that boundary, so I read it as deliberate and did not post it. - Manual sends: the recheck reuses the same
authorizeandonAuthorizationFailure, so a refusal fails the proof, setsdefinitePreBroadcastFailure, and inSendConfirmationViewskips the!lightningPaymentSubmittedbranch throughlightningAuthorizationRefused, so it never lands on the pending screen. The outer catch still finishes the manual allowance attempt as.failed, becauselightningPaymentSubmittedis false. LnurlPayConfirm: settinglightningPaymentHash = nilin the failure callback skips the outer proof-failure branch, and the proof was already removed in the callback.- Allowance path:
PaykitAllowanceLivePayer.recheckBeforeSubmissionkeepsrequestExpiredand maps every other error torequestUnavailable.PaykitPaymentProofService.failLightningPayment(paymentHash:submissionError:)now treats that as unsent, so the executor records.failedand releases the list, not.unknown. The live payer'sensurePaymentAllowedcallsPaykitPaymentRequestService().ensurePaymentAllowed, which is free of side effects (deadline plus alinkedPeersread), so running it twice changes no state. ensurePaymentAllowedon the manager (manual path) re-readslinkedPeersand checksstateGenerationand the identity on every call, so a block or sign-out between the two checks is seen. On refusal it drops the request fromapprovedPaymentRequestIds.- Other
LightningService.sendcallers (QuickPay, swap hold invoice, transfer, move-to-spark) keep the default{}and are unchanged. - Tests read:
testLightningSendRechecksAuthorizationBeforeSubmissionAfterItsAwaitsblocks the peer insidesendand asserts no send, one failure callback and the approval removed, for both routes.
Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the runtime does not report it)
@talosmachina Right: this PR covers the Lightning paths only. Switched to |
pwltr
left a comment
There was a problem hiding this comment.
Reviewed 8d42b27.
Low candidate: A late refusal can discard another request’s Lightning proof. Bitkit/Services/PaykitPaymentProofService.swift:1378 deletes every pending Lightning proof with the same hash. Manual BOLT11 (Bitkit/Views/Wallets/Send/SendConfirmationView.swift:2095), LNURL (Bitkit/Views/Wallets/Send/LnurlPayConfirm.swift:418), and allowance payments (Bitkit/Services/PaykitPaymentProofService.swift:1388) now reach that cleanup when the late authorization check refuses. If two requests share an invoice and the first is pending on LDK, refusing the second can erase the proof record the first needs to deliver its preimage. Scope late-refusal cleanup to the refused request and its payer identity.
Pre-existing Low candidates: A contact block can still land between authorization and dispatch. The peer check runs before the serial LDK queue wait (Bitkit/Services/LightningService.swift:1363) and before Spark’s main-actor spend admission (Bitkit/Managers/SparkManager.swift:703). If contact removal commits in either gap, a valid automatic request can still reach the rail SDK. The same gaps existed on the base.
Review details
Quick review
Model: Claude Opus 5.5
Reasoning effort: high
General review
Model: GPT-6.2 Sol
Reasoning effort: high
Combined review
Model: GPT-6.2 Sol
Reasoning effort: high
@pwltr out of scope: tracked in #1510, because the hash-wide cleanup in |
An on-chain payment request checked its authorization once, then awaited more before the send. The node send now checks it again after its allowance attempt is marked sending, right before the broadcast, and the Spark withdrawal after the same mark, right before Spark sends. A refusal there sends nothing and releases the request. Verified: PaykitRouteTests, PaykitPaymentRequestServiceTests, PaykitPaymentProofServiceTests, SendConfirmationViewTests, PaykitAllowanceExecutorTests, ChannelFreeRailTests; swiftformat --lint
@talosmachina both on-chain paths are covered in 9f4bd27, so |
|
@ovitrif checked against
So |
@talosmachina yes, it is the accepted boundary for both node rails, as on the Android twin. Out of Scope now says so, tracked in #1513. |
…recheck Verified: xcodebuild build-for-testing (arm64 simulator)
Verified: xcodebuild test of PaykitPaymentRequestService, PaykitPaymentProofService, SendConfirmationView, PaykitAllowanceExecutor, ChannelFreeRail and SparkSendKey tests (755 pass); the new test fails on the previous head
Verified: xcodebuild test of the same six test classes (755 pass), swiftformat --lint
Verified: xcodebuild test of PaykitPaymentRequestService, PaykitPaymentProofService, SendConfirmationView, PaykitAllowanceExecutor, ChannelFreeRail and SparkSendKey tests (756 pass), swiftformat --lint
|
Merged master into this PR in 6e45af1 to resolve a conflict in
Also added an Out of Scope entry for retries from the pending screen: #1516. |
Device run on 7905714Ran on an iPhone 17 Pro simulator (iOS 27.0), Debug build of
The PR lists no manual tests or journeys (QA Notes: N/A), so these two cases carry no PR IDs. No refusal at submission was logged in either case. The refusal window itself is covered by the PR's unit tests (not rerun here). |
pwltr
left a comment
There was a problem hiding this comment.
Reviewed 7905714.
Low: A definite Lightning refusal leaves no durable record that the attempt was unsent until the failed outcome is saved. The automatic path (Bitkit/Services/PaykitAllowanceExecutor.swift:1356) and manual BOLT11 path (Bitkit/Views/Wallets/Send/SendConfirmationView.swift:2103) both clear the proof and release the private payment list before recording failure. If the app stops before that write or the write fails, recovery sees a fresh invoice with no outbound payment and leaves the attempt open. The wallet can then hide the unpaid request or reject another attempt as already recorded. Persist the definite refusal in a form recovery can use before cleaning up either path.
Review details
Quick review: Claude Opus 5.5, high reasoning effort
General review: GPT-6.2 Sol, high reasoning effort
Verification: GPT-6.2 Sol, high reasoning effort
@pwltr Agreed. I'm keeping it out of this PR so the approved head and its device run stand; it's tracked in #1522. Merging: approved on 7905714, CI green, and the device run on this head is posted above. |
Closes #1455
Twin: synonymdev/bitkit-android#2070
Description
ensurePaymentAllowed) ran once, before the send's last awaits.recheckBeforeSubmissionclosure, run after its last await before submission: inSparkManager.sendbeforespends.run, and on the node right beforesubmitPayment. Manual request sends (SendConfirmationView,LnurlPayConfirm) pass the request's authorization; a refusal there sends nothing and ends like a refusal up front (proof failed, private list released).PaykitAllowanceLivePayer.payLightning. Any failure of that recheck becomesrequestUnavailable(an expired request staysrequestExpired), whichPaykitPaymentProofServicetreats as unsent, so the attempt ends and its reservation is released.SendConfirmationView.sendOnchainPaymentauthorizes again afteronAuthorized(which marks a manual allowance sending), right before the broadcast; a refusal fails the started proof before the send's guard is cleared and ends asOnchainSendAttemptError.preDispatch; a manual allowance already marked sending ends as failed first. On Spark,PaykitRouteService.payReportingAllowanceauthorizes again aftermarkSending; a refusal ends asSparkWithdrawalNotSentand the allowance attempt fails.Out of Scope
ServiceQueue.background(.ldk), so a block landing while the send waits there is not seen. The block check reads the Paykit SDK, which is async and can't run in the queue's synchronous closure: bug: a node send waiting in its queue pays a contact blocked meanwhile #1513, raised in fix: recheck paykit request authorization at submission #1493 (comment)Design
N/A — no UI changes.
Preview
N/A — no user-visible changes.
QA Notes
Setup
Device run on 7905714 (#1493 (comment)): a Spark Lightning payment request and an on-chain payment request, both paid on a Debug build against bitkit-docker's local Spark stack and payment request fixture (regtest). The block itself lands inside a window a few awaits wide, which unit tests reach and a device can't time.
Journeys
N/A — no user-visible behaviour change.
Manual Tests
N/A
Automated Checks
ChannelFreeRailTests.swift— a send refused at the recheck before submission leaves no holdPaykitAllowanceExecutorTests.swift— a failed allowance recheck ends as a refusal the proof treats as unsentPaykitPaymentRequestServiceTests.swift— a Lightning request send rechecks its authorization before submission, after its awaitsPaykitPaymentRequestServiceTests.swift— an on-chain request send rechecks its authorization after its attempt is marked sending, and a refusal there fails the proof firstPaykitRouteTests.swift— a Spark on-chain payment authorizes again aftermarkSending, and a refusal there sends nothingPaykitPaymentProofServiceTests.swift—requestUnavailableat submission clears the proof as unsent; a rollback failure at the on-chain recheck keeps the matching attempt without a broadcastPaykitPaymentRequestServiceTests.swift— a Lightning recheck refusal of a retry still in doubt comes back pending and keeps its proofSparkSendKeyTests.swift— a Spark leaf-selection retry checks the request's authorization, then its deadlineEvidence
swiftformat --linton 7905714.Gaps
submitPaymenthas no unit test: the node isnilin unit tests.Models used
Claude Opus 5.5(reasoning:medium),Claude Sonnet 5.5(reasoning:high)Gemini 4 Neon(reasoning:high)