Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate light-client tests to vitest #6221

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions packages/light-client/.mocharc.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions packages/light-client/.nycrc.json

This file was deleted.

9 changes: 0 additions & 9 deletions packages/light-client/karma.config.cjs

This file was deleted.

7 changes: 5 additions & 2 deletions packages/light-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@
"lint:fix": "yarn run lint --fix",
"pretest": "yarn run check-types",
"test": "yarn test:unit && yarn test:e2e",
"test:browsers": "yarn karma start karma.config.cjs",
"test:unit": "LODESTAR_PRESET=minimal nyc --cache-dir .nyc_output/.cache -e .ts mocha 'test/unit/**/*.test.ts'",
"test:unit": "vitest --run --dir test/unit/ --coverage",
"test:browsers": "yarn test:browsers:chrome && yarn test:browsers:firefox && yarn test:browsers:electron",
"test:browsers:chrome": "vitest --run --browser chrome --config ./vitest.browser.config.ts --dir test/unit",
"test:browsers:firefox": "vitest --run --browser firefox --config ./vitest.browser.config.ts --dir test/unit",
"test:browsers:electron": "echo 'Electron tests will be introduced back in the future as soon vitest supports electron.'",
"check-readme": "typescript-docs-verifier"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bls from "@chainsafe/bls/switchable";
import bls from "@chainsafe/bls";
import type {PublicKey, Signature} from "@chainsafe/bls/types";
import {Root, ssz, allForks} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
Expand Down
2 changes: 1 addition & 1 deletion packages/light-client/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bls from "@chainsafe/bls/switchable";
import bls from "@chainsafe/bls";
import type {PublicKey} from "@chainsafe/bls/types";
import {BitArray} from "@chainsafe/ssz";
import {altair, Root, ssz} from "@lodestar/types";
Expand Down
2 changes: 1 addition & 1 deletion packages/light-client/src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bls from "@chainsafe/bls/switchable";
import bls from "@chainsafe/bls";
import type {PublicKey, Signature} from "@chainsafe/bls/types";
import {altair, Root, Slot, ssz, allForks} from "@lodestar/types";
import {
Expand Down
2 changes: 2 additions & 0 deletions packages/light-client/test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export async function setup(): Promise<void> {}
export async function teardown(): Promise<void> {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from "chai";
import {describe, it, expect} from "vitest";
import {fromHexString} from "@chainsafe/ssz";
import {ssz, allForks} from "@lodestar/types";
import {createBeaconConfig, createChainForkConfig, defaultChainConfig} from "@lodestar/config";
Expand Down Expand Up @@ -91,7 +91,7 @@ describe("isValidLightClientHeader", function () {
testCases.forEach(([name, header]: [string, allForks.LightClientHeader]) => {
it(name, function () {
const isValid = isValidLightClientHeader(config, header);
expect(isValid).to.be.true;
expect(isValid).toBe(true);
});
});
});
18 changes: 4 additions & 14 deletions packages/light-client/test/unit/sync.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {expect} from "chai";
import {init} from "@chainsafe/bls/switchable";
import {describe, it, expect, afterEach, vi} from "vitest";
import {JsonPath, toHexString} from "@chainsafe/ssz";
import {computeDescriptor, TreeOffsetProof} from "@chainsafe/persistent-merkle-tree";
import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH} from "@lodestar/params";
Expand All @@ -21,21 +20,15 @@ import {
lastInMap,
} from "../utils/utils.js";
import {startServer, ServerOpts} from "../utils/server.js";
import {isNode} from "../../src/utils/utils.js";
import {computeSyncPeriodAtSlot} from "../../src/utils/clock.js";
import {LightClientRestTransport} from "../../src/transport/rest.js";

const SOME_HASH = Buffer.alloc(32, 0xff);

describe("sync", () => {
vi.setConfig({testTimeout: 30_000});
const afterEachCbs: (() => Promise<unknown> | unknown)[] = [];

before("init bls", async () => {
// This process has to be done manually because of an issue in Karma runner
// https://github.com/karma-runner/karma/issues/3804
await init(isNode ? "blst-native" : "herumi");
});

afterEach(async () => {
await Promise.all(afterEachCbs);
afterEachCbs.length = 0;
Expand Down Expand Up @@ -168,16 +161,13 @@ describe("sync", () => {
});

// Ensure that the lightclient head is correct
expect(lightclient.getHead().beacon.slot).to.equal(targetSlot, "lightclient.head is not the targetSlot head");
expect(lightclient.getHead().beacon.slot).toBe(targetSlot);

// Fetch proof of "latestExecutionPayloadHeader.stateRoot"
const {proof, header} = await getHeadStateProof(lightclient, api, [["latestExecutionPayloadHeader", "stateRoot"]]);

const recoveredState = ssz.bellatrix.BeaconState.createFromProof(proof, header.beacon.stateRoot);
expect(toHexString(recoveredState.latestExecutionPayloadHeader.stateRoot)).to.equal(
toHexString(executionStateRoot),
"Recovered executionStateRoot from getHeadStateProof() not correct"
);
expect(toHexString(recoveredState.latestExecutionPayloadHeader.stateRoot)).toBe(toHexString(executionStateRoot));
});
});

Expand Down
21 changes: 7 additions & 14 deletions packages/light-client/test/unit/syncInMemory.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from "chai";
import bls, {init} from "@chainsafe/bls/switchable";
import {describe, it, expect, beforeAll, vi} from "vitest";
import bls from "@chainsafe/bls";
import {createBeaconConfig} from "@lodestar/config";
import {chainConfig} from "@lodestar/config/default";
import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH} from "@lodestar/params";
Expand All @@ -9,7 +9,6 @@ import {BeaconChainLcMock} from "../mocks/BeaconChainLcMock.js";
import {processLightClientUpdate} from "../utils/naive/update.js";
import {IBeaconChainLc, prepareUpdateNaive} from "../utils/prepareUpdateNaive.js";
import {getInteropSyncCommittee, getSyncAggregateSigningRoot, SyncCommitteeKeys} from "../utils/utils.js";
import {isNode} from "../../src/utils/utils.js";

