Skip to content
Open
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
61 changes: 61 additions & 0 deletions src/state_transition/block/is_valid_indexed_attestation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,64 @@ pub fn isValidIndexedAttestationIndices(

return true;
}

test "isValidIndexedAttestationIndices - valid sorted unique indices" {
const indices = [_]ValidatorIndex{ 0, 1, 5, 10, 100 };
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
try std.testing.expect(result);
}

test "isValidIndexedAttestationIndices - empty indices returns false" {
const indices = [_]ValidatorIndex{};
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
try std.testing.expect(!result);
}

test "isValidIndexedAttestationIndices - single index valid" {
const indices = [_]ValidatorIndex{42};
const result = try isValidIndexedAttestationIndices(.phase0, 100, &indices);
try std.testing.expect(result);
}

test "isValidIndexedAttestationIndices - duplicate indices returns false" {
const indices = [_]ValidatorIndex{ 1, 1 };
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
try std.testing.expect(!result);
}

test "isValidIndexedAttestationIndices - unsorted indices returns false" {
const indices = [_]ValidatorIndex{ 5, 3, 10 };
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
try std.testing.expect(!result);
}

test "isValidIndexedAttestationIndices - index out of bounds returns false" {
const indices = [_]ValidatorIndex{ 0, 1, 200 };
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
try std.testing.expect(!result);
}

test "isValidIndexedAttestationIndices - index at boundary valid" {
const indices = [_]ValidatorIndex{199};
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
try std.testing.expect(result);
}

test "isValidIndexedAttestationIndices - index at validators_count returns false" {
const indices = [_]ValidatorIndex{200};
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
try std.testing.expect(!result);
}

test "isValidIndexedAttestationIndices - electra allows more indices" {
// Phase0 max = MAX_VALIDATORS_PER_COMMITTEE (2048)
// Electra max = MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT
var indices: [2049]ValidatorIndex = undefined;
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

To improve maintainability and clarity, it's better to use the preset.MAX_VALIDATORS_PER_COMMITTEE constant instead of the magic number 2049. This makes the test's intent explicit—to test one element over the phase0 limit—and ensures the test remains correct if the preset value changes.

This also aligns with the style guide's principle of using compile-time constants to document and enforce invariants.

    const indices_len = preset.MAX_VALIDATORS_PER_COMMITTEE + 1;
    var indices: [indices_len]ValidatorIndex = undefined;
References
  1. The style guide encourages asserting relationships of compile-time constants to document and enforce invariants. While not a direct assertion, using the constant here makes the relationship explicit and the code more maintainable. (link)

for (&indices, 0..) |*v, i| v.* = @intCast(i);

const result_phase0 = try isValidIndexedAttestationIndices(.phase0, 10000, &indices);
try std.testing.expect(!result_phase0);

const result_electra = try isValidIndexedAttestationIndices(.electra, 10000, &indices);
try std.testing.expect(result_electra);
}
Loading