diff --git a/.changeset/fifty-mirrors-raise.md b/.changeset/fifty-mirrors-raise.md new file mode 100644 index 000000000..deb02721e --- /dev/null +++ b/.changeset/fifty-mirrors-raise.md @@ -0,0 +1,5 @@ +--- +"@ponder/utils": patch +--- + +Improved eth_getLogs retry behavior for `https://mainnet.optimism.io`. diff --git a/packages/utils/src/_test/optimism.test.ts b/packages/utils/src/_test/optimism.test.ts index ef9f88b04..0345375c3 100644 --- a/packages/utils/src/_test/optimism.test.ts +++ b/packages/utils/src/_test/optimism.test.ts @@ -1,4 +1,4 @@ -import { HttpRequestError, numberToHex } from "viem"; +import { RpcRequestError, numberToHex } from "viem"; import { expect, test } from "vitest"; import { getLogsRetryHelper } from "../getLogsRetryHelper.js"; import { type Params, getRequest } from "./utils.js"; @@ -15,24 +15,25 @@ test( { address: "0x871f2F2ff935FD1eD867842FF2a7bfD051A5E527", fromBlock: numberToHex(fromBlock), - toBlock: numberToHex(fromBlock + 10_000n), + toBlock: numberToHex(fromBlock + 1_000n), }, ], }); - expect(logs).toHaveLength(8); + expect(logs).toHaveLength(0); }, { timeout: 15_000 }, ); +// Reported as block range but behaves inconsistently test( - "optimism response size", + "optimism block range", async () => { const params: Params = [ { address: "0x4200000000000000000000000000000000000006", fromBlock: numberToHex(fromBlock), - toBlock: numberToHex(fromBlock + 100_000n), + toBlock: numberToHex(fromBlock + 10_000n), }, ]; @@ -41,8 +42,8 @@ test( params, }).catch((error) => error); - expect(error).toBeInstanceOf(HttpRequestError); - expect(JSON.stringify(error)).includes("backend response too large"); + expect(error).toBeInstanceOf(RpcRequestError); + expect(JSON.stringify(error)).includes("Block range is too large"); const retry = getLogsRetryHelper({ params, @@ -53,7 +54,7 @@ test( expect(retry.ranges).toHaveLength(2); expect(retry.ranges![0]).toStrictEqual({ fromBlock: numberToHex(fromBlock), - toBlock: numberToHex(fromBlock + 50_000n), + toBlock: numberToHex(fromBlock + 5_000n), }); }, { timeout: 15_000 }, diff --git a/packages/utils/src/getLogsRetryHelper.ts b/packages/utils/src/getLogsRetryHelper.ts index d07aec9a9..190951733 100644 --- a/packages/utils/src/getLogsRetryHelper.ts +++ b/packages/utils/src/getLogsRetryHelper.ts @@ -238,6 +238,27 @@ export const getLogsRetryHelper = ({ } as const; } + // optimism (new as of 11/25/24) + match = sError.match(/Block range is too large/); + if (match !== null) { + const ranges = chunk({ + params, + range: + (hexToBigInt(params[0].toBlock) - hexToBigInt(params[0].fromBlock)) / + 2n, + }); + + if (isRangeUnchanged(params, ranges)) { + return { shouldRetry: false } as const; + } + + return { + shouldRetry: true, + ranges, + isSuggestedRange: false, + } as const; + } + // base match = sError.match(/block range too large/); if (match !== null) {