Skip to content

fix: wait a check before an unverified spark exit rebuild - #1886

Merged
ovitrif merged 7 commits into
masterfrom
fix/spark-exit-unverified-rebuild-wait
Nov 12, 2026
Merged

ovitrif merged 7 commits into
masterfrom
fix/spark-exit-unverified-rebuild-wait

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Twin: synonymdev/bitkit-ios#1306

Description

  • Fixes the emergency exit rebuilding over a step that may already be sent when a single chain read fails during a check.
  • Since Breez SDK 0.28.2 (chore: bump breez sdk to 0.28.2 #1881), check_recover_funds marks a step Unverified when one outspend query for its inputs fails, and clears it on the next clean check. The driver rebuilt over any unverified unilateral step while nothing else was ready to send. So one rate-limited chain read could rebuild over a step already broadcast, with the same funding. If that build hit InsufficientCpfpFunds, the exit also asked the user for fee funding until a clean check withdrew the ask.
  • The driver now rebuilds only when every unilateral step it can't read was already Unverified in the stored recovery before the current check. A step one failed read turns unverified holds the rebuild until the next check, the same as a step that is ready to send. A step that stays unread rebuilds at the first check where no other step just turned unverified, once a block at most. When other leaves' reads keep failing, that can take a few checks.
  • When funding for an unverified rebuild's fee ask arrives after a clean check read the step again, the rebuild no longer runs: the ask ends and the funding stays at the address. When the payment confirms on a check that turns a step unverified again, the resume waits for the next check, as the check's own rebuild does, since a clean check before it may have sent that step. A tick whose check fails never resumes it.
  • Raised by @coreyphillips on the iOS twin, which gets the same fix.

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Automated Checks

  • SparkExitDriverTest:
    • a step one failed read turns unverified waits for the next check instead of rebuilding: a ready step that one check turns unverified is neither rebuilt over nor sent, and the exit rebuilds only once the next check still can't read it.
    • a step one failed read turns unverified holds the rebuild over another leaf's unread step.
    • a funded unverified ask whose step reads again ends without a rebuild.
    • a funded unverified ask holds its rebuild over a step sent on an earlier clean check: a step sent on a clean check while the payment was unconfirmed is not rebuilt over when the payment is adopted on a tick whose check can't read it, nor on a next tick whose check fails.
    • The existing unverified-rebuild tests keep their results.

@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:30:56.298017Z d32ef9e 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.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d32ef9e9cd

ℹ️ 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".

@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 driver now waits one check before rebuilding over a newly Unverified unilateral step.

  • Spark exits wait a check before rebuilding over a newly unread step.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Check returns an unread unilateral step] --> B{Same transaction unread before this check?}
  B -->|No| C[Save checked recovery and wait]
  C --> A
  B -->|Yes| D{No ready unilateral steps and no rebuild this block?}
  D -->|Yes| E[Try rebuilding]
  D -->|No| C
Loading

Reviews (1) · Last reviewed commit: "fix: wait a check before rebuilding over..." · Reviewed by Greptile

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

@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. Makes the Spark exit driver rebuild over an unverified unilateral step only when that same txid was already Unverified in the stored recovery before this check, so one failed outspend read waits a check instead of rebuilding over a step that may be in flight. Reviewed d32ef9e, full tier, static.

What I checked, and 5 candidates I ruled out

Read in full: SparkExitDriver.kt at the head (check, driveWhileFunding, broadcastReady, buildFeeBump, withConfirmations, needsUnverifiedRebuild), and the new test in SparkExitDriverTest.kt.
Build: not built here. A Bitkit Android review never runs the branch's Gradle code on this box, so this is reasoned from the code and from CI.
CI: build-and-test, lint, build-local and ui-tests were still pending when I posted.

