Skip to content

Commit

Permalink
Tester provider tests to handle rpc errors
Browse files Browse the repository at this point in the history
  • Loading branch information
reedsa committed Jul 26, 2023
1 parent 18b5ee5 commit 8938f76
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
1 change: 1 addition & 0 deletions newsfragments/3061.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return structured JSON-RPC errors for missing or unimplemented eth-tester methods.
28 changes: 28 additions & 0 deletions tests/core/providers/test_tester_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,31 @@ def test_eth_tester_provider_properly_handles_eth_tester_error_messages(
TransactionFailed, match="execution reverted: The error message."
):
provider.make_request(RPCEndpoint("eth_blockNumber"), [])


def test_eth_tester_provider_properly_handles_eth_tester_key_error_messages():
provider = EthereumTesterProvider(api_endpoints={})
response = provider.make_request(RPCEndpoint("eth_blockNumber"), [])

assert response["error"]["code"] == -32601
assert isinstance(response["error"]["data"], KeyError)
assert response["error"]["message"] == "Unknown RPC Endpoint: eth_blockNumber"


def test_eth_tester_provider_properly_handles_eth_tester_not_implmented_error_messages(
mocker,
):
mocker.patch(
"eth_tester.main.EthereumTester.get_block_by_number",
side_effect=NotImplementedError("The error message."),
)

provider = EthereumTesterProvider()
response = provider.make_request(RPCEndpoint("eth_blockNumber"), [])

assert response["error"]["code"] == -32601
assert isinstance(response["error"]["data"], NotImplementedError)
assert (
response["error"]["message"]
== "RPC Endpoint has not been implemented: eth_blockNumber"
)
24 changes: 12 additions & 12 deletions tests/integration/test_ethereum_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ def func_wrapper(self, eth_tester, *args, **kwargs):

class TestEthereumTesterEthModule(EthModuleTest):
test_eth_max_priority_fee_with_fee_history_calculation = not_implemented(
EthModuleTest.test_eth_max_priority_fee_with_fee_history_calculation, ValueError
EthModuleTest.test_eth_max_priority_fee_with_fee_history_calculation,
NotImplementedError,
)
test_eth_max_priority_fee_with_fee_history_calculation_error_dict = not_implemented(
EthModuleTest.test_eth_max_priority_fee_with_fee_history_calculation_error_dict,
Expand All @@ -290,16 +291,16 @@ class TestEthereumTesterEthModule(EthModuleTest):
EthModuleTest.test_eth_sign_transaction_ens_names, ValueError
)
test_eth_submit_hashrate = not_implemented(
EthModuleTest.test_eth_submit_hashrate, ValueError
EthModuleTest.test_eth_submit_hashrate, NotImplementedError
)
test_eth_submit_work = not_implemented(
EthModuleTest.test_eth_submit_work, ValueError
EthModuleTest.test_eth_submit_work, NotImplementedError
)
test_eth_get_raw_transaction = not_implemented(
EthModuleTest.test_eth_get_raw_transaction, ValueError
EthModuleTest.test_eth_get_raw_transaction, KeyError
)
test_eth_get_raw_transaction_raises_error = not_implemented(
EthModuleTest.test_eth_get_raw_transaction, ValueError
EthModuleTest.test_eth_get_raw_transaction, KeyError
)
test_eth_get_raw_transaction_by_block = not_implemented(
EthModuleTest.test_eth_get_raw_transaction_by_block, ValueError
Expand Down Expand Up @@ -588,7 +589,12 @@ class TestEthereumTesterNetModule(NetModuleTest):
class TestEthereumTesterPersonalModule(GoEthereumPersonalModuleTest):
test_personal_sign_and_ecrecover = not_implemented(
GoEthereumPersonalModuleTest.test_personal_sign_and_ecrecover,
ValueError,
NotImplementedError,
)

test_personal_list_wallets = not_implemented(
GoEthereumPersonalModuleTest.test_personal_list_wallets,
KeyError,
)

# Test overridden here since eth-tester returns False
Expand All @@ -598,9 +604,3 @@ def test_personal_unlock_account_failure(self, w3, unlockable_account_dual_type)
unlockable_account_dual_type, "bad-password"
)
assert result is False

@pytest.mark.xfail(
raises=ValueError, reason="list_wallets not implemented in eth-tester"
)
def test_personal_list_wallets(self, w3: "Web3") -> None:
super().test_personal_list_wallets(w3)
5 changes: 3 additions & 2 deletions web3/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
)
from web3.exceptions import (
BadResponseFormat,
MethodUnavailable,
)
from web3.middleware import (
abi_middleware,
Expand Down Expand Up @@ -213,7 +212,9 @@ def formatted_response(
if isinstance(response["error"], dict):
resp_code = response["error"].get("code")
if resp_code == -32601:
raise MethodUnavailable(response["error"])
e = response["error"].get("data")
if isinstance(e, Exception):
raise e
raise ValueError(response["error"])
# NULL_RESPONSES includes None, so return False here as the default
# so we don't apply the null_result_formatters if there is no 'result' key
Expand Down
4 changes: 2 additions & 2 deletions web3/providers/eth_tester/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def _error_response(error: str) -> RPCResponse:
"id": 1,
"jsonrpc": "2.0",
"error": {
"code": -32000,
"code": -32601,
"message": error,
"data": result or "",
"data": result,
},
}
)
Expand Down

0 comments on commit 8938f76

Please sign in to comment.