Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/state_transition/cache/slashings_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,100 @@ pub fn buildFromStateIfNeeded(
slashings_cache.deinit();
slashings_cache.* = new_cache;
}

test "SlashingsCache - initEmpty creates empty cache" {
const allocator = std.testing.allocator;
var cache = try SlashingsCache.initEmpty(allocator);
defer cache.deinit();

try std.testing.expect(cache.latest_block_slot == null);
try std.testing.expect(!cache.isSlashed(0));
try std.testing.expect(!cache.isSlashed(100));
}

test "SlashingsCache - initFromValidators populates slashed bits" {
const allocator = std.testing.allocator;
var validators: [5]Validator = undefined;
@memset(std.mem.asBytes(&validators), 0);
Comment on lines +116 to +117
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 = .{};


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

validators[1].slashed = true;
validators[3].slashed = true;

var cache = try SlashingsCache.initFromValidators(allocator, 42, &validators);
defer cache.deinit();

try std.testing.expectEqual(@as(?Slot, 42), cache.latest_block_slot);
try std.testing.expect(!cache.isSlashed(0));
try std.testing.expect(cache.isSlashed(1));
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.


test "SlashingsCache - isInitialized checks slot" {
const allocator = std.testing.allocator;
var cache = try SlashingsCache.initEmpty(allocator);
defer cache.deinit();

try std.testing.expect(!cache.isInitialized(0));
try std.testing.expect(!cache.isInitialized(42));

cache.updateLatestBlockSlot(42);
try std.testing.expect(cache.isInitialized(42));
try std.testing.expect(!cache.isInitialized(43));
}

test "SlashingsCache - recordValidatorSlashing requires initialization" {
const allocator = std.testing.allocator;
var cache = try SlashingsCache.initEmpty(allocator);
defer cache.deinit();

// Should fail when not initialized
try std.testing.expectError(error.SlashingsCacheUninitialized, cache.recordValidatorSlashing(10, 5));

// Initialize and try again
cache.updateLatestBlockSlot(10);
try cache.recordValidatorSlashing(10, 5);
try std.testing.expect(cache.isSlashed(5));

// Wrong slot should fail
try std.testing.expectError(error.SlashingsCacheUninitialized, cache.recordValidatorSlashing(11, 6));
}

test "SlashingsCache - recordValidatorSlashing grows capacity" {
const allocator = std.testing.allocator;
var cache = try SlashingsCache.initEmpty(allocator);
defer cache.deinit();

cache.updateLatestBlockSlot(0);

// Record slashing for a high index — should grow the bitset
try cache.recordValidatorSlashing(0, 1000);
try std.testing.expect(cache.isSlashed(1000));
try std.testing.expect(!cache.isSlashed(999));
}

test "SlashingsCache - clone creates independent copy" {
const allocator = std.testing.allocator;
var validators: [3]Validator = undefined;
@memset(std.mem.asBytes(&validators), 0);
Comment on lines +179 to +180
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 = .{};

validators[1].slashed = true;

var original = try SlashingsCache.initFromValidators(allocator, 10, &validators);
defer original.deinit();

var cloned = try original.clone(allocator);
defer cloned.deinit();

// Both should see validator 1 as slashed
try std.testing.expect(cloned.isSlashed(1));
try std.testing.expectEqual(@as(?Slot, 10), cloned.latest_block_slot);

// Modify original — clone should be unaffected
original.updateLatestBlockSlot(20);
try original.recordValidatorSlashing(20, 2);

try std.testing.expectEqual(@as(?Slot, 10), cloned.latest_block_slot);
try std.testing.expect(!cloned.isSlashed(2));
}
Loading