Repository navigation
fix: find exit funding past a restored counter - #1305
Conversation
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. |
|
Triage: milestone 3.2.0. This PR fixes the 3.2.0 Spark emergency exit after a restore and closes no issue, so it carries the milestone itself. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f53eb9dd92
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
[Medium impact] The change reaches Spark exit recovery for wallets with a pending restore. Hold merging until quotes wait for successful funding recovery. FindingsSummaryAdds a forward scan from the restored funding counter and a test for finding coins at that counter.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Restored funding counter] --> B[Recover funding]
B --> C[Scan forward]
C --> D[List addresses holding coins]
D --> E[Save counter and clear pending]
A --> F[Load quote independently]
F --> G[Cache restored index]
G --> H[Start exit with cached index]
Reviews (1) · Last reviewed commit: "fix: find a restored wallet's exit fundi..." · Reviewed by Greptile |
talosmachina
left a comment
There was a problem hiding this comment.
No findings. After a restore, recoverFunding now scans the exit funding addresses forward from the restored counter (20-address gap) as well as reading every address below it, so an exit funded at or past a stale counter is listed as kept change and the counter moves past it. Reviewed f53eb9d, full tier, static: no build, because iOS does not build on this reviewer's Linux host.
What I checked, and 4 candidates I ruled out
Read in full: recoverFunding, nextFundingIndex(_:now:), refreshEarlierAddresses, loadQuote, useQuote, runWhileActive in SparkExitManager.swift; scanNextFundingIndex and nextFundingIndex(counter:record:) in SparkExitFunding.swift; the restore tests in SparkExitFundingIndexTests.swift
Call sites traced: recoverFunding (refreshEarlierAddresses, nextFundingIndex), refreshEarlierAddresses (runWhileActive, the Spark connection screen's .task and .refreshable)
CI: SwiftFormat and Greptile green; Run Tests still running at review time
Ruled out
- Counter moving backwards: the scan starts at the restored index, so
scanned >= restored, and the write still takesmaxwith the stored value. - The active exit's funding listed as kept change:
watchedincludesrecord.fundingaddresses, so a forward-scanned address the stored record owns is skipped. - Greptile's P2, counters of 1000 or more: reaching it takes a thousand exits on one wallet, each a multi-day unilateral exit.
- Greptile's P1, a quote reusing the old address:
nextFundingIndex(_:now:)returns a kept counter without checking the pending flag, so while recovery has not yet succeededloadQuotecan still pick the restored index. That path is the same before this diff, which only changes what a successful recovery finds; it is not introduced here. The Spark connection screen, where the exit starts, already starts recovery in its.task.
Merge confidence: 4/5, no findings, but the unit-test job had not finished when I reviewed; testARestoredCounterThatPredatesTheLastExitsFundingFindsItsCoins covers the change.
Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the runtime does not report it)
jvsena42
left a comment
There was a problem hiding this comment.
No new findings; I added a refinement on Greptile's "Quotes can reuse old funding" thread.
Matches Android synonymdev/bitkit-android#1782 (46c16d103):
- same
scanNextFundingIndex(from:); - same
0..<kept + usedcandidates; - same
maxon the counter write.
The second Android fix isn't needed: try recovery.earlierAddressStore.add(...) (:1277) and every chain read throw before removeObject(pendingKey) (:1280), so a failure keeps the restore pending. Other properties:
- The counter can't move backwards in recovery.
usedonly holds indexes at or above the restored one.watchedkeeps the active exit's funding out of kept change.
Device gate: no device check needed: only an extra row under earlier exit addresses after a restore, which needs a backup that missed the last counter; the unit test covers it. perf: 20+ sequential Esplora reads, only while a restore is pending, repeated per foreground if Esplora keeps failing. CI: Run Tests still pending at f53eb9dd9.
Reviewer model: Claude Fable 5.2 (claude-fable-5-2), Claude Opus 5.5 (claude-opus-5-5)
Reasoning effort: not exposed
coreyphillips
left a comment
There was a problem hiding this comment.
Two independent reviews, nothing blocking a merge.
worth doing, does not block
- A quote taken before the pending recovery finishes still reuses the counter's address (Bitkit/Managers/SparkExitManager.swift:1230-1236)
- Forward scan skips past a forgotten exit's address that received a late payment (Bitkit/Managers/SparkExitManager.swift:1271-1279)
- Forward scans stop at absolute index 1000 (Bitkit/Services/SparkExit/SparkExitFunding.swift:591-594)
nits
- Restore comment still says the exit only looks below the counter (Bitkit/Services/SparkExit/SparkExitFunding.swift:749).
SparkExitFundingIndexes.restoresays "The exit then looks once for funding addresses below it that still hold coins and nothing watches." With this change it also scans forward from the counter and can move the counter.
Updated in 38447fb, along with your three "worth doing" points (answered in their threads). The description now covers them. The Android twin has the same three paths on Re-review is needed only because of this push: f53eb9d...38447fb |
|
The Android fixes are up in synonymdev/bitkit-android#1887. |
coreyphillips
left a comment
There was a problem hiding this comment.
Rechecked after 1 new commit (38447fb). Nothing blocking remains.
addressed
- A quote taken before the pending recovery finishes still reuses the counter's address (Bitkit/Managers/SparkExitManager.swift:1230-1236). Fixed:
SparkExitManager.nextFundingIndex(_:now:)(SparkExitManager.swift:1232-1235) now awaitsrecoverFundingbefore it reads the counter.recoverFundingruns whilependingKeyis set (:1248) and throws before clearing it (:1279-1282), soloadQuote(:896) either gets 4 or fails.start()also writes the counter withmax(:980). I ranSparkExitFundingIndexTestsat 38447fb on a simulator and all 20 passed,testAQuoteWhileARestoreIsPendingScansPastTheRestoredCounterFirstamong them. - Forward scan skips past a forgotten exit's address that received a late payment (Bitkit/Managers/SparkExitManager.swift:1271-1279). Fixed:
watchednow leaves out.cancelledFundingentries (SparkExitManager.swift:1272-1273). A forgotten exit's address that the forward scan finds used and holding coins gets added as.keptChange.SparkExitEarlierAddressStore.add(SparkExitEarlierAddresses.swift:119-126) replaces the cancelled entry with it, so the address is watched for good.testARestoredCounterAtAForgottenExitsAddressListsItsLatePaymentAsKeptChangepassed in my local run. - Restore comment still says the exit only looks below the counter (Bitkit/Services/SparkExit/SparkExitFunding.swift:749). Fixed: The comment at SparkExitFunding.swift:751-752 now describes both reads: each funding address below the counter, and the forward scan that can move the counter.
- Forward scans stop at absolute index 1000 (Bitkit/Services/SparkExit/SparkExitFunding.swift:591-594). Fixed: The scan's limit is now
start + fundingScanMaxAddresses(SparkExitFunding.swift:594-596). I compiled the loop on its own: with start 1000 and address 1000 used, it returns 1001.testARestoredCounterPastTheScanCeilingStillScansForwardpassed in my local run.
new since the last review
worth doing, does not block
- Funding scan still traps when the restored counter is near UInt32.max (Bitkit/Services/SparkExit/SparkExitFunding.swift:594-596)
coreyphillips
left a comment
There was a problem hiding this comment.
Rechecked after 1 new commit (0f9f29c). Nothing blocking remains.
addressed
- Funding scan still traps when the restored counter is near UInt32.max (Bitkit/Services/SparkExit/SparkExitFunding.swift:594-596). Fixed: In
SparkExitFunding.swift:595-596, both loop bounds are now computed in UInt64, andlimitis capped atUInt32.max. Sinceindex < limit, neithernext = index + 1norindex += 1can overflow. I compiled the loop verbatim at -Onone with overflow checks on and ran it fromUInt32.max,max - 1,max - 5,max - 25,max - 500andmax - 1500, with every address used, none used, and mixed. None of them trapped. Frommax - 5with every address used it returnsUInt32.maxafter 5 reads. Starts of 0 and 3 give the same next index and reads as the existing tests expect. The new test asserts that case. One related path is left, but it is older than this PR:Array(0 ..< $0)atSparkExitManager.swift:1269still sizes itself by the restored counter. It needs the same corrupt backup, so I am not raising it here.
jvsena42
left a comment
There was a problem hiding this comment.
Re-approving at 0f9f29c18. The scan bounds now use 64-bit arithmetic, min(start + fundingScanMaxAddresses, UInt32.max). A counter near the top of the range ends the scan instead of overflowing, and index < limit <= UInt32.max keeps next = index + 1 in range. My earlier note on the quote path stands on the Greptile thread.
|
Merging into |
Twin: synonymdev/bitkit-android#1782
Description
recoverFundingalready throws before it clears the pending flag.Design
N/A — no UI changes.
Preview
N/A
QA Notes
Automated Checks
SparkExitFundingIndexTests.testARestoredCounterThatPredatesTheLastExitsFundingFindsItsCoins: with counter 3 restored and address 3 funded, address 3's coins are listed and the counter moves to 4.testAQuoteWhileARestoreIsPendingScansPastTheRestoredCounterFirstandtestAQuoteWhileARestoreIsPendingFailsWhenTheScanFails: a quote after the restore funds from address 4 and lists address 3's coins; a failed scan fails the quote and keeps the restore pending.testARestoredCounterAtAForgottenExitsAddressListsItsLatePaymentAsKeptChange: a forgotten exit's address with a late payment becomes kept change.testARestoredCounterPastTheScanCeilingStillScansForward: counter 1,000 restored, address 1,000's coins are found.testARestoredCounterBringsBackTheFundingAddressesThatStillHoldCoinsnow expects the forward scan's reads, 3 to 22.testAFundingIndexRestoredFromTheBackupIsOnlyScannedForwardFrom(renamed) expects only the forward reads.Not tested