Skip to content

db+waddrmgr: add scan-batch scaffolding and rollback support#1263

Open
yyforyongyu wants to merge 20 commits into
prep-runtime-1from
prep-runtime-2
Open

db+waddrmgr: add scan-batch scaffolding and rollback support#1263
yyforyongyu wants to merge 20 commits into
prep-runtime-1from
prep-runtime-2

Conversation

@yyforyongyu

Copy link
Copy Markdown
Collaborator

Adds the scan-batch scaffolding — db.ScanHorizon / scan-batch query types,
account-name queries, scan-horizon extension helpers, and the
AddressInfo.HasDerivationPath population — together with the waddrmgr
rollback support it builds on (RestoreSyncedTo, EvictDerivedAddresses).

Split out of the runtime Store PR (#1256) for reviewability; content-preserving,
no commit changes. Stacked on prep-runtime-1.

@coveralls

coveralls commented Jun 10, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 28395386750

Warning

No base build found for commit 79e0177 on prep-runtime-1.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 55.353%

Details

  • Patch coverage: 464 uncovered changes across 17 files (1261 of 1725 lines covered, 73.1%).

Uncovered Changes

Top 10 Files by Coverage Impact Changed Covered %
wallet/internal/db/kvdb/txstore.go 458 324 70.74%
wallet/internal/db/pg/txstore_createtx.go 332 271 81.63%
wallet/internal/db/sqlite/txstore_createtx.go 319 265 83.07%
wallet/internal/db/txstore_common.go 219 191 87.21%
wallet/internal/sql/pg/sqlc/transactions.sql.go 25 0 0.0%
wallet/internal/sql/sqlite/sqlc/transactions.sql.go 25 0 0.0%
wallet/internal/sql/pg/sqlc/utxos.sql.go 24 4 16.67%
wallet/internal/sql/sqlite/sqlc/utxos.sql.go 24 4 16.67%
wallet/internal/sql/pg/sqlc/db.go 16 0 0.0%
wallet/internal/sql/sqlite/sqlc/db.go 16 2 12.5%
Total (17 files) 1725 1261 73.1%

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 57043
Covered Lines: 31575
Line Coverage: 55.35%
Coverage Strength: 9442.62 hits per line

💛 - Coveralls

Copilot AI 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.

Pull request overview

This PR adds foundational pieces needed for recovery scan-batch support across the db layer and address manager, including scan-horizon extension helpers, backend query primitives to advance account derivation indexes monotonically, and rollback helpers to undo in-memory state when a write transaction is rolled back.

Changes:

  • Added db scan-horizon data types and shared horizon-extension logic (ExtendScanHorizon) to derive/persist recovered addresses and advance branch next-indexes.
  • Added SQLite/PostgreSQL SQLC queries to monotonically advance next_external_index / next_internal_index during horizon extension.
  • Added AddressInfo.HasDerivationPath population and waddrmgr rollback helpers (RestoreSyncedTo, EvictDerivedAddresses) with unit tests and mock/interface updates.

Reviewed changes

Copilot reviewed 12 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
wallet/internal/sql/sqlite/sqlc/querier.go Extends SQLC Querier interface with next-index advancement methods (SQLite).
wallet/internal/sql/sqlite/sqlc/db.go Prepares/closes new prepared statements and threads them through WithTx (SQLite).
wallet/internal/sql/sqlite/sqlc/accounts.sql.go Adds generated exec methods for AdvanceNext{External,Internal}Index (SQLite).
wallet/internal/sql/sqlite/queries/accounts.sql Adds SQL definitions for monotonic next-index advancement (SQLite).
wallet/internal/sql/pg/sqlc/querier.go Extends SQLC Querier interface with next-index advancement methods (PostgreSQL).
wallet/internal/sql/pg/sqlc/db.go Prepares/closes new prepared statements and threads them through WithTx (PostgreSQL).
wallet/internal/sql/pg/sqlc/accounts.sql.go Adds generated exec methods for AdvanceNext{External,Internal}Index (PostgreSQL).
wallet/internal/sql/pg/queries/accounts.sql Adds SQL definitions for monotonic next-index advancement (PostgreSQL).
wallet/internal/db/scan_batch_common.go Introduces scan-horizon extension scaffolding and derivation loop shared by scan-batch engines.
wallet/internal/db/kvdb/addressstore.go Populates AddressInfo.HasDerivationPath for kvdb-backed address info.
wallet/internal/db/data_types.go Adds scan-batch types (ScanHorizon, ScanBatchParams) and HasDerivationPath; introduces MaxAddressIndex.
wallet/internal/db/addresses_common.go Adjusts address-path conversion to allow imported-xpub HD paths and sets HasDerivationPath for SQL rows.
waddrmgr/sync.go Adds RestoreSyncedTo to revert in-memory synced tip after tx rollback.
waddrmgr/scoped_manager.go Adds EvictDerivedAddresses to undo in-memory derived-address effects after tx rollback.
waddrmgr/scoped_manager_test.go Adds unit tests validating eviction behavior for unlocked/locked managers and multi-account pending derivations.
waddrmgr/interface.go Extends AddrStore/AccountStore interfaces with rollback/eviction methods.
bwtest/mock/addr_store.go Updates test mock to satisfy new AddrStore interface.
bwtest/mock/account_store.go Updates test mock to satisfy new AccountStore interface.
Files not reviewed (6)
  • wallet/internal/sql/pg/sqlc/accounts.sql.go: Language not supported
  • wallet/internal/sql/pg/sqlc/db.go: Language not supported
  • wallet/internal/sql/pg/sqlc/querier.go: Language not supported
  • wallet/internal/sql/sqlite/sqlc/accounts.sql.go: Language not supported
  • wallet/internal/sql/sqlite/sqlc/db.go: Language not supported
  • wallet/internal/sql/sqlite/sqlc/querier.go: Language not supported

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

Comment thread wallet/internal/db/data_types.go Outdated
Comment thread wallet/internal/db/scan_batch_common.go Outdated
Comment thread waddrmgr/scoped_manager.go Outdated
@yyforyongyu

Copy link
Copy Markdown
Collaborator Author

LGTM ✅

(claude-opus-4-8[1m])

Comment thread wallet/internal/db/pg/txstore_batch.go Outdated
Comment thread wallet/internal/db/sqlite/txstore_batch.go
Comment thread wallet/internal/db/kvdb/txstore.go Outdated
Comment thread wallet/internal/db/sqlite/txstore_batch.go Outdated
Comment thread wallet/internal/db/pg/txstore_batch.go
Comment thread wallet/internal/db/kvdb/txstore.go
@yyforyongyu

Copy link
Copy Markdown
Collaborator Author

No issues found by claude-opus-4-8[1m] 📦

@yyforyongyu

Copy link
Copy Markdown
Collaborator Author

No issues found by gpt-5.5 xhigh

@yyforyongyu

Copy link
Copy Markdown
Collaborator Author

No issues found by gpt-5.5 🛰️

@yyforyongyu

Copy link
Copy Markdown
Collaborator Author

No issues found by gpt-5.5 xhigh

Add the pg and sqlite SQL and generated sqlc for reading the UTXOs that
recovery scans should watch. The concrete backend wrappers, db.Store
interface exposure, mock, and tests land in the following implementation
commit.
Add ListOutputsToWatch to the kvdb UTXO store, walking the legacy wtxmgr
OutputsToWatch credits and adapting them into the db-native UtxoInfo
view, with a unit test covering a currently-leased credit.
Add WatchOutputFromRow to convert a ListOutputsToWatch result row into
the public UtxoInfo shape, reading the watch script from the funding
transaction's own output so a partly-owned bare-multisig output is
watched by its on-chain script, mirroring the kvdb credit walk.
Add the sqlite ListOutputsToWatch wrapper over the ListOutputsToWatch
query, converting each row through WatchOutputFromRow.
Add the postgres ListOutputsToWatch wrapper over the ListOutputsToWatch
query, mirroring the sqlite row conversion.
Expose ListOutputsToWatch on the UTXOStore interface now that every
backend implements it, and wire the mock store through to its testify
expectations.
Add TestListOutputsToWatchBareMultisigUsesOutputScript verifying the
recovery watch set reports the on-chain multisig output script for a
partly-owned bare-multisig output, not the credited member address's own
script, locking in parity with the kvdb backend.
Validate duplicate transaction observations against the stored row shape
before treating them as idempotent.
Add TxBatchParams describing a wallet transaction batch and optional
sync-tip update applied atomically, before backend implementations
consume it.

Add the shared batch helpers the backends apply before writing:
parents-first topological sorting and a nil-Tx validator that runs
before sorting.
Add legacyCreditEntries, converting db-native credit addresses into
legacy credit entries with the same output-script membership check
CreateTx applies and a nil-credit fallback keyed on the output script,
and kvdbTxBlockMeta, converting store block metadata into wtxmgr block
metadata. These back the upcoming ApplyTxBatch kvdb implementation.
Add ApplyTxBatch on the kvdb tx store, recording transaction
notifications and an optional wallet sync-tip update in one atomic
walletdb write. Empty batches return before requiring an address
manager.

Validate the batch and sort it parents-first before applying it. This
matches the SQL backends and prevents child-before-parent ordering from
dropping spend edges.
Cover ApplyTxBatch rollback paths that must keep tx state and the live
sync-tip cache atomic.
Add the sqlite ApplyTxBatch implementation, advancing the sync tip
before creating transactions so a tx confirmed in the synced-to block
finds its block row, and skipping transactions that already exist.
Add the postgres ApplyTxBatch implementation, mirroring the sqlite
sync-tip-first ordering and duplicate-tx skip.
Expose ApplyTxBatch on the TxStore interface now that every backend
implements it, and wire the mock store through to its testify
expectations.
Add shared itests verifying ApplyTxBatch stores a tx and advances the
sync tip, and confirms a tx in the same block it syncs to.
Add conformance coverage for duplicate transaction idempotence, edge
replay, and late-discovered child-spend reconciliation.
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