Skip to content

Commit 640021c

Browse files
committed
Move spec constants to global scope to use CONSTANT_CASE
1 parent 4ed5263 commit 640021c

File tree

8 files changed

+23
-19
lines changed

8 files changed

+23
-19
lines changed

packages/persistent-merkle-tree/test/perf/tree.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import {
1010
uint8ArrayToHashObject,
1111
} from "../../src/index.js";
1212

13+
const VALIDATOR_REGISTRY_LIMIT = 1099511627776;
14+
1315
describe("Track the performance of different Tree methods", () => {
14-
const validatorRegistryLimit = 1099511627776;
1516
// add 1 to countToDepth for mix_in_length spec
16-
const depth = countToDepth(BigInt(Math.ceil(validatorRegistryLimit / 4))) + 1;
17+
const depth = countToDepth(BigInt(Math.ceil(VALIDATOR_REGISTRY_LIMIT / 4))) + 1;
1718

1819
const newRoot = new Uint8Array(Array.from({length: 32}, () => 1));
1920
const numBalance = 250_000;

packages/persistent-merkle-tree/test/unit/packedNode.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
packedUintNum64sToLeafNodes,
88
} from "../../src/packedNode.js";
99

10+
const NUMBER_2_POW_32 = 2 ** 32;
11+
1012
describe("subtree / packedNode single node", () => {
1113
const testCases: {
1214
id: string;
@@ -200,7 +202,6 @@ describe("subtree / packedNode single node", () => {
200202

201203
// 1 UintNum64 = 8 bytes
202204
if (testPackedNumbers) {
203-
const number2Pow32 = 2 ** 32;
204205
it(`${id} - packedUintNum64sToLeafNodes, value size=${Math.floor(size / 8)}`, () => {
205206
const values: number[] = [];
206207
const uint8Array = new Uint8Array(Buffer.from(outStr.replace("0x", ""), "hex"));
@@ -211,7 +212,7 @@ describe("subtree / packedNode single node", () => {
211212
if (a === 0xffffffff && b === 0xffffffff) {
212213
values.push(Infinity);
213214
} else {
214-
values.push(b * number2Pow32 + a);
215+
values.push(b * NUMBER_2_POW_32 + a);
215216
}
216217
}
217218

packages/ssz/test/perf/bitlist.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {bench, describe} from "@chainsafe/benchmark";
22
import {BitArray, BitListType} from "../../src/index.js";
33

4+
const MAX_VALIDATORS_PER_COMMITTEE = 2048;
5+
46
// running zipIndexesCommitteeBits() on `bitLen: 2048, bitsSet: 2048` takes 50.904 us/op
57
// However deserializing a BitList to struct `len 2048, set 2048` takes 560.4670 us/op,
68
// so it's far more efficient to keep bitlists as Uint8Arrays and also save memory.
79

810
describe("BitListType types", () => {
9-
const maxValidatorsPerCommittee = 2048;
10-
const CommitteeBits = new BitListType(maxValidatorsPerCommittee);
11+
const CommitteeBits = new BitListType(MAX_VALIDATORS_PER_COMMITTEE);
1112

1213
const testCases: {bitLen: number; bitsSet: number}[] = [
1314
// Realistic mainnet case

packages/ssz/test/perf/iterate.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {bench, describe, setBenchOpts} from "@chainsafe/benchmark";
22
import {ListBasicType, UintNumberType} from "../../src/index.js";
33
import {Validators} from "../lodestarTypes/phase0/sszTypes.js";
44

5+
const VALIDATOR_REGISTRY_LIMIT = 1099511627776;
6+
57
describe("iterate", () => {
68
setBenchOpts({noThreshold: true});
79

@@ -55,9 +57,7 @@ describe("readonly values - iterator vs array", () => {
5557
});
5658

5759
function createBalanceList(count: number) {
58-
const validatorRegistryLimit = 1099511627776;
59-
60-
const balancesList = new ListBasicType(new UintNumberType(8), validatorRegistryLimit);
60+
const balancesList = new ListBasicType(new UintNumberType(8), VALIDATOR_REGISTRY_LIMIT);
6161
const balancesStruct = Array.from({length: count}, () => 31217089836);
6262
return balancesList.toViewDU(balancesStruct);
6363
}

packages/ssz/test/perf/list.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {bench, describe} from "@chainsafe/benchmark";
22
import {LeafNode, Node, subtreeFillToContents} from "@chainsafe/persistent-merkle-tree";
33
import {ListBasicType, UintNumberType} from "../../src/index.js";
44

5+
const VALIDATOR_REGISTRY_LIMIT = 1099511627776;
6+
57
describe("list", () => {
68
const numBalances = 250_000;
79

@@ -83,9 +85,7 @@ describe("subtreeFillToContents", () => {
8385
});
8486

8587
function createBalanceList(count: number) {
86-
const validatorRegistryLimit = 1099511627776;
87-
88-
const balancesList = new ListBasicType(new UintNumberType(8), validatorRegistryLimit);
88+
const balancesList = new ListBasicType(new UintNumberType(8), VALIDATOR_REGISTRY_LIMIT);
8989
const balancesStruct = Array.from({length: count}, () => 31217089836);
9090
const viewDU = balancesList.toViewDU(balancesStruct);
9191
// Prime cache

packages/ssz/test/perf/stateTransitionOps/processAttestations.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {beforeAll, bench, describe} from "@chainsafe/benchmark";
22
import {MutableVector} from "@chainsafe/persistent-ts";
33
import {CompositeViewDU, ListBasicType, UintNumberType} from "../../../src/index.js";
44

5+
const VALIDATOR_REGISTRY_LIMIT = 1099511627776;
6+
57
describe("processAttestations", () => {
68
const vc = 250_000;
79
const attesterShare = 32;
810
const attesterIndices: number[] = [];
911
const statusArr: number[] = [];
10-
11-
const validatorRegistryLimit = 1099511627776;
1212
const uint8Type = new UintNumberType(1);
13-
const epochStatusesType = new ListBasicType(uint8Type, validatorRegistryLimit);
13+
const epochStatusesType = new ListBasicType(uint8Type, VALIDATOR_REGISTRY_LIMIT);
1414

1515
// Helper types
1616
type Statuses = typeof epochStatusesType;

packages/ssz/test/unit/eth2/validators.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {describe, expect, it, vi} from "vitest";
22
import {CompositeViewDU, ListCompositeType, ValueOf, toHexString} from "../../../src/index.js";
33
import {ValidatorContainer, ValidatorNodeStruct} from "../../lodestarTypes/phase0/sszTypes.js";
44

5+
const VALIDATOR_REGISTRY_LIMIT = 1099511627776;
6+
57
type Validator = ValueOf<typeof ValidatorContainer>;
68
const validator: Validator = {
79
pubkey: Buffer.alloc(48, 0xaa),
@@ -56,8 +58,7 @@ describe("Container with BranchNodeStruct", () => {
5658
}
5759

5860
describe("ValidatorNodeStruct in List", () => {
59-
const validatorRegistryLimit = 1099511627776;
60-
const ValidatorsListType = new ListCompositeType(ValidatorNodeStruct, validatorRegistryLimit);
61+
const ValidatorsListType = new ListCompositeType(ValidatorNodeStruct, VALIDATOR_REGISTRY_LIMIT);
6162

6263
it("edit then read", () => {
6364
const validatorListTB = ValidatorsListType.defaultViewDU();

packages/ssz/test/unit/regressions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from "../../src/index.js";
1515
import {uint32NumType, uint64NumType} from "../utils/primitiveTypes.js";
1616

17+
const VALIDATOR_REGISTRY_LIMIT = 1099511627776;
1718
// Compilation of various issues from SSZ and Lodestar libs
1819

1920
describe("Regressions / known issues", () => {
@@ -75,8 +76,7 @@ describe("Regressions / known issues", () => {
7576
validatorIndexes.push(i);
7677
}
7778

78-
const validatorRegistryLimit = 1099511627776;
79-
const type = new ListBasicType(uint64NumType, validatorRegistryLimit);
79+
const type = new ListBasicType(uint64NumType, VALIDATOR_REGISTRY_LIMIT);
8080
// This is the logic to calculate activeIndexRoots in processFinalUpdates
8181
const hash = Buffer.from(type.hashTreeRoot(validatorIndexes)).toString("hex");
8282
expect(hash).to.equal("ba1031ba1a5daab0d49597cfa8664ce2b4c9b4db6ca69fbef51e0a9a325a3b63");

0 commit comments

Comments
 (0)