refactor: don't use un-needed for loop

This commit is contained in:
Derrick Hammer 2023-07-13 01:46:45 -04:00
parent 17cb00231c
commit 5aa37d4a61
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 35 additions and 35 deletions

View File

@ -5,13 +5,12 @@ import {
getCurrentSlot, getCurrentSlot,
deserializeSyncCommittee, deserializeSyncCommittee,
} from "@lodestar/light-client/utils"; } from "@lodestar/light-client/utils";
import { init } from "@chainsafe/bls/switchable"; import bls, { init } from "@chainsafe/bls/switchable";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
import { fromHexString, toHexString } from "@chainsafe/ssz"; import { fromHexString, toHexString } from "@chainsafe/ssz";
import { deserializePubkeys, getDefaultClientConfig } from "#util.js"; import { deserializePubkeys, getDefaultClientConfig } from "#util.js";
import { capella, LightClientUpdate } from "#types.js"; import { capella, LightClientUpdate } from "#types.js";
import bls from "@chainsafe/bls/switchable.js"; import { assertValidLightClientUpdate } from "@lodestar/light-client/validation";
import { assertValidLightClientUpdate } from "@lodestar/light-client/validation.js";
export interface BaseClientOptions { export interface BaseClientOptions {
prover: IProver; prover: IProver;
@ -121,43 +120,44 @@ export default abstract class BaseClient {
currentPeriod: number, currentPeriod: number,
startCommittee: Uint8Array[], startCommittee: Uint8Array[],
): Promise<{ syncCommittee: Uint8Array[]; period: number }> { ): Promise<{ syncCommittee: Uint8Array[]; period: number }> {
for (let period = startPeriod; period < currentPeriod; period += 1) { try {
try { const updates = await this.options.prover.getSyncUpdate(
const updates = await this.options.prover.getSyncUpdate( startPeriod,
period, currentPeriod - startPeriod,
currentPeriod, );
for (let i = 0; i < updates.length; i++) {
const curPeriod = startPeriod + i;
const update = updates[i];
const updatePeriod = computeSyncPeriodAtSlot(
update.attestedHeader.beacon.slot,
); );
for (let i = 0; i < updates.length; i++) { const validOrCommittee = await this.syncUpdateVerifyGetCommittee(
const curPeriod = period + i; startCommittee,
const update = updates[i]; curPeriod,
update,
);
const validOrCommittee = await this.syncUpdateVerifyGetCommittee( if (!(validOrCommittee as boolean)) {
startCommittee, console.log(`Found invalid update at period(${curPeriod})`);
curPeriod, return {
update, syncCommittee: startCommittee,
); period: curPeriod,
};
if (!(validOrCommittee as boolean)) {
console.log(`Found invalid update at period(${curPeriod})`);
return {
syncCommittee: startCommittee,
period: curPeriod,
};
}
await this.options.store.addUpdate(period, update);
startCommittee = validOrCommittee as Uint8Array[];
period = curPeriod;
} }
} catch (e) {
console.error(`failed to fetch sync update for period(${period})`); await this.options.store.addUpdate(curPeriod, update);
return {
syncCommittee: startCommittee, startCommittee = validOrCommittee as Uint8Array[];
period,
};
} }
} catch (e) {
console.error(`failed to fetch sync update for period(${startPeriod})`);
return {
syncCommittee: startCommittee,
period: startPeriod,
};
} }
return { return {
syncCommittee: startCommittee, syncCommittee: startCommittee,