-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(slashing-protection): more robust solution to determine min epoch
Epochs array might have more than ~10^5 elements, cannot use `Math.min` as it would throw `RangeError: Maximum call stack size exceeded`.
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/validator/test/unit/slashingProtection/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {expect} from "chai"; | ||
import {Epoch} from "@lodestar/types"; | ||
import {minEpoch} from "../../../src/slashingProtection/utils.js"; | ||
|
||
describe("slashingProtection / utils / minEpoch", () => { | ||
it("should return the minimum epoch from an array of epochs", () => { | ||
const epochs: Epoch[] = [15, 10, 20, 30, 5, 1, 50]; | ||
const result = minEpoch(epochs); | ||
expect(result).to.equal(1); | ||
}); | ||
|
||
it("should return the only epoch if epochs array only contains one element", () => { | ||
const epochs: Epoch[] = [10]; | ||
const result = minEpoch(epochs); | ||
expect(result).to.equal(10); | ||
}); | ||
|
||
it("should return null if epochs array is empty", () => { | ||
const epochs: Epoch[] = []; | ||
const result = minEpoch(epochs); | ||
expect(result).to.equal(null); | ||
}); | ||
|
||
it("should not throw 'RangeError: Maximum call stack size exceeded' for huge epoch arrays", () => { | ||
const epochs = Array.from({length: 1e6}, (_, index) => index); | ||
expect(() => minEpoch(epochs)).to.not.throw(RangeError); | ||
}); | ||
}); |