fix(chain)!: reject changesets that replace the genesis block - #2331
Open
Brijesh-Thakkar wants to merge 1 commit into
Open
Brijesh-Thakkar wants to merge 1 commit into
Brijesh-Thakkar wants to merge 1 commit into
Conversation
Brijesh-Thakkar
requested review from
evanlinjin and
oleonardolima
as code owners
September 29, 2026 21:08
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 #2309
Description
LocalChain::apply_changesetwas the only mutation entry point that did not protect the genesis block. AChangeSetcontaining(0, Some(hash))with a hash different from the chain's genesis madeapply_changeset_to_checkpointrebuild the chain viaLocalChain::from_blockswithout comparing against the old genesis, so the chain silently moved onto a different genesis.apply_update(viamerge_chains),insert_blockanddisconnect_fromalready refuse this.This adds a check at the top of
apply_changeset_to_checkpoint. If the changeset has a height-0 block whose hash differs from the chain's genesis, it returns the newApplyBlockError::CannotReplaceGenesis { expected }before anything is applied, so the chain is left unchanged. A matching genesis entry still applies, andfrom_changesetis unaffected because it seeds genesis from the same changeset.Notes to the reviewers
ApplyBlockErroris not#[non_exhaustive], so adding a variant is a breaking change for exhaustive matches. I can add#[non_exhaustive]or use a different approach if you prefer.merge_chainsalready rejects a differing genesis before reaching this code, so its new match arm only satisfies the exhaustive match and maps toCannotConnectError { try_include_height: 0 }.get(0), which only returns checkpoints with data. This matches the!is_placeholder()check inmerge_chains. ALocalChaincannot hold a placeholder genesis, so I could not write a test for that case.(0, None)already fails withMissingGenesisand is unchanged. A test pins that behavior.expected, consistent withPrevBlockhashMismatch.bdk_chainfeature and no_std builds,cargo docwith-D warnings, and the 1.85.0 MSRV build and tests locally. The other crates' per-feature builds are left to CI.Changelog notice
Changed
LocalChain::apply_changesetreturns the newApplyBlockError::CannotReplaceGenesis, and leaves the chain unchanged, when the changeset would replace the genesis block.LocalChain::apply_changesetsilently replaces the genesis block #2309ApplyBlockErrorhas a new variant, so exhaustive matches must handle it.Checklists
All Submissions:
Bugfixes: