Skip to content

Commit

Permalink
p-retry already does backoff
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Sep 1, 2024
1 parent 2d48272 commit 9e584cf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 80 deletions.
7 changes: 1 addition & 6 deletions packages/cli/src/deploy/ensureContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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...`),
},
),
];
Expand Down
48 changes: 19 additions & 29 deletions packages/cli/src/deploy/ensureFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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...`),
},
);
}),
Expand Down
35 changes: 12 additions & 23 deletions packages/cli/src/deploy/ensureModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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`);
Expand All @@ -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...`),
},
),
),
Expand Down
19 changes: 3 additions & 16 deletions packages/cli/src/deploy/ensureSystems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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...`),
},
),
),
Expand Down Expand Up @@ -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..."),
},
),
),
Expand All @@ -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..."),
},
),
),
Expand Down
7 changes: 1 addition & 6 deletions packages/cli/src/deploy/ensureTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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...`),
},
);
}),
Expand Down

0 comments on commit 9e584cf

Please sign in to comment.