Skip to content

Commit

Permalink
fix: log range error for optimism public RPC (#1284)
Browse files Browse the repository at this point in the history
* fix: new optimism error message

* chore: changeset
  • Loading branch information
typedarray authored Nov 26, 2024
1 parent 38ffc03 commit 0bb76fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-mirrors-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ponder/utils": patch
---

Improved eth_getLogs retry behavior for `https://mainnet.optimism.io`.
17 changes: 9 additions & 8 deletions packages/utils/src/_test/optimism.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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),
},
];

Expand All @@ -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,
Expand All @@ -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 },
Expand Down
21 changes: 21 additions & 0 deletions packages/utils/src/getLogsRetryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0bb76fd

Please sign in to comment.