Skip to content

fix(bitcoin): retry the first full scan and make Retry trigger a real scan - #521

Draft
rvelaz wants to merge 6 commits into
mainfrom
fix/stale-balances-03-btc-scan-reliability
Draft

rvelaz wants to merge 6 commits into
mainfrom
fix/stale-balances-03-btc-scan-reliability

Conversation

@rvelaz

@rvelaz rvelaz commented Oct 26, 2026 •

Copy link
Copy Markdown

Draft. Updated 9 Nov 2026: merged main (includes #543 and #530 fetch policy). Overlap with #543 resolved: kept main’s publish-before-repair ordering; this PR’s queue/retry logic runs after normal sync, not instead of it. Manifest reflects 3.3.0 bundle.

  • Package: @metamask/bitcoin-wallet-snap

Explanation

Bug

After a failed first full scan, BTC can stay “Balance unavailable” long after the network recovers. Retry does not start a new scan; cron may not retry the initial scan promptly.

How to reproduce

  1. Build this branch’s bitcoin snap. Add core/extension unavailable UI PRs if testing Retry in the wallet.
  2. Onboard/import BTC; block Esplora (DevTools request blocking or flaky network) until the first full scan fails.
  3. Unblock the network and click Retry on Bitcoin (or wait without Retry).

Expected: Retry triggers a real scan; first heal retries within seconds.
Actual on main (with #519 only): Retry re-reads local state; recovery can take minutes.

Without a browser: AccountUseCases.test.ts first-heal / full-scan queue cases.

Summary

Persistent full-scan queue with backoff; one scan per account; onboarding avoids duplicate discovery+full scans; balance requests on unknown accounts trigger scans.

Why

Generic upstream failures (timeouts, 5xx, rate limits) abort the first full scan. On main, failed initial scans were not reliably retried, Retry was a no-op, and repair migration could block behind a stuck account.

Relation to #543 (merged 3 Nov 2026): #543 fixes stale balances when repair scans blocked publishing updates (CronHandler publishes sync results before #repairNextAccount). This PR targets never-synced accounts and explicit Retry—not duplicated. Merge kept #543 publish-before-repair; dropped any pre-sync queue path that fought that ordering.

Validation

  • yarn workspace @metamask/bitcoin-wallet-snap test: 794 passed, 2026-11-09 (ac0b4430).
  • Build + ESLint (bitcoin package): pass.

Changelog

Unreleased → Fixed: retry first full scan; Retry triggers scan (#521).

… scan

- FullScanQueue: persistent per-account full-scan queue (reuses the
  rescanV1 state) with backoff 30 s..5 min; a failing account rotates to
  the tail, so neither new accounts nor the repair migration are blocked by
  a stuck one; never-synced accounts are re-queued
- the balance is treated as unknown (isBalanceUnknown) until the first full
  scan succeeds; BitcoinAccount.hasSynchronized() tells a never-synced
  wallet (genesis checkpoint) apart
- one scan per account: single-flight full scans, discovery scans reused
  for account creation (10 min TTL), at most 2 immediate scans on create;
  a reused discovery scan (discovery gap) that found activity is followed
  by a standard-gap scan
- a balance request on an unknown account (the wallet's Retry) requests an
  on-demand full scan (30 s..2 min backoff)
- the first successful sync after a failed scan requests a recovery scan
  within 5 s
@rvelaz
rvelaz force-pushed the fix/stale-balances-03-btc-scan-reliability branch from d0acea0 to 679210f Compare October 28, 2026 10:27
@rvelaz
rvelaz changed the base branch from fix/stale-balances-02-btc-requests to main October 28, 2026 10:27
rvelaz and others added 5 commits November 9, 2026 12:36
Resolve CronHandler conflicts: keep #543 publish-before-repair flow and
rescanV1 repair; retain PR full-scan queue reliability after regular sync.
Adapt createMany to main repository API; migrate affected tests to Vitest.

Co-authored-by: Raul Velaz <rvelaz@users.noreply.github.com>
Co-authored-by: Raul Velaz <rvelaz@users.noreply.github.com>
Co-authored-by: Raul Velaz <rvelaz@users.noreply.github.com>
Co-authored-by: Raul Velaz <rvelaz@users.noreply.github.com>
Co-authored-by: Raul Velaz <rvelaz@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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