db: add synced-block, watched-output, and tx-batch Store methods#1262
db: add synced-block, watched-output, and tx-batch Store methods#1262yyforyongyu wants to merge 24 commits into
Conversation
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.
Coverage Report for CI Build 27319137450Warning No base build found for commit Coverage: 54.632%Details
Uncovered Changes
Coverage RegressionsRequires a base build to compare against. How to fix this → Coverage Stats
💛 - Coveralls |
f244206 to
19ba9fd
Compare
There was a problem hiding this comment.
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) andGetBlocksInRangesqlc query for SQL backends; adapt legacy kvdb synced-tip reads. - Add recovery-facing
ListOutputsToWatchacross backends, including SQL implementations that derive the watched script from funding txraw_tx. - Add runtime batch/maintenance operations:
ApplyTxBatch,DeleteExpiredLeases, and kvdbRollbackToBlock, 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.
|
LGTM ✅ ( |
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.
19ba9fd to
e0df3ca
Compare
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.
e0df3ca to
04092cf
Compare
Adds the runtime
db.Storequery/batch surface and its backend implementations:ListSyncedBlocks,DeleteExpiredLeases, theRollbackToBlockblock rewind,ListOutputsToWatch, andApplyTxBatch— plus the runtime query types and theDB 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).