Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): add retry to getLogs when getting resource ID's #2709

Merged
merged 6 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could check the error to see if it contains "block is out of range" and only retry those? but I am happy just retrying all here since its a relatively safe operation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I also like that it makes the retry logic more self-explanatory

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
Loading