refactor: reduce rpc methods to consensus_updates, consensus_optimistic_update, and execution_request

This commit is contained in:
Derrick Hammer 2023-07-14 00:01:42 -04:00
parent 9d80b557f6
commit a80f61b06f
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 35 additions and 65 deletions

View File

@ -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<Uint8Array> {
request: ConsensusCommitteeUpdateRequest,
): Promise<Uint8Array[]> {
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<Uint8Array> {
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<Uint8Array> {
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<object> {
console.log("consensus_optimistic_update request received");
if (!client.latestOptimisticUpdate) {
throw new Error("optimistic update not ready");
}
return client.latestOptimisticUpdate;
},
});
},
};