From ccaca65a900ec75adc1605e5b22caab4182587dd Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 15 Sep 2023 22:43:46 -0400 Subject: [PATCH] feat: add syncFromCheckpoint method --- src/baseClient.ts | 2 +- src/client/client.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/baseClient.ts b/src/baseClient.ts index c1336b2..26f34b1 100644 --- a/src/baseClient.ts +++ b/src/baseClient.ts @@ -56,7 +56,7 @@ export default abstract class BaseClient extends EventEmitter { return this._latestOptimisticUpdate as Uint8Array; } - private _latestPeriod: number = -1; + protected _latestPeriod: number = -1; get latestPeriod(): number { return this._latestPeriod; diff --git a/src/client/client.ts b/src/client/client.ts index 8c191c4..195de1a 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -1,6 +1,8 @@ import BaseClient, { BaseClientOptions } from "#baseClient.js"; import { IProver, IVerifyingProviderConstructor } from "#interfaces.js"; import { IClientVerifyingProvider } from "#client/verifyingProvider.js"; +import { LightClientUpdate } from "#types.js"; +import { computeSyncPeriodAtSlot } from "@lodestar/light-client/utils/index.js"; interface Config extends BaseClientOptions { prover: IProver; @@ -19,7 +21,10 @@ export default class Client extends BaseClient { async sync(): Promise { await super.sync(); + await this.boot(); + } + private async boot() { if (!this.provider) { const { blockHash, blockNumber } = await this.getNextValidExecutionInfo(); const factory = this.options.provider; @@ -40,6 +45,16 @@ export default class Client extends BaseClient { } } + public async syncFromCheckpoint(checkpoint: LightClientUpdate) { + this._latestPeriod = computeSyncPeriodAtSlot( + checkpoint.attestedHeader.beacon.slot, + ); + this.latestCommittee = checkpoint.nextSyncCommittee.pubkeys; + this.booted = true; + + await super.sync(); + } + public async rpcCall(method: string, params: any) { return this.provider?.rpcMethod(method, params); }