diff --git a/src/functions/start.ts b/src/functions/start.ts index ba969b6..5cb51cd 100644 --- a/src/functions/start.ts +++ b/src/functions/start.ts @@ -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 { @@ -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) { @@ -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; diff --git a/src/services/eth.ts b/src/services/eth.ts index ddf5977..096b6ca 100644 --- a/src/services/eth.ts +++ b/src/services/eth.ts @@ -1,6 +1,6 @@ import axios, { AxiosInstance } from "axios"; -type EthValidatorStatus = +export type EthValidatorStatus = | "pending_initialized" | "pending_queued" | "active_ongoing" @@ -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(`/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; diff --git a/src/services/kapi.ts b/src/services/kapi.ts index e1d4b84..9cf963d 100644 --- a/src/services/kapi.ts +++ b/src/services/kapi.ts @@ -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(`/v1/modules/1/operators/keys`, { params: { operatorIndex, + strUsed, }, });