-
Notifications
You must be signed in to change notification settings - Fork 44
fix: check LN payment status before resetting on dev fee timeout #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+456
−48
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
efedc88
fix: check LN payment status before resetting on dev fee timeout
mostronator 1ae150a
fix: move items before test module to satisfy clippy lint
grunch 9ae2fe8
fix: store real payment hash before sending dev fee payment
grunch 1d395b7
docs: update dev fee timeout safety for two-phase payment flow
grunch 1597d0a
gRPC stream error is silently discarded, producing a misleading error…
grunch da94768
Add an empty-string guard before decoding
grunch 5be5f59
fix: reuse single LndConnector in dev fee scheduler job
grunch 7ed9c36
fix format
grunch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Dev Fee Timeout Safety — Duplicate Payment Prevention | ||
|
|
||
| ## Overview | ||
|
|
||
| When a dev fee Lightning payment times out, Mostrod queries the LN node for | ||
| the actual payment status before deciding whether to reset and retry. This | ||
| prevents duplicate payments caused by race conditions between timeouts and | ||
| in-flight payments. | ||
|
|
||
| ## Problem | ||
|
|
||
| Originally, when the 50-second timeout expired during a dev fee payment, the | ||
| code unconditionally reset `dev_fee_paid = false` and cleared the payment hash. | ||
| However, a timeout does not mean the payment failed — the Lightning payment | ||
| could still be in-flight or may have succeeded after the timeout window. | ||
|
|
||
| This created a race condition: | ||
|
|
||
| 1. Payment initiated, times out locally (but still in-flight on LN) | ||
| 2. Code resets order to unpaid state | ||
| 3. Original payment succeeds on LN (but local state already reset) | ||
| 4. Next scheduler cycle finds the order as "unpaid" and initiates a second payment | ||
| 5. **Result: double payment** | ||
|
|
||
| See [Issue #568](https://github.com/MostroP2P/mostro/issues/568) for full details. | ||
|
|
||
| ## Solution | ||
|
|
||
| ### Two-phase payment flow (`src/app/release.rs`, `src/scheduler.rs`) | ||
|
|
||
| The dev fee payment is split into two phases: | ||
|
|
||
| 1. **Resolve phase** (`resolve_dev_fee_invoice`): LNURL resolution + invoice | ||
| decode to extract the real LN payment hash. | ||
| 2. **Send phase** (`send_dev_fee_payment`): Sends the pre-resolved invoice via | ||
| LND. | ||
|
|
||
| The real payment hash is stored in `dev_fee_payment_hash` **before** the payment | ||
| is dispatched. This ensures that on timeout or crash, the hash is always | ||
| available for querying LND. | ||
|
|
||
| ### `LndConnector::check_payment_status()` (`src/lightning/mod.rs`) | ||
|
|
||
| Queries the LN node for the current status of a payment using `TrackPaymentV2`. | ||
| Returns the LND `PaymentStatus` enum (Succeeded, InFlight, Failed, Unknown). | ||
|
|
||
| ### `check_dev_fee_payment_status()` (`src/scheduler.rs`) | ||
|
|
||
| Helper that: | ||
| 1. Extracts the payment hash from the order (skips `PENDING-` markers, which | ||
| are legacy placeholders that cannot be tracked on LND) | ||
| 2. Decodes the hex hash to bytes | ||
| 3. Queries LND with a 10-second timeout | ||
| 4. If payment succeeded: marks order as paid in DB | ||
| 5. Returns a `DevFeePaymentState` enum for the caller | ||
|
|
||
| With the two-phase flow, new payments always have a real hash stored before | ||
| sending, so step 1 passes through to the LND query. The `PENDING-` guard | ||
| remains only for backward compatibility with legacy markers from before this | ||
| change — those correctly return `DevFeePaymentState::Unknown` since there is | ||
| genuinely no trackable hash. | ||
|
|
||
| ### Timeout handler in `job_process_dev_fee_payment()` (`src/scheduler.rs`) | ||
|
|
||
| Instead of unconditionally resetting on timeout: | ||
|
|
||
| | LN Payment Status | Action | | ||
| |-------------------|--------| | ||
| | **Succeeded** | Mark as paid in DB, do NOT reset | | ||
| | **InFlight** | Skip reset, leave state intact (payment may still complete) | | ||
| | **Failed** | Safe to reset `dev_fee_paid = false` and retry | | ||
| | **Unknown** | Skip reset to err on the side of caution (avoid duplicate) | | ||
|
|
||
| ### Stale real-hash cleanup (`src/scheduler.rs`) | ||
|
|
||
| A cleanup pass runs each cycle for orders that have `dev_fee_paid = true` and a | ||
| real (non-PENDING) payment hash. This handles crash recovery: if the process | ||
| crashes between storing the hash and receiving LND confirmation, the cleanup | ||
| queries LND and resets failed payments for retry. | ||
|
|
||
| ### Design Principle | ||
|
|
||
| **When in doubt, don't retry.** A missed dev fee payment can be recovered | ||
| manually, but a duplicate payment is money lost. The code errs on the side of | ||
| caution — only resetting when the LN node confirms the payment definitively | ||
| failed. | ||
|
|
||
| ## Related | ||
|
|
||
| - Issue: [#568](https://github.com/MostroP2P/mostro/issues/568) | ||
| - Dev fee invoice resolution: `src/app/release.rs` (`resolve_dev_fee_invoice`) | ||
| - Dev fee payment send: `src/app/release.rs` (`send_dev_fee_payment`) | ||
| - Dev fee scheduler: `src/scheduler.rs` (`job_process_dev_fee_payment`) | ||
| - Dev fee documentation: `docs/DEV_FEE.md` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.