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

Host: Small cache of blocks by hash #1738

Merged
merged 7 commits into from
Jan 9, 2024
Merged

Conversation

BedrockSquirrel
Copy link
Collaborator

Why this change is needed

We are currently quite inefficient with re-requesting blocks when:

  • checking for forks on every block
  • if the host is busy when a new block comes in, we re-request it when they're ready to process it (exacerbated by multiple enclaves)

This is especially unfortunate for remote L1 data service like infura, there is a cost in latency and it counts against our request quota.

What changes were made as part of this PR

Add a small cache (using simple, popular LRU cache lib) for recent blocks so that the client doesn't go to RPC for the same data unnecessarily.

I did a test with a hit/miss counter to make sure it's getting used, it's at ~50% hit rate during resyncing (less during live streaming).

PR checks pre-merging

Please indicate below by ticking the checkbox that you have read and performed the required
PR checks

  • PR checks reviewed and performed

Copy link

coderabbitai bot commented Jan 9, 2024

Walkthrough

The update involves enhancing the gethRPCClient structure by incorporating a blockCache to store recent blocks. This optimization aims to reduce redundant network calls to fetch blocks by first checking the cache. The creation function for the Ethereum client now also initializes this new cache. Additionally, the method responsible for retrieving blocks by hash has been modified to leverage the cache, potentially improving performance by reducing the number of remote procedure calls (RPC).

Changes

File Path Change Summary
.../geth_rpc_client.go Added blockCache field to gethRPCClient struct; updated NewEthClientFromURL to initialize the cache; modified BlockByHash to utilize the cache before fetching from RPC.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f6f147a and 99448b2.
Files ignored due to filter (2)
  • go.mod
  • go.sum
Files selected for processing (1)
  • go/ethadapter/geth_rpc_client.go (3 hunks)
Additional comments: 2
go/ethadapter/geth_rpc_client.go (2)
  • 26-34: The introduction of a constant _defaultBlockCacheSize with a value of 51 is a good practice as it avoids magic numbers in the code and makes it easy to change the cache size in one place.

  • 44-44: The addition of the blockCache field to the gethRPCClient struct is consistent with the PR objectives to cache blocks and reduce redundant network requests.

@@ -49,12 +52,17 @@
}

logger.Trace(fmt.Sprintf("Initialized eth node connection - addr: %s", rpcURL))

// cache recent blocks to avoid re-fetching them (they are often re-used for checking for forks etc.)
blkCache, _ := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize)
Copy link

Choose a reason for hiding this comment

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

The cache is initialized without error handling. While lru.New is unlikely to return an error with a hardcoded size, it's best practice to handle the error instead of ignoring it. This ensures that any future changes that might cause an error won't be silently ignored.

- blkCache, _ := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize)
+ blkCache, err := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize)
+ if err != nil {
+     return nil, fmt.Errorf("failed to create block cache: %w", err)
+ }

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
blkCache, _ := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize)
blkCache, err := lru.New[gethcommon.Hash, *types.Block](_defaultBlockCacheSize)
if err != nil {
return nil, fmt.Errorf("failed to create block cache: %w", err)
}

Comment on lines +192 to +206
block, found := e.blockCache.Get(hash)
if found {
return block, nil
}

// not in cache, fetch from RPC
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()

return e.client.BlockByHash(ctx, hash)
block, err := e.client.BlockByHash(ctx, hash)
if err != nil {
return nil, err
}
e.blockCache.Add(hash, block)
return block, nil
Copy link

Choose a reason for hiding this comment

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

The BlockByHash method has been correctly modified to check the cache before making an RPC call. However, there is a potential issue with the type assertion after retrieving the block from the cache. The found value only indicates whether the key exists, not whether the type assertion will succeed. A type assertion should be performed to ensure the value is of the expected type.

- block, found := e.blockCache.Get(hash)
+ cachedBlock, found := e.blockCache.Get(hash)
+ if found {
+     block, ok := cachedBlock.(*types.Block)
+     if !ok {
+         e.logger.Error("cache returned a non-block type")
+         return nil, fmt.Errorf("cache integrity error: expected *types.Block, got %T", cachedBlock)
+     }
+     return block, nil
+ }

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
block, found := e.blockCache.Get(hash)
if found {
return block, nil
}
// not in cache, fetch from RPC
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
return e.client.BlockByHash(ctx, hash)
block, err := e.client.BlockByHash(ctx, hash)
if err != nil {
return nil, err
}
e.blockCache.Add(hash, block)
return block, nil
cachedBlock, found := e.blockCache.Get(hash)
if found {
block, ok := cachedBlock.(*types.Block)
if !ok {
e.logger.Error("cache returned a non-block type")
return nil, fmt.Errorf("cache integrity error: expected *types.Block, got %T", cachedBlock)
}
return block, nil
}
// not in cache, fetch from RPC
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
block, err := e.client.BlockByHash(ctx, hash)
if err != nil {
return nil, err
}
e.blockCache.Add(hash, block)
return block, nil

if err != nil {
return nil, err
}
e.blockCache.Add(hash, block)
Copy link

Choose a reason for hiding this comment

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

The block is added to the cache without checking if the block is nil. Adding a nil block to the cache could lead to unexpected behavior when the cache is accessed later. It would be prudent to add a nil check before caching the block.

+ if block != nil {
+     e.blockCache.Add(hash, block)
+ }

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
e.blockCache.Add(hash, block)
if block != nil {
e.blockCache.Add(hash, block)
}

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 99448b2 and 609496e.
Files selected for processing (1)
  • go/ethadapter/geth_rpc_client.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • go/ethadapter/geth_rpc_client.go

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 609496e and 7904ffd.
Files selected for processing (1)
  • go/ethadapter/geth_rpc_client.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • go/ethadapter/geth_rpc_client.go

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7904ffd and f083c16.
Files selected for processing (1)
  • go/ethadapter/geth_rpc_client.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • go/ethadapter/geth_rpc_client.go

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f083c16 and 7778ef7.
Files selected for processing (1)
  • go/ethadapter/geth_rpc_client.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • go/ethadapter/geth_rpc_client.go

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7778ef7 and 8f564c5.
Files selected for processing (1)
  • go/ethadapter/geth_rpc_client.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • go/ethadapter/geth_rpc_client.go

)

const (
connRetryMaxWait = 10 * time.Minute // after this duration, we will stop retrying to connect and return the failure
connRetryInterval = 500 * time.Millisecond
_maxRetryPriceIncreases = 5
_retryPriceMultiplier = 1.2
_defaultBlockCacheSize = 51 // enough for 50 request batch size and one for previous block
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is just a finger in the air placeholder, I tried it with 200 and it was fine but doesn't give you many more cache hits so I went for a reasonable minimum.

We can make this configurable at some point if it ever becomes useful.

@BedrockSquirrel BedrockSquirrel merged commit f72e986 into main Jan 9, 2024
2 checks passed
@BedrockSquirrel BedrockSquirrel deleted the matt/cache-blocks branch January 9, 2024 15:02
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.

2 participants