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

Use StateRoot as Key for Cache #7540

Merged
merged 9 commits into from
Oct 19, 2020
Merged

Use StateRoot as Key for Cache #7540

merged 9 commits into from
Oct 19, 2020

Conversation

nisdas
Copy link
Member

@nisdas nisdas commented Oct 15, 2020

What type of PR is this?

Feature Improvement

What does this PR do? Why is it needed?

  • It adds in the stateroot of the previous epoch as the cache key.
  • Rejects zerohash as a cache key.

Which issues(s) does this PR fix?

Other notes for review
cc @terencechain

}
if proposerIndices != nil {
return proposerIndices[state.Slot()%params.BeaconConfig().SlotsPerEpoch], nil
}
}
if err := UpdateProposerIndicesInCache(state, e); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Should we move UpdateProposerIndicesInCache inside !bytes.Equal(r, params.BeaconConfig().ZeroHash[:]) as well?

return nil, nil
}

b.lock.RLock()
Copy link
Member

Choose a reason for hiding this comment

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

i think the lock should be before b.state.StateRoots == nil? It's a read there

Copy link
Member Author

Choose a reason for hiding this comment

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

True, although this is a general pattern in all our getters which in hindsight looks incorrect. I can fix it in another pr.

return 0, errors.Wrap(err, "could not interface with committee cache")
}
if proposerIndices != nil {
return proposerIndices[state.Slot()%params.BeaconConfig().SlotsPerEpoch], nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps a length check here, just to be extra defensive even if it is unlikely to happen

Copy link
Member

Choose a reason for hiding this comment

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

@rauljordan I think there is sufficient length check above. We know that len(proposerIndices) is exactly SlotsPerEpoch so when we do slot%SlotsPerEpoch, we know that there cannot be out of bounds since the max value of that operation would be the last value in the slice.

terencechain
terencechain previously approved these changes Oct 17, 2020
@nisdas nisdas marked this pull request as ready for review October 17, 2020 06:01
@nisdas nisdas requested a review from a team as a code owner October 17, 2020 06:01
terencechain
terencechain previously approved these changes Oct 17, 2020
terencechain
terencechain previously approved these changes Oct 17, 2020
@codecov
Copy link

codecov bot commented Oct 17, 2020

Codecov Report

Merging #7540 into master will decrease coverage by 0.02%.
The diff coverage is 63.15%.

@@            Coverage Diff             @@
##           master    #7540      +/-   ##
==========================================
- Coverage   61.70%   61.68%   -0.03%     
==========================================
  Files         424      424              
  Lines       29884    29925      +41     
==========================================
+ Hits        18441    18459      +18     
- Misses       8484     8496      +12     
- Partials     2959     2970      +11     

@nisdas nisdas marked this pull request as draft October 17, 2020 11:37
@nisdas nisdas added the Blocked Blocked by research or external factors label Oct 17, 2020
@nisdas nisdas marked this pull request as ready for review October 17, 2020 13:11
@@ -348,9 +349,9 @@ func UpdateCommitteeCache(state *stateTrie.BeaconState, epoch uint64) error {

// UpdateProposerIndicesInCache updates proposer indices entry of the committee cache.
func UpdateProposerIndicesInCache(state *stateTrie.BeaconState, epoch uint64) error {
// The cache uses the block root at the last epoch slot as key. (e.g. for epoch 1, the key is root at slot 31)
// The cache uses the state root at the last epoch slot as key. (e.g. for epoch 1, the key is root at slot 31)
Copy link
Member

Choose a reason for hiding this comment

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

This comment is still correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks will fix it tomorrow

if err != nil {
return 0, errors.Wrap(err, "could not interface with committee cache")
}
if proposerIndices != nil && len(proposerIndices) == int(params.BeaconConfig().SlotsPerEpoch) {
Copy link
Member

Choose a reason for hiding this comment

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

I would argue that if length does not equal to slots per epoch, It should return an error here. The consumers should know why this fail and proposer cache is not working since there will be performance penalties

return 0, errors.Wrap(err, "could not interface with committee cache")
}
if proposerIndices != nil {
return proposerIndices[state.Slot()%params.BeaconConfig().SlotsPerEpoch], nil
Copy link
Member

Choose a reason for hiding this comment

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

@rauljordan I think there is sufficient length check above. We know that len(proposerIndices) is exactly SlotsPerEpoch so when we do slot%SlotsPerEpoch, we know that there cannot be out of bounds since the max value of that operation would be the last value in the slice.

@prestonvanloon
Copy link
Member

LGTM. We ran this PR in production environment for 12 hours without significant issue.

@prestonvanloon prestonvanloon merged commit 329fbff into master Oct 19, 2020
@delete-merged-branch delete-merged-branch bot deleted the changeCacheKey branch October 19, 2020 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked Blocked by research or external factors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants