From 4582956533483137a190def55c6fa96bdf383306 Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Mon, 4 Mar 2024 17:49:22 +0100 Subject: [PATCH 1/2] feat: allow to select bls implementation --- packages/light-client/src/index.ts | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/packages/light-client/src/index.ts b/packages/light-client/src/index.ts index deac9f66f4d9..5f60be8db104 100644 --- a/packages/light-client/src/index.ts +++ b/packages/light-client/src/index.ts @@ -1,5 +1,6 @@ import mitt from "mitt"; import {init as initBls} from "@chainsafe/bls/switchable"; +import {Implementation} from "@chainsafe/bls/types"; import {fromHexString, toHexString} from "@chainsafe/ssz"; import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD} from "@lodestar/params"; import {phase0, RootHex, Slot, SyncPeriod, allForks} from "@lodestar/types"; @@ -108,7 +109,7 @@ export class Lightclient { private runStatus: RunStatus = {code: RunStatusCode.stopped}; - constructor({config, logger, genesisData, bootstrap, transport}: LightclientInitArgs) { + private constructor({config, logger, genesisData, bootstrap, transport}: LightclientInitArgs) { this.genesisTime = genesisData.genesisTime; this.genesisValidatorsRoot = typeof genesisData.genesisValidatorsRoot === "string" @@ -149,15 +150,16 @@ export class Lightclient { static async initializeFromCheckpointRoot( args: Omit & { checkpointRoot: phase0.Checkpoint["root"]; + blsImplementation?: Implementation; } ): Promise { - const {transport, checkpointRoot} = args; + const {transport, checkpointRoot, blsImplementation} = args; // Initialize the BLS implementation. This may requires initializing the WebAssembly instance // so why it's an async process. This should be initialized once before any bls operations. // This process has to be done manually because of an issue in Karma runner // https://github.com/karma-runner/karma/issues/3804 - await initBls(isNode ? "blst-native" : "herumi"); + await initBls(blsImplementation ?? (isNode ? "blst-native" : "herumi")); // Fetch bootstrap state with proof at the trusted block root const {data: bootstrap} = await transport.getBootstrap(toHexString(checkpointRoot)); @@ -189,12 +191,6 @@ export class Lightclient { } async sync(fromPeriod: SyncPeriod, toPeriod: SyncPeriod): Promise { - // Initialize the BLS implementation. This may requires initializing the WebAssembly instance - // so why it's a an async process. This should be initialized once before any bls operations. - // This process has to be done manually because of an issue in Karma runner - // https://github.com/karma-runner/karma/issues/3804 - await initBls(isNode ? "blst-native" : "herumi"); - const periodRanges = chunkifyInclusiveRange(fromPeriod, toPeriod, MAX_PERIODS_PER_REQUEST); for (const [fromPeriodRng, toPeriodRng] of periodRanges) { @@ -211,12 +207,6 @@ export class Lightclient { } private async runLoop(): Promise { - // Initialize the BLS implementation. This may requires initializing the WebAssembly instance - // so why it's a an async process. This should be initialized once before any bls operations. - // This process has to be done manually because of an issue in Karma runner - // https://github.com/karma-runner/karma/issues/3804 - await initBls(isNode ? "blst-native" : "herumi"); - // eslint-disable-next-line no-constant-condition while (true) { const currentPeriod = computeSyncPeriodAtSlot(this.currentSlot); From bd3e29e986f16dc2679c2553329c4a70802d0a85 Mon Sep 17 00:00:00 2001 From: Julien Eluard Date: Wed, 6 Mar 2024 14:43:33 +0100 Subject: [PATCH 2/2] chore: remove unneeded init --- packages/light-client/src/index.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/packages/light-client/src/index.ts b/packages/light-client/src/index.ts index 5f60be8db104..133a157b0e5b 100644 --- a/packages/light-client/src/index.ts +++ b/packages/light-client/src/index.ts @@ -1,13 +1,10 @@ import mitt from "mitt"; -import {init as initBls} from "@chainsafe/bls/switchable"; -import {Implementation} from "@chainsafe/bls/types"; import {fromHexString, toHexString} from "@chainsafe/ssz"; import {EPOCHS_PER_SYNC_COMMITTEE_PERIOD} from "@lodestar/params"; import {phase0, RootHex, Slot, SyncPeriod, allForks} from "@lodestar/types"; import {createBeaconConfig, BeaconConfig, ChainForkConfig} from "@lodestar/config"; import {isErrorAborted, sleep} from "@lodestar/utils"; import {getCurrentSlot, slotWithFutureTolerance, timeUntilNextEpoch} from "./utils/clock.js"; -import {isNode} from "./utils/utils.js"; import {chunkifyInclusiveRange} from "./utils/chunkify.js"; import {LightclientEmitter, LightclientEvent} from "./events.js"; import {getLcLoggerConsole, ILcLogger} from "./utils/logger.js"; @@ -109,7 +106,7 @@ export class Lightclient { private runStatus: RunStatus = {code: RunStatusCode.stopped}; - private constructor({config, logger, genesisData, bootstrap, transport}: LightclientInitArgs) { + constructor({config, logger, genesisData, bootstrap, transport}: LightclientInitArgs) { this.genesisTime = genesisData.genesisTime; this.genesisValidatorsRoot = typeof genesisData.genesisValidatorsRoot === "string" @@ -150,16 +147,9 @@ export class Lightclient { static async initializeFromCheckpointRoot( args: Omit & { checkpointRoot: phase0.Checkpoint["root"]; - blsImplementation?: Implementation; } ): Promise { - const {transport, checkpointRoot, blsImplementation} = args; - - // Initialize the BLS implementation. This may requires initializing the WebAssembly instance - // so why it's an async process. This should be initialized once before any bls operations. - // This process has to be done manually because of an issue in Karma runner - // https://github.com/karma-runner/karma/issues/3804 - await initBls(blsImplementation ?? (isNode ? "blst-native" : "herumi")); + const {transport, checkpointRoot} = args; // Fetch bootstrap state with proof at the trusted block root const {data: bootstrap} = await transport.getBootstrap(toHexString(checkpointRoot));