Skip to content

fix: find a restored wallet's exit funding before the next quote - #1887

Merged
ovitrif merged 2 commits into
masterfrom
fix/spark-exit-restore-quote-scan
Nov 12, 2026
Merged

ovitrif merged 2 commits into
masterfrom
fix/spark-exit-restore-quote-scan

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Twin: synonymdev/bitkit-ios#1305

Description

Three gaps in the restore scan of #1782, raised by @coreyphillips on the iOS twin, which gets the same fixes:

  • A quote taken right after a restore could still reuse a lost exit's funding address. The quote read the restored index without waiting for the restore's scan to run. Now, while a restore is pending, an exit quote runs that scan before it picks its funding index. If the scan fails, the quote fails. A start never lowers an index the scan moved.
  • A forgotten exit's funding address that got a late payment stopped being tracked after a restore. The scan moved the index past it, but the address stayed listed only as a cancelled exit's, which is watched for about four weeks. It is now listed again as kept change, so it stays watched.
  • The forward scan read nothing from a restored index of 1,000 or more, because its read budget was an absolute index. The budget now counts from where the scan starts.

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Automated Checks

  • SparkExitRepoTest:
    • a quote while a restore is pending scans past the restored funding index first: with index 3 restored and address 3 funded, the quote funds from address 4 and lists address 3's coins.
    • a quote while a restore is pending fails when the scan fails: no quote, nothing kept.
    • a restored funding index at a forgotten exit's address lists its late payment as kept change.
  • SparkExitFundingTest: a funding scan from past its ceiling still reads forward.

Not tested

  • No device run: staging it needs a restore whose backup missed the last exit's index. The unit tests cover each path.

@ovitrif ovitrif self-assigned this Nov 12, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 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-12T11:39:14.400544Z 5f22319 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.

@greptile-apps

greptile-apps Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

The PR checks restored exit funding before choosing the next funding address.

  • Restore quotes now check funding addresses before choosing the next one.
  • Restored late payments stay listed as kept change.
  • Funding scans can continue beyond index 1,000.

The added tests cover restored-index recovery, scan failure, cancelled-address promotion, and scans starting at 1,000. Tests were inspected, not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Request exit quote] --> B{Recovery pending?}
  B -->|No| E[Choose next funding index]
  B -->|Yes| C[Scan funding addresses]
  C -->|Read or save fails| D[Fail quote and keep recovery pending]
  C -->|Success| F[Keep found coins and save higher index]
  F --> E
  E --> G[Start exit without lowering saved index]
Loading

Reviews (1) · Last reviewed commit: "fix: find a restored wallet's exit fundi..." · 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. While a restore is pending, an exit quote now runs the funding scan before it picks its index, a start never lowers an index the scan moved, a forgotten exit's funding address that holds coins is re-listed as kept change, and the scan ceiling counts from its start. Reviewed 5f22319, full tier.

What I checked, and 5 candidates I ruled out

Read in full: SparkExitRepo.kt (loadQuote, start, cancel/forget, refreshEarlierAddresses, nextFundingIndex, recoverFunding), SparkExitFunding.kt (SparkExitMath.nextFundingIndex, scanNextFundingIndex, fundingIndexAfterForget), SparkExitEarlierAddress.kt (isWatched, SparkExitEarlierAddressStore.add), the BackupRepo.kt restore hunk, and the four new tests.
Call sites traced: nextFundingIndex() has one caller (loadQuote); recoverFunding is called from it and from refreshEarlierAddresses.
Build: not built here. An unrequested review of this repo is reasoned from the code and CI. build-and-test, lint and ui-tests were still pending at review time; Greptile passed.

Ruled out

  • Quote after a restore with no chain hosts: chain null skips the scan, as before. nextFundingIndex then falls back to the kept index and the record, so this is not new behaviour.
  • Extra chain reads on every quote: recoverFunding returns null before reading anything unless fundingIndexKey is in sparkExitFundingRecoveryPending (or no index and no record exist), and a successful scan clears that flag.
  • Quote and foreground refresh scanning at once: both writes take maxOf of the cached index, and earlierAddressStore.add is idempotent per address under its file lock, so the two cannot regress the index or duplicate an entry.
  • A still-watched cancelled address converted to kept change: add replaces a CANCELLED_FUNDING entry with KEPT_CHANGE only when the address holds coins, which widens the watch from 28 days to always. A later exit that reuses the address still removes the entry in start.
  • Scan ceiling overflow: from + FUNDING_SCAN_MAX_ADDRESSES is computed in ULong and clamped to UInt.MAX_VALUE, and the new test covers a start at the old ceiling.

Merge confidence: 4/5. No findings and the change is covered by SparkExitRepoTest and SparkExitFundingTest, but build-and-test and lint had not finished when I read the PR.

Reviewer model: claude-opus-5-5
Reasoning effort: Unknown (not present in the run metadata available to this review)

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 286930c (run).

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

@ovitrif ovitrif added this to the 3.2.0 milestone Nov 12, 2026
@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Triage: milestone 3.2.0. This PR fixes the Spark emergency exit (#1782), which is 3.2.0 work, and closes no issue, so it carries the milestone itself.

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

@coreyphillips found on the iOS twin (synonymdev/bitkit-ios#1305) that the scan's gap term overflows for a restored index near the top of its range. On Android UInt wraps instead of trapping, so the scan would stop early there. Fixed the same way in 286930c: the loop compares in 64 bits, with its limit capped at UInt.MAX_VALUE. Test: a funding scan from the top of the index range ends instead of wrapping around.

Re-review is needed only because of this push: 5f22319...286930c

@jvsena42 jvsena42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No findings. This is the Android side of the quote-before-recovery gap I reproduced on synonymdev/bitkit-ios#1305.

  • The counter only goes up: start uses maxOf(current, quoteIndex + 1) (:815), recoverFunding writes maxOf(current, scanned) (:1187), and the restore merge is a per-key max.
  • A scan error fails the quote closed: it fails before pending is set, with no cache write and the restore still pending. a quote while a restore is pending fails when the scan fails covers it.
  • Non-pending quotes are unaffected: they cost one cache read and no network.
  • No funds path changes: no payment, broadcast or signing path is touched.

One note, not blocking: recoverFunding has no in-flight sharing. A quote opened while the foreground scan is running starts a second full scan against the single mainnet read host, and one failed read fails that quote. A Mutex with a re-read of the pending flag would let the quote reuse the running scan.

Device gate: no device check needed: it needs a restore whose backup missed the last exit's index; the unit tests cover it. perf: the first quote after a restore now waits for the scan (20+ sequential reads); worth timing once on a restored wallet.

Reviewer model: Claude Fable 5.2 (claude-fable-5-2), Claude Opus 5.5 (claude-opus-5-5)
Reasoning effort: not exposed

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

Two independent reviews, nothing blocking a merge.

nits

  • Start's never-lower funding index branch has no test (app/src/main/java/to/bitkit/repositories/SparkExitRepo.kt:815)

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Merging into master: approved by @jvsena42 on 286930c, required checks green, and the one review thread (a test for the never-lower start) answered and deferred to the next Spark exit PR.

@ovitrif
ovitrif merged commit 4b98e62 into master Nov 12, 2026
23 checks passed
@ovitrif
ovitrif deleted the fix/spark-exit-restore-quote-scan branch November 12, 2026 12:33
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.

4 participants