Ruled out

  • stored is not the previous check's result: check copies checked.recovery into the record it returns, so the next tick's stored carries the statuses of the last check. When a check throws, broadcastWithoutSdk leaves recovery alone, so the comparison goes back to the last successful check, which is still "before this one".
  • A step one read turned unverified gets sent anyway: broadcastReady only sends Ready steps or cooperative unverified ones, so a unilateral step waits, as the PR says. The new test checks this (chain.broadcasts.none { it.txid == "n2" }).
  • A fresh build whose steps come back unverified now waits two blocks: no. The adopted build's recovery is stored, so the next check that still can't read the step finds it in unreadBefore and rebuilds then. The unverifiedRebuiltAtHeight once-per-block guard is unchanged.
  • Reads that flip back and forth (Unverified, Ready, Unverified) never rebuild: on the Ready checks the step is sent, which is what a rebuild is for, so nothing gets stuck.
  • The fee-bump and funding paths skip the new guard: buildFeeBump sets unverifiedRebuiltAtHeight itself and runs before this guard. driveWhileFunding never calls needsUnverifiedRebuild, and its isAskOver still clears the ask on a clean check.

Codex's KDoc comment is a style point that has already been raised, so I left it out.

Merge confidence: 4/5. Files read whole and the change is covered by a step one failed read turns unverified waits for the next check instead of rebuilding, but I did not build it here and CI was still pending.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed (the run metadata available to this reviewer does not expose a setting)

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 60d49ce (run).

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

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

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

