Skip to content

Commit 27845aa

Browse files
Suggestions for: feat(consume): add ExceptionMapper support to the consume-engine simulator (#1464)
* refactor(consume): cache exception mappers in session-scoped fixture * refactor(consume): make string match more robust by applying `lower()` * feat(consume): allow disabling of strict exception checking per fork * refactor(consume/hive): create `strict_exception_matching` fixture --------- Co-authored-by: Mario Vega <[email protected]>
1 parent 6e2af40 commit 27845aa

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

Diff for: src/pytest_plugins/consume/hive_simulators/conftest.py

+43-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def pytest_addoption(parser):
4848
dest="disable_strict_exception_matching",
4949
default="",
5050
help=(
51-
"Comma-separated list of the names of clients which should NOT use strict "
51+
"Comma-separated list of client names and/or forks which should NOT use strict "
5252
"exception matching."
5353
),
5454
)
@@ -208,20 +208,31 @@ def buffered_genesis(client_genesis: dict) -> io.BufferedReader:
208208
return io.BufferedReader(cast(io.RawIOBase, io.BytesIO(genesis_bytes)))
209209

210210

211+
@pytest.fixture(scope="session")
212+
def client_exception_mapper_cache():
213+
"""Cache for exception mappers by client type."""
214+
return {}
215+
216+
211217
@pytest.fixture(scope="function")
212218
def client_exception_mapper(
213-
client_type: ClientType,
219+
client_type: ClientType, client_exception_mapper_cache
214220
) -> ExceptionMapper | None:
215-
"""Return the exception mapper for the client type."""
216-
for client in EXCEPTION_MAPPERS:
217-
if client in client_type.name:
218-
return EXCEPTION_MAPPERS[client]
219-
return None
221+
"""Return the exception mapper for the client type, with caching."""
222+
if client_type.name not in client_exception_mapper_cache:
223+
for client in EXCEPTION_MAPPERS:
224+
if client in client_type.name:
225+
client_exception_mapper_cache[client_type.name] = EXCEPTION_MAPPERS[client]
226+
break
227+
else:
228+
client_exception_mapper_cache[client_type.name] = None
229+
230+
return client_exception_mapper_cache[client_type.name]
220231

221232

222233
@pytest.fixture(scope="session")
223234
def disable_strict_exception_matching(request: pytest.FixtureRequest) -> List[str]:
224-
"""Return the list of clients that should NOT use strict exception matching."""
235+
"""Return the list of clients or forks that should NOT use strict exception matching."""
225236
config_string = request.config.getoption("disable_strict_exception_matching")
226237
return config_string.split(",") if config_string else []
227238

@@ -232,7 +243,30 @@ def client_strict_exception_matching(
232243
disable_strict_exception_matching: List[str],
233244
) -> bool:
234245
"""Return True if the client type should use strict exception matching."""
235-
return not any(client in client_type.name for client in disable_strict_exception_matching)
246+
return not any(
247+
client.lower() in client_type.name.lower() for client in disable_strict_exception_matching
248+
)
249+
250+
251+
@pytest.fixture(scope="function")
252+
def fork_strict_exception_matching(
253+
fixture: BlockchainFixtureCommon,
254+
disable_strict_exception_matching: List[str],
255+
) -> bool:
256+
"""Return True if the fork should use strict exception matching."""
257+
# NOTE: `in` makes it easier for transition forks ("Prague" in "CancunToPragueAtTime15k")
258+
return not any(
259+
fork.lower() in fixture.fork.lower() for fork in disable_strict_exception_matching
260+
)
261+
262+
263+
@pytest.fixture(scope="function")
264+
def strict_exception_matching(
265+
client_strict_exception_matching: bool,
266+
fork_strict_exception_matching: bool,
267+
) -> bool:
268+
"""Return True if the test should use strict exception matching."""
269+
return client_strict_exception_matching and fork_strict_exception_matching
236270

237271

238272
@pytest.fixture(scope="function")

Diff for: src/pytest_plugins/consume/hive_simulators/engine/test_via_engine.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_blockchain_via_engine(
2222
eth_rpc: EthRPC,
2323
engine_rpc: EngineRPC,
2424
fixture: BlockchainEngineFixture,
25-
client_strict_exception_matching: bool,
25+
strict_exception_matching: bool,
2626
):
2727
"""
2828
1. Check the client genesis block hash matches `fixture.genesis.block_hash`.
@@ -100,7 +100,7 @@ def test_blockchain_via_engine(
100100
f'returned exception: "{payload_response.validation_error}" '
101101
f'(mapper: "{payload_response.validation_error.mapper_name}")'
102102
)
103-
if client_strict_exception_matching:
103+
if strict_exception_matching:
104104
logger.fail(message)
105105
raise Exception(message)
106106
else:
@@ -115,11 +115,12 @@ def test_blockchain_via_engine(
115115
f'got: "{payload_response.validation_error}" '
116116
f'expected: "{payload.validation_error}"'
117117
)
118-
if client_strict_exception_matching:
118+
if strict_exception_matching:
119119
logger.fail(message)
120120
raise Exception(message)
121121
else:
122122
logger.warning(message)
123+
123124
except JSONRPCError as e:
124125
logger.info(f"JSONRPC error encountered: {e.code} - {e.message}")
125126
if payload.error_code is None:

0 commit comments

Comments
 (0)