Refactor changeset handling to use mutable references#19
Draft
evanlinjin wants to merge 2 commits into
Draft
Conversation
Rewrite mutation methods on `IndexedTxGraph`, `LocalChain`, and `KeychainTxOutIndex` to take `&mut C where C: AsMut<ChangeSet>` as their last parameter, instead of returning the `ChangeSet`. Where a method also has a non-`ChangeSet` return value (e.g. the revealed `Indexed<ScriptBuf>`), that return is preserved. This eliminates the "forget-to-merge" footgun: there is no `ChangeSet` to discard. Composed-aggregate callers can pass `&mut walletcs` to each call instead of projecting into the right field by hand — one `AsMut` impl per sub-type and the simple direct-`&mut ChangeSet` case keeps working unchanged via the new self-`AsMut` blanket on each `ChangeSet` variant. `IndexedTxGraph` methods touched: `apply_block_relevant`, `apply_block`, `apply_update`, `insert_txout`, `insert_tx`, `insert_anchor`, `insert_seen_at`, `insert_evicted_at`, `batch_insert_relevant`, `batch_insert_relevant_unconfirmed`, `batch_insert_unconfirmed`, `batch_insert_relevant_evicted_at`, `reindex`. `LocalChain` methods touched: `apply_update`, `apply_header_connected_to`, `apply_header`, `insert_block`, `disconnect_from`. `KeychainTxOutIndex` methods touched: `reveal_to_target`, `reveal_to_target_multi`, `reveal_next_spk`, `next_unused_spk`, `lookahead_to_target`. `TxGraph`'s mutation surface is intentionally left untouched — `IndexedTxGraph` wraps it internally and merges the resulting changeset into the caller's `out`. https://claude.ai/code/session_01ATUB8fmcjzYwTEK1u2UcA6
Apply the &mut sink pattern to `IndexedTxGraph::from_changeset` and `LocalChain::from_genesis` — the two constructors that previously returned `(Self, ChangeSet)`. `IndexedTxGraph::from_changeset` now takes its `&mut ChangeSet` as the last parameter and treats it as both input (the persisted state to load from) and output (the same buffer is rewritten with the full reconstructed state, including any deltas produced by the internal `.reindex()` pass). The caller passes their persisted changeset in, and after the call the same buffer contains everything that needs to be saved back. `LocalChain::from_genesis` now takes a `&mut ChangeSet` sink for the initial state (the genesis block). This is pure-output — there is no input changeset — but the sink-last pattern is preserved for consistency. Most existing callers simply discarded the returned changeset; they now need to materialize a `ChangeSet::default()` to pass in. The remaining `from_changeset` constructors (`TxGraph`, `LocalChain`, `KeychainTxOutIndex`) take `ChangeSet` by value with no output and have no forget-to-merge footgun, so they are left unchanged. https://claude.ai/code/session_01ATUB8fmcjzYwTEK1u2UcA6
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.
Description
This PR refactors the changeset handling pattern across the codebase to use mutable references (
&mut C) instead of returningChangeSetvalues. This change affects multiple key APIs:Core changes:
IndexedTxGraphmethods (from_changeset,reindex,apply_update,insert_tx,insert_txout,insert_anchor,insert_seen_at,insert_evicted_at,batch_insert_*) now take a mutable changeset reference parameter instead of returning a changesetKeychainTxOutIndexmethods (reveal_next_spk,reveal_to_target,reveal_to_target_multi,lookahead_to_target,next_unused_spk) now take a mutable changeset reference parameterLocalChainmethods (from_genesis,apply_update,apply_header,apply_header_connected_to,insert_block,disconnect_from) now take a mutable changeset reference parameterKey benefits:
API pattern change:
let changeset = graph.insert_tx(tx);let mut changeset = ChangeSet::default(); graph.insert_tx(tx, &mut changeset);The refactoring uses a generic
AsMut<ChangeSet>trait bound to allow flexibility in how changesets are stored and passed around.Notes to the reviewers
from_changesetmethod now reads from and writes back to the same changeset reference, making it clear that the changeset is being modified with reindex deltasChangelog notice
Breaking Changes:
IndexedTxGraph::from_changesetnow takes a mutable changeset reference parameter and returnsSelfinstead of(Self, ChangeSet)IndexedTxGraph::reindexnow takes a mutable changeset reference parameter instead of returning a changesetIndexedTxGraphmutation methods now take a mutable changeset reference parameter instead of returning a changesetKeychainTxOutIndexmethods that modify state now take a mutable changeset reference parameter instead of returning a changesetLocalChain::from_genesisnow takes a mutable changeset reference parameter and returnsSelfinstead of(Self, ChangeSet)LocalChain::apply_updateand related methods now take a mutable changeset reference parameter instead of returning a changesetChecklists
All Submissions:
Bugfixes:
https://claude.ai/code/session_01ATUB8fmcjzYwTEK1u2UcA6