Skip to content

Refactor changeset handling to use mutable references#19

Draft
evanlinjin wants to merge 2 commits into
masterfrom
claude/gifted-cannon-X2ECe
Draft

Refactor changeset handling to use mutable references#19
evanlinjin wants to merge 2 commits into
masterfrom
claude/gifted-cannon-X2ECe

Conversation

@evanlinjin

Copy link
Copy Markdown
Owner

Description

This PR refactors the changeset handling pattern across the codebase to use mutable references (&mut C) instead of returning ChangeSet values. This change affects multiple key APIs:

Core changes:

  • IndexedTxGraph methods (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 changeset
  • KeychainTxOutIndex methods (reveal_next_spk, reveal_to_target, reveal_to_target_multi, lookahead_to_target, next_unused_spk) now take a mutable changeset reference parameter
  • LocalChain methods (from_genesis, apply_update, apply_header, apply_header_connected_to, insert_block, disconnect_from) now take a mutable changeset reference parameter

Key benefits:

  • Eliminates unnecessary allocations and copies of changeset data
  • Allows callers to accumulate changes across multiple operations into a single changeset
  • Simplifies the API by making it clear that changes are being written into the provided reference
  • Enables better control over when changesets are persisted

API pattern change:

  • Old: let changeset = graph.insert_tx(tx);
  • New: 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

  • All method signatures have been updated consistently across the codebase
  • The from_changeset method now reads from and writes back to the same changeset reference, making it clear that the changeset is being modified with reindex deltas
  • All existing tests have been updated to use the new API pattern
  • The change is breaking but provides a cleaner, more efficient API going forward

Changelog notice

Breaking Changes:

  • IndexedTxGraph::from_changeset now takes a mutable changeset reference parameter and returns Self instead of (Self, ChangeSet)
  • IndexedTxGraph::reindex now takes a mutable changeset reference parameter instead of returning a changeset
  • All IndexedTxGraph mutation methods now take a mutable changeset reference parameter instead of returning a changeset
  • KeychainTxOutIndex methods that modify state now take a mutable changeset reference parameter instead of returning a changeset
  • LocalChain::from_genesis now takes a mutable changeset reference parameter and returns Self instead of (Self, ChangeSet)
  • LocalChain::apply_update and related methods now take a mutable changeset reference parameter instead of returning a changeset

Checklists

All Submissions:

Bugfixes:

  • This pull request breaks the existing API

https://claude.ai/code/session_01ATUB8fmcjzYwTEK1u2UcA6

claude added 2 commits May 28, 2026 01:43
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
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.

2 participants