Skip to content

Commit

Permalink
Add exponential backoff to wait for node
Browse files Browse the repository at this point in the history
  • Loading branch information
Mimi TxFusion committed Oct 13, 2023
1 parent c4544f3 commit d6ccc16
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/hardhat-zksync-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matterlabs/hardhat-zksync-node",
"version": "0.0.1-beta.2",
"version": "0.0.1-beta.3",
"description": "Hardhat plugin to run zkSync era-test-node locally",
"repository": "github:matter-labs/hardhat-zksync",
"homepage": "https://github.com/matter-labs/hardhat-zksync/tree/main/packages/hardhat-zksync-node",
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-zksync-node/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const PORT_CHECK_DELAY = 500;
export const RPC_ENDPOINT_PATH = 'eth_chainId';

export const ZKSYNC_ERA_TEST_NODE_NETWORK_NAME = 'zkSyncEraTestNode';
export const BASE_URL = `http://localhost`;
export const BASE_URL = `http://127.0.0.1`;
export const NETWORK_ACCOUNTS = {
REMOTE: 'remote',
};
Expand Down
23 changes: 16 additions & 7 deletions packages/hardhat-zksync-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ export async function getLatestRelease(owner: string, repo: string, userAgent: s
if (error.response) {
// The request was made and the server responded with a status code outside of the range of 2xx
throw new ZkSyncNodePluginError(
`Failed to get latest release for ${owner}/${repo}. Status: ${
error.response.status
`Failed to get latest release for ${owner}/${repo}. Status: ${error.response.status
}, Data: ${JSON.stringify(error.response.data)}`
);
} else if (error.request) {
Expand Down Expand Up @@ -317,32 +316,42 @@ export async function isPortAvailable(port: number): Promise<boolean> {
}

export async function waitForNodeToBeReady(port: number, maxAttempts: number = 20): Promise<void> {
const rpcEndpoint = `http://localhost:${port}`;
const rpcEndpoint = `http://127.0.0.1:${port}`;

const payload = {
jsonrpc: '2.0',
method: 'eth_chainId',
params: [],
id: new Date().getTime(), // Unique ID for the request
id: new Date().getTime(),
};

let attempts = 0;
let waitTime = 1000; // Initial wait time in milliseconds
const backoffFactor = 2;
const maxWaitTime = 30000; // Maximum wait time (e.g., 30 seconds)

while (attempts < maxAttempts) {
try {
const response = await axios.post(rpcEndpoint, payload);

if (response.data && response.data.result) {
return; // The node responded with a valid chain ID
}
} catch (e) {
} catch (e: any) {
// console.error(`Attempt ${attempts + 1} failed with error:`, e.message);
// If it fails, it will just try again
}

attempts++;
await new Promise((r) => setTimeout(r, 1000)); // Wait for 1000ms before the next attempt.

// Wait before the next attempt
await new Promise((r) => setTimeout(r, waitTime));

// Update the wait time for the next attempt
waitTime = Math.min(waitTime * backoffFactor, maxWaitTime);
}

throw new ZkSyncNodePluginError("Server didn't respond after multiple attempts");
throw new Error("Server didn't respond after multiple attempts");
}

export async function getAvailablePort(startPort: number, maxAttempts: number): Promise<number> {
Expand Down

0 comments on commit d6ccc16

Please sign in to comment.