Skip to content

perf: load watch-only accounts after node start - #1584

Open
ovitrif wants to merge 5 commits into
masterfrom
perf/watch-only-accounts-after-start
Open

ovitrif wants to merge 5 commits into
masterfrom
perf/watch-only-accounts-after-start

Conversation

@ovitrif

@ovitrif ovitrif commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1583
Twin: synonymdev/bitkit-android#2248

Description

  • Builds the node without the Paykit Server watch-only accounts, because each one adds about 2 s to the build (22.5 s with 5 accounts vs 13.4 s without on the Android twin, 3.2.0 RC3)
  • Loads the accounts into the running node after each start in a detached task, each attempt holding background time (BackgroundTime, beginBackgroundTask), so the load keeps going when the app closes right after start; a transient failure retries every 15 s while the same node runs, a rejected xpub does not, and the wallet syncs afterwards
  • Reads the store under the account lock before the load, so an approval in flight at start counts
  • Starts one loader per node; a repeated start of a running node keeps the one it has, and the loader holds the node weakly
  • Loads each account in its own LDK queue hop and stops once the node is stopped or replaced, keeping its pending unloads for the next node, so sends are not held behind all of them
  • Removes the account reconcile from sync(), which the load after start replaces
  • Updates Docs/pubky-auth-companion-claims.md, which said the accounts load before the node starts
  • Adds an AGENTS.md rule: network work that must survive the app closing runs in a detached task with BackgroundTime, and BGTaskScheduler for work that must run later while the app stays closed

Out of Scope

Design

N/A — no UI changes.

Preview

N/A — no user-visible changes.

QA Notes

Setup

  • Mainnet (Release configuration) or staging wallet on this PR's build, with 2 or more Paykit Server accounts approved (Pubky auth sheet with the watch-only account claim) and tracking on
  • Console.app or the app's debug log to read node start and account load lines

Journeys

  • J1: journeys/pubky-marketplace/approval-before-node-starts.xml — route changed: an account approved before the node starts now loads after start instead of at build; step 22 now expects only "Registered derived on-chain wallet account"

Manual Tests

  • 1. cold start → node build no longer grows per account; Loaded Paykit Server accounts logs shortly after Node started
  • 2. close the app right after the node starts, reopen later → watch-only account balances show
  • 3. toggle tracking of a Paykit Server account off and on → balance hides and returns

Automated Checks

  • updated LightningServiceConfigTests.swift, WatchOnlyAccountServiceTests.swift
  • Local verification: xcodebuild test (LightningServiceConfigTests, WatchOnlyAccountServiceTests, BackgroundTimeTests, PubkyAuthApprovalSheetTests), swiftformat --lint

Evidence

Gaps

  • None

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Gemini 4 Neon (reasoning: high)
  • Review rounds: 2

Build the node without the Paykit Server accounts, each of which added about
2 s to the build, and load them into the running node after start in a
detached task with background time, so the load keeps going when the app
closes right after start.

- Reconcile loads each account in its own LDK queue hop.
- Wallet sync no longer reconciles accounts; the load retries a transient
  failure every 15 s while the same node runs, and skips a rejected xpub.

Verified: xcodebuild test (LightningServiceConfigTests, WatchOnlyAccountServiceTests, BackgroundTimeTests, PubkyAuthApprovalSheetTests), swiftformat --lint
Address the pre-PR self review:
- Check the node before and inside each LDK queue hop, so a stopped or
  replaced node gets no more accounts.
- Run the load also when only unloads are pending, and sync only when there
  are accounts to load.
- Sync after a rejected account too, re-check the node before syncing, and
  hold background time per attempt instead of across retry sleeps.

Verified: xcodebuild test (LightningServiceConfigTests, WatchOnlyAccountServiceTests, BackgroundTimeTests, PubkyAuthApprovalSheetTests), swiftformat --lint
@ovitrif ovitrif added this to the 3.3.0 milestone Dec 3, 2026
@ovitrif ovitrif self-assigned this Dec 3, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 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-12-03T16:30:50.021833Z 357038b 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.

@ovitrif
ovitrif requested review from a team, ben-kaufman and jvsena42 and removed request for a team December 3, 2026 16:30
@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The reviewed changes appear safe to merge; no actionable issues were found.

Summary

Moves Paykit server account loading out of node construction and into background work after startup.

  • The Lightning node starts before Paykit accounts finish loading.

Verification: git diff --check passed. iOS tests were not run in this Linux environment. Checking the PR's draft status was blocked by missing GitHub authentication.

Reviewer model: gpt-6.1-sol
Reasoning effort: Unknown
Metadata note: The review configuration supplies the model identifier; no accessible execution metadata supplied the reasoning setting.

Reviews (1) · Last reviewed commit: "fix: stop the account load once the node..." · Reviewed by Greptile

