Skip to content

Commit

Permalink
fix(block-logs-stream): handle proxyd errors (#2726)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
Kooshaba and holic authored Apr 25, 2024
1 parent 189050b commit bf16e72
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-cycles-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/block-logs-stream": patch
---

Added detection and handling for proxyd rate limit and block range errors.
27 changes: 22 additions & 5 deletions packages/block-logs-stream/src/fetchLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ export type FetchLogsResult<TAbiEvents extends readonly AbiEvent[]> = {
logs: GetLogsReturnType<undefined, TAbiEvents, true, BlockNumber, BlockNumber>;
};

const RATE_LIMIT_ERRORS = [
"rate limit exceeded",
// https://github.com/ethereum-optimism/optimism/blob/4fb534ab3d924ac87383e1e70ae4872340d68d9d/proxyd/backend.go#L83
"over rate limit",
// https://github.com/ethereum-optimism/optimism/blob/4fb534ab3d924ac87383e1e70ae4872340d68d9d/proxyd/backend.go#L88
"sender is over rate limit",
];

const BLOCK_RANGE_TOO_LARGE_ERRORS = [
"block range exceeded",
// https://github.com/ethereum-optimism/optimism/blob/4fb534ab3d924ac87383e1e70ae4872340d68d9d/proxyd/backend.go#L110
"backend response too large",
// https://github.com/ethereum-optimism/optimism/blob/4fb534ab3d924ac87383e1e70ae4872340d68d9d/proxyd/rewriter.go#L36
"block range is too large",
// https://github.com/ethereum-optimism/optimism/blob/4fb534ab3d924ac87383e1e70ae4872340d68d9d/proxyd/backend.go#L98
// https://github.com/ethereum-optimism/optimism/blob/4fb534ab3d924ac87383e1e70ae4872340d68d9d/proxyd/rewriter.go#L35
"block is out of range",
];

/**
* An asynchronous generator function that fetches logs from the blockchain in a range of blocks.
*
Expand Down Expand Up @@ -79,22 +98,20 @@ export async function* fetchLogs<TAbiEvents extends readonly AbiEvent[]>({
debug("error getting logs:", String(error));
if (!(error instanceof Error)) throw error;

// TODO: figure out actual rate limit message for RPCs
if (error.message.includes("rate limit exceeded") && retryCount < maxRetryCount) {
if (retryCount < maxRetryCount && RATE_LIMIT_ERRORS.some((e) => error.message.includes(e))) {
const seconds = 2 * retryCount;
debug(`too many requests, retrying in ${seconds}s`, error);
await wait(1000 * seconds);
retryCount += 1;
continue;
}

// TODO: figure out actual block range exceeded message for RPCs
if (error.message.includes("block range exceeded")) {
if (BLOCK_RANGE_TOO_LARGE_ERRORS.some((e) => error.message.includes(e))) {
blockRange /= 2n;
if (blockRange <= 0n) {
throw new Error("can't reduce block range any further");
}
debug("block range exceeded, trying a smaller block range", error);
debug("block range exceeded or too many logs in range, trying a smaller block range", error);
// TODO: adjust maxBlockRange down if we consistently hit this for a given block range size
continue;
}
Expand Down

0 comments on commit bf16e72

Please sign in to comment.