Skip to content

feat(entropy-tester): add retries #2855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/entropy-tester/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/entropy-tester",
"version": "1.1.0",
"version": "1.2.0",
"description": "Utility to test entropy provider callbacks",
"private": true,
"type": "module",
Expand Down
84 changes: 60 additions & 24 deletions apps/entropy-tester/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { z } from "zod";

const DEFAULT_RETRIES = 3;

type LoadedConfig = {
contract: EvmEntropyContract;
interval: number;
retries: number;
};

function timeToSeconds(timeStr: string): number {
Expand Down Expand Up @@ -46,6 +49,7 @@ async function loadConfig(configPath: string): Promise<LoadedConfig[]> {
"chain-id": z.string(),
interval: z.string(),
"rpc-endpoint": z.string().optional(),
retries: z.number().default(DEFAULT_RETRIES),
}),
);
const configContent = (await import(configPath, {
Expand Down Expand Up @@ -78,7 +82,7 @@ async function loadConfig(configPath: string): Promise<LoadedConfig[]> {
evmChain.networkId,
);
}
return { contract: firstContract, interval };
return { contract: firstContract, interval, retries: config.retries };
});
return loadedConfigs;
}
Expand Down Expand Up @@ -188,31 +192,63 @@ export const main = function () {
privateKeyFileContent.replace("0x", "").trimEnd(),
);
logger.info("Running");
const promises = configs.map(async ({ contract, interval }) => {
const child = logger.child({ chain: contract.chain.getId() });
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (true) {
try {
await Promise.race([
testLatency(contract, privateKey, child),
new Promise((_, reject) =>
setTimeout(() => {
reject(
new Error(
"Timeout: 120s passed but testLatency function was not resolved",
),
const promises = configs.map(
async ({ contract, interval, retries }) => {
const child = logger.child({ chain: contract.chain.getId() });
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (true) {
let lastError: Error | undefined;
let success = false;

for (let attempt = 1; attempt <= retries; attempt++) {
try {
await Promise.race([
testLatency(contract, privateKey, child),
new Promise((_, reject) =>
setTimeout(() => {
reject(
new Error(
"Timeout: 120s passed but testLatency function was not resolved",
),
);
}, 120_000),
),
]);
success = true;
break;
} catch (error) {
lastError = error as Error;
child.warn(
{ attempt, maxRetries: retries, error: error },
`Attempt ${attempt.toString()}/${retries.toString()} failed, ${attempt < retries ? "retrying..." : "all retries exhausted"}`,
);

if (attempt < retries) {
// Wait a bit before retrying (exponential backoff, max 10s)
const backoffDelay = Math.min(
2000 * Math.pow(2, attempt - 1),
10_000,
);
await new Promise((resolve) =>
setTimeout(resolve, backoffDelay),
);
}, 120_000),
),
]);
} catch (error) {
child.error(error, "Error testing latency");
}
}
}

if (!success && lastError) {
child.error(
{ error: lastError, retriesExhausted: retries },
"All retries exhausted, callback was not called.",
);
}

await new Promise((resolve) =>
setTimeout(resolve, interval * 1000),
);
}
await new Promise((resolve) =>
setTimeout(resolve, interval * 1000),
);
}
});
},
);
await Promise.all(promises);
},
)
Expand Down
Loading