Skip to content

Commit bdba6ca

Browse files
committed
test(stf): add unit tests for isValidIndexedAttestationIndices
Cover edge cases for the indexed attestation validation logic: - Valid sorted unique indices - Empty indices (reject) - Single index (accept) - Duplicate indices (reject) - Unsorted indices (reject) - Out-of-bounds index (reject) - Boundary index (validators_count - 1 valid, validators_count invalid) - Electra vs phase0 max indices limit 🤖 Generated with AI assistance
1 parent f684a9f commit bdba6ca

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

src/state_transition/block/is_valid_indexed_attestation.zig

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,64 @@ pub fn isValidIndexedAttestationIndices(
7272

7373
return true;
7474
}
75+
76+
test "isValidIndexedAttestationIndices - valid sorted unique indices" {
77+
const indices = [_]ValidatorIndex{ 0, 1, 5, 10, 100 };
78+
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
79+
try std.testing.expect(result);
80+
}
81+
82+
test "isValidIndexedAttestationIndices - empty indices returns false" {
83+
const indices = [_]ValidatorIndex{};
84+
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
85+
try std.testing.expect(!result);
86+
}
87+
88+
test "isValidIndexedAttestationIndices - single index valid" {
89+
const indices = [_]ValidatorIndex{42};
90+
const result = try isValidIndexedAttestationIndices(.phase0, 100, &indices);
91+
try std.testing.expect(result);
92+
}
93+
94+
test "isValidIndexedAttestationIndices - duplicate indices returns false" {
95+
const indices = [_]ValidatorIndex{ 1, 1 };
96+
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
97+
try std.testing.expect(!result);
98+
}
99+
100+
test "isValidIndexedAttestationIndices - unsorted indices returns false" {
101+
const indices = [_]ValidatorIndex{ 5, 3, 10 };
102+
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
103+
try std.testing.expect(!result);
104+
}
105+
106+
test "isValidIndexedAttestationIndices - index out of bounds returns false" {
107+
const indices = [_]ValidatorIndex{ 0, 1, 200 };
108+
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
109+
try std.testing.expect(!result);
110+
}
111+
112+
test "isValidIndexedAttestationIndices - index at boundary valid" {
113+
const indices = [_]ValidatorIndex{199};
114+
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
115+
try std.testing.expect(result);
116+
}
117+
118+
test "isValidIndexedAttestationIndices - index at validators_count returns false" {
119+
const indices = [_]ValidatorIndex{200};
120+
const result = try isValidIndexedAttestationIndices(.phase0, 200, &indices);
121+
try std.testing.expect(!result);
122+
}
123+
124+
test "isValidIndexedAttestationIndices - electra allows more indices" {
125+
// Phase0 max = MAX_VALIDATORS_PER_COMMITTEE (2048)
126+
// Electra max = MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT
127+
var indices: [2049]ValidatorIndex = undefined;
128+
for (&indices, 0..) |*v, i| v.* = @intCast(i);
129+
130+
const result_phase0 = try isValidIndexedAttestationIndices(.phase0, 10000, &indices);
131+
try std.testing.expect(!result_phase0);
132+
133+
const result_electra = try isValidIndexedAttestationIndices(.electra, 10000, &indices);
134+
try std.testing.expect(result_electra);
135+
}

0 commit comments

Comments
 (0)