Skip to content

Commit

Permalink
check rpcs in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
seleniumforest committed Nov 21, 2022
1 parent 9400837 commit ed77104
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
34 changes: 15 additions & 19 deletions src/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ const getChainData = async (registryName) => {
};

const filterAliveRestRpcs = async (rpcs) => {
let aliveRestRpcs = [];

for (let rpc of rpcs) {
let alive = await Promise.allSettled(rpcs.map(async rpc => {
try {
let response = await axios({
method: "GET",
Expand All @@ -74,27 +72,26 @@ const filterAliveRestRpcs = async (rpcs) => {
});

if (!response || response.status !== 200)
continue;
return;

let blockTime = Date.parse(response.data.block.header.time);
let blockTime = Date.parse(response?.data?.block?.header?.time);
let now = Date.now();

if (Math.abs(now - blockTime) < oldBlockMs) {
aliveRestRpcs.push(rpc);
continue;
//console.log(`${rpc.address} is alive, sync block ${response?.data?.block?.header?.height}`);
return rpc;
}

//console.log(`${rpc.address} is alive, but not synced`);
} catch (err) {
//console.log(`${rpc.address} is dead. Error ${err?.message}`)
}
}
}))

return aliveRestRpcs;
return alive.map(x => x.value).filter(x => !!x);
}

const filterAliveRpcs = async (rpcs) => {
let aliveRpcs = [];

for (let rpc of rpcs) {
let alive = await Promise.allSettled(rpcs.map(async rpc => {
try {
let response = await axios({
method: "GET",
Expand All @@ -103,23 +100,22 @@ const filterAliveRpcs = async (rpcs) => {
});

if (!response || response.status !== 200)
continue;
return;

let blockTime = Date.parse(response?.data?.result?.sync_info?.latest_block_time);
let now = Date.now();
if (Math.abs(now - blockTime) < oldBlockMs) {
//console.log(`${rpc.address} is alive, sync block ${response.data.result.sync_info.latest_block_height}`);
aliveRpcs.push(rpc);
continue;
return rpc;
}

//console.log(`${rpc.address} is alive, but not synced`);
} catch (err) {
//console.log(`${rpc.address} is dead. Error ${err?.message}`)
//console.log(`${rpc.address} is dead. Error ${err?.message}`)
}
}
}));

return aliveRpcs;
return alive.map(x => x.value).filter(x => !!x);
}

const reportStats = (networkName, rpcAddress, rpcType, result) => {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Registry } = require("@cosmjs/proto-signing");
const { defaultRegistryTypes } = require("@cosmjs/stargate");
const big = require("big.js");
const osmojs = require("osmojs");
const { getValidatorProfiles, getValidatorInfo } = require("./requests");
const { getValidatorInfo } = require("./requests");
const sifchainDecoder = require("../chain-specific/sifchain/tx");
const { getValidatorByAddress, saveValidator } = require("./db");

Expand Down
2 changes: 1 addition & 1 deletion src/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const getTxsInBlock = async (networkName, height) => {
const pollForLatestHeight = async (networkName) => {
let endpoints = getEndpoints(networkName, "rpc");

let results = await Promise.all(endpoints.map(async (e) => {
let results = await Promise.allSettled(endpoints.map(async (e) => {
let url = `${e.address}/status`
let { data } = await axios({
method: "GET",
Expand Down

0 comments on commit ed77104

Please sign in to comment.