Add prev_blockhash validation to CheckPoint#2115
Add prev_blockhash validation to CheckPoint#2115evanlinjin wants to merge 11 commits intobitcoindevkit:masterfrom
prev_blockhash validation to CheckPoint#2115Conversation
Additionally, `insert` now panics if the genesis block gets displaced (if it existed in the first place). Co-authored-by: valued mammal <valuedmammal@protonmail.com>
Make `TestLocalChain` and `ExpectedResult` generic over checkpoint data type `D`, allowing the same test infrastructure to work with both `BlockHash` and `TestBlock` types. Add `merge_chains_with_prev_blockhash` test to verify that `prev_blockhash` correctly invalidates conflicting blocks and connects disjoint chains. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Add comprehensive tests for CheckPoint::push error cases: - Push fails when height is not greater than current - Push fails when prev_blockhash conflicts with self - Push succeeds when prev_blockhash matches Include tests for CheckPoint::insert conflict handling: - Insert with conflicting prev_blockhash - Insert purges conflicting tail - Insert between conflicting checkpoints 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: valued mammal <valuedmammal@protonmail.com>
Use `D: Clone` instead of `D: Copy`.
Introduce `ApplyBlockError` enum with two variants: - `MissingGenesis`: genesis block is missing or would be altered - `PrevBlockhashMismatch`: block's `prev_blockhash` doesn't match expected This replaces `MissingGenesisError` in several `LocalChain` methods: - `from_blocks` - `from_changeset` - `apply_changeset` Also adds test cases for `merge_chains` with `prev_blockhash` scenarios: - Update displaces invalid block below point of agreement - Update fills gap with matching `prev_blockhash` - Cascading eviction through multiple blocks 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Explain the purpose of `CheckPointEntry` and its two variants: - `Occupied`: real checkpoint at this height - `Placeholder`: implied by `prev_blockhash` from checkpoint above Also fix typo: "atleast" → "at least" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
This PR is actually ready for review now! |
nymius
left a comment
There was a problem hiding this comment.
Approach ACK 2c29ff1
I think the "ghost" checkpoint approach is good enough for managing the new "not-really-in-chain" checkpoints.
After detaining myself for a while trying to understand for the first time the inner workings of merge_chain, I think it is hard to read, and it's going to get hairy to maintain in the future.
I will come back with some ideas to address that.
Something similar happens with the CheckPointEntry::prev recursion. Some unit test will come handy in the future.
I liked the format of TestLocalChain, the tables were very useful.
| #[derive(Clone, Debug, PartialEq)] | ||
| pub enum ApplyBlockError { | ||
| /// Genesis block is missing or would be altered. | ||
| MissingGenesis, |
There was a problem hiding this comment.
I would split this error in two to keep a single semantic for each one: MissingGenesis and ConflictingGenesis or similar
| // Update block that doesn't exist in the original chain | ||
| (o, Some(u)) if Some(u.height()) > o.map(|o| o.height()) => { | ||
| changeset.blocks.insert(u.height(), Some(u.data())); | ||
| // Only append to `ChangeSet` when this is an actual checkpoint. |
There was a problem hiding this comment.
What's the definition of "actual checkpoint": one that isn't a Placeholder or one that contains some data?
Why would it be a checkpoint with no data but at the same time is not a Placeholder?
Wouldn't be better to rewrite this comment to not leave to the reader the definition of "actual checkpoint"?
There was a problem hiding this comment.
"actual checkpoint" is a checkpoint that is not a placeholder, a.k.a. a checkpoint that actually contains data.
Actual checkpoints are persisted, placeholder checkpoints are not. If this is a placeholder checkpoint, we don't want to persist it as placeholder checkpoints are already implied by actual checkpoints (and there is no data to persist anyway).
There was a problem hiding this comment.
Ok, so all Placeholders are "Checkpoints without data" and all "Checkpoints without data" are Placeholders, i.e., there is a one-to-one mapping between the wrapper iter and the actual structs.
| // Continue traversing down (if possible). | ||
| match cp.prev() { | ||
| Some(prev) => cp = prev, | ||
| None => break None, |
There was a problem hiding this comment.
When we hit None on this iterator, it means we have exhausted CheckPoint so we are beyond genesis block?
There was a problem hiding this comment.
No. The original chain may not have a genesis. We will not hit this if the original chain has a genesis block.
There was a problem hiding this comment.
The original chain may not have a genesis.
There is some no-buggy reason this could happen?
There was a problem hiding this comment.
I haven't encountered it in person, but if there's no reason to error/panic, then we shouldn't do it.
CheckPoint is an update type, chain sources might merge chains together before forming an update (not sure).
| // Verify chain structure | ||
| assert_eq!(result.height(), 5, "tip should be at height 5"); | ||
| // Should have: checkpoint 5 only | ||
| assert_eq!( |
There was a problem hiding this comment.
What's the difference between displaced and purged if 4 is not part of the chain?
There was a problem hiding this comment.
Purged is "transitively displaced".
In this test, 5' has a prev_blockhash that displaces 4. Since, the original chain connects 4 to 6, if 4 is displaced, then 6 is purged.
| assert_eq!(new_cp.height(), 105); | ||
| assert_eq!(new_cp.hash(), hash!("block_105")); | ||
|
|
||
| // Verify chain structure: 100, 105 |
There was a problem hiding this comment.
This is not precisely verifying chain structure. An explicit check for both CheckPoints will be more accurate.
| blockhash: hash!("block_0"), | ||
| prev_blockhash: hash!("genesis_parent"), | ||
| }; | ||
| let block_1 = TestBlock { |
There was a problem hiding this comment.
Isn't this redundant for this test case?
| blockhash: hash!("block_0"), | ||
| prev_blockhash: hash!("genesis_parent"), | ||
| }; | ||
| let block_1 = TestBlock { |
There was a problem hiding this comment.
Isn't this redundant for this test case?
| /// extended. | ||
| /// height order, or there are any `prev_blockhash` mismatches, then returns an `Err(..)` | ||
| /// containing the last checkpoint that would have been extended. | ||
| pub fn from_blocks(blocks: impl IntoIterator<Item = (u32, D)>) -> Result<Self, Option<Self>> { |
There was a problem hiding this comment.
Doesn't this merit an Error and Returns section?
@nymius thanks for looking into that! For this PR, it's good enough to just understand how |
Yes, not intention to turn this into a refactor. Something for another PR. |
Closes #2021
Related to #2076
Replaces #2024
Replaces #2091
Description
This PR adds
prev_blockhashawareness toCheckPoint, enabling proper chain validation when merging checkpoint chains that store block headers or similar data with previous block hash information.Notes to the reviewers
This PR replaces some prior attempts:
feat(chain)!: make
CheckPointdatafield optional #2024 - where we made theCheckPoint::dataoptional - however this resulted in internal complexity and an API with annoying edge cases. The tests from this PR were still useful.feat(chain)!: Add
prev_blockhashtoToBlockHashtrait #2091 - This second attempt had some good ideas, but was distracted from the goal of EnsureCheckPointchain methods validate and link via previous blockhash #2021. I mostly reused theCheckPoint::insertimplementation of that PR.Changelog notice
Checklists
All Submissions:
New Features: