refactor: reduce rpc methods to consensus_updates, consensus_optimistic_update, and execution_request
This commit is contained in:
parent
9d80b557f6
commit
a80f61b06f
100
src/index.ts
100
src/index.ts
|
@ -1,15 +1,12 @@
|
||||||
import type { Plugin, PluginAPI } from "@lumeweb/interface-relay";
|
import type { Plugin, PluginAPI } from "@lumeweb/interface-relay";
|
||||||
import {
|
import {
|
||||||
ConsensusCommitteeHashesRequest,
|
ConsensusCommitteeUpdateRequest,
|
||||||
ConsensusCommitteePeriodRequest,
|
|
||||||
createDefaultClient,
|
createDefaultClient,
|
||||||
getConsensusOptimisticUpdate,
|
|
||||||
} from "@lumeweb/libethsync/node";
|
} from "@lumeweb/libethsync/node";
|
||||||
import { RPCRequestRaw } from "@lumeweb/libethsync/client";
|
import { RPCRequestRaw } from "@lumeweb/libethsync/client";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
const EXECUTION_RPC_URL =
|
const EXECUTION_RPC_URL = "https://rpc.ankr.com/eth";
|
||||||
"https://solemn-small-frost.discover.quiknode.pro/dbbe3dc75a8b828611df3f12722de5cc88214947/";
|
|
||||||
|
|
||||||
const CONSENSUS_RPC_URL = "https://www.lightclientdata.org";
|
const CONSENSUS_RPC_URL = "https://www.lightclientdata.org";
|
||||||
|
|
||||||
|
@ -26,70 +23,55 @@ const plugin: Plugin = {
|
||||||
const client = createDefaultClient(CONSENSUS_RPC_URL);
|
const client = createDefaultClient(CONSENSUS_RPC_URL);
|
||||||
await client.sync();
|
await client.sync();
|
||||||
|
|
||||||
api.registerMethod("consensus_committee_hashes", {
|
api.registerMethod("consensus_updates", {
|
||||||
cacheable: false,
|
cacheable: false,
|
||||||
async handler(
|
async handler(
|
||||||
request: ConsensusCommitteeHashesRequest,
|
request: ConsensusCommitteeUpdateRequest,
|
||||||
): Promise<Uint8Array> {
|
): Promise<Uint8Array[]> {
|
||||||
if (!(request?.start && typeof request.start === "number")) {
|
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")) {
|
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++) {
|
request.count = Math.min(request.count, 5);
|
||||||
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"');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!client.isSynced) {
|
if (!client.isSynced) {
|
||||||
await client.sync();
|
await client.sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < 4; i++) {
|
const updates: Uint8Array[] = [];
|
||||||
try {
|
|
||||||
return await client.store.getCommittee(
|
for (let i = 0; i < request.count; i++) {
|
||||||
request.period === "latest"
|
const period = request?.start + i;
|
||||||
? client.latestPeriod
|
if (!client.store.hasUpdate(period)) {
|
||||||
: request.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 };
|
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;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue