Skip to content

Commit

Permalink
fix(cli): add retry to getLogs when getting resource ID's (#2709)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
yonadaaa and holic authored Apr 23, 2024
1 parent 2c9b16c commit dbc7e06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-pots-switch.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 25 additions & 9 deletions packages/cli/src/deploy/getResourceIds.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);

Expand Down

0 comments on commit dbc7e06

Please sign in to comment.