-
Notifications
You must be signed in to change notification settings - Fork 12
test(cache): add unit tests for SlashingsCache #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
|
||
| // Mark validators 1 and 3 as slashed | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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)); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests for |
||
|
|
||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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)); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For zero-initializing the
validatorsarray, you can use the more idiomatic Zig.{}initializer. This is cleaner and more readable than usingundefinedfollowed by@memset.