-
Notifications
You must be signed in to change notification settings - Fork 38
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
Conversation
WalkthroughThe update involves enhancing the Changes
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this 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
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 thegethRPCClient
struct is consistent with the PR objectives to cache blocks and reduce redundant network requests.
go/ethadapter/geth_rpc_client.go
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
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) | |
} |
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 |
There was a problem hiding this comment.
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.
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) |
There was a problem hiding this comment.
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.
e.blockCache.Add(hash, block) | |
if block != nil { | |
e.blockCache.Add(hash, block) | |
} |
There was a problem hiding this 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
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
There was a problem hiding this 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
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
There was a problem hiding this 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
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
There was a problem hiding this 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
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
There was a problem hiding this 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
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 |
There was a problem hiding this comment.
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.
Why this change is needed
We are currently quite inefficient with re-requesting blocks when:
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