relay-plugin-eth/src/client/prover.ts

40 lines
1.2 KiB
TypeScript

import * as altair from "@lodestar/types/altair";
import { IProver } from "./interfaces.js";
import { LightClientUpdate } from "./types.js";
import { handleGETRequest } from "./utils.js";
export default class Prover implements IProver {
cachedSyncUpdate: Map<number, LightClientUpdate> = new Map();
private serverUrl: string;
constructor(serverUrl: string) {
this.serverUrl = serverUrl;
}
async _getSyncUpdates(
startPeriod: number,
maxCount: number
): Promise<LightClientUpdate[]> {
const res = await handleGETRequest(
`${this.serverUrl}/eth/v1/beacon/light_client/updates?start_period=${startPeriod}&count=${maxCount}`,
false
);
return res.map((u: any) => altair.ssz.LightClientUpdate.fromJson(u.data));
}
async getSyncUpdate(
period: number,
currentPeriod: number,
cacheCount: number
): Promise<LightClientUpdate> {
const _cacheCount = Math.min(currentPeriod - period + 1, cacheCount);
if (!this.cachedSyncUpdate.has(period)) {
const vals = await this._getSyncUpdates(period, _cacheCount);
for (let i = 0; i < _cacheCount; i++) {
this.cachedSyncUpdate.set(period + i, vals[i]);
}
}
return this.cachedSyncUpdate.get(period)!;
}
}