Skip to content

fix: bind send approval to the prepared mining fee - #1952

Merged
ovitrif merged 7 commits into
masterfrom
fix/bind-send-approval-to-prepared-fee
Nov 17, 2026
Merged

ovitrif merged 7 commits into
masterfrom
fix/bind-send-approval-to-prepared-fee

Conversation

@jvsena42

@jvsena42 jvsena42 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Fixes #1657
Twin: synonymdev/bitkit-ios#1350

This PR binds an on-chain send's approval to the mining fee of the transaction that is actually broadcast. Before, Confirm showed an estimate, the transaction was prepared again after the swipe (coin selection could pick different inputs), and it was broadcast without comparing its fee to the one approved.

Description

  • LightningRepo.sendOnChain takes an optional reviewer. After preparation and before retainPreparedReceipt, a fee at or below the approved one sends as before. A higher fee suspends on the reviewer while the prepared handle is held, then broadcasts that same handle once. Spark funding and Spark exit pass no reviewer and keep their existing OnchainSendChangedError.
  • AppViewModel captures the fee shown at the start of the send as the approved fee and, on a higher prepared fee, puts the actual fee and amount on Confirm, re-arms the fee warnings and requires a new swipe plus PIN/biometric. Paykit payer and ensurePaymentAllowed are re-checked after approval. Edits and tag changes are blocked during the review.
  • The review ends without sending, through the existing not-dispatched cleanup, when it is cancelled, after 2 minutes, when the user leaves Confirm/PIN, or when the node stops. A cancel the user did not make shows "payment not sent", deferred until the app is back in the foreground and unlocked if it happened in the background. The prepared handle is released before the node stops, and a handle from a replaced node is refused before anything is persisted. Review endings log at info, not error.
  • On-chain max sends now show the drain fee (estimateSendAllFee), which is what the send prepares. With an anchor reserve the old fixed-amount estimate was lower than the drain, so the confirmation understated the fee. Max-send fee warnings use the shown fee.
  • New string wallet__send_fee_changed_not_sent; the review toast reuses the existing fees-changed strings.

Out of Scope

  • Hardware-wallet sends: HwSendViewModel composes again before signing without an in-app fee comparison; the device confirms the signed transaction. Left out because fix: hold hardware sends before broadcasting #1772 is reworking that path.
  • LightningService.stop() callers outside stopLocked (e.g. resetNetworkGraph) do not release the reviewed handle first; the stale-node check refuses it on approval.
  • Root cause of the coin-selection mismatch (estimate lets LDK choose inputs, send uses the settings algorithm) is not changed; this PR guards the fee instead.

Design

N/A — no design available. Reuses the existing Confirm screen and toasts.

Preview

N/A — not run on a device yet.

QA Notes

Setup

devDebug build on an emulator against staging regtest, fresh wallet funded with ./lsp, PIN with "Also require for payments" on, coin selection First-In-First-Out. Confirm preselects inputs and the send reuses them, so an ordinary send does not diverge; the reproduction (in prepared-fee-review.xml) is an amount within about one fee of the whole balance, so preselection fails, plus a deposit confirmed while Confirm is open.

Journeys

  • J1: new prepared-fee-review.xml — a higher prepared fee is shown for re-approval, then sent once; Back sends nothing; includes the working reproduction and emulator driving notes

Manual Tests

N/A

Automated Checks

  • added LightningRepoTest.kt — higher fee is not broadcast before approval and is broadcast once after; cancel, deadline expiry, node stop and a replaced node send nothing and release the attempt; request attempts are released through the existing cleanup
  • added AppViewModelSendFlowTest.kt — review shows the actual fee, needs a new swipe, sends once; dismiss, reset, timeout, leaving Confirm and node interruption send nothing with the right toast; max sends show and warn on the drain fee; tags blocked during review
  • updated SparkExitRepoTest.kt, SparkFundingViewModelTest.kt — stubs match the new parameter

