Skip to content

Commit d85d9e5

Browse files
tcoratgerclaude
andauthored
refactor(testing): hoist codec dispatch tables out of run() (#911)
Two codec fixtures rebuilt a constant lookup table on every call. The peer-identifier fixture rebuilt a string-to-enum map each run, even though the literal key-type values already match the enum member names. Look the member up directly by name instead, dropping the map. The decode-failure fixture rebuilt its decoder table each call. Hoist it to a module-level constant so it is built once. Both fixtures emit byte-identical vectors: the enum lookup yields the same members as the map, and the decoder table holds the same callables. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent bb1d020 commit d85d9e5

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

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

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

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

56
from pydantic import Field
67

@@ -558,14 +559,9 @@ class PeerIdentifierDerivation(StrictBaseModel):
558559

559560
def run(self) -> PeerIdentifierOutput:
560561
"""Derive the identifier and assert the Base58 roundtrip."""
561-
key_type_map = {
562-
"ed25519": KeyType.ED25519,
563-
"secp256k1": KeyType.SECP256K1,
564-
"ecdsa": KeyType.ECDSA,
565-
"rsa": KeyType.RSA,
566-
}
562+
# The literal values match the enum member names once upper-cased.
567563
protobuf = PublicKeyProtobuf(
568-
key_type=key_type_map[self.key_type], key_data=from_hex(self.public_key)
564+
key_type=KeyType[self.key_type.upper()], key_data=from_hex(self.public_key)
569565
)
570566
peer_id = PeerId.from_public_key(protobuf)
571567
peer_id_string = str(peer_id)
@@ -661,6 +657,18 @@ class DecodeFailureOutput(StrictBaseModel):
661657
"""Name of the decoder that must reject the input."""
662658

663659

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

@@ -683,17 +691,8 @@ class DecodeFailure(StrictBaseModel):
683691

684692
def attempt_decode(self) -> Exception | None:
685693
"""Run the decoder on the malformed input and return what it raised."""
686-
decoders = {
687-
"varint": decode_varint,
688-
"snappy_frame": frame_decompress,
689-
"snappy_block": decompress,
690-
"gossipsub_rpc": RPC.decode,
691-
"reqresp_request": decode_request,
692-
"reqresp_response": ResponseCode.decode,
693-
"enr": ENR.from_rlp,
694-
}
695694
try:
696-
decoders[self.decoder](from_hex(self.raw_bytes))
695+
_DECODERS_BY_NAME[self.decoder](from_hex(self.raw_bytes))
697696
except Exception as exception:
698697
return exception
699698
return None

0 commit comments

Comments
 (0)