Skip to content
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

Catch newly raised geth errors when tx indexing in progress #3216

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3216.bugfix.rst
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.
6 changes: 3 additions & 3 deletions tests/core/contracts/test_contract_panic_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from tests.core.contracts.utils import (
deploy,
)
from web3._utils.contract_error_handling import (
PANIC_ERROR_CODES,
)
from web3._utils.contract_sources.contract_data.panic_errors_contract import (
PANIC_ERRORS_CONTRACT_DATA,
)
from web3._utils.error_formatters_utils import (
PANIC_ERROR_CODES,
)
from web3.exceptions import (
ContractPanicError,
)
Expand Down
35 changes: 35 additions & 0 deletions tests/core/eth-module/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
RECEIPT_TIMEOUT = 0.2


def _tx_indexing_response_iterator():
while True:
yield {"error": {"message": "transaction indexing in progress"}}
yield {"error": {"message": "transaction indexing in progress"}}
yield {"result": {"status": "0x1"}}


@pytest.mark.parametrize(
"transaction",
(
Expand Down Expand Up @@ -202,6 +209,18 @@ def test_unmined_transaction_wait_for_receipt(w3, request_mocker):
assert txn_receipt["blockHash"] is not None


def test_eth_wait_for_transaction_receipt_transaction_indexing_in_progress(
w3, request_mocker
):
i = _tx_indexing_response_iterator()
with request_mocker(
w3,
mock_responses={"eth_getTransactionReceipt": lambda *_: next(i)},
Copy link
Contributor

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! 😎

Copy link
Collaborator

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!

):
receipt = w3.eth.wait_for_transaction_receipt(f"0x{'00' * 32}")
assert receipt == {"status": 1}


def test_get_transaction_formatters(w3, request_mocker):
non_checksummed_addr = "0xB2930B35844A230F00E51431ACAE96FE543A0347" # all uppercase
unformatted_transaction = {
Expand Down Expand Up @@ -304,3 +323,19 @@ def test_get_transaction_formatters(w3, request_mocker):
)

assert received_tx == expected


# --- async --- #


@pytest.mark.asyncio
async def test_async_wait_for_transaction_receipt_transaction_indexing_in_progress(
async_w3, request_mocker
):
i = _tx_indexing_response_iterator()
async with request_mocker(
async_w3,
mock_responses={"eth_getTransactionReceipt": lambda *_: next(i)},
):
receipt = await async_w3.eth.wait_for_transaction_receipt(f"0x{'00' * 32}")
assert receipt == {"status": 1}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ContractLogicError,
ContractPanicError,
OffchainLookup,
TransactionIndexingInProgress,
)
from web3.types import (
RPCResponse,
Expand Down Expand Up @@ -164,3 +165,21 @@ def raise_contract_logic_error_on_revert(response: RPCResponse) -> RPCResponse:
raise ContractLogicError("execution reverted", data=data)

return response


def raise_transaction_indexing_error_if_indexing(response: RPCResponse) -> RPCResponse:
"""
Raise an error if ``eth_getTransactionReceipt`` returns an error indicating that
transactions are still being indexed.
"""

error = response.get("error")
if not isinstance(error, str) and error is not None:
message = error.get("message")
if message is not None:
if all(
idx_key_phrases in message for idx_key_phrases in ("index", "progress")
):
raise TransactionIndexingInProgress(message)

return response
8 changes: 5 additions & 3 deletions web3/_utils/method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
from web3._utils.abi import (
is_length,
)
from web3._utils.contract_error_handling import (
raise_contract_logic_error_on_revert,
)
from web3._utils.encoding import (
hexstr_if_str,
to_hex,
)
from web3._utils.error_formatters_utils import (
raise_contract_logic_error_on_revert,
raise_transaction_indexing_error_if_indexing,
)
from web3._utils.filters import (
AsyncBlockFilter,
AsyncLogFilter,
Expand Down Expand Up @@ -791,6 +792,7 @@ def subscription_formatter(value: Any) -> Union[HexBytes, HexStr, Dict[str, Any]
ERROR_FORMATTERS: Dict[RPCEndpoint, Callable[..., Any]] = {
RPC.eth_estimateGas: raise_contract_logic_error_on_revert,
RPC.eth_call: raise_contract_logic_error_on_revert,
RPC.eth_getTransactionReceipt: raise_transaction_indexing_error_if_indexing,
}


Expand Down
6 changes: 3 additions & 3 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@
HexBytes,
)

from web3._utils.contract_error_handling import (
PANIC_ERROR_CODES,
)
from web3._utils.empty import (
empty,
)
from web3._utils.ens import (
ens_addresses,
)
from web3._utils.error_formatters_utils import (
PANIC_ERROR_CODES,
)
from web3._utils.fee_utils import (
PRIORITY_FEE_MIN,
)
Expand Down
99 changes: 78 additions & 21 deletions web3/_utils/module_testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The 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
)
Expand All @@ -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 = (
Expand All @@ -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)
Expand All @@ -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")

Expand Down Expand Up @@ -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 = (
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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)}
3 changes: 2 additions & 1 deletion web3/eth/async_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
OffchainLookup,
TimeExhausted,
TooManyRequests,
TransactionIndexingInProgress,
TransactionNotFound,
)
from web3.method import (
Expand Down Expand Up @@ -517,7 +518,7 @@ async def _wait_for_tx_receipt_with_timeout(
while True:
try:
tx_receipt = await self._transaction_receipt(_tx_hash)
except TransactionNotFound:
except (TransactionNotFound, TransactionIndexingInProgress):
tx_receipt = None
if tx_receipt is not None:
break
Expand Down
3 changes: 2 additions & 1 deletion web3/eth/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
OffchainLookup,
TimeExhausted,
TooManyRequests,
TransactionIndexingInProgress,
TransactionNotFound,
)
from web3.method import (
Expand Down Expand Up @@ -485,7 +486,7 @@ def wait_for_transaction_receipt(
while True:
try:
tx_receipt = self._transaction_receipt(transaction_hash)
except TransactionNotFound:
except (TransactionNotFound, TransactionIndexingInProgress):
tx_receipt = None
if tx_receipt is not None:
break
Expand Down
9 changes: 9 additions & 0 deletions web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ class TransactionNotFound(Web3Exception):
pass


class TransactionIndexingInProgress(Web3Exception):
"""
Raised when a transaction receipt is not yet available due to transaction indexing
still being in progress.
"""

pass


class BlockNotFound(Web3Exception):
"""
Raised when the block id used to lookup a block in a jsonrpc call cannot be found.
Expand Down
2 changes: 1 addition & 1 deletion web3/providers/eth_tester/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from web3 import (
Web3,
)
from web3._utils.contract_error_handling import (
from web3._utils.error_formatters_utils import (
OFFCHAIN_LOOKUP_FIELDS,
PANIC_ERROR_CODES,
)
Expand Down