diff --git a/.changeset/five-pots-switch.md b/.changeset/five-pots-switch.md new file mode 100644 index 0000000000..a2551cbaa8 --- /dev/null +++ b/.changeset/five-pots-switch.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/cli": patch +--- + +Deploying now retries on "block is out of range" errors, for cases where the RPC is load balanced and out of sync. diff --git a/packages/cli/src/deploy/getResourceIds.ts b/packages/cli/src/deploy/getResourceIds.ts index 85f6481317..8798b3018a 100644 --- a/packages/cli/src/deploy/getResourceIds.ts +++ b/packages/cli/src/deploy/getResourceIds.ts @@ -1,8 +1,9 @@ -import { Client, parseAbiItem, Hex } from "viem"; +import { Client, parseAbiItem, Hex, HttpRequestError } from "viem"; import { getLogs } from "viem/actions"; import { storeSpliceStaticDataEvent } from "@latticexyz/store"; import { WorldDeploy, storeTables } from "./common"; import { debug } from "./debug"; +import pRetry from "p-retry"; export async function getResourceIds({ client, @@ -15,15 +16,30 @@ export async function getResourceIds({ // TODO: PR to viem's getLogs to accept topics array so we can filter on all store events and quickly recreate this table's current state debug("looking up resource IDs for", worldDeploy.address); - const logs = await getLogs(client, { - strict: true, - address: worldDeploy.address, - fromBlock: worldDeploy.deployBlock, - toBlock: worldDeploy.stateBlock, - event: parseAbiItem(storeSpliceStaticDataEvent), - args: { tableId: storeTables.store_ResourceIds.tableId }, - }); + const logs = await pRetry( + () => + getLogs(client, { + strict: true, + address: worldDeploy.address, + fromBlock: worldDeploy.deployBlock, + toBlock: worldDeploy.stateBlock, + event: parseAbiItem(storeSpliceStaticDataEvent), + args: { tableId: storeTables.store_ResourceIds.tableId }, + }), + { + retries: 3, + onFailedAttempt: async (error) => { + const shouldRetry = + error instanceof HttpRequestError && + error.status === 400 && + error.message.includes('block is out of range'); + if (!shouldRetry) { + throw error; + } + }, + }, + ); const resourceIds = logs.map((log) => log.args.keyTuple[0]); debug("found", resourceIds.length, "resource IDs for", worldDeploy.address);