Skip to content

Commit

Permalink
add coinbase rpc to getLogsRetryRanges (#1065)
Browse files Browse the repository at this point in the history
* add coinbase rpc to getLogsRetryRanges

* chore: changeset
  • Loading branch information
kyscott18 authored Sep 4, 2024
1 parent a1557d9 commit 7c17ff3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/light-weeks-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ponder/utils": patch
---

Added retry range detection for coinbase rpcs.
5 changes: 4 additions & 1 deletion packages/utils/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ RPC_URL_INFURA_1=...
RPC_URL_QUICKNODE_1=...

# Mainnet chainstack url
RPC_URL_CHAINSTACK_1=...
RPC_URL_CHAINSTACK_1=...

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

const request = getRequest(process.env.RPC_URL_COINBASE_8453!);
const fromBlock = 10_000_000n;
const maxBlockRange = 999n;

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

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

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

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

expect(error).toBeInstanceOf(LimitExceededRpcError);
expect(JSON.stringify(error)).includes(
"please limit the query to at most 1000 blocks",
);

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

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

// coinbase
match = sError.match(/please limit the query to at most ([\d,.]+) blocks/);
if (match !== null) {
const ranges = chunk({
params,
range: BigInt(match[1]!.replace(/[,.]/g, "")) - 1n,
});

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

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

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

0 comments on commit 7c17ff3

Please sign in to comment.