Skip to content

Wallet: support a custom unilateral exit address and fee rate - #1656

Merged
bhandras merged 3 commits into
mainfrom
sdk/manual-unilateral-exit-terms-20261126
Nov 27, 2026
Merged

bhandras merged 3 commits into
mainfrom
sdk/manual-unilateral-exit-terms-20261126

Conversation

@bhandras

@bhandras bhandras commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

What this enables

A wallet can choose a Bitcoin payout address and an initial fee rate before starting a unilateral exit. The read-only selection preview prices those settings, counts shared recovery transactions once, and estimates the extra fee funding separately from the network fees.

How it works

  • Validate the destination on the wallet’s network and apply the selected rate and destination dust policy to the quote. Return the validated settings and reasons for individual refusals. Previewing creates neither funding addresses nor exit jobs.
  • Persist the payout script and rate in the same transaction that marks the VTXO as exiting. Restart recovery reads the saved terms. The chosen rate applies to newly tracked packages and the final sweep. Existing shared packages keep their current publication settings.
  • Keep a funding shortfall actionable only when a deposit can help. An uneconomical or dusty exit keeps its refusal reason and receives no funding address.
  • Require explicit acknowledgement for custom terms. Record an external payout as an outflow instead of wallet proceeds.

Migration 34 supports both schema 33 histories. It ensures both tables exist and preserves saved exit settings and pending signed fee splits. The selection preview estimates confirmed change reuse. The current admission RPC still checks and admits each VTXO separately.

Verification

Generated protobuf and SQL artifacts with the repository tooling. Changed-code lint and focused daemon, SDK, unroll, and database tests passed. Migration tests cover preserved manual exit settings and a preserved pending fee split. Built the iOS simulator framework with the mobile bridge patch applied.

Persist the destination and initial rate atomically with admission.
Upgrade either schema 33 history without dropping terms or fee splits.
Use the persisted payout for the sweep and the reviewed initial rate
for new packages. Keep dust and uneconomical exits out of funding.
Expose custom payout and fee terms through the wallet RPC and SDK.
Price shared recovery transactions in the read-only selection preview.
Require explicit acknowledgement to start with custom terms.
@bhandras
bhandras force-pushed the sdk/manual-unilateral-exit-terms-20261126 branch from 60acb76 to 33fcd4c Compare November 27, 2026 14:00
@bhandras

Copy link
Copy Markdown
Member Author

/gateway review

@lightninglabs-gateway

Copy link
Copy Markdown

Cannot run /gateway review on a draft PR. Mark it ready for review first.

@bhandras
bhandras marked this pull request as ready for review November 27, 2026 14:14
@bhandras

Copy link
Copy Markdown
Member Author

/gateway review

Please review 33fcd4c. Focus on preserving the two schema 33 histories, atomic persistence and restart of custom payout terms, read-only preview pricing and dust checks, fee inputs, and the explicit acknowledgement for custom options. Flag concrete regressions in this diff, with the reachable caller, consequence and existing guard.

@lightninglabs-gateway

lightninglabs-gateway Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

❌ gateway review failed: code-review skill failed across all auth modes: You've hit your org's monthly spend limit · ask your admin to raise it at https://claude.ai/admin-settings/usage · your weekly limit resets Nov 29, 6am (UTC)

See workflow logs for details.

@bhandras

Copy link
Copy Markdown
Member Author

Claude: Verdict: approve 33fcd4c4.

I found no blockers. I traced each item in scope through the diff and its callers. I ran the targeted tests in db, unroll, txconfirm, waved, vtxo, swapwallet and sdk/wavewalletdk, and all pass.

Verified

Two schema 33 histories

  • Public 33 already has wallet_fee_split. Migration 34's CREATE TABLE IF NOT EXISTS leaves the pending split alone and adds vtxo_manual_exit_options.
  • The other 33 already has vtxo_manual_exit_options but no wallet_fee_split. Migration 34 adds the missing table and keeps the saved exit terms.
  • No Go post-migration step is tied to version 33.
  • TestManualExitMigrationAfterFeeSplit and TestManualExitMigrationFromMobile33 cover both paths.

