-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve(retryProvider): Avoid retry on eth_call reverts #757
Open
pxrl
wants to merge
1
commit into
master
Choose a base branch
from
pxrl/revert
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { getOriginFromURL } from "../utils/NetworkUtils"; | |
import { CacheProvider } from "./cachedProvider"; | ||
import { compareRpcResults, createSendErrorWithMessage, formatProviderError } from "./utils"; | ||
import { PROVIDER_CACHE_TTL } from "./constants"; | ||
import { JsonRpcError } from "./types"; | ||
import { Logger } from "winston"; | ||
|
||
export class RetryProvider extends ethers.providers.StaticJsonRpcProvider { | ||
|
@@ -88,9 +89,27 @@ export class RetryProvider extends ethers.providers.StaticJsonRpcProvider { | |
): Promise<[ethers.providers.StaticJsonRpcProvider, unknown]> => { | ||
return this._trySend(provider, method, params) | ||
.then((result): [ethers.providers.StaticJsonRpcProvider, unknown] => [provider, result]) | ||
.catch((err) => { | ||
.catch((err: unknown) => { | ||
if (JsonRpcError.is(err)) { | ||
const { | ||
error: { code, message }, | ||
} = err; | ||
// [-32768, -32100] is reserved by the JSON-RPC spec. | ||
// [-32099, -32000] is allocated for implementation-defined responses. | ||
// Everything else is available for use by the application space. | ||
// Most node implementations return 3 for an eth_call revert, but some return -32000. | ||
// See also https://www.jsonrpc.org/specification | ||
if (code < -32768 || code > -32100) { | ||
if (method === "eth_call" && message.toLowerCase().includes("revert")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could maybe also be conditional on |
||
// Reverts will probably be the same across all providers, so don't waste time rotating. | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
// Append the provider and error to the error array. | ||
errors.push([provider, err?.stack || err?.toString()]); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
errors.push([provider, (err as any)?.stack || err?.toString()]); | ||
|
||
// If there are no new fallback providers to use, terminate the recursion by throwing an error. | ||
// Otherwise, we can try to call another provider. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
import { object, number, string } from "superstruct"; | ||
|
||
export type RPCProvider = "ALCHEMY" | "DRPC" | "INFURA" | "INFURA_DIN"; | ||
export type RPCTransport = "https" | "wss"; | ||
|
||
export const JsonRpcError = object({ | ||
error: object({ | ||
code: number(), | ||
message: string(), | ||
// data is optional and has no reliable type, so skip it. | ||
}), | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still todo after discussion w/ @mrice32: update
_trySend()
such that it can identify a revert on an eth_call and short-circuit the retry loop that is inevitably occurring. This would shave off at least 3+ seconds per call.