Skip to content

fix(scan): make payment checks awaitable and self-healing - #211

Open
theanmolsharma wants to merge 1 commit into
masterfrom
theanmolsharma/sync-screen-jank-fix
Open

theanmolsharma wants to merge 1 commit into
masterfrom
theanmolsharma/sync-screen-jank-fix

Conversation

@theanmolsharma

Copy link
Copy Markdown
Collaborator

First of four stacked PRs that fix the Sync screen's "Check again" giving no feedback and the screen feeling janky. This one is the engine half. The screen itself is in the next PR.

Why "Check again" usually did nothing visible

  • A caught-up check emitted no state at all. performScan returned 0 at its early returns, so the screen couldn't tell that anything had happened.
  • fetchTransactions() only starts the scan. It awaits the Electrum sync and then fires the scan without awaiting it, so a caller can't know when a check ends or whether it failed.
  • A failed tip lookup left error stuck. A later successful check that found no new block didn't clear it, so Retry couldn't either.
  • Polling only started after a scan that had scanned blocks. A wallet already at the tip at launch never polled, even though the screen says "Watching for new payments".
  • A tap right after "caught up" joined a finished scan. activeScanPromise stayed set during the spent check, after idle had already been emitted, and Electrum calls have no timeout.

What changes

  • checkForPayments() is added to HDSilentPaymentsWallet, IScannableWallet and isScannable.
    • It syncs the regular addresses (fetchBalance → fetchTransactions) and scans side by side. If a scan is already running, it joins it.
    • It settles when the scan has settled and the regular sync has either finished or run for 20 s.
    • It resolves with the number of new silent-payment outputs, and rejects when the scan fails. A failed Electrum sync is only logged.
    • Concurrent calls share one run.
    • Running the two side by side is safe because neither replaces _utxo, which is also why fetchUtxo isn't part of the check.
    • fetchTransactions() stays fire-and-forget for pull-to-refresh, which holds a lock and gives up after 30 s.
  • A caught-up check counts as a success. It clears a stale error, emitting only when something changed, and starts polling.
  • Polling keeps going after a failure, with backoff. Polls are spaced 30 s, 1 m, 2 m and 4 m apart, then every 5 m, so errors clear themselves. One success resets the backoff.
  • A pending birth-height lookup stops polling. Only refreshes retry it, so polls can't use up its three attempts.
  • The scan slot is freed before the spent check. The spent check is also skipped for a cancelled scan.
  • The home banner's Retry uses checkForPayments.

Not in this PR

Two causes are already covered by open PRs, so they're left alone here:

Runtime version

JS only, so it ships over the air.

Testing

  • New sp-scan-lifecycle.test.ts with 16 cases. All but one fail against master; the exception is the regression guard against re-emitting an unchanged state.
  • Full suite: 70 suites, 689 tests. Lint and tsc are clean.

Stack

  1. This PR
  2. Sync screen feedback and smoothness
  3. Keeping the JS thread free while scanning
  4. Stopping scan updates from re-rendering the whole app

🤖 Generated with Claude Code

The Sync screen's "Check again" couldn't tell when a check ended or how it went: a check that found
the wallet caught up returned without a word, and fetchTransactions only starts the scan.

- checkForPayments() syncs the regular addresses and scans side by side, joining a scan already in
  flight, and settles once both have. It rejects when the scan fails.
- A caught-up check counts as a successful one: it clears an error an earlier check left behind and
  starts polling, so a wallet already at the tip at launch watches for new blocks too.
- Polling carries on after a failed check, backing off to every 5 minutes, so the error clears itself
  once the indexer is back.
- The scan slot is freed before the spent check, so a tap right after "caught up" runs a real check,
  and an Electrum call that never answers can't wedge every later scan.
- The home banner's Retry uses checkForPayments too.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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