Skip to content

Commit 5f84ce4

Browse files
authored
fix(plugins/consume-engine): Fix Engine API error check (#1133)
1 parent a204ab9 commit 5f84ce4

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/ethereum_test_rpc/rpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ def post_request(self, method: str, *params: Any, extra_headers: Dict | None = N
8282
response_json = response.json()
8383

8484
if "error" in response_json:
85-
exception = JSONRPCError(**response_json["error"])
86-
raise exception.exception(method)
85+
raise JSONRPCError(**response_json["error"])
8786

8887
assert "result" in response_json, "RPC response didn't contain a result field"
8988
result = response_json["result"]

src/ethereum_test_rpc/types.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@
1010
from ethereum_test_types import Withdrawal
1111

1212

13-
class JSONRPCError(CamelModel):
13+
class JSONRPCError(Exception):
1414
"""Model to parse a JSON RPC error response."""
1515

1616
code: int
1717
message: str
1818

19+
def __init__(self, code: int | str, message: str, **kwargs):
20+
"""Initialize the JSONRPCError."""
21+
self.code = int(code)
22+
self.message = message
23+
1924
def __str__(self) -> str:
2025
"""Return string representation of the JSONRPCError."""
2126
return f"JSONRPCError(code={self.code}, message={self.message})"
2227

23-
def exception(self, method) -> Exception:
24-
"""Return exception representation of the JSONRPCError."""
25-
return Exception(
26-
f"Error calling JSON RPC {method}, code: {self.code}, " f"message: {self.message}"
27-
)
28-
2928

3029
class TransactionByHashResponse(CamelModel):
3130
"""Represents the response of a transaction by hash request."""

src/pytest_plugins/consume/hive_simulators/engine/test_via_engine.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from ethereum_test_fixtures import BlockchainEngineFixture
99
from ethereum_test_rpc import EngineRPC, EthRPC
10-
from ethereum_test_rpc.types import ForkchoiceState, PayloadStatusEnum
10+
from ethereum_test_rpc.types import ForkchoiceState, JSONRPCError, PayloadStatusEnum
1111
from pytest_plugins.consume.hive_simulators.exceptions import GenesisBlockMismatchExceptionError
1212

1313
from ...decorator import fixture_format
@@ -52,13 +52,24 @@ def test_via_engine(
5252
for i, payload in enumerate(blockchain_fixture.payloads):
5353
with total_payload_timing.time(f"Payload {i + 1}") as payload_timing:
5454
with payload_timing.time(f"engine_newPayloadV{payload.new_payload_version}"):
55-
payload_response = engine_rpc.new_payload(
56-
*payload.params,
57-
version=payload.new_payload_version,
58-
)
59-
assert payload_response.status == (
60-
PayloadStatusEnum.VALID if payload.valid() else PayloadStatusEnum.INVALID
61-
), f"unexpected status: {payload_response}"
55+
try:
56+
payload_response = engine_rpc.new_payload(
57+
*payload.params,
58+
version=payload.new_payload_version,
59+
)
60+
assert payload_response.status == (
61+
PayloadStatusEnum.VALID
62+
if payload.valid()
63+
else PayloadStatusEnum.INVALID
64+
), f"unexpected status: {payload_response}"
65+
except JSONRPCError as e:
66+
if payload.error_code is None:
67+
raise Exception(f"unexpected error: {e.code} - {e.message}") from e
68+
if e.code != payload.error_code:
69+
raise Exception(
70+
f"unexpected error code: {e.code}, expected: {payload.error_code}"
71+
) from e
72+
6273
if payload.valid():
6374
with payload_timing.time(
6475
f"engine_forkchoiceUpdatedV{payload.forkchoice_updated_version}"

0 commit comments

Comments
 (0)