-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathindex.ts
271 lines (242 loc) Β· 9.49 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import {routes} from "@lodestar/api";
import {ApplicationMethods} from "@lodestar/api/server";
import {
BeaconStateAllForks,
CachedBeaconStateAltair,
computeEpochAtSlot,
computeStartSlotAtEpoch,
getCurrentEpoch,
getRandaoMix,
} from "@lodestar/state-transition";
import {EPOCHS_PER_HISTORICAL_VECTOR} from "@lodestar/params";
import {ApiError} from "../../errors.js";
import {ApiModules} from "../../types.js";
import {
filterStateValidatorsByStatus,
getStateValidatorIndex,
getValidatorStatus,
getStateResponse,
toValidatorResponse,
} from "./utils.js";
export function getBeaconStateApi({
chain,
config,
}: Pick<ApiModules, "chain" | "config">): ApplicationMethods<routes.beacon.state.Endpoints> {
async function getState(
stateId: routes.beacon.StateId
): Promise<{state: BeaconStateAllForks; executionOptimistic: boolean; finalized: boolean}> {
return getStateResponse(chain, stateId);
}
return {
async getStateRoot({stateId}) {
const {state, executionOptimistic, finalized} = await getState(stateId);
return {
data: {root: state.hashTreeRoot()},
meta: {executionOptimistic, finalized},
};
},
async getStateFork({stateId}) {
const {state, executionOptimistic, finalized} = await getState(stateId);
return {
data: state.fork,
meta: {executionOptimistic, finalized},
};
},
async getStateRandao({stateId, epoch}) {
const {state, executionOptimistic, finalized} = await getState(stateId);
const stateEpoch = computeEpochAtSlot(state.slot);
const usedEpoch = epoch ?? stateEpoch;
if (!(stateEpoch < usedEpoch + EPOCHS_PER_HISTORICAL_VECTOR && usedEpoch <= stateEpoch)) {
throw new ApiError(400, "Requested epoch is out of range");
}
const randao = getRandaoMix(state, usedEpoch);
return {
data: {randao},
meta: {executionOptimistic, finalized},
};
},
async getStateFinalityCheckpoints({stateId}) {
const {state, executionOptimistic, finalized} = await getState(stateId);
return {
data: {
currentJustified: state.currentJustifiedCheckpoint,
previousJustified: state.previousJustifiedCheckpoint,
finalized: state.finalizedCheckpoint,
},
meta: {executionOptimistic, finalized},
};
},
async getStateValidators({stateId, validatorIds = [], statuses = []}) {
const {state, executionOptimistic, finalized} = await getStateResponse(chain, stateId);
const currentEpoch = getCurrentEpoch(state);
const {validators, balances} = state; // Get the validators sub tree once for all the loop
const {pubkey2index} = chain.getHeadState().epochCtx;
const validatorResponses: routes.beacon.ValidatorResponse[] = [];
if (validatorIds.length) {
for (const id of validatorIds) {
const resp = getStateValidatorIndex(id, state, pubkey2index);
if (resp.valid) {
const validatorIndex = resp.validatorIndex;
const validator = validators.getReadonly(validatorIndex);
if (statuses.length && !statuses.includes(getValidatorStatus(validator, currentEpoch))) {
continue;
}
const validatorResponse = toValidatorResponse(
validatorIndex,
validator,
balances.get(validatorIndex),
currentEpoch
);
validatorResponses.push(validatorResponse);
}
}
return {
data: validatorResponses,
meta: {executionOptimistic, finalized},
};
} else if (statuses.length) {
const validatorsByStatus = filterStateValidatorsByStatus(statuses, state, pubkey2index, currentEpoch);
return {
data: validatorsByStatus,
meta: {executionOptimistic, finalized},
};
}
// TODO: This loops over the entire state, it's a DOS vector
const validatorsArr = state.validators.getAllReadonlyValues();
const balancesArr = state.balances.getAll();
const resp: routes.beacon.ValidatorResponse[] = [];
for (let i = 0; i < validatorsArr.length; i++) {
resp.push(toValidatorResponse(i, validatorsArr[i], balancesArr[i], currentEpoch));
}
return {
data: resp,
meta: {executionOptimistic, finalized},
};
},
async postStateValidators(args, context) {
return this.getStateValidators(args, context);
},
async getStateValidator({stateId, validatorId}) {
const {state, executionOptimistic, finalized} = await getStateResponse(chain, stateId);
const {pubkey2index} = chain.getHeadState().epochCtx;
const resp = getStateValidatorIndex(validatorId, state, pubkey2index);
if (!resp.valid) {
throw new ApiError(resp.code, resp.reason);
}
const validatorIndex = resp.validatorIndex;
return {
data: toValidatorResponse(
validatorIndex,
state.validators.getReadonly(validatorIndex),
state.balances.get(validatorIndex),
getCurrentEpoch(state)
),
meta: {executionOptimistic, finalized},
};
},
async getStateValidatorBalances({stateId, validatorIds = []}) {
const {state, executionOptimistic, finalized} = await getStateResponse(chain, stateId);
if (validatorIds.length) {
const headState = chain.getHeadState();
const balances: routes.beacon.ValidatorBalance[] = [];
for (const id of validatorIds) {
if (typeof id === "number") {
if (state.validators.length <= id) {
continue;
}
balances.push({index: id, balance: state.balances.get(id)});
} else {
const index = headState.epochCtx.pubkey2index.get(id);
if (index != null && index <= state.validators.length) {
balances.push({index, balance: state.balances.get(index)});
}
}
}
return {
data: balances,
meta: {executionOptimistic, finalized},
};
}
// TODO: This loops over the entire state, it's a DOS vector
const balancesArr = state.balances.getAll();
const resp: routes.beacon.ValidatorBalance[] = [];
for (let i = 0; i < balancesArr.length; i++) {
resp.push({index: i, balance: balancesArr[i]});
}
return {
data: resp,
meta: {executionOptimistic, finalized},
};
},
async postStateValidatorBalances(args, context) {
return this.getStateValidatorBalances(args, context);
},
async getEpochCommittees({stateId, ...filters}) {
const {state, executionOptimistic, finalized} = await getStateResponse(chain, stateId);
const stateCached = state as CachedBeaconStateAltair;
if (stateCached.epochCtx === undefined) {
throw new ApiError(400, `No cached state available for stateId: ${stateId}`);
}
const epoch = filters.epoch ?? computeEpochAtSlot(state.slot);
const startSlot = computeStartSlotAtEpoch(epoch);
const decisionRoot = stateCached.epochCtx.getShufflingDecisionRoot(epoch);
const shuffling = await chain.shufflingCache.get(epoch, decisionRoot);
if (!shuffling) {
throw new ApiError(
400,
`No shuffling found to calculate committees for epoch: ${epoch} and decisionRoot: ${decisionRoot}`
);
}
const committees = shuffling.committees;
const committeesFlat = committees.flatMap((slotCommittees, slotInEpoch) => {
const slot = startSlot + slotInEpoch;
if (filters.slot !== undefined && filters.slot !== slot) {
return [];
}
return slotCommittees.flatMap((committee, committeeIndex) => {
if (filters.index !== undefined && filters.index !== committeeIndex) {
return [];
}
return [
{
index: committeeIndex,
slot,
validators: Array.from(committee),
},
];
});
});
return {
data: committeesFlat,
meta: {executionOptimistic, finalized},
};
},
/**
* Retrieves the sync committees for the given state.
* @param epoch Fetch sync committees for the given epoch. If not present then the sync committees for the epoch of the state will be obtained.
*/
async getEpochSyncCommittees({stateId, epoch}) {
// TODO: Should pick a state with the provided epoch too
const {state, executionOptimistic, finalized} = await getStateResponse(chain, stateId);
// TODO: If possible compute the syncCommittees in advance of the fork and expose them here.
// So the validators can prepare and potentially attest the first block. Not critical tho, it's very unlikely
const stateEpoch = computeEpochAtSlot(state.slot);
if (stateEpoch < config.ALTAIR_FORK_EPOCH) {
throw new ApiError(400, "Requested state before ALTAIR_FORK_EPOCH");
}
const stateCached = state as CachedBeaconStateAltair;
if (stateCached.epochCtx === undefined) {
throw new ApiError(400, `No cached state available for stateId: ${stateId}`);
}
const syncCommitteeCache = stateCached.epochCtx.getIndexedSyncCommitteeAtEpoch(epoch ?? stateEpoch);
return {
data: {
validators: syncCommitteeCache.validatorIndices,
// TODO: This is not used by the validator and will be deprecated soon
validatorAggregates: [],
},
meta: {executionOptimistic, finalized},
};
},
};
}