From 51d6d239425b81a62456a018f1fae04476789785 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 13 Jul 2023 02:25:05 -0400 Subject: [PATCH] refactor: getLatestExecution needs to actually call optimistic update, but add a callback in the client options to avoid creating a subclass --- src/baseClient.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/baseClient.ts b/src/baseClient.ts index f77ba2c..4ef9996 100644 --- a/src/baseClient.ts +++ b/src/baseClient.ts @@ -8,13 +8,18 @@ import { import bls, { init } from "@chainsafe/bls/switchable"; import { Mutex } from "async-mutex"; import { fromHexString, toHexString } from "@chainsafe/ssz"; -import { deserializePubkeys, getDefaultClientConfig } from "#util.js"; -import { capella, LightClientUpdate } from "#types.js"; +import { + deserializePubkeys, + getDefaultClientConfig, + optimisticUpdateVerify, +} from "#util.js"; +import { capella, LightClientUpdate, OptimisticUpdate } from "#types.js"; import { assertValidLightClientUpdate } from "@lodestar/light-client/validation"; export interface BaseClientOptions { prover: IProver; store: IStore; + optimisticUpdateCallback: () => Promise; } export default abstract class BaseClient { @@ -106,8 +111,19 @@ export default abstract class BaseClient { protected async getLatestExecution(): Promise { await this._sync(); - const update = capella.ssz.LightClientUpdate.deserialize( - this.store.getUpdate(this.latestPeriod), + const update = await this.options.optimisticUpdateCallback(); + + const verify = await optimisticUpdateVerify( + this.latestCommittee as Uint8Array[], + update, + ); + // TODO: check the update agains the latest sync commttee + if (!verify.correct) { + console.error(`Invalid Optimistic Update: ${verify.reason}`); + return null; + } + console.log( + `Optimistic update verified for slot ${update.attestedHeader.beacon.slot}`, ); return {