@coreyphillips Your recheck on the iOS twin (synonymdev/bitkit-ios#1306) found that the guard still rebuilt over a step one failed read had just turned unverified, when another leaf's step was already unread. Fixed here the same way in 6b436dc: the rebuild needs every unverified unilateral step to have been unverified before this check. New test: a step one failed read turns unverified holds the rebuild over another leaf's unread step.

Because the push was needed anyway, it also moves the private function's KDoc into a comment at the call site, which was Codex's point.

Re-review is needed only because of this push: d32ef9e...6b436dc

@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. In breez 0.28.2, status_after_check marks a step Unverified only when an outspend read fails that check, and a clean check clears it (unilateral_exit.rs:1077-1097); a third-party spend or reorg arrives as Redo, which this doesn't touch. So:

  • The hold can't stall an exit for good. A step that stays unread is in unreadBefore on the next tick and rebuilds one tick later, and a step that flaps is sent on its clean tick.
  • The hold returns before unverifiedRebuiltAtHeight is set, so no block is lost.
  • Unverified already wasn't broadcast by broadcastReady, so timelock exposure is unchanged.
  • Fewer rebuilds means fewer savings funding asks.

The iOS twin synonymdev/bitkit-ios#1306 (8150b1480, bfa6268a2) uses the same predicate and ships the same two tests.

Device gate: no device check needed: no UI change, and it only matters with a failing chain read during an exit. perf: at most one extra tick before an unverified rebuild.

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.

Rechecked after 1 new commit (6b436dc). Nothing blocking remains.

new since the last review

worth doing, does not block

  • A step that stays unread can now wait several blocks for its rebuild (app/src/main/java/to/bitkit/repositories/SparkExitDriver.kt:808-810)

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

@coreyphillips Your recheck on the iOS twin (synonymdev/bitkit-ios#1306) found that a funded unverified ask could still rebuild over a step a clean check had made ready and sent. The Android resume in buildOnceFunded had the same path. Fixed the same way in f204417: with a unilateral step Ready, or none unverified, the resume ends the ask instead of building. Test: a funded unverified ask whose step reads again ends without a rebuild.

Re-review is needed only because of this push: 6b436dc...f204417

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

Rechecked after 2 new commits (f204417, 26aa61c). One point still blocks a merge.

addressed

  • A step that stays unread can now wait several blocks for its rebuild (app/src/main/java/to/bitkit/repositories/SparkExitDriver.kt:808-810). Fixed: The point asked that the call-site comment and the description be corrected if the delay is acceptable. SparkExitDriver.kt:548-549 now says a step that stays unread is rebuilt at the first check, at most once a block, where no other step just turned unverified. The description now says the same and adds that this can take a few checks while other leaves' reads keep failing. needsUnverifiedRebuild (SparkExitDriver.kt:812-823) is unchanged, so the new wording matches the code.

new since the last review

needs changing before merge

  • Funded unverified ask still rebuilds over a step sent on an earlier clean check (app/src/main/java/to/bitkit/repositories/SparkExitDriver.kt:207-217)

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

Re-approving at 26aa61c77. f20441738 ends a funded unverified ask without rebuilding once its unilateral steps read again (any Ready, or none Unverified). A rebuild then would conflict with the step about to be sent at the same fee. The funding already adopted stays in the record for a later rebuild or the leftover sweep, so the savings payment the user approved is neither repeated nor lost. The new SparkExitDriverTest case covers it, and iOS synonymdev/bitkit-ios#1306 (0869a9e34) uses the same predicate.

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed 45bd734: the funded unverified ask's resume now holds over a step this tick's check just turned unverified, using the recovery from before the check, as the check path's rebuild does. A step sent on an earlier clean check is no longer rebuilt over when the payment confirms on a tick whose read fails. New SparkExitDriverTest case covers the sequence. The description says the same.

Re-review is needed only because of this push: 26aa61c...45bd734

@ovitrif
ovitrif dismissed coreyphillips’s stale review November 12, 2026 12:57

Fixed in 45bd734; the push asks for a re-review.

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

Rechecked after 1 new commit (45bd734). Nothing blocking remains.

addressed

  • Funded unverified ask still rebuilds over a step sent on an earlier clean check (app/src/main/java/to/bitkit/repositories/SparkExitDriver.kt:207-217). Fixed: tick now passes start.recovery to buildOnceFunded (SparkExitDriver.kt:157), and the resume returns early when any unilateral Unverified step was not Unverified in that recovery (SparkExitDriver.kt:224-230), matching needsUnverifiedRebuild. I traced your sequence through the new test at SparkExitDriverTest.kt:603-642. On the tick at 1_001 the funding is adopted, but unreadBefore taken from the ready recovery is empty while n2 is Unverified, so it returns funded and recover is never called. I could not run the test because the Breez bindings and bitkit-core are not in the local Gradle cache.

new since the last review

worth doing, does not block

  • Funded resume still rebuilds over a just-unread step when the next check fails (app/src/main/java/to/bitkit/repositories/SparkExitDriver.kt:224-233)

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed fe43ec3, test only: the new hold test now asserts the payment was adopted on the tick that holds the rebuild, so it cannot pass without reaching the hold. Same assertion as the iOS twin, synonymdev/bitkit-ios#1306 (31f884871), from @coreyphillips's nit there.

Re-review is needed only because of this push: 45bd734...fe43ec3

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

Re-approving at 45bd7340c. buildOnceFunded now takes the tick-start recovery as stored. A funded unverified resume holds while any unilateral step was turned unverified by this check rather than an earlier one, the same rule as the check's own rebuild. A step that stays unread is in unreadBefore on the next tick and resumes then, so it can't stall. The hold returns before unverifiedRebuiltAtHeight is set, so no block is lost, and nothing new is paid. iOS synonymdev/bitkit-ios#1306 (d5940383c) does the same with before.recovery.

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Pushed 60d49ce: the funded unverified ask's resume now skips a tick whose check failed, so it needs two checks that read a step unread before it rebuilds, as the check path does. The hold test now covers a failed check after the hold.

Re-review is needed only because of this push: fe43ec3...60d49ce

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

Rechecked after 2 new commits (fe43ec3, 60d49ce). Nothing blocking remains.

addressed

  • Funded resume still rebuilds over a just-unread step when the next check fails (app/src/main/java/to/bitkit/repositories/SparkExitDriver.kt:224-233). Fixed: driveWhileFunding now returns null when the check throws or there is no SDK (SparkExitDriver.kt:497-503). tick then runs broadcastWithoutSdk on the same record as before and sets isChecked = false (:146-149). buildOnceFunded returns the held record before the unreadBefore and height guards (:229), so a failed check never rebuilds, as on the check path. I traced your tail at SparkExitDriverTest.kt:644-648: the fake's empty checks deque throws on removeFirst, and unverifiedRebuiltAtHeight is still 1_000 at tip 1_001. Without :229 build would reach recover, so the assertion separates the two. The !isDrivingWhileFunding path keeps isChecked = true but cannot reach the unverified-ask branch, because that branch needs NeedsFunding with a recovery and no rebuild. I could not run the test because breez_sdk_spark and bitkit-core are not in the local Gradle cache.

the reviewers disagree, your call

  • QA Notes leave out the hold test that covers the last two claims (app/src/test/java/to/bitkit/repositories/SparkExitDriverTest.kt:604)

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

Re-approving at 60d49ce1f. driveWhileFunding now returns null when no check ran (no SDK, or the check failed), and the caller still falls back to broadcastWithoutSdk, so the send path is unchanged. A funded unverified resume no longer rebuilds on a tick whose check failed, matching the check path. fe43ec300 adds the assertion that the held resume adopted its payment.

@ovitrif

ovitrif commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Merging into master: @coreyphillips and @jvsena42 approved 60d49ce, every required check passes, and no review thread is open.

@ovitrif
ovitrif merged commit 2f18eb6 into master Nov 12, 2026
23 checks passed
@ovitrif
ovitrif deleted the fix/spark-exit-unverified-rebuild-wait branch November 12, 2026 13:56
@ovitrif

ovitrif commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

QA for 2f18eb69b on master, with Breez SDK 0.28.2 from synonymdev/bitkit-android#1881 chore: bump breez sdk to 0.28.2:
A full in-app emergency exit (synonymdev/bitkit-android#1782 feat: spark emergency exit) on Spark's hosted regtest, Pay from savings, on an Android emulator, with the dev debug build's ESPLORA_URL set to the Spark regtest Esplora proxy (scripts/spark-regtest-esplora.py):

  • ✅ Start and funding: the quote showed ₿ 28 672 to exit, ₿ 9 077 network fees, about ₿ 19 595 to receive, ₿ 15 253 fee funding and ₿ 6 229 staying on Spark. Savings paid ₿ 15 675 in one payment (₿ 15 253 plus a ₿ 422 fee), and the screen showed Paid from savings with its transaction ID (f9055513…7c5e). Funding confirmed at 3705611, then Publishing 1/29, with the timelock at Ready in 24d.
  • 🟠 Done: still running, now at Publishing 18/29 at height 3705630. The last timelock clears at height 3709026, about 29 h at hosted regtest's block rate. No exit rebuild in the log so far. I'll post here when it ends.

The start, from the Spark screen to Paid from savings, and the exit after its funding confirmed:

Start Funding confirmed
p12x.mp4
p22x.mp4

@ovitrif

ovitrif commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

Emergency exit result, 2f18eb69b with Breez SDK 0.28.2: this finishes the exit started in my comment above.

  • ✅ Done: 29/29 exit transactions published, and the exit stayed on generation 1 the whole run. The log has no exit rebuild ("Rebuilding Spark exit diverged on-chain") and no "lost a race".
  • ✅ Sweep: ₿ 27 792 reached savings in 1a819a6b263650c3dc10ee3c08514cf7801f4bd60e0939b542579962a7a6c5f9 at height 3709037. The quote estimated about ₿ 19 595, and network fees came in under it.
  • ✅ Leftover fee funding: ₿ 5 382 went back to savings in 4bee65940a772c19399180a98ada4e438477db6f57e0d45f12ab7be2546c34c5 at height 3709041.
  • ✅ Balances: Savings went from ₿ 9 325 after funding to ₿ 42 499, which is 9,325 + 27,792 + 5,382. Spending kept the ₿ 6 229 the quote said stays on Spark.
  • ⚠️ The leftover sweep logged a failed fee read (HTTP '404' for '/fee-estimates', which the regtest Esplora proxy does not serve). The sweep was still built, accepted and confirmed.

The recording shows Exit complete with both transaction IDs, the Spark screen, and Savings after the exit.

p32x.mp4

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