Skip to content

Commit

Permalink
FIX: avoid request limit for validator keys (#7)
Browse files Browse the repository at this point in the history
- Fixed request limit for validator keys
- Added method to request validator states chunked
- Added option to request only used keys
  • Loading branch information
daverolo authored May 5, 2023
1 parent 133a658 commit 30ad770
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/functions/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const operatorName = appConfig.operatorName;
const fakeKeys = appConfig.fakeKeys;
const validatorChains = appConfig.validatorChains;
const slashedValidatorsDB = new SlashedValidator(db);
const getStateValidatosChunkSize = 500;

export const startLoop = async (init = false) => {
try {
Expand All @@ -36,12 +37,13 @@ export const startLoop = async (init = false) => {
const validatorUrl =
validatorChain && validatorChain.hasOwnProperty("url") ? validatorChain.url : "https://beaconcha.in/validator/";

const operatorKeys = await kapi.findOperatorKeys(operatorIndex);
const operatorKeys = await kapi.findOperatorKeys(operatorIndex, true);

const currentSlashedValidators = await eth.getStateValidators({
const currentSlashedValidators = await eth.getStateValidatorsChunked({
stateId: "head",
validatorIds: operatorKeys.map((ky) => ky.key),
status: fakeKeys ? [] : ["active_slashed", "exited_slashed"],
chunkSize: getStateValidatosChunkSize,
});

if (fakeKeys) {
Expand All @@ -61,10 +63,11 @@ export const startLoop = async (init = false) => {

let pastSlashedValidators;
if (pastSlashedValidatorPubKeys.length) {
const pastSlashedValidatorsRes = await eth.getStateValidators({
const pastSlashedValidatorsRes = await eth.getStateValidatorsChunked({
stateId: "head",
validatorIds: pastSlashedValidatorPubKeys.map((ky) => ky),
status: [],
chunkSize: getStateValidatosChunkSize,
});

pastSlashedValidators = pastSlashedValidatorsRes.data;
Expand Down
52 changes: 47 additions & 5 deletions src/services/eth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosInstance } from "axios";

type EthValidatorStatus =
export type EthValidatorStatus =
| "pending_initialized"
| "pending_queued"
| "active_ongoing"
Expand Down Expand Up @@ -51,15 +51,57 @@ class ETH {
validatorIds: string[];
status: EthValidatorStatus[];
}) {
const params: any = {};

if (validatorIds.length) params.id = validatorIds.join(",");

if (status.length) params.status = status.join(",");

const res = await this.agent.get<GetStateValidatorsResponse>(`/eth/v1/beacon/states/${stateId}/validators`, {
params: {
id: validatorIds.join(","),
status: status.join(","),
},
params: params,
});

return res.data;
}

async getStateValidatorsChunked({
stateId,
validatorIds,
status,
chunkSize,
}: {
stateId: string;
validatorIds: string[];
status: EthValidatorStatus[];
chunkSize: number;
}) {
const chunks = [];
let keys: string[] = [];
for (const key of validatorIds) {
keys.push(key);
if (keys.length == chunkSize) {
chunks.push(keys);
keys = [];
}
}
if (keys.length) chunks.push(keys);
let resultData: any[] = [];
let result: GetStateValidatorsResponse = {
execution_optimistic: false,
finalized: false,
data: [],
};
for (const chunk of chunks) {
result = await this.getStateValidators({
stateId: stateId,
validatorIds: chunk,
status: status,
});
resultData = resultData.concat(result.data);
}
result.data = resultData;
return result;
}
}

export default ETH;
4 changes: 3 additions & 1 deletion src/services/kapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ class KApi {
return res.data.data.operators;
}

async findOperatorKeys(operatorIndex: number) {
async findOperatorKeys(operatorIndex: number, used = false) {
const strUsed = used == true ? "true" : "false";
const res = await this.agent.get<GetOperatorKeysResponse>(`/v1/modules/1/operators/keys`, {
params: {
operatorIndex,
strUsed,
},
});

Expand Down

0 comments on commit 30ad770

Please sign in to comment.