Skip to content

feat: add slashing cache#199

Merged
wemeetagain merged 120 commits intomainfrom
gr/slashed_cache
Feb 2, 2026
Merged

feat: add slashing cache#199
wemeetagain merged 120 commits intomainfrom
gr/slashed_cache

Conversation

@GrapeBaBa
Copy link
Copy Markdown
Contributor

@GrapeBaBa GrapeBaBa commented Jan 28, 2026

Motivation

This PR introduces a slashing cache which to eliminate the slash status checking hotspot during the process attestation. It has been tested which could reduce a lot of time cost using bench_process_block. Refer to the lighthouse https://github.com/sigp/lighthouse/blob/stable/consensus/types/src/beacon_state.rs#L704.

Fix #179

wemeetagain and others added 30 commits January 8, 2026 11:01
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
wemeetagain and others added 15 commits January 21, 2026 16:51
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @GrapeBaBa, 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 introduces a new SlashingsCache mechanism to optimize the process of checking validator slashing statuses within the state transition logic. By maintaining an in-memory cache of slashed validators, the system can avoid costly lookups into the main beacon state, particularly during block processing and attestation handling. This enhancement aims to improve the overall performance and efficiency of state transitions by providing a quick way to determine if a validator has been slashed.

Highlights

  • New Slashing Cache: Introduced a new SlashingsCache struct and its associated functions to efficiently track the indices of slashed validators using a DynamicBitSet.
  • Integration with CachedBeaconState: The SlashingsCache has been integrated into the CachedBeaconState struct, ensuring it is initialized, cloned, and deinitialized alongside the main state. New helper methods were added to CachedBeaconState for interacting with the slashing cache.
  • Optimized Validator Slashing Checks: Modified various state transition functions, including processAttestationsAltair, processAttesterSlashing, processProposerSlashing, and slashValidator, to utilize the new slashings_cache for faster lookups and updates of validator slashing statuses, replacing direct state tree access.
  • Performance Improvements: The primary goal of this feature is to improve the performance of state transitions by reducing the overhead of checking validator slashing statuses, especially during block processing and attestation handling.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a SlashingsCache to optimize checking for slashed validators, which is a commendable performance enhancement. The implementation using a DynamicBitSet is efficient, and the cache is well-integrated into the block processing logic. I've identified some opportunities to reduce code duplication in the cache initialization logic, which will improve maintainability. Additionally, there's a minor issue with an import path that could lead to a compilation error. Overall, this is a solid contribution.

_ = @import("utils/pubkey_index_map.zig");
_ = @import("utils/reference_count.zig");
_ = @import("utils/root_cache.zig");
_ = @import("cache/RootCache.zig");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The import path cache/RootCache.zig seems to have a typo. According to the repository's style guide, filenames should be in snake_case. The file is named root_cache.zig, so the import path should reflect that. This typo will likely cause a compilation error.

    _ = @import("cache/root_cache.zig");
References
  1. The style guide specifies that file names should use snake_case. The import cache/RootCache.zig uses CamelCase, which violates this rule. (link)

Comment thread src/state_transition/cache/state_cache.zig Outdated
Comment thread src/state_transition/cache/state_cache.zig Outdated
Base automatically changed from comptime-fork to main January 29, 2026 15:55
Signed-off-by: Chen Kai <281165273grape@gmail.com>
Signed-off-by: Chen Kai <281165273grape@gmail.com>
@GrapeBaBa GrapeBaBa marked this pull request as ready for review January 30, 2026 13:39
@GrapeBaBa GrapeBaBa requested a review from a team as a code owner January 30, 2026 13:39
Copilot AI review requested due to automatic review settings January 30, 2026 13:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a cached view of slashed validators (SlashingsCache) and threads it through block/operation processing so that slashing-related checks can avoid repeatedly reading from the SSZ tree, while staying consistent with the beacon state.

Changes:

  • Add SlashingsCache with initialization, cloning, and incremental update APIs, and attach it to CachedBeaconState.
  • Wire the cache through block processing (processBlock, processOperations, proposer/attester slashing, attestations) and update altair attestation processing to use the cache for unslashed-balance accounting.
  • Update tests, spec runners, and benchmarks to pass and initialize the new cache where needed, and expose a helper to build the cache from a state.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/spec/runner/operations.zig Passes cached_state.slashings_cache into attestation, attester slashing, and proposer slashing spec test runners to exercise the new cache-aware APIs.
src/state_transition/state_transition.zig Extends the main stateTransition block path to pass post_cached_state.slashings_cache into processBlock, making the cache part of normal block processing.
src/state_transition/root.zig Re-exports buildSlashingsCacheFromStateIfNeeded so callers (e.g., benchmarks) can initialize the cache from an arbitrary state.
src/state_transition/cache/state_cache.zig Adds a SlashingsCache field to CachedBeaconState, wires it through creation/cloning/deinit, and exposes helper methods to query/update the cache.
src/state_transition/cache/slashings_cache.zig Introduces SlashingsCache (bitset of slashed validators keyed by latest block slot) and a buildFromStateIfNeeded helper to rebuild the cache from the state’s validators when required.
src/state_transition/block/slash_validator.zig Extends slashValidator to accept a SlashingsCache pointer and record a validator slashing in the cache in sync with the state’s slashed flag.
src/state_transition/block/process_proposer_slashing.zig Adds allocator and SlashingsCache parameters, builds the cache on demand, and calls slashValidator with the cache.
src/state_transition/block/process_operations.zig Threads SlashingsCache through processOperations and down into proposer/attester slashing and attestation processing.
src/state_transition/block/process_block.zig Makes processBlock cache-aware: builds the slashings cache once per state header when needed, keeps its slot in sync, and forwards it into processOperations.
src/state_transition/block/process_attester_slashing.zig Adds SlashingsCache usage, ensuring the cache is built and updated when slashing validators from attester slashings.
src/state_transition/block/process_attestations.zig Extends the API with a SlashingsCache pointer and ensures the cache is initialized before delegating to phase0/altair-specific attestation logic.
src/state_transition/block/process_attestation_altair.zig Uses SlashingsCache.isSlashed instead of reading the slashed flag from the tree when updating target unslashed balance increments, reducing tree reads.
bench/state_transition/process_block.zig Updates block/operations benchmarks to pass a SlashingsCache and builds it once from the benchmark state before running bench cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wemeetagain wemeetagain merged commit d5dcf7c into main Feb 2, 2026
16 checks passed
twoeths pushed a commit that referenced this pull request Mar 3, 2026
**Motivation**

This PR introduces a slashing cache which to eliminate the slash status
checking hotspot during the process attestation. It has been tested
which could reduce a lot of time cost using `bench_process_block`. Refer
to the lighthouse
https://github.com/sigp/lighthouse/blob/stable/consensus/types/src/beacon_state.rs#L704.

Fix #179

---------

Signed-off-by: Chen Kai <281165273grape@gmail.com>
Co-authored-by: Cayman <caymannava@gmail.com>
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.

can we add slashed cache to avoid duplicate get validator slashed

3 participants