Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loadblock returns err on invalid range #7811

Merged
merged 11 commits into from
Nov 16, 2020

Conversation

terencechain
Copy link
Member

What type of PR is this?

Bug

What does this PR do? Why is it needed?

Supersedes #7781

This PR fixes LoadBlocks to return err in the event of input start slot > end slot. It also updates the test to conform to this standard. The previous written tests have cut corners and violated this assumptions

Which issues(s) does this PR fix?

Fixes #7620

Other notes for review

@terencechain terencechain added the Ready For Review A pull request ready for code review label Nov 13, 2020
@terencechain terencechain requested a review from a team as a code owner November 13, 2020 18:35
@terencechain terencechain self-assigned this Nov 13, 2020
Comment on lines 228 to 232
// In the event where last valid block slot is less than current state slot,
// we can just process start state up to input slot.
if lastValidSlot >= startState.Slot() {
return processSlotsStateGen(ctx, startState, slot)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this comment correct? I can't make the connection between the text and the if statement.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! Thanks

@@ -219,12 +219,24 @@ func (s *State) loadStateBySlot(ctx context.Context, slot uint64) (*state.Beacon
return nil, err
}

// In the event where start state is greater or equal to requested slot,
// we should just return the start state.
if startState.Slot() >= slot {
Copy link
Member

Choose a reason for hiding this comment

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

wouldn't that be an error condition ?

lastSaved, err := s.beaconDB.HighestSlotStatesBelow(ctx, slot+1)

According to this method it should always be <= slot

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed and this is not necessary. Removed

// Gather the last saved block root and the slot number.
lastValidRoot, lastValidSlot, err := s.lastSavedBlock(ctx, slot)
if err != nil {
return nil, errors.Wrap(err, "could not get last valid block for hot state using slot")
}

// In the event where current state slot is greater or equal to last valid block slot,
// we should just process state up to input slot.
if startState.Slot() >= lastValidSlot {
Copy link
Member

Choose a reason for hiding this comment

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

This also sounds like an impossible condition, no ? How would the state.Slot be larger than the block's slot. If there is
no block with that particular slot, its not possible to generate the state with that particular slot.

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed, but it's still better to be defensive here due to bugs from downstream functions. I added a comment to mention this is an impossible condition

Copy link
Member

Choose a reason for hiding this comment

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

In that case shouldn't we fail over here and return an error ? Having something like this might mask a bigger issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

not necessarily, it just means the last available block slot is less than the start state slot. This serves as a short cut and also makes a lot of tests happy.

Example:

  • request state with slot 200
  • last valid state in DB given slot index is at slot 100
  • last valid block in DB given slot index is <= slot 100
  • in this case, we can short cut and advance state from slot 100 to 200 without running rest of the logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Load Blocks Should Return Error On Invalid Range
3 participants