Skip to content

db: add synced-block, watched-output, and tx-batch Store methods#1262

Open
yyforyongyu wants to merge 24 commits into
impl-psbt-storefrom
prep-runtime-1
Open

db: add synced-block, watched-output, and tx-batch Store methods#1262
yyforyongyu wants to merge 24 commits into
impl-psbt-storefrom
prep-runtime-1

Conversation

@yyforyongyu

Copy link
Copy Markdown
Collaborator

Adds the runtime db.Store query/batch surface and its backend implementations:
ListSyncedBlocks, DeleteExpiredLeases, the RollbackToBlock block rewind,
ListOutputsToWatch, and ApplyTxBatch — plus the runtime query types and the
DB conformance itests.

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

Add ListSyncedBlocksQuery and its inclusive height range before backend
implementations so each store can wire the same contract-ready parameters
independently. Synced block metadata is shared chain state, so the query
carries no wallet ID.
Add the pg and sqlite SQL and generated sqlc for reading synced block
metadata over an inclusive height range. The concrete backend wrappers,
db.Store interface exposure, mocks, and tests land in the following
implementation commit.
Add ListSyncedBlocks to the kvdb Store, reading block hashes from the
legacy address manager over the requested inclusive height range, with a
unit test covering the synced-tip read path.
Add newSQLiteRows and the rowDBTX queryRows stub so sqlc :many scan
paths can be exercised against an in-memory sqlite handle without
standing up a real store.
@coveralls

coveralls commented Jun 10, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 27319137450

Warning

No base build found for commit 9244bdc on impl-psbt-store.
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: 54.632%

Details

  • Patch coverage: 331 uncovered changes across 19 files (463 of 794 lines covered, 58.31%).

Uncovered Changes

Top 10 Files by Coverage Impact Changed Covered %
wallet/internal/db/kvdb/txstore.go 214 121 56.54%
wallet/internal/bwtest/mock/store.go 26 0 0.0%
wallet/internal/db/pg/walletstore_listsyncedblocks.go 71 50 70.42%
wallet/internal/sql/pg/sqlc/utxos.sql.go 20 0 0.0%
wallet/internal/sql/sqlite/sqlc/utxos.sql.go 20 0 0.0%
wallet/internal/db/pg/txstore_batch.go 62 44 70.97%
wallet/internal/db/kvdb/walletstore.go 44 27 61.36%
wallet/internal/db/sqlite/txstore_batch.go 60 44 73.33%
wallet/internal/db/sqlite/walletstore_listsyncedblocks.go 55 39 70.91%
wallet/internal/sql/pg/sqlc/db.go 16 0 0.0%
Total (20 files) 794 463 58.31%

Coverage Regressions

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


Coverage Stats

Coverage Status
Relevant Lines: 54951
Covered Lines: 30021
Line Coverage: 54.63%
Coverage Strength: 13590.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

Adds new runtime db.Store query/batch methods across kvdb + SQLite/PostgreSQL backends, plus supporting sqlc queries and integration tests, to support block-range sync metadata reads, recovery watch-output enumeration, lease cleanup, atomic tx batching, and rollback semantics.

Changes:

  • Add ListSyncedBlocks (range read) and GetBlocksInRange sqlc query for SQL backends; adapt legacy kvdb synced-tip reads.
  • Add recovery-facing ListOutputsToWatch across backends, including SQL implementations that derive the watched script from funding tx raw_tx.
  • Add runtime batch/maintenance operations: ApplyTxBatch, DeleteExpiredLeases, and kvdb RollbackToBlock, with new unit/itest coverage.

Reviewed changes

