*Add mutex lock around syncing to prevent race conditions

This commit is contained in:
Derrick Hammer 2023-03-29 00:11:59 -04:00
parent 9beb2a6c58
commit 236338485b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 7 additions and 0 deletions

View File

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