function getSyncCommittee(
syncCommitteesKeys: Map<SyncPeriod, SyncCommitteeKeys>,
Expand All @@ -25,7 +24,7 @@ function getSyncCommittee(

describe("syncInMemory", function () {
// In browser test this process is taking more time than default 2000ms
this.timeout(10000);
vi.setConfig({testTimeout: 10000});

// Fixed params
const genValiRoot = Buffer.alloc(32, 9);
Expand All @@ -35,20 +34,14 @@ describe("syncInMemory", function () {
let updateData: {chain: IBeaconChainLc; blockWithSyncAggregate: altair.BeaconBlock};
let update: altair.LightClientUpdate;

before("init bls", async () => {
// This process has to be done manually because of an issue in Karma runner
// https://github.com/karma-runner/karma/issues/3804
await init(isNode ? "blst-native" : "herumi");
});

before("BLS sanity check", () => {
beforeAll(() => {
const sk = bls.SecretKey.fromBytes(Buffer.alloc(32, 1));
expect(sk.toPublicKey().toHex()).to.equal(
expect(sk.toPublicKey().toHex()).toBe(
"0xaa1a1c26055a329817a5759d877a2795f9499b97d6056edde0eea39512f24e8bc874b4471f0501127abb1ea0d9f68ac1"
);
});

before("Generate data for prepareUpdate", () => {
beforeAll(() => {
// Create a state that has as nextSyncCommittee the committee 2
const finalizedBlockSlot = SLOTS_PER_EPOCH * EPOCHS_PER_SYNC_COMMITTEE_PERIOD + 1;
const headerBlockSlot = finalizedBlockSlot + 1;
Expand Down Expand Up @@ -107,6 +100,6 @@ describe("syncInMemory", function () {
},
};

expect(() => processLightClientUpdate(config, store, update, currentSlot)).to.not.throw();
expect(() => processLightClientUpdate(config, store, update, currentSlot)).not.toThrow();
});
});
4 changes: 2 additions & 2 deletions packages/light-client/test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from "chai";
import {describe, it, expect} from "vitest";
import {isValidMerkleBranch} from "../../src/utils/verifyMerkleBranch.js";
import {computeMerkleBranch} from "../utils/utils.js";

Expand All @@ -9,6 +9,6 @@ describe("utils", () => {
const index = 22;
const {root, proof} = computeMerkleBranch(leaf, depth, index);

expect(isValidMerkleBranch(leaf, proof, depth, index, root)).to.equal(true);
expect(isValidMerkleBranch(leaf, proof, depth, index, root)).toBe(true);
});
});
4 changes: 2 additions & 2 deletions packages/light-client/test/unit/utils/chunkify.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from "chai";
import {describe, it, expect} from "vitest";
import {chunkifyInclusiveRange} from "../../../src/utils/chunkify.js";

describe("utils / chunkifyInclusiveRange", () => {
Expand All @@ -20,7 +20,7 @@ describe("utils / chunkifyInclusiveRange", () => {

for (const {id, from, to, max, result} of testCases) {
it(id, () => {
expect(chunkifyInclusiveRange(from, to, max)).to.deep.equal(result);
expect(chunkifyInclusiveRange(from, to, max)).toEqual(result);
});
}
});
17 changes: 5 additions & 12 deletions packages/light-client/test/unit/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from "chai";
import bls, {init} from "@chainsafe/bls/switchable";
import {describe, it, expect, beforeAll, vi} from "vitest";
import bls from "@chainsafe/bls";
import {Tree} from "@chainsafe/persistent-merkle-tree";
import {altair, ssz} from "@lodestar/types";
import {chainConfig} from "@lodestar/config/default";
Expand All @@ -14,26 +14,19 @@ import {
import {assertValidLightClientUpdate} from "../../src/validation.js";
import {LightClientSnapshotFast, SyncCommitteeFast} from "../../src/types.js";
import {defaultBeaconBlockHeader, getSyncAggregateSigningRoot, signAndAggregate} from "../utils/utils.js";
import {isNode} from "../../src/utils/utils.js";

describe("validation", function () {
// In browser test this process is taking more time than default 2000ms
// specially on the CI
this.timeout(15000);
vi.setConfig({testTimeout: 15000});

const genValiRoot = Buffer.alloc(32, 9);
const config = createBeaconConfig(chainConfig, genValiRoot);

let update: altair.LightClientUpdate;
let snapshot: LightClientSnapshotFast;

before("prepare bls", async () => {
// This process has to be done manually because of an issue in Karma runner
// https://github.com/karma-runner/karma/issues/3804
await init(isNode ? "blst-native" : "herumi");
});

before("prepare data", function () {
beforeAll(function () {
// Update slot must > snapshot slot
// attestedHeaderSlot must == updateHeaderSlot + 1
const snapshotHeaderSlot = 1;
Expand Down Expand Up @@ -106,6 +99,6 @@ describe("validation", function () {
});

it("should validate valid update", () => {
expect(() => assertValidLightClientUpdate(config, snapshot.nextSyncCommittee, update)).to.not.throw();
expect(() => assertValidLightClientUpdate(config, snapshot.nextSyncCommittee, update)).not.toThrow();
});
});
2 changes: 1 addition & 1 deletion packages/light-client/test/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bls from "@chainsafe/bls/switchable";
import bls from "@chainsafe/bls";
import {PointFormat, PublicKey, SecretKey} from "@chainsafe/bls/types";
import {hasher, Tree} from "@chainsafe/persistent-merkle-tree";
import {BitArray, fromHexString} from "@chainsafe/ssz";
Expand Down
7 changes: 0 additions & 7 deletions packages/light-client/tsconfig.e2e.json

This file was deleted.

14 changes: 14 additions & 0 deletions packages/light-client/vitest.browser.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {defineConfig, mergeConfig} from "vitest/config";
import vitestConfig from "../../vitest.base.browser.config";

export default mergeConfig(
vitestConfig,
defineConfig({
test: {
globalSetup: ["./test/globalSetup.ts"],
},
optimizeDeps: {
exclude: ["@chainsafe/blst"],
},
})
);
11 changes: 11 additions & 0 deletions packages/light-client/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {defineConfig, mergeConfig} from "vitest/config";
import vitestConfig from "../../vitest.base.config";

export default mergeConfig(
vitestConfig,
defineConfig({
test: {
globalSetup: ["./test/globalSetup.ts"],
},
})
);
5 changes: 0 additions & 5 deletions packages/light-client/webpack.test.config.cjs

This file was deleted.