Skip to content

fix: add slot and epoch validation to epoch committees endpoint#7941

Merged
wemeetagain merged 9 commits intoChainSafe:unstablefrom
bomanaps:fix/lodestar-invalid-epoch-slot-error
Sep 4, 2025
Merged

fix: add slot and epoch validation to epoch committees endpoint#7941
wemeetagain merged 9 commits intoChainSafe:unstablefrom
bomanaps:fix/lodestar-invalid-epoch-slot-error

Conversation

@bomanaps
Copy link
Contributor

Motivation

Description
This PR fixes an inconsistency in Lodestar's Beacon-API behavior when querying:

curl "http://127.0.0.1:9596/eth/v1/beacon/states/head/committees?epoch=2&slot=118"

Previously, Lodestar would return a 500 Internal Server Error when the provided slot did not belong to the given epoch (e.g., epoch=2&slot=118), due to an unhandled EPOCH_CONTEXT_ERROR_DECISION_ROOT_EPOCH_OUT_OF_RANGE error.

Screenshot 2025-06-10 at 08 15 05 Screenshot 2025-06-10 at 08 14 33

Closes #7882

Steps to test or reproduce

@bomanaps bomanaps requested a review from a team as a code owner June 10, 2025 07:42
@CLAassistant
Copy link

CLAassistant commented Jun 10, 2025

CLA assistant check
All committers have signed the CLA.

@bomanaps
Copy link
Contributor Author

Hello @nflaig Could you please review this when you have time?

@nflaig
Copy link
Member

nflaig commented Sep 2, 2025

@bomanaps are you planning to continue working on this? it resolves an error we see on the devnet as well and would be good to get this in soon

@bomanaps bomanaps requested a review from nflaig September 2, 2025 17:40
Comment on lines 274 to 276
if (error instanceof Error && error.message?.includes("EPOCH_CONTEXT_ERROR_DECISION_ROOT_EPOCH_OUT_OF_RANGE")) {
throw new ApiError(400, `Invalid epoch value: epoch ${epoch} is out of range`);
}
Copy link
Member

@nflaig nflaig Sep 3, 2025

Choose a reason for hiding this comment

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

as noted in #7941 (comment) I don't see why this is necessary, we check the epoch above to make sure it's within one epoch of state epoch

const stateEpoch = computeEpochAtSlot(state.slot);
if (filters.slot !== undefined) {
const epochStartSlot = computeStartSlotAtEpoch(epoch);
const epochEndSlot = epochStartSlot + 32 - 1;
Copy link
Member

Choose a reason for hiding this comment

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

this will break with minimal preset as an epoch only has 8 slots, always need to use SLOTS_PER_EPOCH

}
}
if (Math.abs(epoch - stateEpoch) > 1) {
throw new ApiError(400, `Invalid epoch value: epoch ${epoch} is out of range`);
Copy link
Member

Choose a reason for hiding this comment

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

would be useful to return state epoch and explain in the error that the epoch needs to be within one epoch, "out of range" doesn't provide enough details imo

@nflaig nflaig changed the title fix(api): invalid-epoch-slot-error fix: add slot and epoch validation to epoch committees endpoint Sep 3, 2025
nflaig
nflaig previously approved these changes Sep 3, 2025
Copy link
Member

@nflaig nflaig left a comment

Choose a reason for hiding this comment

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

LGTM - I addressed all comments from above, the change is much simpler now as well, thanks @bomanaps for looking into this

@codecov
Copy link

codecov bot commented Sep 3, 2025

Codecov Report

❌ Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.11%. Comparing base (7f2271a) to head (6adb5f4).
⚠️ Report is 6 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #7941      +/-   ##
============================================
- Coverage     54.11%   54.11%   -0.01%     
============================================
  Files           849      849              
  Lines         64043    64051       +8     
  Branches       4854     4855       +1     
============================================
+ Hits          34659    34662       +3     
- Misses        29307    29312       +5     
  Partials         77       77              
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nflaig
Copy link
Member

nflaig commented Sep 3, 2025

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds validation to the /eth/v1/beacon/states/{state_id}/committees endpoint to handle cases where slot and epoch query parameters are inconsistent. The changes correctly introduce two checks: one to ensure a provided slot falls within the specified epoch, and another to ensure the requested epoch is within one epoch of the state's epoch. This prevents an unhandled error that previously resulted in a 500 response. The implementation is correct and effectively resolves the issue. I've suggested a minor reordering of the validation checks to improve logical flow by failing earlier on more fundamental constraints.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@wemeetagain wemeetagain merged commit 3795e32 into ChainSafe:unstable Sep 4, 2025
51 of 57 checks passed
@wemeetagain
Copy link
Member

🎉 This PR is included in v1.34.0 🎉

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.

Inconsistent Response of Endpoint /eth/v1/beacon/states/{state_id}/committees

4 participants