Skip to content

Commit

Permalink
feat: improve validator fetch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Nov 20, 2024
1 parent d6af0c6 commit 40dcf58
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ export async function launchNetwork() {
network$.peerCount = peerCount;
});

let lastValidatorRequest = { epoch: Infinity, ts: 0 };
async function updateValidators() {
const now = Date.now();
if (currentEpoch === lastValidatorRequest.epoch || now - lastValidatorRequest.ts < 60000) return;
lastValidatorRequest = { epoch: currentEpoch, ts: now };

await client.waitForConsensusEstablished();
const contract = (await retry(
() => client.getAccount(STAKING_CONTRACT_ADDRESS) as Promise<PlainStakingContract>,
Expand Down Expand Up @@ -251,8 +256,9 @@ export async function launchNetwork() {
};

const { config } = useConfig();
const apiValidators = await fetch(config.staking.validatorsEndpoint)
.then((res) => res.json()).catch(() => []) as ApiValidator[];
const apiValidators = await retry<ApiValidator[]>(
() => fetch(config.staking.validatorsEndpoint).then((res) => res.json()).catch(() => []), 1000, 3,
);
// TODO: Make it work even in the case this request fails
const validatorData: Record<string, ApiValidator> = {};
for (const apiValidator of apiValidators) {
Expand Down

0 comments on commit 40dcf58

Please sign in to comment.