Skip to content

fix(node-api): return 404 for unknown state_id roots#3142

Open
NikhilSharmaWe wants to merge 2 commits into
berachain:mainfrom
NikhilSharmaWe:fix/node-api-state-id-404
Open

fix(node-api): return 404 for unknown state_id roots#3142
NikhilSharmaWe wants to merge 2 commits into
berachain:mainfrom
NikhilSharmaWe:fix/node-api-state-id-404

Conversation

@NikhilSharmaWe

Copy link
Copy Markdown

Summary

When a client queries a Beacon API endpoint with a valid hex state_id that is not indexed on the node (e.g. GET /eth/v1/beacon/states/0x<unknown-root>/validators), StateIDToHeight returned an error that was not wrapped with types.ErrNotFound. Middleware therefore mapped the response to HTTP 500 instead of the spec-defined HTTP 404 ("State not found").

This change wraps state root lookup failures with types.ErrNotFound at the mapping layer, fixing all /states/{state_id}/* endpoints in one place. The underlying storage error (including the state root) is preserved in the error chain, consistent with how GetBlockHeaders handles unknown parent block roots.

block.ErrBlockStoreNotEnabled is passed through unchanged, since that indicates a server configuration issue rather than a missing state.

Changes

  • node-api/handlers/utils/mappings.go: wrap GetSlotByStateRoot failures with types.ErrNotFound; exclude ErrBlockStoreNotEnabled
  • node-api/handlers/utils/mappings_test.go: unit tests for unknown state root, block store disabled, and existing resolution paths

Test plan

  • go test ./node-api/handlers/utils/... -count=1

Follow-up

BlockIDToHeight has the same class of issue for unknown block_id roots (GetBlockHeaderByID, GetBlobSidecars). Happy to follow up in a separate PR if this direction looks good.

Signed-off-by: Nikhil Sharma <nikhilsharma230303@gmail.com>
@NikhilSharmaWe NikhilSharmaWe requested a review from a team as a code owner June 30, 2026 11:49
Copilot AI review requested due to automatic review settings June 30, 2026 11:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Beacon API /eth/v1/beacon/states/{state_id}/* endpoints returning HTTP 500 for valid-but-unknown state_id roots by ensuring the mapping layer wraps “unknown state root” lookup failures with the handlers/types.ErrNotFound sentinel so the response middleware correctly maps them to HTTP 404. It explicitly preserves block.ErrBlockStoreNotEnabled as a non-404 configuration/runtime error.

Changes:

  • Wrap GetSlotByStateRoot lookup failures with handlers/types.ErrNotFound (except block.ErrBlockStoreNotEnabled) to produce spec-defined 404s.
  • Add unit tests covering unknown state roots, block store disabled, and existing resolution paths (head/slot).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
node-api/handlers/utils/mappings.go Wrap state-root lookup errors with types.ErrNotFound while passing through ErrBlockStoreNotEnabled.
node-api/handlers/utils/mappings_test.go Adds coverage for state ID resolution paths and error classification.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +102 to +107
for _, target := range tc.errIs {
require.ErrorIs(t, err, target)
}
for _, target := range tc.errNot {
require.NotErrorIs(t, err, target)
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 6cdd464

Signed-off-by: Nikhil Sharma <nikhilsharma230303@gmail.com>
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