Skip to content

Commit

Permalink
chore: test all settled
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed Aug 19, 2024
1 parent c576f7b commit d52e314
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
8 changes: 2 additions & 6 deletions tests/anvil.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -15,7 +13,7 @@ class Anvil {
networkName: "gnosis",
rpcTimeout: 1000,
runtimeRpcs: null,
networkRpcs: this.availableRpcs,
networkRpcs: null,
proxySettings: {
logger: null,
logTier: "ok",
Expand Down Expand Up @@ -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;
Expand Down
59 changes: 36 additions & 23 deletions types/rpc-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down

0 comments on commit d52e314

Please sign in to comment.