Atomic admission and restart

  • UpdateVTXOStatusWithExitOptions (db/vtxo_store.go:907) writes three things in one transaction: the UnilateralExit status, the terms row, and the optional reservation release.
  • processOutbox saves status updates before it sends the ExpiringNotification. So the unroll job cannot start before its terms are on disk.
  • The unroll actor loads its descriptor through GetVTXO, which reads the terms for UnilateralExit and Spent. Orphan recovery after a crash rebuilds the same payout.
  • When a job ends, its child actor is stopped. A re-admission gets a fresh actor, not a cached descriptor.

Stale terms are cleared

  • clearStaleManualExitOptions deletes the terms on Live, Expired, and on a plain UnilateralExit write.
  • I checked every caller of UpdateVTXOStatus. None writes UnilateralExit over an exit that is already running, so active terms are never wiped.
  • InsertVTXO is ON CONFLICT DO UPDATE, so it cannot trigger the cascade delete.

Reviewed payout and rate reach the sweep and txconfirm

  • buildSweepTx uses the saved script and rate only under the standard timeout policy. validateManualExitTarget already limits custom terms to standard Bitcoin VTXOs.
  • repriceSweep copies the old output script, so a fee-rejected sweep keeps the chosen destination.
  • In txconfirm, the first publication of a zero-value anchor package uses InitialFeeRateSatPerVByte, clamped to the broadcaster maximum. Later bumps use the estimator, as documented.

Ledger

  • DestinationOwnWallet now comes from an owned-script lookup on the actual sweep output.
  • A foreign payout is booked as an outflow, and the proceeds UTXO record is skipped.

Preview costs and funding addresses

  • A funding address is created only when FundingShortfallSat > 0.
  • Dust and uneconomical plans now return zero funding figures and no address. The rewritten test drives the real planner.
  • TotalEstimatedFeeSat is set only when every entry was quoted. The proto says it can overestimate when exits share recovery transactions.

Backwards compatibility

  • A new client sending custom terms to an older daemon uses the ..._WITH_CUSTOM_EXIT_OPTIONS ack. The old daemon rejects that ack, so the exit fails closed and never uses the default destination.
  • An old client sending to a new daemon is unchanged.
  • A cooperative exit that carries custom fields is rejected.

Non-blocking follow-ups

1. New terms on an exit already in progress are silently ignored.

  • Trigger: Exit with custom terms on an outpoint that is already UnilateralExit.
  • Path: UnrollWithOptions returns Created=false early (waved/rpc_server.go:5319). swapwallet returns that with no error.
  • Impact: the earlier terms stay in force. Funds still go to a destination the user chose earlier, or to the wallet. Nothing is lost, but the caller may think the new address was applied.
  • Existing recovery: a detailed ExitStatus shows the saved sweep_address.
  • Smallest fix: when options are given, compare them with desc.ManualExitOptions and return FailedPrecondition if they differ.

2. A preview against an older daemon ignores custom terms.

  • Trigger: a new SDK calls GetExitPlan or PreviewUnilateralExit with a sweep address or fee rate against an older daemon.
  • Impact: the quote silently uses the estimator rate and the default destination. It is read-only, and the later Exit still fails closed.
  • Smallest fix: in the SDK, fail when an address was requested but the echoed SweepAddress is empty.

3. Test gap.

  • No test drives the whole chain: ForceUnrollEvent → VTXO actor outbox → UpdateVTXOStatusWithExitOptions → unroll GetVTXO → sweep.
  • Each piece is covered on its own. One actor-level test would lock the ordering in.

Pre-existing or documented limits (no action needed)

  • txconfirm dedups by txid. A recovery transaction shared with another exit keeps the first requester's rate, as EnsureConfirmedReq documents.
  • The manual rate sets only the first publication. Interval bumps and sweep reprices use the estimator.
  • The custom-rate limit comes from the unroll max fee rate. If that is ever set above txconfirm's max, txconfirm clamps the package rate without notice.
  • The test for the other schema 33 history builds the pre-existing table from the new up-migration. Matching columns is therefore an assumption of the test, not something it proves. If an existing table differed, IF NOT EXISTS would keep it and the new queries would fail at runtime.

@bhandras
bhandras marked this pull request as draft November 27, 2026 15:40
@bhandras
bhandras marked this pull request as ready for review November 27, 2026 16:09
@bhandras
bhandras merged commit e34995f into main Nov 27, 2026
29 of 30 checks passed
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.

1 participant