Skip to content

Commit

Permalink
retry eth_getLogs for publicnode (#1110)
Browse files Browse the repository at this point in the history
* Add retry for publicnode

* chore: changeset

* cleanup

---------

Co-authored-by: Kyle Scott <[email protected]>
  • Loading branch information
chenxsan and kyscott18 authored Sep 19, 2024
1 parent e2b5496 commit 6a5c8a5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tricky-tigers-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ponder/utils": patch
---

Added retry mechanism for publicnode.
2 changes: 1 addition & 1 deletion packages/utils/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ RPC_URL_QUICKNODE_1=...
RPC_URL_CHAINSTACK_1=...

# Coinbase url
RPC_URL_COINBASE_8453=...
RPC_URL_COINBASE_8453=...
58 changes: 58 additions & 0 deletions packages/utils/src/_test/publicnode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { numberToHex } from "viem";
import { expect, test } from "vitest";
import { getLogsRetryHelper } from "../getLogsRetryHelper.js";
import { type Params, UNI, fromBlock, getRequest } from "./utils.js";

const request = getRequest("https://ethereum-rpc.publicnode.com");
const maxBlockRange = 50_000n;

test("publicnode success", async () => {
const logs = await request({
method: "eth_getLogs",
params: [
{
address: UNI,
fromBlock: numberToHex(fromBlock),
toBlock: numberToHex(fromBlock + maxBlockRange),
},
],
});

expect(logs).toHaveLength(203);
});

test("publicnode block range", async () => {
const params: Params = [
{
fromBlock: numberToHex(fromBlock),
toBlock: numberToHex(fromBlock + maxBlockRange + 1n),
},
];

const error = await request({
method: "eth_getLogs",
params,
}).catch((error) => error);

expect(JSON.stringify(error)).includes("exceed maximum block range: 50000");

const retry = getLogsRetryHelper({
params,
error: error,
});

expect(retry).toStrictEqual({
isSuggestedRange: true,
shouldRetry: true,
ranges: [
{
fromBlock: numberToHex(fromBlock),
toBlock: numberToHex(fromBlock + maxBlockRange),
},
{
fromBlock: numberToHex(fromBlock + maxBlockRange + 1n),
toBlock: numberToHex(fromBlock + maxBlockRange + 1n),
},
],
});
});
19 changes: 19 additions & 0 deletions packages/utils/src/getLogsRetryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,25 @@ export const getLogsRetryHelper = ({
} as const;
}

// publicnode
match = sError.match(/maximum block range: ([\d,.]+)/);
if (match !== null) {
const ranges = chunk({
params,
range: BigInt(match[1]!.replace(/[,.]/g, "")),
});

if (isRangeUnchanged(params, ranges)) {
return { shouldRetry: false } as const;
}

return {
shouldRetry: true,
ranges,
isSuggestedRange: true,
} as const;
}

// No match found
return {
shouldRetry: false,
Expand Down

0 comments on commit 6a5c8a5

Please sign in to comment.