Skip to content

perf(sync): reduce wallet sync lookup churn and add tracing#58

Merged
dolcalmi merged 12 commits intomainfrom
perf/bdk-sync-cache-warmup
Apr 17, 2026
Merged

perf(sync): reduce wallet sync lookup churn and add tracing#58
dolcalmi merged 12 commits intomainfrom
perf/bdk-sync-cache-warmup

Conversation

@dolcalmi
Copy link
Copy Markdown

@dolcalmi dolcalmi commented Apr 17, 2026

Summary

  • warm script pubkey and transaction caches during iteration paths to reduce repeated bdk.script_pubkeys.find_path and bdk.transactions.find_by_id calls
  • treat UtxoError::UtxoAlreadySettledError during soft-deleted cleanup as non-fatal (rollback + undelete + continue) to avoid retry cascades
  • add tracing instrumentation to SqlxWalletDb BatchOperations and Database trait methods to make sync call ordering and cache behavior observable
  • harden script path decoding with checked i32->u32 conversion and add focused cache invariant tests/comments to document subtle in-memory snapshot behavior

dolcalmi added 10 commits April 16, 2026 18:00
Restructure SqlxWalletDb internals to isolate context, batch state, and shared caches while keeping BDK trait behavior compatible. This avoids eager cache hydration side effects, makes cache errors recoverable, and commits DB writes before cache mutation for safer sync behavior at large wallet scale.
Move keychain filtering into SQL and add a lightweight iter_txs(false) path to reduce wallet sync query cost. Replace panic-prone unimplemented/unwrap/expect paths in pg wallet database adapters with recoverable errors to avoid process crashes under inconsistent data.
Simplify control flow and batching code in the Postgres BDK adapters without changing behavior. This keeps recent sync performance optimizations while reducing duplication and making cache/DB paths easier to reason about.
Execute script pubkeys, utxos, and transactions persistence in a single SQL transaction during commit_batch to avoid partial DB state on failures. Also unify tx lookup behavior for raw vs summary reads, remove unused script_pubkeys load-all path, and document retained non-transactional persist helpers.
Avoid repeated full load_all deserialization in iter_txs(true) by memoizing the raw transaction cache after first successful load. Preserve cache correctness for staged updates and add focused unit coverage for the raw-cache-loaded flag.
Add focused unit coverage for transaction cache behavior used by iter_txs summary/raw paths, and harden summary loading with negative-height validation. Also strengthen raw-cache-loaded atomic ordering with acquire/release semantics.
Reduce duplication and clarify control flow in the BDK Postgres adapters while preserving the recent atomic commit and cache semantics. This keeps behavior unchanged but makes error handling and query paths easier to reason about.
Respect the Database::get_tx include_raw flag by stripping raw transaction data when requested. Also document the process-local raw tx cache hint and clarify commit_batch atomic scope boundaries.
Warm script-path and tx-summary caches during iteration to reduce repeated find_path/find_by_id lookups in wallet sync. Treat soft-delete cleanup for already-settled UTXOs as a non-fatal condition to prevent retry cascades.
Add tracing spans across SqlxWalletDb BatchOperations and Database trait methods so sync call ordering and cache-hit paths are visible in traces.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to improve wallet sync performance and observability by reducing repeated DB lookups (via cache hydration), making a specific UTXO cleanup error non-fatal, and adding tracing spans around BDK database/batch operations.

Changes:

  • Treat UtxoError::UtxoAlreadySettledError during soft-deleted UTXO cleanup as non-fatal by rolling back and undeleting the BDK UTXO row.
  • Add a list_scripts_with_paths query and use it to hydrate an in-process script pubkey+path cache, tracked via a loaded bitmask.
  • Add tracing::instrument spans to SqlxWalletDb BatchOperations and Database trait method implementations.

Reviewed changes

Copilot reviewed 3 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/job/sync_wallet.rs Makes soft-deleted UTXO cleanup resilient to already-settled Bria UTXOs by restoring BDK visibility and continuing.
src/bdk/pg/script_pubkeys.rs Adds a scripts+paths query/helper to support bulk cache hydration and avoid repeated find_path calls.
src/bdk/pg/mod.rs Introduces script pubkey hydration flags and uses bulk loading to warm caches; adds tracing around DB/batch calls.
.sqlx/query-a49bfbc8c8ca875c146ac678f5f5633b758af6a7070b939032394b6525d4a7c3.json SQLx offline metadata for the new keychain_id + keychain_kind scripts-with-paths query.
.sqlx/query-846df15d0a9709ad69c5ff8e6c3e6679816cf5c9f86ea2b4a9d1b95458ec56a1.json SQLx offline metadata for the new keychain_id scripts-with-paths query.
Files not reviewed (2)
  • .sqlx/query-846df15d0a9709ad69c5ff8e6c3e6679816cf5c9f86ea2b4a9d1b95458ec56a1.json: Language not supported
  • .sqlx/query-a49bfbc8c8ca875c146ac678f5f5633b758af6a7070b939032394b6525d4a7c3.json: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/bdk/pg/script_pubkeys.rs Outdated
Comment thread src/bdk/pg/mod.rs
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 5 changed files in this pull request and generated 2 comments.

Files not reviewed (2)
  • .sqlx/query-846df15d0a9709ad69c5ff8e6c3e6679816cf5c9f86ea2b4a9d1b95458ec56a1.json: Language not supported
  • .sqlx/query-a49bfbc8c8ca875c146ac678f5f5633b758af6a7070b939032394b6525d4a7c3.json: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/bdk/pg/mod.rs
Comment thread src/bdk/pg/mod.rs
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 5 changed files in this pull request and generated no new comments.

Files not reviewed (2)
  • .sqlx/query-846df15d0a9709ad69c5ff8e6c3e6679816cf5c9f86ea2b4a9d1b95458ec56a1.json: Language not supported
  • .sqlx/query-a49bfbc8c8ca875c146ac678f5f5633b758af6a7070b939032394b6525d4a7c3.json: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dolcalmi dolcalmi merged commit 7b7c556 into main Apr 17, 2026
9 checks passed
@dolcalmi dolcalmi deleted the perf/bdk-sync-cache-warmup branch April 17, 2026 01:56
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.

2 participants