diff --git a/tests/anvil.ts b/tests/anvil.ts index 2d4ecd8..309bf76 100644 --- a/tests/anvil.ts +++ b/tests/anvil.ts @@ -1,11 +1,9 @@ import { spawnSync } from "child_process"; -import { networkRpcs } from "../types/constants"; import { RPCHandler } from "../types/rpc-handler"; class Anvil { rpcs: string[] = []; rpcHandler: RPCHandler | null = null; - availableRpcs = [...networkRpcs["100"].rpcs]; async init() { this.rpcHandler = new RPCHandler({ @@ -15,7 +13,7 @@ class Anvil { networkName: "gnosis", rpcTimeout: 1000, runtimeRpcs: null, - networkRpcs: this.availableRpcs, + networkRpcs: null, proxySettings: { logger: null, logTier: "ok", @@ -60,9 +58,7 @@ class Anvil { if (anvil.status !== 0) { console.log(`Anvil failed to start with RPC: ${rpc}`); console.log(`Retrying with next RPC...`); - this.availableRpcs.shift(); - await this.init(); - return this.spawner(this.rpcs[0]); + return this.spawner(this.rpcs.shift()); } return true; diff --git a/types/rpc-service.ts b/types/rpc-service.ts index 2cef52e..1ae8d47 100644 --- a/types/rpc-service.ts +++ b/types/rpc-service.ts @@ -57,32 +57,45 @@ export class RPCService { throw new Error(rpcUrl); } } - async function getFirstSuccessfulRequest(requests: string[]) { - if (requests.length === 0) { - throw new Error("All requests failed."); - } - const promisesToResolve = requests.map((rpcUrl) => requestEndpoint(rpcUrl)); + const promises = runtimeRpcs.map((rpcUrl) => requestEndpoint(rpcUrl)); + // async function getFirstSuccessfulRequest(requests: string[]) { + // if (requests.length === 0) { + // throw new Error("All requests failed."); + // } + // const promisesToResolve = requests.map((rpcUrl) => requestEndpoint(rpcUrl)); + // + // try { + // const res = await Promise.race(promisesToResolve); + // if (!res.success) { + // throw new Error(res.rpcUrl); + // } + // return res; + // } catch (err) { + // if (err instanceof Error && requests.includes(err.message)) { + // return getFirstSuccessfulRequest(requests.filter((request) => request !== err.message)); + // } + // return getFirstSuccessfulRequest(requests.slice(1)); + // } + // } + // const fastest = await getFirstSuccessfulRequest(runtimeRpcs); + // + // if (fastest.success) { + // latencies[`${networkId}__${fastest.rpcUrl}`] = fastest.duration; + // } - try { - const res = await Promise.race(promisesToResolve); - if (!res.success) { - throw new Error(res.rpcUrl); - } - return res; - } catch (err) { - if (err instanceof Error && requests.includes(err.message)) { - return getFirstSuccessfulRequest(requests.filter((request) => request !== err.message)); + const allResults = await Promise.allSettled(promises); + + allResults.forEach((result) => { + if (result.status === "fulfilled" && result.value.success) { + latencies[`${networkId}__${result.value.rpcUrl}`] = result.value.duration; + } else if (result.status === "fulfilled") { + const fulfilledResult = result.value; + const index = runtimeRpcs.indexOf(fulfilledResult.rpcUrl); + if (index > -1) { + runtimeRpcs.splice(index, 1); } - return getFirstSuccessfulRequest(requests.slice(1)); } - } - const fastest = await getFirstSuccessfulRequest(runtimeRpcs); - - if (fastest.success) { - latencies[`${networkId}__${fastest.rpcUrl}`] = fastest.duration; - runtimeRpcs = runtimeRpcs.filter((o) => o === fastest.rpcUrl); - } - + }); return { latencies, runtimeRpcs }; }