Skip to content

Commit 1ef93a9

Browse files
tcoratgerclaude
andauthored
refactor(testing): inline networking decode-failure dispatch (leanEthereum#1156)
Replace the module-level decoder lookup table with a match in its sole consumer. The table mapped seven names to already-imported decoder callables and was only ever read by the decode-failure attempt, so the dispatch now lives where it is used. - Delete the _DECODERS_BY_NAME table and its now-orphaned Callable and Final imports. - Dispatch by structural pattern matching on the decoder discriminator. - Keep the hex decode inside the try, so malformed-hex input is still caught and reported as the rejection. Pure structural refactor: emitted vectors are byte-identical (verified by filling tests/consensus/lstar/networking before and after). Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent f4b15b5 commit 1ef93a9

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

packages/testing/src/consensus_testing/test_fixtures/networking_codec.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Networking codec test fixture for wire-format conformance testing."""
22

3-
from collections.abc import Callable
4-
from typing import Annotated, ClassVar, Final, Literal
3+
from typing import Annotated, ClassVar, Literal
54

65
from pydantic import Field
76

@@ -654,18 +653,6 @@ class DecodeFailureOutput(StrictBaseModel):
654653
"""Name of the decoder that must reject the input."""
655654

656655

657-
_DECODERS_BY_NAME: Final[dict[str, Callable[[bytes], object]]] = {
658-
"varint": decode_varint,
659-
"snappy_frame": frame_decompress,
660-
"snappy_block": decompress,
661-
"gossipsub_rpc": RPC.decode,
662-
"reqresp_request": decode_request,
663-
"reqresp_response": ResponseCode.decode,
664-
"enr": ENR.from_rlp,
665-
}
666-
"""Wire-format decoders keyed by the name a rejection vector targets."""
667-
668-
669656
class DecodeFailure(StrictBaseModel):
670657
"""Assert that a wire-format decoder rejects malformed input."""
671658

@@ -689,7 +676,22 @@ class DecodeFailure(StrictBaseModel):
689676
def attempt_decode(self) -> Exception | None:
690677
"""Run the decoder on the malformed input and return what it raised."""
691678
try:
692-
_DECODERS_BY_NAME[self.decoder](from_hex(self.raw_bytes))
679+
raw_bytes = from_hex(self.raw_bytes)
680+
match self.decoder:
681+
case "varint":
682+
decode_varint(raw_bytes)
683+
case "snappy_frame":
684+
frame_decompress(raw_bytes)
685+
case "snappy_block":
686+
decompress(raw_bytes)
687+
case "gossipsub_rpc":
688+
RPC.decode(raw_bytes)
689+
case "reqresp_request":
690+
decode_request(raw_bytes)
691+
case "reqresp_response":
692+
ResponseCode.decode(raw_bytes)
693+
case "enr":
694+
ENR.from_rlp(raw_bytes)
693695
except Exception as exception:
694696
return exception
695697
return None

0 commit comments

Comments
 (0)