Skip to content

test(cache): add unit tests for SlashingsCache#257

Merged
wemeetagain merged 1 commit intoChainSafe:mainfrom
lodekeeper-z:test/slashings-cache-unit-tests
Mar 25, 2026
Merged

test(cache): add unit tests for SlashingsCache#257
wemeetagain merged 1 commit intoChainSafe:mainfrom
lodekeeper-z:test/slashings-cache-unit-tests

Conversation

@lodekeeper-z
Copy link
Copy Markdown
Contributor

Add comprehensive unit tests for SlashingsCache covering:

  • initEmpty creates empty cache with null slot
  • initFromValidators populates slashed bits correctly
  • isInitialized checks slot matching
  • recordValidatorSlashing requires initialization (errors on uninitialized)
  • recordValidatorSlashing grows bitset capacity for high validator indices
  • clone creates independent copy (mutations don't cross)

These tests exercise the core cache API without needing a full BeaconState, ensuring correctness of the bitset-based slashing tracking.

🤖 Generated with AI assistance

Add comprehensive unit tests covering:
- initEmpty creates empty cache with null slot
- initFromValidators populates slashed bits correctly
- isInitialized checks slot matching
- recordValidatorSlashing requires initialization
- recordValidatorSlashing grows bitset capacity
- clone creates independent copy

🤖 Generated with AI assistance
@lodekeeper-z lodekeeper-z requested a review from a team as a code owner March 21, 2026 23:12
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 comprehensive suite of unit tests for the SlashingsCache component. These tests validate the core functionalities of the cache, including initialization, state management, validator slashing recording, and cloning behavior. By thoroughly testing the bitset-based slashing tracking, this PR significantly enhances the reliability and correctness of the SlashingsCache without relying on a full BeaconState.

Highlights

  • Unit Tests for initEmpty: Added a test to verify that initEmpty correctly creates an empty cache with a null slot.
  • Unit Tests for initFromValidators: Included a test to ensure initFromValidators accurately populates slashed bits based on provided validators.
  • Unit Tests for isInitialized: Implemented tests to confirm isInitialized correctly checks for slot matching.
  • Unit Tests for recordValidatorSlashing (Initialization): Added tests to verify that recordValidatorSlashing requires proper initialization and handles uninitialized states by returning an error.
  • Unit Tests for recordValidatorSlashing (Capacity Growth): Included a test to confirm recordValidatorSlashing correctly grows the bitset capacity when recording slashings for high validator indices.
  • Unit Tests for clone: Implemented a test to ensure the clone method creates an independent copy, preventing mutations in the original from affecting the cloned instance.

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

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 adds a solid and comprehensive set of unit tests for SlashingsCache, covering its core functionality like initialization, recording slashings, and cloning. The tests are well-structured and effectively validate the cache's behavior. I've made a few suggestions to use more idiomatic Zig for initialization, to adhere to the comment formatting style, and to add a test for an edge case to further improve test coverage, in line with the repository's style guide on exhaustive testing.

Comment on lines +116 to +117
var validators: [5]Validator = undefined;
@memset(std.mem.asBytes(&validators), 0);
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.

medium

For zero-initializing the validators array, you can use the more idiomatic Zig .{} initializer. This is cleaner and more readable than using undefined followed by @memset.

    var validators: [5]Validator = .{};

var validators: [5]Validator = undefined;
@memset(std.mem.asBytes(&validators), 0);

// Mark validators 1 and 3 as slashed
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.

medium

The style guide (line 304) specifies that comments should be complete sentences, starting with a capital letter and ending with a period. Please apply this to all new comments in the tests (e.g., here and on lines 152, 155, 160, 171, 189, 193).

    // Mark validators 1 and 3 as slashed.

try std.testing.expect(!cache.isSlashed(2));
try std.testing.expect(cache.isSlashed(3));
try std.testing.expect(!cache.isSlashed(4));
}
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.

medium

The tests for initFromValidators are good. To improve test exhaustiveness, as encouraged by the style guide (line 81: "tests must test exhaustively"), please consider adding a test case for when initFromValidators is called with an empty validators slice. This would verify correct handling of this important edge case.

Comment on lines +179 to +180
var validators: [3]Validator = undefined;
@memset(std.mem.asBytes(&validators), 0);
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.

medium

Similar to the other test, you can use the more idiomatic Zig .{} initializer here to zero-initialize the validators array. This improves readability and is the preferred way to create a zeroed array.

    var validators: [3]Validator = .{};

@wemeetagain wemeetagain merged commit 97fd54c into ChainSafe:main Mar 25, 2026
13 checks passed
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.

3 participants