Merged
Conversation
79eecb3 to
d390391
Compare
Merged
prestonvanloon
pushed a commit
that referenced
this pull request
Mar 19, 2026
This PR addresses multiple issues in the hdiff code: 1. the function `hasStateUsingStateDiff()` that's called in `db.HasState()` was returning true for any slot that fell on the hdiff tree levels. This is wrong because those states might not actually be saved. This PR fixes that by actually looking in the db to check. 2. add the call to `saveStateByDiff()` in `db.SaveState()` to actually save the states in the hdiff tree. this was missed previously. the reason for the if guard `s.stateDiffCache != nil` over this call is that when syncing from genesis, the call to initialize state diff comes after the call of `SaveState()` to save the genesis state. which would cause a panic here. with this guard, that save goes to the legacy `stateBucket`. but the state will later get saved in the hdiff tree when `initializeStateDiff()` is called. 3. adds an early return to `getStateUsingStateDiff()` when `computeLevel(slot)` returns -1. 4. moves the call to `initializeStateDiff()` higher when doing checkpoint sync. specifically moves it before the call to `SaveState()`. otherwise there is a bug: state diff tries to save that state in the tree because it has offset 0 from the genesis call, then it would error out because the checkpoint slot doesn't have the necessary parents in the tree. (the offset isn't updated yet). this move makes it so the offset is updated before saving the state. the `SaveState()` call is also skipped after initializing state diff, because it's redundant. ~~**NOTE:** maybe if this pattern is good, we want to do the same in genesis.go to avoid writing to the legacy `stateBucket` entirely.~~ This is done in #16534 5. other small fixes.
beacon-chain/db/kv/blocks.go
Outdated
Comment on lines
683
to
687
| hasStateInDB := s.HasState(ctx, blockRoot) | ||
| return s.db.Update(func(tx *bolt.Tx) error { | ||
| hasStateInDB := tx.Bucket(stateBucket).Get(blockRoot[:]) != nil | ||
| if !(hasStateInDB || hasStateSummary) { | ||
| return errors.New("no state or state summary found with head block root") | ||
| } |
Member
There was a problem hiding this comment.
Let's move this if statement outside of the write transaction too.
if !(hasStateInDB || hasStateSummary) {
return errors.New("no state or state summary found with head block root")
}
prestonvanloon
approved these changes
Mar 19, 2026
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.
In 3 places, in order to see if db has a state, we're directly reading from the
stateBucketin a bolt tx. This would cause problems when using state diffs.So this PR moves those checks outside of the tx and calls the
HasState()function which already accounts for everything.due to a bug where these
HasState()calls result in a panic because thestateDiffCacheis not initialized before the calls, the call to initialize state diff was moved higher to ensure we have the cache before doing anything.