From a80f61b06fe0ba0adec62f651e1d8a7bd48a6f26 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 14 Jul 2023 00:01:42 -0400 Subject: [PATCH] refactor: reduce rpc methods to consensus_updates, consensus_optimistic_update, and execution_request --- src/index.ts | 100 ++++++++++++++++++--------------------------------- 1 file changed, 35 insertions(+), 65 deletions(-) diff --git a/src/index.ts b/src/index.ts index d6d5bb4..17ba14c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,12 @@ import type { Plugin, PluginAPI } from "@lumeweb/interface-relay"; import { - ConsensusCommitteeHashesRequest, - ConsensusCommitteePeriodRequest, + ConsensusCommitteeUpdateRequest, createDefaultClient, - getConsensusOptimisticUpdate, } from "@lumeweb/libethsync/node"; import { RPCRequestRaw } from "@lumeweb/libethsync/client"; import axios from "axios"; -const EXECUTION_RPC_URL = - "https://solemn-small-frost.discover.quiknode.pro/dbbe3dc75a8b828611df3f12722de5cc88214947/"; +const EXECUTION_RPC_URL = "https://rpc.ankr.com/eth"; const CONSENSUS_RPC_URL = "https://www.lightclientdata.org"; @@ -26,70 +23,55 @@ const plugin: Plugin = { const client = createDefaultClient(CONSENSUS_RPC_URL); await client.sync(); - api.registerMethod("consensus_committee_hashes", { + api.registerMethod("consensus_updates", { cacheable: false, async handler( - request: ConsensusCommitteeHashesRequest, - ): Promise { + request: ConsensusCommitteeUpdateRequest, + ): Promise { if (!(request?.start && typeof request.start === "number")) { - throw new Error('start required and must be a number"'); + throw new Error("start required and must be a number"); } if (!(request?.count && typeof request.count === "number")) { - throw new Error('count required and must be a number"'); + throw new Error("count required and must be a number"); } - for (let i = 0; i < 4; i++) { - try { - return await client.store.getCommitteeHashes( - request.start, - request.count, - ); - } catch (e) { - if (i === 3) { - return e; - } - await client.sync(); - } - } - - return new Uint8Array(); - }, - }); - - api.registerMethod("consensus_committee_period", { - cacheable: false, - async handler( - request: ConsensusCommitteePeriodRequest, - ): Promise { - if ( - !( - request?.period && - (typeof request.period == "number" || request.period === "latest") - ) - ) { - throw new Error('period required and must be a number or "latest"'); - } + request.count = Math.min(request.count, 5); if (!client.isSynced) { await client.sync(); } - for (let i = 0; i < 4; i++) { - try { - return await client.store.getCommittee( - request.period === "latest" - ? client.latestPeriod - : request.period, + const updates: Uint8Array[] = []; + + for (let i = 0; i < request.count; i++) { + const period = request?.start + i; + if (!client.store.hasUpdate(period)) { + throw new Error( + `update at period ${request?.start} does not exist`, ); - } catch (e) { - if (i === 3) { - return e; - } - await client.sync(); } + + updates.push(client.store.getUpdate(period)); } - return new Uint8Array(); + return updates; + }, + }); + + api.registerMethod("consensus_optimistic_update", { + cacheable: false, + async handler(): Promise { + if (!client.isSynced) { + await client.sync(); + } + + let execInfo = await client.getLatestExecution(); + + if (!execInfo) { + throw new Error("optimistic update not ready"); + } + + return client.latestOptimisticUpdate; }, }); @@ -100,18 +82,6 @@ const plugin: Plugin = { return { ...ret, id: request.id ?? ret.id }; }, }); - - api.registerMethod("consensus_optimistic_update", { - cacheable: false, - async handler(): Promise { - console.log("consensus_optimistic_update request received"); - if (!client.latestOptimisticUpdate) { - throw new Error("optimistic update not ready"); - } - - return client.latestOptimisticUpdate; - }, - }); }, };