The check before the account load now reads the store under the
lifecycle lock, so an approval that began before the node ran counts.
Each load failure is logged once, unload failures are logged too, and
"Loaded Paykit Server accounts" logs only while the node is current.
The approval-before-node-starts journey no longer expects the build log
line, since the build loads no accounts.

Verified: xcodebuild test (LightningServiceConfigTests, WatchOnlyAccountServiceTests, BackgroundTimeTests, PubkyAuthApprovalSheetTests), swiftformat --lint

@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. Builds the node without the Paykit Server accounts and loads them into the running node after start, in a detached task holding background time, retrying transient failures while the same node runs. Reviewed 357038b, full tier, static: per the repo's Bitkit rules iOS is not built on this reviewer's Linux host, so this is reasoned from the code and CI.

What I checked, and 5 candidates I ruled out

Read in full: the diff of LightningService.swift (start, sync, setWatchOnlyAccountTracking, loadWatchOnlyAccountsInBackground, loadWatchOnlyAccounts, isRejectedPaykitServerAccount, reconcileWatchOnlyAccountTracking), LightningServiceConfigTests.swift, the AGENTS.md and changelog additions.
Call sites traced: WatchOnlyAccountManager.reconcileTracking / setTrackingEnabled / beginSetupAuthorization, WatchOnlyAccountStore.reconciliationSnapshot / finishReconciliation, BackupService restore into the store, WalletViewModel.start and its own sync, ServiceQueue error wrapping, AppError(ldkError:).
ldk-node at the pinned 0.7.0-rc.85 (3f45b8e): Wallet::add_onchain_wallet_account, NodeMetrics::get_wallet_sync_timestamp, collect_additional_sync_requests, Electrum sync_onchain_wallet.
CI at review time: SwiftFormat, validate and Greptile green; Run Tests, Run Integration Tests and build-local still pending.

Ruled out

  • Rejected xpub retried forever: ldk-node returns Error::InvalidSeedBytes for a mismatched xpub, ServiceQueue wraps it in AppError with underlyingError set, and isRejectedPaykitServerAccount unwraps it, so the loop stops; a transient error is rethrown ahead of it, as the comment says.
  • Accounts loaded after start miss their history: derived-account scan completion is runtime-only in ldk-node (get_wallet_sync_timestamp returns None for account_index != 0), so each runtime registration full-scans on the next sync, the same as a build-time account.
  • Post-load sync joins an in-flight sync and skips the new accounts: possible when it lands during WalletViewModel.start's own sync, but the 10 s background wallet sync reads the account list again, so the accounts are picked up a cycle later rather than lost.
  • Load into a stopped or replaced node: isCurrent compares node identity before each attempt and before the sync, and reconcileWatchOnlyAccountTracking checks self.node === node around and inside every LDK hop.
  • Toggle or approval racing the background load: both go through WatchOnlyAccountManager.lifecycleCoordinator, which reconcileTracking also holds, and setWatchOnlyAccountTracking with no node still only stores, which the load after the next start picks up.

Merge confidence: 4/5, static review only (iOS not built here) and the unit and integration jobs had not finished.

Reviewer model: claude-opus-5-5
Reasoning effort: Not exposed

Metadata note: the run exposes the model id; it does not expose a reasoning-effort setting.

The account reconcile throws when the node is stopped or replaced
between hops, so pending unloads are kept for the next node instead of
cleared. The loader holds the node weakly, checks it before each retry,
and a repeated start of a running node keeps the loader it has. An
invalid xpub is no longer retried. Adds tests for the snapshot lock
wait, the skipped sync and the failure order, and corrects the
companion claims doc and the AGENTS.md background work example.

Verified: xcodebuild test (LightningServiceConfigTests, WatchOnlyAccountServiceTests, BackgroundTimeTests, PubkyAuthApprovalSheetTests), swiftformat --lint
@ovitrif

ovitrif commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

QA for f9da6d5:
Tested on iPhone 17e simulator (iOS 26.4), regtest, against the bitkit-docker Pubky marketplace fixture (local Pubky testnet and Paykit Server).

🟢 J1, 1. Cold start, 2. Close right after start, 3. Toggle tracking
J1: approval-before-node-starts

With VSS held, the setup URL cold-started the app and Approve and Authorize ran before the node started. The success screen showed with no error toast, and the account's tracking was stored without a node. Once VSS was released, account 1 loaded 0.6 s after the node started, and the list shows one account at m/84'/1'/1' with tracking on.

ios-j12x.mp4

1. Cold start

With three tracked accounts, the node built in 24 ms and started without them; all three loaded within 0.8 s of start.

ios-m12x.mp4

2. Close right after start

The app was terminated 0.25 s after the node started, before any account loaded. On reopen all three accounts loaded, and Savings shows the 70,000 sats held by account 1.

ios-m22x.mp4

3. Toggle tracking

Tracking on, off and on again: Savings went to 70,000, then 0, then 70,000.

ios-m32x.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.

bug: wallet start slows down with each paykit server account

2 participants