2023-03-19 21:17:05 +00:00
|
|
|
import type { Plugin, PluginAPI } from "@lumeweb/interface-relay";
|
2023-07-11 07:55:23 +00:00
|
|
|
import {
|
|
|
|
ConsensusCommitteeHashesRequest,
|
|
|
|
ConsensusCommitteePeriodRequest,
|
|
|
|
createDefaultClient,
|
|
|
|
getConsensusOptimisticUpdate,
|
|
|
|
} from "@lumeweb/libethsync/node";
|
|
|
|
import { RPCRequestRaw } from "@lumeweb/libethsync/client";
|
|
|
|
import axios from "axios";
|
2023-03-27 12:04:12 +00:00
|
|
|
|
2023-03-19 21:17:05 +00:00
|
|
|
const EXECUTION_RPC_URL =
|
2023-03-29 16:09:46 +00:00
|
|
|
"https://solemn-small-frost.discover.quiknode.pro/dbbe3dc75a8b828611df3f12722de5cc88214947/";
|
2023-03-19 21:17:05 +00:00
|
|
|
|
|
|
|
const CONSENSUS_RPC_URL = "https://www.lightclientdata.org";
|
|
|
|
|
2023-03-27 12:04:12 +00:00
|
|
|
interface ExecutionRequest {
|
|
|
|
method: string;
|
2023-03-29 04:02:16 +00:00
|
|
|
params: any[];
|
2023-03-27 12:04:12 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 07:55:23 +00:00
|
|
|
const executionClient = axios.create({ baseURL: EXECUTION_RPC_URL });
|
2023-03-29 09:31:54 +00:00
|
|
|
|
2023-03-19 21:17:05 +00:00
|
|
|
const plugin: Plugin = {
|
|
|
|
name: "eth",
|
|
|
|
async plugin(api: PluginAPI): Promise<void> {
|
2023-07-11 07:55:23 +00:00
|
|
|
const client = createDefaultClient(CONSENSUS_RPC_URL);
|
2023-03-29 04:02:16 +00:00
|
|
|
await client.sync();
|
|
|
|
|
|
|
|
api.registerMethod("consensus_committee_hashes", {
|
2023-03-19 21:17:05 +00:00
|
|
|
cacheable: false,
|
2023-03-29 04:02:16 +00:00
|
|
|
async handler(
|
2023-07-07 11:16:18 +00:00
|
|
|
request: ConsensusCommitteeHashesRequest,
|
2023-03-29 04:02:16 +00:00
|
|
|
): Promise<Uint8Array> {
|
2023-07-11 07:55:23 +00:00
|
|
|
if (!(request?.start && typeof request.start === "number")) {
|
2023-03-29 04:02:16 +00:00
|
|
|
throw new Error('start required and must be a number"');
|
|
|
|
}
|
2023-07-11 07:55:23 +00:00
|
|
|
if (!(request?.count && typeof request.count === "number")) {
|
2023-03-29 04:02:16 +00:00
|
|
|
throw new Error('count required and must be a number"');
|
|
|
|
}
|
2023-03-27 12:04:12 +00:00
|
|
|
|
2023-07-11 07:55:23 +00:00
|
|
|
let hashes: Uint8Array = new Uint8Array();
|
2023-03-29 04:02:16 +00:00
|
|
|
|
2023-07-11 07:55:23 +00:00
|
|
|
for (let i = 0; i < 4; i++) {
|
2023-03-29 04:02:16 +00:00
|
|
|
try {
|
2023-07-11 07:55:23 +00:00
|
|
|
hashes = client.store.getCommitteeHashes(
|
|
|
|
request.start,
|
|
|
|
request.count,
|
|
|
|
);
|
2023-03-29 04:02:16 +00:00
|
|
|
} catch (e) {
|
2023-07-11 07:55:23 +00:00
|
|
|
if (i === 3) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
await client.sync();
|
2023-03-29 04:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashes;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
api.registerMethod("consensus_committee_period", {
|
|
|
|
cacheable: false,
|
|
|
|
async handler(
|
2023-07-07 11:16:18 +00:00
|
|
|
request: ConsensusCommitteePeriodRequest,
|
2023-03-29 04:02:16 +00:00
|
|
|
): 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) {
|
|
|
|
await client.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
let committee;
|
2023-03-27 12:04:12 +00:00
|
|
|
|
2023-07-11 07:55:23 +00:00
|
|
|
for (let i = 0; i < 4; i++) {
|
2023-03-29 04:02:16 +00:00
|
|
|
try {
|
2023-07-11 07:55:23 +00:00
|
|
|
committee = client.store.getCommittee(
|
2023-07-07 11:16:18 +00:00
|
|
|
request.period === "latest"
|
|
|
|
? client.latestPeriod
|
|
|
|
: request.period,
|
2023-03-29 04:02:16 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2023-07-11 07:55:23 +00:00
|
|
|
if (i === 3) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
await client.sync();
|
2023-03-29 04:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 07:55:23 +00:00
|
|
|
try {
|
|
|
|
committee = client.store.getCommittee(
|
|
|
|
request.period === "latest" ? client.latestPeriod : request.period,
|
|
|
|
);
|
|
|
|
} catch {
|
|
|
|
await client.sync();
|
|
|
|
}
|
|
|
|
|
2023-03-29 04:02:16 +00:00
|
|
|
return committee;
|
2023-03-19 21:17:05 +00:00
|
|
|
},
|
|
|
|
});
|
2023-03-29 04:02:16 +00:00
|
|
|
|
2023-03-27 12:04:12 +00:00
|
|
|
api.registerMethod("execution_request", {
|
2023-03-19 21:17:05 +00:00
|
|
|
cacheable: false,
|
2023-07-11 07:55:23 +00:00
|
|
|
async handler(request: RPCRequestRaw): Promise<object> {
|
|
|
|
const ret = (await executionClient.post<any>("/", request)).data;
|
2023-03-29 08:58:17 +00:00
|
|
|
return { ...ret, id: request.id ?? ret.id };
|
2023-03-29 04:02:16 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
api.registerMethod("consensus_optimistic_update", {
|
|
|
|
cacheable: false,
|
|
|
|
async handler(): Promise<object> {
|
2023-07-11 07:55:23 +00:00
|
|
|
return getConsensusOptimisticUpdate();
|
2023-03-19 21:17:05 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default plugin;
|