From 9e584cfbed380087f088f435d865854a96ee1f87 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Sun, 1 Sep 2024 11:36:13 +0100 Subject: [PATCH] p-retry already does backoff --- packages/cli/src/deploy/ensureContract.ts | 7 +--- packages/cli/src/deploy/ensureFunctions.ts | 48 +++++++++------------- packages/cli/src/deploy/ensureModules.ts | 35 ++++++---------- packages/cli/src/deploy/ensureSystems.ts | 19 ++------- packages/cli/src/deploy/ensureTables.ts | 7 +--- 5 files changed, 36 insertions(+), 80 deletions(-) diff --git a/packages/cli/src/deploy/ensureContract.ts b/packages/cli/src/deploy/ensureContract.ts index e2e8259d5c..5a4cfd4925 100644 --- a/packages/cli/src/deploy/ensureContract.ts +++ b/packages/cli/src/deploy/ensureContract.ts @@ -4,7 +4,6 @@ import { contractSizeLimit, salt } from "./common"; import { sendTransaction } from "@latticexyz/common"; import { debug } from "./debug"; import pRetry from "p-retry"; -import { wait } from "@latticexyz/common/utils"; export type Contract = { bytecode: Hex; @@ -56,11 +55,7 @@ export async function ensureContract({ }), { retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to deploy ${debugLabel}, retrying in ${delay}ms...`); - await wait(delay); - }, + onFailedAttempt: () => debug(`failed to deploy ${debugLabel}, retrying...`), }, ), ]; diff --git a/packages/cli/src/deploy/ensureFunctions.ts b/packages/cli/src/deploy/ensureFunctions.ts index 3f29bd65ce..49c9edf102 100644 --- a/packages/cli/src/deploy/ensureFunctions.ts +++ b/packages/cli/src/deploy/ensureFunctions.ts @@ -4,7 +4,6 @@ import { getFunctions } from "@latticexyz/world/internal"; import { WorldDeploy, WorldFunction, worldAbi } from "./common"; import { debug } from "./debug"; import pRetry from "p-retry"; -import { wait } from "@latticexyz/common/utils"; export async function ensureFunctions({ client, @@ -46,44 +45,35 @@ export async function ensureFunctions({ return Promise.all( toAdd.map((func) => { const { namespace } = hexToResource(func.systemId); - if (namespace === "") { - return pRetry( - () => - writeContract(client, { - chain: client.chain ?? null, - address: worldDeploy.address, - abi: worldAbi, - // TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645) + + // TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645) + const params = + namespace === "" + ? ({ functionName: "registerRootFunctionSelector", - args: [func.systemId, func.systemFunctionSignature, func.systemFunctionSignature], - }), - { - retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to register function ${func.signature}, retrying in ${delay}ms...`); - await wait(delay); - }, - }, - ); - } + args: [ + func.systemId, + // use system function signature as world signature + func.systemFunctionSignature, + func.systemFunctionSignature, + ], + } as const) + : ({ + functionName: "registerFunctionSelector", + args: [func.systemId, func.systemFunctionSignature], + } as const); + return pRetry( () => writeContract(client, { chain: client.chain ?? null, address: worldDeploy.address, abi: worldAbi, - // TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645) - functionName: "registerFunctionSelector", - args: [func.systemId, func.systemFunctionSignature], + ...params, }), { retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to register function ${func.signature}, retrying in ${delay}ms...`); - await wait(delay); - }, + onFailedAttempt: () => debug(`failed to register function ${func.signature}, retrying...`), }, ); }), diff --git a/packages/cli/src/deploy/ensureModules.ts b/packages/cli/src/deploy/ensureModules.ts index 7b3fcc6dd0..49ed384147 100644 --- a/packages/cli/src/deploy/ensureModules.ts +++ b/packages/cli/src/deploy/ensureModules.ts @@ -2,7 +2,7 @@ import { Client, Transport, Chain, Account, Hex, BaseError } from "viem"; import { writeContract } from "@latticexyz/common"; import { Library, Module, WorldDeploy, worldAbi } from "./common"; import { debug } from "./debug"; -import { isDefined, wait } from "@latticexyz/common/utils"; +import { isDefined } from "@latticexyz/common/utils"; import pRetry from "p-retry"; import { ensureContractsDeployed } from "./ensureContractsDeployed"; @@ -41,23 +41,16 @@ export async function ensureModules({ // append module's ABI so that we can decode any custom errors const abi = [...worldAbi, ...mod.abi]; const moduleAddress = mod.prepareDeploy(deployerAddress, libraries).address; - return mod.installAsRoot - ? await writeContract(client, { - chain: client.chain ?? null, - address: worldDeploy.address, - abi, - // TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645) - functionName: "installRootModule", - args: [moduleAddress, mod.installData], - }) - : await writeContract(client, { - chain: client.chain ?? null, - address: worldDeploy.address, - abi, - // TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645) - functionName: "installModule", - args: [moduleAddress, mod.installData], - }); + // TODO: replace with batchCall (https://github.com/latticexyz/mud/issues/1645) + const params = mod.installAsRoot + ? ({ functionName: "installRootModule", args: [moduleAddress, mod.installData] } as const) + : ({ functionName: "installModule", args: [moduleAddress, mod.installData] } as const); + return writeContract(client, { + chain: client.chain ?? null, + address: worldDeploy.address, + abi, + ...params, + }); } catch (error) { if (error instanceof BaseError && error.message.includes("Module_AlreadyInstalled")) { debug(`module ${mod.name} already installed`); @@ -73,11 +66,7 @@ export async function ensureModules({ }, { retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to install module ${mod.name}, retrying in ${delay}ms...`); - await wait(delay); - }, + onFailedAttempt: () => debug(`failed to install module ${mod.name}, retrying...`), }, ), ), diff --git a/packages/cli/src/deploy/ensureSystems.ts b/packages/cli/src/deploy/ensureSystems.ts index 356af81b35..c018db1043 100644 --- a/packages/cli/src/deploy/ensureSystems.ts +++ b/packages/cli/src/deploy/ensureSystems.ts @@ -4,7 +4,6 @@ import { Library, System, WorldDeploy, worldAbi } from "./common"; import { debug } from "./debug"; import { getSystems } from "./getSystems"; import { getResourceAccess } from "./getResourceAccess"; -import { wait } from "@latticexyz/common/utils"; import pRetry from "p-retry"; import { ensureContractsDeployed } from "./ensureContractsDeployed"; @@ -87,11 +86,7 @@ export async function ensureSystems({ }), { retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to register system ${resourceToLabel(system)}, retrying in ${delay}ms...`); - await wait(delay); - }, + onFailedAttempt: () => debug(`failed to register system ${resourceToLabel(system)}, retrying...`), }, ), ), @@ -153,11 +148,7 @@ export async function ensureSystems({ }), { retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to revoke access, retrying in ${delay}ms...`); - await wait(delay); - }, + onFailedAttempt: () => debug("failed to revoke access, retrying..."), }, ), ), @@ -173,11 +164,7 @@ export async function ensureSystems({ }), { retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to grant access, retrying in ${delay}ms...`); - await wait(delay); - }, + onFailedAttempt: () => debug("failed to grant access, retrying..."), }, ), ), diff --git a/packages/cli/src/deploy/ensureTables.ts b/packages/cli/src/deploy/ensureTables.ts index 2a2e1d2153..6f2deac46b 100644 --- a/packages/cli/src/deploy/ensureTables.ts +++ b/packages/cli/src/deploy/ensureTables.ts @@ -13,7 +13,6 @@ import { import { debug } from "./debug"; import { getTables } from "./getTables"; import pRetry from "p-retry"; -import { wait } from "@latticexyz/common/utils"; import { Table } from "@latticexyz/config"; export async function ensureTables({ @@ -59,11 +58,7 @@ export async function ensureTables({ }), { retries: 3, - onFailedAttempt: async (error) => { - const delay = error.attemptNumber * 500; - debug(`failed to register table ${resourceToLabel(table)}, retrying in ${delay}ms...`); - await wait(delay); - }, + onFailedAttempt: () => debug(`failed to register table ${resourceToLabel(table)}, retrying...`), }, ); }),