diff --git a/packages/hardhat-zksync-node/package.json b/packages/hardhat-zksync-node/package.json index 1e13aac88..b4aab73a2 100644 --- a/packages/hardhat-zksync-node/package.json +++ b/packages/hardhat-zksync-node/package.json @@ -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", diff --git a/packages/hardhat-zksync-node/src/constants.ts b/packages/hardhat-zksync-node/src/constants.ts index df346153c..109786112 100644 --- a/packages/hardhat-zksync-node/src/constants.ts +++ b/packages/hardhat-zksync-node/src/constants.ts @@ -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', }; diff --git a/packages/hardhat-zksync-node/src/utils.ts b/packages/hardhat-zksync-node/src/utils.ts index e8258e44c..8e3ffbdea 100644 --- a/packages/hardhat-zksync-node/src/utils.ts +++ b/packages/hardhat-zksync-node/src/utils.ts @@ -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) { @@ -317,16 +316,20 @@ export async function isPortAvailable(port: number): Promise { } export async function waitForNodeToBeReady(port: number, maxAttempts: number = 20): Promise { - 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); @@ -334,15 +337,21 @@ export async function waitForNodeToBeReady(port: number, maxAttempts: number = 2 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 {