Evidence

  • ./gradlew testDevDebugUnitTest at f5ae14a: 6163 tests, 0 failures. detekt --rerun-tasks and lintDevDebug pass.
  • Two independent review rounds; all findings fixed in 9847c17 and f5ae14a.
  • Device run on emulator at f5ae14a, 2026-11-17 (recordings kept locally). The higher fee reproduced on every attempt with the J1 recipe:
    • Approve: Confirm 180 sats, review 277 with the Fees changed toast; Speed/Fee, amount and Add Tag did nothing during the review; second swipe asked for the PIN again; paid 277 once (dc605f47…462a). PIN → sent 0.8 s.
    • Back: Confirm 110, review 209, Back closed the sheet in under 0.4 s, nothing sent; the next 5,000-sat send paid its shown 141 (0df897ff…1fea).
    • 2-minute window: cancelled 120.2 s after the review started with the not-sent toast, nothing sent.
    • Background during review: node stopped 8.6 s after Home, review cancelled, nothing sent.
    • Ordinary send: Confirm 141, paid 141, one swipe (a55560f8…481d).
    • Max send: Confirm 314, paid 314, no second prompt (cc9e2f4e…1a64).
    • No crashes or ANRs; no second sendOnChain, selection or fee calculation after a review started.

Review round 1 (fixes at 94675a0):

  • e74236a releases the prepared send at once when a review ends without approval, so its change output is not left reserved until GC (Greptile, talosmachina). a review that ends without approval releases the prepared send at once failed before the fix.
  • 94675a0 closes the send sheet after review cleanup only if it still belongs to the same send (Greptile). a review ended by its node stopping does not close a send opened during its cleanup failed before the fix.
  • Full testDevDebugUnitTest (6167), detekt and lint pass.

Gaps

  • ⚠️ Not verified on a device: 0db5986 (not-sent toast deferred to foreground and unlock; review endings logged at info). Its device run showed the toast fired while backgrounded and was missed; the fix is covered by AppViewModelSendFlowTest.kt.
  • Side findings from the device run, not caused by this PR: the Received Bitcoin sheet can stack over Confirm after a deposit and take the swipe; the first swipe after a sheet change is sometimes ignored.
  • The SendSheet null back-stack-entry skip is Compose code with no unit test.
  • New string translations are machine-written, not reviewed by native speakers.
  • f5ae14a and 0db5986 were not independently re-reviewed.

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: Not exposed)
  • Implementation: Claude Fable 5.2 (reasoning: Not exposed)
  • Review: Claude Fable 5.2 (reasoning: Not exposed)
  • Review rounds: 2

🤖 Generated with Claude Code

jvsena42 and others added 3 commits November 17, 2026 07:54
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 94675a0 (run).

Download bitkit-dev-debug universal APK (expires in 30 days).

jvsena42 and others added 2 commits November 17, 2026 10:32
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jvsena42
jvsena42 marked this pull request as ready for review November 17, 2026 13:33
@jvsena42
jvsena42 requested review from a team, ben-kaufman and ovitrif and removed request for a team November 17, 2026 13:33
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The reviewed changes appear safe to merge; both previous findings are fixed.

Summary

The PR requires renewed approval when a prepared Bitcoin transaction costs more than the fee first shown.

  • A higher prepared fee sends only after the user approves it again.
  • Max sends show the fee for spending the wallet’s spendable coins.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Prepare transaction] --> B{Fee above approved fee?}
  B -->|No| E[Save receipt and broadcast]
  B -->|Yes| C[Show actual fee for approval]
  C -->|Approved on same node| E
  C -->|Cancelled or interrupted| D[Release prepared transaction]
  D --> F[Clean up unsent attempt]
  F --> G[Close only the original send sheet]
Loading

Reviews (2) · Last reviewed commit: "fix: keep a new send open when an ended ..." · Reviewed by Greptile

@ovitrif

ovitrif commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Triage: this PR goes in the 3.2.0 milestone under #1657. Twin: synonymdev/bitkit-ios#1350.

@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.

1 finding (1 P2). Holds an on-chain send whose prepared mining fee is above the approved one and asks for a fresh swipe and PIN on the actual fee instead of failing it, with the review torn down on node stop, sheet dismissal or a 2 min timeout. Reviewed 0d85e0170, full tier, static.

