Skip to content

Commit

Permalink
fix: light-client unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewkeil committed Apr 17, 2024
1 parent 1acce0c commit fa7893e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/light-client/test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export async function setup(): Promise<void> {}
import {initBls} from "../src/utils/index.js";

export async function setup(): Promise<void> {
await initBls("herumi");
}
export async function teardown(): Promise<void> {}
6 changes: 5 additions & 1 deletion packages/light-client/test/unit/sync.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {describe, it, expect, afterEach, vi} from "vitest";
import {describe, it, expect, afterEach, vi, beforeAll} 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 @@ -22,13 +22,17 @@ import {
import {startServer, ServerOpts} from "../utils/server.js";
import {computeSyncPeriodAtSlot} from "../../src/utils/clock.js";
import {LightClientRestTransport} from "../../src/transport/rest.js";
import {initBls} from "../../src/utils/bls.js";

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

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

beforeAll(async () => {
await initBls("herumi");
});
afterEach(async () => {
await Promise.all(afterEachCbs);
afterEachCbs.length = 0;
Expand Down
3 changes: 2 additions & 1 deletion packages/light-client/test/unit/syncInMemory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe("syncInMemory", function () {
);
});

beforeAll(() => {
beforeAll(async () => {
await initBls("herumi");
// 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
10 changes: 5 additions & 5 deletions packages/light-client/test/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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 All @@ -15,6 +14,7 @@ import {altair, phase0, Slot, ssz, SyncPeriod, allForks} from "@lodestar/types";
import {SyncCommitteeFast} from "../../src/types.js";
import {computeSigningRoot} from "../../src/utils/domain.js";
import {getLcLoggerConsole} from "../../src/utils/logger.js";
import {getBls} from "../../src/utils/bls.js";

const CURRENT_SYNC_COMMITTEE_INDEX = 22;
const CURRENT_SYNC_COMMITTEE_DEPTH = 5;
Expand All @@ -32,7 +32,7 @@ export const SOME_HASH = Buffer.alloc(32, 0xaa);

export function signAndAggregate(message: Uint8Array, sks: SecretKey[]): altair.SyncAggregate {
const sigs = sks.map((sk) => sk.sign(message));
const aggSig = bls.Signature.aggregate(sigs).toBytes();
const aggSig = getBls().Signature.aggregate(sigs).toBytes();
return {
syncCommitteeBits: BitArray.fromBoolArray(sks.map(() => true)),
syncCommitteeSignature: aggSig,
Expand Down Expand Up @@ -67,19 +67,19 @@ export type SyncCommitteeKeys = {
export function getInteropSyncCommittee(period: SyncPeriod): SyncCommitteeKeys {
const skBytes = Buffer.alloc(32, 0);
skBytes.writeInt32BE(1 + period);
const sk = bls.SecretKey.fromBytes(skBytes);
const sk = getBls().SecretKey.fromBytes(skBytes);
const pk = sk.toPublicKey();
const pks = Array.from({length: SYNC_COMMITTEE_SIZE}, () => pk);

const pkBytes = pk.toBytes(PointFormat.compressed);
const pksBytes = Array.from({length: SYNC_COMMITTEE_SIZE}, () => pkBytes);

const aggPk = bls.PublicKey.aggregate(pks);
const aggPk = getBls().PublicKey.aggregate(pks);

function signAndAggregate(message: Uint8Array): altair.SyncAggregate {
const sig = sk.sign(message);
const sigs = Array.from({length: SYNC_COMMITTEE_SIZE}, () => sig);
const aggSig = bls.Signature.aggregate(sigs).toBytes();
const aggSig = getBls().Signature.aggregate(sigs).toBytes();
return {
syncCommitteeBits: BitArray.fromBoolArray(sigs.map(() => true)),
syncCommitteeSignature: aggSig,
Expand Down
5 changes: 5 additions & 0 deletions vitest.base.unit.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ export default defineConfig({
],
},
},
resolve: {
alias: {
"@chainsafe/blst": path.join(__dirname, "scripts/vitest/polyfills/emptyModule.js"),
},
},
});

0 comments on commit fa7893e

Please sign in to comment.