Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reedsa committed Sep 12, 2023
1 parent c74a620 commit dc00e6f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
47 changes: 43 additions & 4 deletions tests/core/manager/test_response_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
}
NONE_RESPONSE = {"jsonrpc": "2.0", "id": 1, "result": None}
ZERO_X_RESPONSE = {"jsonrpc": "2.0", "id": 1, "result": "0x"}
INVALID_JSONRPC_RESP_FORMAT = {
"jsonrpc": "999",
"error": {
"code": -32000,
"message": "Requested block number is in a range that is not available yet, "
"because the ancient block sync is still in progress.",
},
}
UNEXPECTED_RESPONSE_FORMAT = {"jsonrpc": "2.0", "id": 1}
ANOTHER_UNEXPECTED_RESP_FORMAT = {
"name": "LimitError",
Expand All @@ -50,6 +58,13 @@
"available",
},
}
MISSING_CODE_RESP_FORMAT = {
"jsonrpc": "2.0",
"error": {
"message": "the method eth_getTransactionByHash does not exist/is not "
"available",
},
}
INVALID_MESSAGE_RESP_FORMAT = {
"jsonrpc": "2.0",
"error": {
Expand Down Expand Up @@ -185,21 +200,33 @@ def test_formatted_response_raises_errors(
TransactionNotFound,
"Transaction with hash: '0x01' not found.",
),
(
INVALID_JSONRPC_RESP_FORMAT,
(),
identity,
identity,
BadResponseFormat,
f"The response was in an unexpected format and unable to be parsed. "
f'The "jsonrpc" field must be present with a value of "2.0". '
f"The raw response is: {INVALID_JSONRPC_RESP_FORMAT}",
),
(
UNEXPECTED_RESPONSE_FORMAT,
(),
identity,
identity,
BadResponseFormat,
f"The response was in an unexpected format and unable to be parsed. The raw response is: {UNEXPECTED_RESPONSE_FORMAT}", # noqa: E501
f"The response was in an unexpected format and unable to be parsed. "
f"The raw response is: {UNEXPECTED_RESPONSE_FORMAT}",
),
(
ANOTHER_UNEXPECTED_RESP_FORMAT,
(),
identity,
identity,
BadResponseFormat,
f"The response was in an unexpected format and unable to be parsed. The raw response is: {ANOTHER_UNEXPECTED_RESP_FORMAT}", # noqa: E501
f"The response was in an unexpected format and unable to be parsed. "
f"The raw response is: {ANOTHER_UNEXPECTED_RESP_FORMAT}",
),
(
INVALID_CODE_RESP_FORMAT,
Expand All @@ -209,10 +236,22 @@ def test_formatted_response_raises_errors(
BadResponseFormat,
re.escape(
f"The response was in an unexpected format and unable to be parsed. "
f"The error is: error['code'] must be an integer. "
f"error['code'] must be an integer. "
f"The raw response is: {INVALID_CODE_RESP_FORMAT}"
),
),
(
MISSING_CODE_RESP_FORMAT,
(),
identity,
identity,
BadResponseFormat,
re.escape(
f"The response was in an unexpected format and unable to be parsed. "
f"error['code'] must be an integer. "
f"The raw response is: {MISSING_CODE_RESP_FORMAT}"
),
),
(
INVALID_MESSAGE_RESP_FORMAT,
(),
Expand All @@ -221,7 +260,7 @@ def test_formatted_response_raises_errors(
BadResponseFormat,
re.escape(
f"The response was in an unexpected format and unable to be parsed. "
f"The error is: error['message'] must be a string, integer or null. "
f"error['message'] must be a string. "
f"The raw response is: {INVALID_MESSAGE_RESP_FORMAT}"
),
),
Expand Down
58 changes: 57 additions & 1 deletion tests/core/utilities/test_method_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
RPC,
)
from web3.exceptions import (
ContractCustomError,
ContractLogicError,
ContractPanicError,
OffchainLookup,
)
from web3.types import (
RPCResponse,
)

# OpenEthereum/default case:
REVERT_WITH_MSG = RPCResponse(
{
"jsonrpc": "2.0",
Expand Down Expand Up @@ -45,6 +47,42 @@
}
)

OFFCHAIN_LOOKUP_ERROR = RPCResponse(
{
"jsonrpc": "2.0",
"error": {
"code": -32601,
# keccak hash for OffchainLookup(address,string[],bytes,bytes4,bytes)
"data": "0x556f18302ad20acabcb7abd78e46143543eff9de9c87236fb097a1398d41115e", # noqa: E501
},
"id": 1,
}
)

SOLIDITY_PANIC_ERROR = RPCResponse(
{
"jsonrpc": "2.0",
"error": {
"code": -32601,
# keccak hash for Panic("0x00")
"data": "0x8d38ad3815d96c20b434751afbfc963108fb93355287c65ea179f470d074b982", # noqa: E501
},
"id": 1,
}
)

SOLIDITY_CUSTOM_ERROR = RPCResponse(
{
"jsonrpc": "2.0",
"error": {
"code": -32601,
# keccak hash for Error("There was a custom error")
"data": "0x1fbc1b4970cb0e8bb59e70ff5ba55ce3575f3befbabf16fd7111ab7ab5cd1a84", # noqa: E501
},
"id": 1,
}
)

OTHER_ERROR = RPCResponse(
{
"jsonrpc": "2.0",
Expand Down Expand Up @@ -181,6 +219,24 @@ def test_get_revert_reason(response, expected) -> None:
raise_contract_logic_error_on_revert(response)


def test_get_revert_reason_offchain_lookup_error() -> None:
data = OFFCHAIN_LOOKUP_ERROR["error"]["data"]
with pytest.raises(OffchainLookup, match=data):
raise_contract_logic_error_on_revert(OFFCHAIN_LOOKUP_ERROR)


def test_get_revert_reason_solidity_panic_error() -> None:
with pytest.raises(ContractPanicError, match=SOLIDITY_PANIC_ERROR["error"]["data"]):
raise_contract_logic_error_on_revert(SOLIDITY_PANIC_ERROR)


def test_get_revert_reason_solidity_custom_error() -> None:
with pytest.raises(
ContractCustomError, match=SOLIDITY_CUSTOM_ERROR["error"]["data"]
):
raise_contract_logic_error_on_revert(SOLIDITY_CUSTOM_ERROR)


def test_get_revert_reason_other_error() -> None:
assert raise_contract_logic_error_on_revert(OTHER_ERROR) is OTHER_ERROR

Expand Down

0 comments on commit dc00e6f

Please sign in to comment.