What I checked, and 5 candidates I ruled out

Read: the diff of LightningRepo.kt (checkApprovedSpend, stopLocked, sendOnChain around the prepare step), OnchainSendCoordinator.kt, LightningService.prepareOnchainSend, AppViewModel.kt (reviewPreparedFee, onConfirmPay, onSwipeToPay, handleSanityChecks, getFeeEstimate, resetSendState), SendSheet.kt, SendConfirmScreen.kt; ldk-node PreparedOnchainSend (broadcast, Drop) at the fork's main.
Not built: per this repo's track rules an auto review does not run the branch's Gradle build; this is reasoned from code and CI.
CI: build-and-test, lint, detekt, compose-ui and build-local green; e2e-tests-local - lightning_security still pending at review time.

Ruled out

  • Double release on stop: stopLocked takes the reference with getAndSet(null), and the review path releases only when its compareAndSet(prepared, null) wins, so destroy runs once.
  • Broadcast after release: broadcast checks isReleased under its mutex before submit, and isUsable also checks the node and wallet index the receipt was signed with.
  • An old swipe approving the new fee: onConfirmPay completes the review only when isSwiped was set by a swipe made after the review started, and reviewPreparedFee clears shouldConfirmPay when it opens.
  • Editing the reviewed send: changesReviewedSend blocks fee, amount, address and method events, tags are frozen, back closes the sheet, and the final isSameSend check compares amount, address and fee.
  • Fee-over-threshold warnings skipped: the review drops the two fee warnings from confirmedWarnings and handleSanityChecks re-runs on preparedFeeSats.

Merge confidence: 4/5, one P2 on the cancel path, and the change was not built here.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (no setting is reported by this runner)

@jvsena42
jvsena42 marked this pull request as draft November 17, 2026 14:49
jvsena42 and others added 2 commits November 17, 2026 12:00
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@ovi-reviewer ovi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: ⚠️ Not verified — draft PR.

Findings: No new findings reported; code review was skipped. The existing prepared-handle cleanup finding and stale send-sheet cleanup finding were not independently verified or duplicated here.

QA for 0d85e01:

Review and approve a higher mining fee ⏭️ J1.1: not driven while the PR is draft.

The base repository rules require AI reviewers to skip drafts until ready for review.

Cancel a higher-fee payment ⏭️ J1.2: not driven while the PR is draft.

Skipped under the same draft-review rule.

Coverage: full review of 5fa540e9..0d85e01 was requested but not performed. No build, unit tests or device cases ran; all changed runtime behavior remains unverified by this run. Author-reported results were read as context and are not counted as passes.

Twin: synonymdev/bitkit-ios#1350 targets the same fee-approval behavior; implementation parity was not reviewed.


Reviewed by gpt-6.1-sol-high

@jvsena42
jvsena42 marked this pull request as ready for review November 17, 2026 15:02

@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.

Re-review of 94675a051 after 0d85e0170. 1 earlier finding: 1 fixed. Greptile's two threads: both fixed. 0 new findings. Full tier on the delta, static.

What I checked

Delta read: git diff 0d85e0170..94675a051, LightningRepo.checkApprovedSpend and stopLocked, PreparedOnchainSend.release/broadcast, AppViewModel.proceedWithPayment through showOnchainPaymentFailure, the three new tests.

Earlier finding (unreleased handle on a cancelled review): fixed in e74236a40. finally now releases unless isKept, and isKept is set only after an approval on a usable handle. A review that throws (back, dismissal, route lost, timeout, Paykit re-check) and the node-stop branch both release now. compareAndSet against stopLocked's getAndSet(null) keeps discard to one call, and an approved handle is still never released before broadcast.

Greptile, old cleanup closing a new sheet: fixed in 94675a051. session is taken in proceedWithPayment before the transition delay, passed through proceedWithOnchainPayment and the failure cleanup, and compared with sendSession after failOnchainPayment and reconcile() return. Both places that open a new send or dismiss the sheet bump sendSession, so a send opened during cleanup is never closed. The node-stop branch is gated the same way now.

