Skip to content

test(epoch): add unit tests for inactivity leak functions#262

Open
lodekeeper-z wants to merge 4 commits intoChainSafe:mainfrom
lodekeeper-z:test/inactivity-leak-unit-tests
Open

test(epoch): add unit tests for inactivity leak functions#262
lodekeeper-z wants to merge 4 commits intoChainSafe:mainfrom
lodekeeper-z:test/inactivity-leak-unit-tests

Conversation

@lodekeeper-z
Copy link
Copy Markdown
Contributor

Add unit tests for getFinalityDelay and isInInactivityLeak in inactivity_leak.zig.

Coverage added:

  • getFinalityDelay: normal delay calculation, edge cases (epoch=1)
  • isInInactivityLeak: boundary at MIN_EPOCHS_TO_INACTIVITY_PENALTY (delay=4 → no leak, delay=5 → leak), extreme cases

These are pure functions with zero prior test coverage.

🤖 Generated with AI assistance

Add unit tests for getFinalityDelay and isInInactivityLeak covering:
- Normal delay calculation
- Boundary conditions (delay exactly at threshold)
- Edge cases (epoch=1, finalized=0)
- Leak detection above/below MIN_EPOCHS_TO_INACTIVITY_PENALTY

🤖 Generated with AI assistance
@lodekeeper-z lodekeeper-z requested a review from a team as a code owner March 22, 2026 08:24
@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 significantly improves the reliability of the epoch state transition logic by introducing dedicated unit tests for the getFinalityDelay and isInInactivityLeak functions. These additions ensure the accurate calculation of finality delays and the correct identification of inactivity leak conditions, thereby bolstering the robustness of critical blockchain mechanisms.

Highlights

  • Unit Test Coverage: Added comprehensive unit tests for the getFinalityDelay function, covering normal delay calculations and edge cases such as epoch=1.
  • Inactivity Leak Logic Validation: Introduced unit tests for the isInInactivityLeak function, validating its behavior at the MIN_EPOCHS_TO_INACTIVITY_PENALTY boundary and extreme cases.
  • Pure Function Testing: Provided initial test coverage for these pure functions which previously had none, improving code reliability.

🧠 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 valuable unit tests for getFinalityDelay and isInInactivityLeak, which previously had no test coverage. The tests cover normal operation, edge cases, and boundary conditions correctly. My review includes a couple of suggestions to improve the test comments to better align with the project's style guide, specifically regarding test descriptions and comment formatting. Overall, this is a good addition to improve code quality and robustness.

Comment on lines +23 to +32
test "getFinalityDelay" {
// current_epoch=10, finalized_epoch=5: previous=9, delay=9-5=4
try std.testing.expectEqual(@as(u64, 4), getFinalityDelay(10, 5));
// current_epoch=10, finalized_epoch=9: previous=9, delay=0
try std.testing.expectEqual(@as(u64, 0), getFinalityDelay(10, 9));
// current_epoch=10, finalized_epoch=0: previous=9, delay=9
try std.testing.expectEqual(@as(u64, 9), getFinalityDelay(10, 0));
// edge: current_epoch=1, finalized_epoch=0: previous=0, delay=0
try std.testing.expectEqual(@as(u64, 0), getFinalityDelay(1, 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

The test comments could be improved to better align with the repository's style guide. Rule #301 suggests adding a description at the top of tests to explain their goal, and Rule #304 specifies that comments on their own line should be sentences. Restructuring the comments will improve readability and adherence to the style guide.

test "getFinalityDelay" {
    // Tests the calculation of finality delay for various epoch differences.

    // Case: current_epoch=10, finalized_epoch=5 -> previous=9, delay=4.
    try std.testing.expectEqual(@as(u64, 4), getFinalityDelay(10, 5));
    // Case: current_epoch=10, finalized_epoch=9 -> previous=9, delay=0.
    try std.testing.expectEqual(@as(u64, 0), getFinalityDelay(10, 9));
    // Case: current_epoch=10, finalized_epoch=0 -> previous=9, delay=9.
    try std.testing.expectEqual(@as(u64, 9), getFinalityDelay(10, 0));
    // Edge case: current_epoch=1, finalized_epoch=0 -> previous=0, delay=0.
    try std.testing.expectEqual(@as(u64, 0), getFinalityDelay(1, 0));
}
References
  1. Rule feat: add clock module for slot/epoch timing (Zig 0.16) #301: Tests should include a top-level description of their goal and methodology to help readers understand the test's purpose without needing to read the implementation. (link)
  2. Rule test: add negative BLS verify and FAV cases #304: Comments should be formatted as complete sentences with proper capitalization and punctuation. Comments on their own line should be sentences, while comments after a line of code can be phrases. (link)

Comment on lines +34 to +44
test "isInInactivityLeak" {
// MIN_EPOCHS_TO_INACTIVITY_PENALTY = 4
// Not in leak: delay <= 4
try std.testing.expect(!isInInactivityLeak(10, 9)); // delay=0
try std.testing.expect(!isInInactivityLeak(10, 5)); // delay=4 (not strictly greater)

// In leak: delay > 4
try std.testing.expect(isInInactivityLeak(10, 4)); // delay=5
try std.testing.expect(isInInactivityLeak(10, 0)); // delay=9
try std.testing.expect(isInInactivityLeak(100, 0)); // delay=99
}
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 getFinalityDelay test, the comments in this test block can be improved to follow the style guide. Adding a descriptive comment at the top (Rule #301) and rephrasing comments to be full sentences (Rule #304) will enhance clarity and consistency.

test "isInInactivityLeak" {
    // Tests the inactivity leak condition by checking boundaries around MIN_EPOCHS_TO_INACTIVITY_PENALTY.
    // An inactivity leak occurs if the finality delay is strictly greater than 4.

    // Test cases that are not in an inactivity leak (delay <= 4).
    try std.testing.expect(!isInInactivityLeak(10, 9)); // delay=0
    try std.testing.expect(!isInInactivityLeak(10, 5)); // delay=4 (not strictly greater)

    // Test cases that are in an inactivity leak (delay > 4).
    try std.testing.expect(isInInactivityLeak(10, 4)); // delay=5
    try std.testing.expect(isInInactivityLeak(10, 0)); // delay=9
    try std.testing.expect(isInInactivityLeak(100, 0)); // delay=99
}
References
  1. Rule feat: add clock module for slot/epoch timing (Zig 0.16) #301: Tests should include a top-level description of their goal and methodology to help readers understand the test's purpose without needing to read the implementation. (link)
  2. Rule test: add negative BLS verify and FAV cases #304: Comments should be formatted as complete sentences with proper capitalization and punctuation. Comments on their own line should be sentences, while comments after a line of code can be phrases. (link)

GrapeBaBa
GrapeBaBa previously approved these changes Mar 28, 2026
@spiral-ladder
Copy link
Copy Markdown
Collaborator

made these changes:

  • make getFinalityDelay not pub (it's never used elsewhere, so why expose it)
  • remove getFinalityDelay tests since it's already tested in isInInactivityLeak tests
  • simplify isInInactivityLeak test

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