Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from jchen86/lodestar-multiple-keys
Browse files Browse the repository at this point in the history
Fix multiple issues that causes app to crash
  • Loading branch information
jimmygchen authored Jun 2, 2022
2 parents 1eb4d14 + 8fe7fdb commit fc1c2f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/beacon-api-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import qs from 'qs';

class BeaconAPIClient {
constructor(beaconAPIs) {
Expand All @@ -9,13 +10,20 @@ class BeaconAPIClient {
}

async getValidators(stateId, pubKeys) {
const { data } = await this.queryEndpoint(`/eth/v1/beacon/states/${stateId}/validators?id=${pubKeys.join(',')}`)
const options = {
params: { id: pubKeys },
paramsSerializer: (params) => {
return qs.stringify(params, { arrayFormat: 'repeat' })
}
};

const { data } = await this.queryEndpoint(`/eth/v1/beacon/states/${stateId}/validators`, options)
return data;
}

async queryEndpoint(url) {
async queryEndpoint(url, options = {}) {
try {
const data = await this.http.get(url)
const data = await this.http.get(url, options)
return data
} catch (err) {
console.log(`Request to ${url} failed with error ${err.message}`)
Expand Down
24 changes: 23 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
export const validatorShortName = (pubkey) => pubkey.slice(2,9);
export const validatorShortName = (pubkey) => pubkey.slice(2, 9);

const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))

export async function withRetry(fn, options = {}) {
const { maxAttempts = 5, interval = 3000 } = options;
let attempts = 0;

while (attempts < maxAttempts) {
try {
return await fn();
} catch (err) {
attempts++;
console.warn(`${attempts} of ${maxAttempts} attempts failed with error: ${err}`);

if (attempts == maxAttempts) {
throw new Error(`Failed after ${attempts} attempts: ${err}`);
}

await sleep(interval);
}
}
}
20 changes: 12 additions & 8 deletions src/validator-polling-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const SECONDS_PER_SLOT = 12;
const SLOTS_PER_EPOCH = 32;
const SECONDS_PER_EPOCH = SECONDS_PER_SLOT * SLOTS_PER_EPOCH;

import { validatorShortName } from './utils.js';
import { validatorShortName, withRetry } from './utils.js';

class ValidatorPollingService {
#pollingIntervalSeconds;
Expand Down Expand Up @@ -31,7 +31,7 @@ class ValidatorPollingService {
if (!allValid) throw new Error(`Failed to add validator key(s)`)

this.#validatorPubKeys = this.#validatorPubKeys.concat(newPubKeys);

console.log(`Validator(s) added: ${newPubKeys.map(k => validatorShortName(k)).join(',')}`)
}

Expand Down Expand Up @@ -67,13 +67,17 @@ class ValidatorPollingService {
this.#genesisTime = await this.#beaconApiClient.getGenesisTime();
}

const currentSlot = Math.floor((new Date().getTime() / 1000 - this.#genesisTime) / SECONDS_PER_SLOT);
const previousEpochSlot = currentSlot - SLOTS_PER_EPOCH;
const queryValidatorStates = () => {
const currentSlot = Math.floor((new Date().getTime() / 1000 - this.#genesisTime) / SECONDS_PER_SLOT) - 1;
const previousEpochSlot = currentSlot - SLOTS_PER_EPOCH;

return Promise.all([
this.#beaconApiClient.getValidators(currentSlot, this.#validatorPubKeys),
this.#beaconApiClient.getValidators(previousEpochSlot, this.#validatorPubKeys),
]);
};

const [currentEpochData, previousEpochData] = await Promise.all([
this.#beaconApiClient.getValidators(currentSlot, this.#validatorPubKeys),
this.#beaconApiClient.getValidators(previousEpochSlot, this.#validatorPubKeys),
]);
const [currentEpochData, previousEpochData] = await withRetry(queryValidatorStates, { interval: SECONDS_PER_EPOCH });

const validatorStates = mergeValidatorData(currentEpochData.data, previousEpochData.data)
this.#listeners.forEach(listener => listener(validatorStates));
Expand Down

0 comments on commit fc1c2f0

Please sign in to comment.