diff --git a/src/client/client.ts b/src/client/client.ts index 7091bd4..8e50ea3 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -31,6 +31,7 @@ import * as phase0 from "@lodestar/types/phase0"; import * as bellatrix from "@lodestar/types/bellatrix"; import { init } from "@chainsafe/bls/switchable"; import NodeCache from "node-cache"; +import { Mutex } from "async-mutex"; export default class Client { latestCommittee?: Uint8Array[]; @@ -46,6 +47,8 @@ export default class Client { private beaconChainAPIURL: string; private store: IStore; + private syncMutex = new Mutex(); + constructor(prover: IProver, store: IStore, beaconUrl: string) { this.prover = prover; this.store = store; @@ -228,11 +231,15 @@ export default class Client { } protected async _sync() { + await this.syncMutex.acquire(); + const currentPeriod = this.getCurrentPeriod(); if (currentPeriod > this._latestPeriod) { this.latestCommittee = await this.syncFromGenesis(); this._latestPeriod = currentPeriod; } + + this.syncMutex.release(); } // committee and prover index of the first honest prover