feat: add syncFromCheckpoint method

This commit is contained in:
Derrick Hammer 2023-09-15 22:43:46 -04:00
parent 178df36f93
commit ccaca65a90
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 16 additions and 1 deletions

View File

@ -56,7 +56,7 @@ export default abstract class BaseClient extends EventEmitter {
return this._latestOptimisticUpdate as Uint8Array; return this._latestOptimisticUpdate as Uint8Array;
} }
private _latestPeriod: number = -1; protected _latestPeriod: number = -1;
get latestPeriod(): number { get latestPeriod(): number {
return this._latestPeriod; return this._latestPeriod;

View File

@ -1,6 +1,8 @@
import BaseClient, { BaseClientOptions } from "#baseClient.js"; import BaseClient, { BaseClientOptions } from "#baseClient.js";
import { IProver, IVerifyingProviderConstructor } from "#interfaces.js"; import { IProver, IVerifyingProviderConstructor } from "#interfaces.js";
import { IClientVerifyingProvider } from "#client/verifyingProvider.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 { interface Config extends BaseClientOptions {
prover: IProver; prover: IProver;
@ -19,7 +21,10 @@ export default class Client extends BaseClient {
async sync(): Promise<void> { async sync(): Promise<void> {
await super.sync(); await super.sync();
await this.boot();
}
private async boot() {
if (!this.provider) { if (!this.provider) {
const { blockHash, blockNumber } = await this.getNextValidExecutionInfo(); const { blockHash, blockNumber } = await this.getNextValidExecutionInfo();
const factory = this.options.provider; 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) { public async rpcCall(method: string, params: any) {
return this.provider?.rpcMethod(method, params); return this.provider?.rpcMethod(method, params);
} }