diff --git a/packages/validator/src/slashingProtection/utils.ts b/packages/validator/src/slashingProtection/utils.ts index 916f0eca927..c78b4b990e4 100644 --- a/packages/validator/src/slashingProtection/utils.ts +++ b/packages/validator/src/slashingProtection/utils.ts @@ -28,7 +28,7 @@ export function numToString(num: number): string { } export function minEpoch(epochs: Epoch[]): Epoch | null { - return epochs.length > 0 ? Math.min(...epochs) : null; + return epochs.length > 0 ? epochs.reduce((minEpoch, epoch) => (minEpoch < epoch ? minEpoch : epoch)) : null; } export function uniqueVectorArr(buffers: Uint8Array[]): Uint8Array[] { diff --git a/packages/validator/test/unit/slashingProtection/utils.test.ts b/packages/validator/test/unit/slashingProtection/utils.test.ts new file mode 100644 index 00000000000..a77f2961f21 --- /dev/null +++ b/packages/validator/test/unit/slashingProtection/utils.test.ts @@ -0,0 +1,20 @@ +import {expect} from "chai"; +import {minEpoch} from "../../../src/slashingProtection/utils.js"; + +describe("slashingProtection / utils / minEpoch", () => { + it("should return the minimum epoch from an array of epochs", () => { + expect(minEpoch([15, 10, 20, 30, 5, 1, 50])).to.equal(1); + }); + + it("should return the only epoch if epochs array only contains one element", () => { + expect(minEpoch([10])).to.equal(10); + }); + + it("should return null if epochs array is empty", () => { + expect(minEpoch([])).to.equal(null); + }); + + it("should not throw 'RangeError: Maximum call stack size exceeded' for huge epoch arrays", () => { + expect(() => minEpoch(Array.from({length: 1e6}, (_, index) => index))).to.not.throw(RangeError); + }); +});