fix: consistently check block input cache before checking hot db for blocks - #8823
Conversation
Summary of ChangesHello @nflaig, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the block retrieval strategy across the beacon node by consistently prioritizing an in-memory cache before querying the persistent database. This change aims to enhance performance by reducing database I/O and improves the system's robustness by addressing edge cases where blocks might be present in memory but not yet fully persisted, resolving an issue identified by e2e tests. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the issue of inconsistently checking the block input cache. By replacing direct database lookups with calls to chain.getBlockByRoot or implementing a cache-first logic in StateRegenerator, the changes ensure that the in-memory cache is always consulted before querying the database. This not only improves performance by avoiding unnecessary DB access but also resolves edge cases where a block might not yet be persisted. Additionally, the PR includes a crucial bug fix in SeenBlockInput's pruning logic, ensuring the cache evicts the oldest blocks as intended. The changes are well-implemented and improve the robustness and efficiency of block handling.
|
|
||
| if (itemsToDelete > 0) { | ||
| const sorted = [...this.blockInputs.entries()].sort((a, b) => b[1].slot - a[1].slot); | ||
| const sorted = [...this.blockInputs.entries()].sort((a, b) => a[1].slot - b[1].slot); |
There was a problem hiding this comment.
This is a great catch! Sorting in descending order of slot (b[1].slot - a[1].slot) would cause the cache to prune the newest blocks first, which is incorrect for an age-based eviction strategy. Changing the sort to ascending order (a[1].slot - b[1].slot) correctly ensures that the oldest blocks (by slot) are removed when the cache is full. This is an important correctness fix for the cache pruning logic.
Performance Report✔️ no performance regression detected Full benchmark results
|
lodekeeper
left a comment
There was a problem hiding this comment.
LGTM! 🔥
Good catch on the race condition between async block import and db persistence.
Changes look correct:
- Using
chain.getBlockByRoot()instead of directdb.block.get()ensures we check the in-memory cache first - Passing
seenBlockInputCacheto regen modules for consistency - Fixed the cache pruning sort order in
seenGossipBlockInput.ts(was descending, now ascending — so oldest entries get pruned instead of newest) - Better error message in
onForkChoiceFinalized(was printingheadBlockwhich was null) - Tests updated appropriately
The sort fix is especially nice — pruning newest entries first would have been counterproductive.
|
@lodekeeper did we catch all occurrences of |
|
Good question! I searched the codebase and found several remaining In
In
In API
These might also benefit from checking the blockInputCache first, though some may be intentionally different (e.g., archive block lookups are separate). Should I open a follow-up PR to address these consistently? 🔥 |
@lodekeeper make sure to look at the branch |
|
You're right, my bad! I was checking against Looking at
The remaining |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #8823 +/- ##
=========================================
Coverage 52.36% 52.36%
=========================================
Files 848 848
Lines 63686 63673 -13
Branches 4714 4714
=========================================
- Hits 33347 33343 -4
+ Misses 30270 30261 -9
Partials 69 69 🚀 New features to boost your workflow:
|
|
🎉 This PR is included in v1.40.0 🎉 |
Follow up on #8784 to consistently check block input cache before checking hot db for blocks.
This avoids edge cases where block is not yet persistent in db, also querying db if we can serve it from in-memory cache seems unnecessary.
This issue was caught by e2e tests