-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathindex.ts
54 lines (48 loc) · 2.27 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {fromHex} from "@lodestar/utils";
import {routes} from "@lodestar/api";
import {ApplicationMethods} from "@lodestar/api/server";
import {MAX_REQUEST_LIGHT_CLIENT_UPDATES, MAX_REQUEST_LIGHT_CLIENT_COMMITTEE_HASHES} from "@lodestar/params";
import {ApiModules} from "../types.js";
// TODO: Import from lightclient/server package
export function getLightclientApi({
chain,
config,
}: Pick<ApiModules, "chain" | "config">): ApplicationMethods<routes.lightclient.Endpoints> {
return {
async getLightClientUpdatesByRange({startPeriod, count}) {
const maxAllowedCount = Math.min(MAX_REQUEST_LIGHT_CLIENT_UPDATES, count);
const periods = Array.from({length: maxAllowedCount}, (_ignored, i) => i + startPeriod);
const updates = await Promise.all(periods.map((period) => chain.lightClientServer.getUpdate(period)));
return {
data: updates,
meta: {versions: updates.map((update) => config.getForkName(update.attestedHeader.beacon.slot))},
};
},
async getLightClientOptimisticUpdate() {
const update = chain.lightClientServer.getOptimisticUpdate();
if (update === null) {
throw Error("No optimistic update available");
}
return {data: update, meta: {version: config.getForkName(update.attestedHeader.beacon.slot)}};
},
async getLightClientFinalityUpdate() {
const update = chain.lightClientServer.getFinalityUpdate();
if (update === null) {
throw Error("No finality update available");
}
return {data: update, meta: {version: config.getForkName(update.attestedHeader.beacon.slot)}};
},
async getLightClientBootstrap({blockRoot}) {
const bootstrapProof = await chain.lightClientServer.getBootstrap(fromHex(blockRoot));
return {data: bootstrapProof, meta: {version: config.getForkName(bootstrapProof.header.beacon.slot)}};
},
async getLightClientCommitteeRoot({startPeriod, count}) {
const maxAllowedCount = Math.min(MAX_REQUEST_LIGHT_CLIENT_COMMITTEE_HASHES, count);
const periods = Array.from({length: maxAllowedCount}, (_ignored, i) => i + startPeriod);
const committeeHashes = await Promise.all(
periods.map((period) => chain.lightClientServer.getCommitteeRoot(period))
);
return {data: committeeHashes};
},
};
}