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

Cache ETH1 headers on startup for eth1data voting #7541

Merged
merged 27 commits into from
Oct 20, 2020

Conversation

rkapka
Copy link
Contributor

@rkapka rkapka commented Oct 15, 2020

What type of PR is this?

Optimization

What does this PR do? Why is it needed?

This PR caches ETH1 headers on POW initialization so that the cache can be utilized when voting for eth1data. This will optimize the voting algorithm because an RPC call to the ETH1 endpoint is made for every non-cached header.

Zinken testnet cache miss difference:

  • without caching: 280
  • with caching: 3

Which issues(s) does this PR fix?

N/A

Other notes for review

Unblocks #7362

@rkapka rkapka changed the title Get powchain block from cache Cache ETH1 headers on startup for eth1data voting Oct 15, 2020
@rkapka rkapka marked this pull request as ready for review October 19, 2020 12:43
@rkapka rkapka requested a review from a team as a code owner October 19, 2020 12:44
@rkapka rkapka added the Ready For Review A pull request ready for code review label Oct 19, 2020
blocksPerVotingPeriod := params.BeaconConfig().EpochsPerEth1VotingPeriod * params.BeaconConfig().SlotsPerEpoch *
params.BeaconConfig().SecondsPerSlot / params.BeaconConfig().SecondsPerETH1Block

end := lastKnownHeader.Number.Uint64() - params.BeaconConfig().Eth1FollowDistance
Copy link
Contributor

Choose a reason for hiding this comment

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

Check for underflow, even though this cant happen after block height 1024, it can mess up some people running dev goerli chains


end := lastKnownHeader.Number.Uint64() - params.BeaconConfig().Eth1FollowDistance
// We fetch twice the number of headers just to be safe.
start := end - 2*blocksPerVotingPeriod
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

blocksPerVotingPeriod := params.BeaconConfig().EpochsPerEth1VotingPeriod * params.BeaconConfig().SlotsPerEpoch *
params.BeaconConfig().SecondsPerSlot / params.BeaconConfig().SecondsPerETH1Block

end := lastKnownHeader.Number.Uint64() - params.BeaconConfig().Eth1FollowDistance
Copy link
Member

Choose a reason for hiding this comment

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

Please use the helper to calculate this:

func (s *Service) followBlockHeight(ctx context.Context) (uint64, error) {

_, err := s.batchRequestHeaders(start, end)
if err != nil {
// Caching failure is not critical, so we just log a warning.
log.Warningf("Unable to cache headers: %v", err)
Copy link
Member

@nisdas nisdas Oct 19, 2020

Choose a reason for hiding this comment

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

this should be an error, failure to request headers could lead to a block proposal with invalid eth1data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. We no longer call the ETH1 endpoint when we have a cache miss.

if err != nil {
return false, nil, err
}
span.AddAttributes(trace.BoolAttribute("blockCacheHit", true))
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be a metric maintained with prometheus?

@prylabs-bulldozer prylabs-bulldozer bot merged commit ec0af98 into master Oct 20, 2020
@delete-merged-branch delete-merged-branch bot deleted the get-powchain-block-from-cache branch October 20, 2020 05:10
nisdas pushed a commit that referenced this pull request Oct 20, 2020
@nisdas nisdas mentioned this pull request Oct 20, 2020
2 tasks
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.

None yet

4 participants