Not built: Bitkit Android reviews are reasoned from code and CI here.

CI at 94675a051: build-and-test, build-local, lint and detekt green. The red build is from a superseded run: its BUILD_RESULT and COMPOSE_RESULT are cancelled, so it is not this change. compose-ui and the e2e shards were still pending when I posted.

Merge confidence: 4/5, no open findings. It is not 5 because this is a send-path change that was not built here and the e2e shards had not finished.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (this runner does not report a setting)

@ovi-reviewer ovi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: ✅ Approve

Findings: No new findings. The prepared-handle release and stale send-sheet cleanup findings are fixed at 94675a0, consistent with the earlier re-review.

QA for 94675a0:

Built from 94675a0. DevDebug on an Android 16 Pixel 10 Pro emulator, staging regtest, FIFO coin selection and PIN required for payments.

Review and approve a higher mining fee ✅ Fee rose from 180 to 277 sats; a fresh swipe and PIN sent it once.

J1.1: The amount stayed at 99,820 sats. Fee, amount and tag edits did not open another screen during review. The sent activity showed the approved 277-sat fee. App logs confirmed one sendOnChain invocation and one created sent activity.

J1-1-2x.2x.mp4

Higher-fee approval and the resulting activity, at 2× speed.

Cancel a higher-fee payment ✅ Back cancelled the 207-sat review without sending.

J1.2: The original fee was 110 sats. Back closed the sheet without a success screen or error toast, the full 99,903-sat balance remained, and a new 5,000-sat send reached Confirm. App logs contained no new sent activity for the cancelled attempt.

J1-2-2x.2x.mp4

Cancellation and a fresh confirmation, at 2× speed.

Report a review interrupted in the background ✅ Node stop cancelled the review; the not-sent toast appeared after unlocking.

J1.3: The fee rose from 180 to 277 sats. Backgrounding stopped the node and cancelled the review, as checked in app logs. Returning showed the PIN lock first; unlocking showed the not-sent toast. The full 149,903-sat balance remained, with no new sent activity.

J1-3-2x.2x.mp4

Background interruption, foreground lock and deferred toast, at 2× speed.

Let a higher-fee review expire ✅ The review ended after two minutes without sending.

J1.4: The fee rose from 246 to 345 sats. Without another approval, the sheet closed and the not-sent toast appeared. App logs measured 120.04 seconds from review to cancellation and contained no new sent activity. The full 199,903-sat balance remained; a fresh 5,000-sat send reached Confirm.

J1-4-2x.2x.mp4

Review expiry and a fresh confirmation, at 2× speed.

Check: Prepared-handle and send-session cleanup ✅ Focused regression tests passed.

./gradlew assembleDevDebug and ./gradlew testDevDebugUnitTest --tests to.bitkit.repositories.LightningRepoTest --tests to.bitkit.viewmodels.AppViewModelSendFlowTest passed at 94675a0. LightningRepoTest.kt verifies immediate release after an unsuccessful review and retention through approved broadcast. AppViewModelSendFlowTest.kt verifies preservation of a newer send sheet while the previous send's reconciliation finishes.

Coverage: delta 0d85e01…94675a0, four files, plus the previously open cleanup findings. Concurrent cleanup and release counts were checked by unit tests, rather than device instrumentation. The unchanged ordinary-send and max-send paths rely on the author's device evidence at f5ae14a13; they were not repeated. No planned case remains unrun.

Twin: synonymdev/bitkit-ios#1350 fix: bind send approval to the prepared mining fee enforces the same approval behavior; iOS retains the candidate across send invocations while Android waits within the original send.


Reviewed by gpt-6.1-sol-high

@ovitrif ovitrif 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.

utAck

@ovitrif

ovitrif commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Merged, as it was approved and green.

@ovitrif
ovitrif merged commit 2ce626e into master Nov 17, 2026
36 of 40 checks passed
@ovitrif
ovitrif deleted the fix/bind-send-approval-to-prepared-fee branch November 17, 2026 17:25
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: initial send can exceed the reviewed mining fee

3 participants