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

Add cache to Eth1 RPC functions #5347

Merged
merged 9 commits into from
Apr 9, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion beacon-chain/powchain/block_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func (s *Service) BlockHashByHeight(ctx context.Context, height *big.Int) (commo
func (s *Service) BlockTimeByHeight(ctx context.Context, height *big.Int) (uint64, error) {
ctx, span := trace.StartSpan(ctx, "beacon-chain.web3service.BlockTimeByHeight")
defer span.End()
if exists, blkInfo, err := s.blockCache.BlockInfoByHeight(height); exists || err != nil {
if err != nil {
return 0, err
}
span.AddAttributes(trace.BoolAttribute("blockCacheHit", true))
return blkInfo.Number.Uint64(), nil
}
span.AddAttributes(trace.BoolAttribute("blockCacheHit", false))
block, err := s.blockFetcher.BlockByNumber(ctx, height)
if err != nil {
return 0, errors.Wrap(err, "could not query block with given height")
Expand All @@ -72,8 +80,9 @@ func (s *Service) BlockTimeByHeight(ctx context.Context, height *big.Int) (uint6
// This is a naive implementation that will use O(ETH1_FOLLOW_DISTANCE) calls to cache
// or ETH1. This is called for multiple times but only changes every
// SlotsPerEth1VotingPeriod (1024 slots) so the whole method should be cached.
// How should caching the whole method be done?
Copy link
Member

Choose a reason for hiding this comment

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

? do we need this comment

Copy link
Contributor Author

@0xKiwi 0xKiwi Apr 8, 2020

Choose a reason for hiding this comment

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

Just for personal tracking as I try to figure out whats needed, will remove before the PR is ready for review.

func (s *Service) BlockNumberByTimestamp(ctx context.Context, time uint64) (*big.Int, error) {
ctx, span := trace.StartSpan(ctx, "beacon-chain.web3service.BlockByTimestamp")
ctx, span := trace.StartSpan(ctx, "beacon-chain.web3service.BlockNumberByTimestamp")
defer span.End()

head, err := s.blockFetcher.BlockByNumber(ctx, nil)
Expand Down