-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Catch newly raised geth errors when tx indexing in progress #3216
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
Merged
fselmo
merged 3 commits into
ethereum:main
from
fselmo:handle-geth-indexing-waiting-for-tx
Feb 2, 2024
Merged
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 hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Handle new geth errors related to waiting for a transaction receipt while transactions are still being indexed. |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -45,21 +45,51 @@ def test_my_w3(w3, request_mocker): | |
|
||
assert w3.eth.block_number == 0 | ||
|
||
``mock_results`` is a dict mapping method names to the desired "result" object of | ||
the RPC response. ``mock_errors`` is a dict mapping method names to the desired | ||
"error" object of the RPC response. If a method name is not in either dict, | ||
the request is made as usual. | ||
Example with async and a mocked response object: | ||
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. 💯 Thanks for expanding this with a nice example! |
||
|
||
async def test_my_w3(async_w3, request_mocker): | ||
def _iter_responses(): | ||
while True: | ||
yield {"error": {"message": "transaction indexing in progress"}} | ||
yield {"error": {"message": "transaction indexing in progress"}} | ||
yield {"result": {"status": "0x1"}} | ||
|
||
iter_responses = _iter_responses() | ||
|
||
async with request_mocker( | ||
async_w3, | ||
mock_responses={ | ||
"eth_getTransactionReceipt": lambda *_: next(iter_responses) | ||
}, | ||
): | ||
# assert that the first two error responses are handled and the result | ||
# is eventually returned when present | ||
assert await w3.eth.get_transaction_receipt("0x1") == "0x1" | ||
|
||
|
||
- ``mock_results`` is a dict mapping method names to the desired "result" object of | ||
the RPC response. | ||
- ``mock_errors`` is a dict mapping method names to the desired | ||
"error" object of the RPC response. | ||
-``mock_responses`` is a dict mapping method names to the entire RPC response | ||
object. This can be useful if you wish to return an iterator which returns | ||
different responses on each call to the method. | ||
|
||
If a method name is not present in any of the dicts above, the request is made as | ||
usual. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
w3: Union["AsyncWeb3", "Web3"], | ||
mock_results: Dict[Union["RPCEndpoint", str], Any] = None, | ||
mock_errors: Dict[Union["RPCEndpoint", str], Any] = None, | ||
mock_responses: Dict[Union["RPCEndpoint", str], Any] = None, | ||
): | ||
self.w3 = w3 | ||
self.mock_results = mock_results or {} | ||
self.mock_errors = mock_errors or {} | ||
self.mock_responses = mock_responses or {} | ||
self._make_request: Union["AsyncMakeRequestFn", "MakeRequestFn"] = ( | ||
w3.provider.make_request | ||
) | ||
|
@@ -83,7 +113,10 @@ def _mock_request_handler( | |
self.w3 = cast("Web3", self.w3) | ||
self._make_request = cast("MakeRequestFn", self._make_request) | ||
|
||
if method not in self.mock_errors and method not in self.mock_results: | ||
if all( | ||
method not in mock_dict | ||
for mock_dict in (self.mock_errors, self.mock_results, self.mock_responses) | ||
): | ||
return self._make_request(method, params) | ||
|
||
request_id = ( | ||
|
@@ -93,7 +126,18 @@ def _mock_request_handler( | |
) | ||
response_dict = {"jsonrpc": "2.0", "id": request_id} | ||
|
||
if method in self.mock_results: | ||
if method in self.mock_responses: | ||
mock_return = self.mock_responses[method] | ||
if callable(mock_return): | ||
mock_return = mock_return(method, params) | ||
|
||
if "result" in mock_return: | ||
mock_return = {"result": mock_return["result"]} | ||
elif "error" in mock_return: | ||
mock_return = self._create_error_object(mock_return["error"]) | ||
|
||
mocked_response = merge(response_dict, mock_return) | ||
elif method in self.mock_results: | ||
mock_return = self.mock_results[method] | ||
if callable(mock_return): | ||
mock_return = mock_return(method, params) | ||
|
@@ -102,12 +146,7 @@ def _mock_request_handler( | |
error = self.mock_errors[method] | ||
if callable(error): | ||
error = error(method, params) | ||
code = error.get("code", -32000) | ||
message = error.get("message", "Mocked error") | ||
mocked_response = merge( | ||
response_dict, | ||
{"error": merge({"code": code, "message": message}, error)}, | ||
) | ||
mocked_response = merge(response_dict, self._create_error_object(error)) | ||
else: | ||
raise Exception("Invariant: unreachable code path") | ||
|
||
|
@@ -140,7 +179,10 @@ async def _async_mock_request_handler( | |
self.w3 = cast("AsyncWeb3", self.w3) | ||
self._make_request = cast("AsyncMakeRequestFn", self._make_request) | ||
|
||
if method not in self.mock_errors and method not in self.mock_results: | ||
if all( | ||
method not in mock_dict | ||
for mock_dict in (self.mock_errors, self.mock_results, self.mock_responses) | ||
): | ||
return await self._make_request(method, params) | ||
|
||
request_id = ( | ||
|
@@ -150,7 +192,22 @@ async def _async_mock_request_handler( | |
) | ||
response_dict = {"jsonrpc": "2.0", "id": request_id} | ||
|
||
if method in self.mock_results: | ||
if method in self.mock_responses: | ||
mock_return = self.mock_responses[method] | ||
|
||
if callable(mock_return): | ||
mock_return = mock_return(method, params) | ||
elif iscoroutinefunction(mock_return): | ||
# this is the "correct" way to mock the async make_request | ||
mock_return = await mock_return(method, params) | ||
|
||
if "result" in mock_return: | ||
mock_return = {"result": mock_return["result"]} | ||
elif "error" in mock_return: | ||
mock_return = self._create_error_object(mock_return["error"]) | ||
|
||
mocked_result = merge(response_dict, mock_return) | ||
elif method in self.mock_results: | ||
mock_return = self.mock_results[method] | ||
if callable(mock_return): | ||
# handle callable to make things easier since we're mocking | ||
|
@@ -167,13 +224,7 @@ async def _async_mock_request_handler( | |
error = error(method, params) | ||
elif iscoroutinefunction(error): | ||
error = await error(method, params) | ||
|
||
code = error.get("code", -32000) | ||
message = error.get("message", "Mocked error") | ||
mocked_result = merge( | ||
response_dict, | ||
{"error": merge({"code": code, "message": message}, error)}, | ||
) | ||
mocked_result = merge(response_dict, self._create_error_object(error)) | ||
|
||
else: | ||
raise Exception("Invariant: unreachable code path") | ||
|
@@ -192,3 +243,9 @@ async def _coro( | |
return await decorator(_coro)(self.w3.provider, method, params) | ||
else: | ||
return mocked_result | ||
|
||
@staticmethod | ||
def _create_error_object(error: Dict[str, Any]) -> Dict[str, Any]: | ||
code = error.get("code", -32000) | ||
message = error.get("message", "Mocked error") | ||
return {"error": merge({"code": code, "message": message}, error)} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
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.
This iterator approach is really cool! 😎
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.
yeah, this is way cleaner than the v6 way! nice!