Copilot reviewed 31 out of 35 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
wallet/internal/sql/sqlite/sqlc/utxos.sql.go Adds sqlc ListOutputsToWatch query + scan wrapper.
wallet/internal/sql/sqlite/sqlc/querier.go Exposes GetBlocksInRange and ListOutputsToWatch on sqlite Querier.
wallet/internal/sql/sqlite/sqlc/db.go Prepares/closes new sqlite prepared statements.
wallet/internal/sql/sqlite/sqlc/blocks.sql.go Adds sqlc GetBlocksInRange query + scan wrapper (sqlite).
wallet/internal/sql/sqlite/queries/utxos.sql Adds ListOutputsToWatch SQL definition (sqlite).
wallet/internal/sql/sqlite/queries/blocks.sql Adds GetBlocksInRange SQL definition (sqlite).
wallet/internal/sql/pg/sqlc/utxos.sql.go Adds sqlc ListOutputsToWatch query + scan wrapper.
wallet/internal/sql/pg/sqlc/querier.go Exposes GetBlocksInRange and ListOutputsToWatch on pg Querier.
wallet/internal/sql/pg/sqlc/db.go Prepares/closes new pg prepared statements.
wallet/internal/sql/pg/sqlc/blocks.sql.go Adds sqlc GetBlocksInRange query + scan wrapper (pg).
wallet/internal/sql/pg/queries/utxos.sql Adds ListOutputsToWatch SQL definition (pg).
wallet/internal/sql/pg/queries/blocks.sql Adds GetBlocksInRange SQL definition (pg).
wallet/internal/db/utxo_store_common.go Adds WatchOutputFromRow helper to derive watch scripts from funding tx raw bytes.
wallet/internal/db/sqlite/walletstore_listsyncedblocks.go Implements sqlite ListSyncedBlocks using one range query and gap detection.
wallet/internal/db/sqlite/utxostore_listoutputstowatch.go Implements sqlite ListOutputsToWatch using sqlc results + WatchOutputFromRow.
wallet/internal/db/sqlite/utxostore_listleasedoutputs.go Adds sqlite DeleteExpiredLeases.
wallet/internal/db/sqlite/txstore_batch.go Implements sqlite ApplyTxBatch with optional sync-tip update.
wallet/internal/db/sqlite/runtime_test.go Adds sqlite unit test for ListSyncedBlocks.
wallet/internal/db/pg/walletstore_listsyncedblocks.go Implements pg ListSyncedBlocks using one range query and gap detection.
wallet/internal/db/pg/utxostore_listoutputstowatch.go Implements pg ListOutputsToWatch using sqlc results + WatchOutputFromRow.
wallet/internal/db/pg/utxostore_listleasedoutputs.go Adds pg DeleteExpiredLeases.
wallet/internal/db/pg/txstore_batch.go Implements pg ApplyTxBatch with optional sync-tip update.
wallet/internal/db/pg/runtime_test.go Adds pg unit test coverage for ListSyncedBlocks mapping.
wallet/internal/db/pg/backend_rows_test.go Extends pg sqlc DBTX test stub to support :many scan paths.
wallet/internal/db/kvdb/walletstore.go Adds kvdb ListSyncedBlocks via legacy address manager.
wallet/internal/db/kvdb/walletstore_test.go Adds kvdb test for ListSyncedBlocks.
wallet/internal/db/kvdb/utxostore.go Adds kvdb ListOutputsToWatch + DeleteExpiredLeases via legacy wtxmgr.
wallet/internal/db/kvdb/utxostore_test.go Adds kvdb tests for lease cleanup + watch-output inclusion of leased credits.
wallet/internal/db/kvdb/txstore.go Adds kvdb ApplyTxBatch and implements RollbackToBlock with atomic sync-tip rewind.
wallet/internal/db/kvdb/txstore_test.go Adds kvdb tests for rollback and tx-batch semantics (including credit validation).
wallet/internal/db/itest/utxo_store_edge_cases_test.go Adds itest coverage for SQL lease cleanup correctness.
wallet/internal/db/itest/tx_utxo_store_test.go Adds itest coverage for multisig watch-script correctness + tx-batch behavior.
wallet/internal/db/interface.go Extends runtime store interfaces with new methods.
wallet/internal/db/data_types.go Adds new runtime query/params types (ListSyncedBlocksQuery, TxBatchParams).
wallet/internal/bwtest/mock/store.go Updates mock store to satisfy expanded interfaces.
Files not reviewed (4)
  • wallet/internal/sql/pg/sqlc/blocks.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/pg/sqlc/utxos.sql.go: Language not supported

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

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

Copy link
Copy Markdown
Collaborator Author

LGTM ✅

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

Add the sqlite ListSyncedBlocks wrapper over GetBlocksInRange, mapping
ascending block rows positionally onto the requested height range, with
a unit test reading a stored block.
Add the postgres ListSyncedBlocks wrapper over GetBlocksInRange,
mirroring the sqlite range-to-height mapping, with a unit test building
a block from a single row.
Expose ListSyncedBlocks on the WalletStore interface now that every
backend implements it, and wire the mock store through to its testify
expectations.
Expose DeleteExpiredLeases on the UTXOStore interface with its mock, the
kvdb implementation over the legacy wtxmgr path, and the pg/sqlite
implementations, with a kvdb unit test and shared itest coverage.
Implement the kvdb RollbackToBlock as the atomic chain-reorg rollback API:
in one walletdb update it rewinds the wallet sync tip to the stored
fork-point block at height-1 and rolls legacy transaction state back to the
same boundary, mirroring the legacy SetSyncedTo + wtxmgr Rollback rewind.
The sync tip is only rewound when the wallet is synced at or above the
rollback boundary.

The pg and sqlite RollbackToBlock already clamp every affected wallet's
sync state to the fork point inside the same write transaction, so this
gives every backend one atomic reorg-rollback API and removes the need for
a separate sync-tip update.
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.
Add TxBatchParams describing a wallet transaction batch and optional
sync-tip update applied atomically, before the backend implementations that
consume it.
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 a batch of transaction
notifications through the legacy wtxmgr path and an optional wallet
sync-tip update in one atomic walletdb write. An empty batch with no
sync-tip update returns before requiring an address manager.
Add unit tests covering ApplyTxBatch recording a tx and advancing the
sync tip, an empty batch needing no address store, a credit/output
script mismatch, and the nil-credit fallback keyed on the output script.
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.
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