fix!: Execute queries inside a Transaction#18
Merged
ValuedMammal merged 2 commits intobitcoindevkit:masterfrom May 1, 2026
Merged
fix!: Execute queries inside a Transaction#18ValuedMammal merged 2 commits intobitcoindevkit:masterfrom
ValuedMammal merged 2 commits intobitcoindevkit:masterfrom
Conversation
The wallet now begins a transaction before reading/ writing the ChangeSet and only commits after all queries have succeeded. This makes database operations atomic by preventing partial writes and improves performance by flushing all queries to the database at once. BREAKING: Low level async Store functions are changed to accept `&mut Connection` as parameter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #17
Description
Every write path previously executed N individual SQL statements directly against the connection pool, each running in its own implicit auto-commit transaction. If any statement failed mid-way, the database was left in a partially-applied state, e.g. a
ChangeSetwhosetx_graphwas written but whoselocal_chainwas not.This PR wraps all writes in an explicit
BEGIN/COMMITtransaction so that aChangeSeteither lands completely or not at all.Notes to the reviewers
wallet:Store::write_changesetnow opens a singlesqlxtransaction and threads it through every write method (write_network,write_keychain_descriptors,write_local_chain,write_tx_graph,write_keychain_txout,write_locked_outpoints). The transaction is committed only after all writes succeed; any error causes an implicit rollback.async_store:The lower-level write methods (write_tx_graph,write_local_chain,write_keychain_txout) and their read counterparts now acceptconn: &mut SqliteConnectionrather than operating on&self.pool. This makes them usable inside a caller-owned transaction without leaking transaction management concerns into the individual methods.The performance improvement is a secondary benefit. SQLite still amortises WAL-frame overhead across the single
COMMIT, reducing internal bookkeeping compared to N auto-commit boundaries.Changelog
BREAKING — all lower-level read/write methods that previously took
&selfare now member functions acceptingconn: &mut SqliteConnection. Callers who invoked these directly must now supply a connection or transaction handle.async_store:Store::write_tx_graphStore::write_local_chainStore::write_keychain_txoutStore::read_tx_graphStore::read_local_chainStore::read_keychain_txoutwallet:Store::write_networkStore::write_keychain_descriptorsStore::write_locked_outpointsStore::read_networkStore::read_keychain_descriptorsStore::read_locked_outpointsThe high-level entry points
Store::write_changesetandStore::read_changesetare unchanged and remain the recommended API.