refactor(testing): unify duplicated hex codec helpers - #905
Merged
Conversation
The same 0x-prefixed hex decoder was copy-pasted under three forms across the test fixtures: _unhex in the gossipsub handler, _from_hex in the networking codec, and an inline bytes.fromhex(...removeprefix) in the ssz fixture. The networking codec also carried its own _to_hex inverse, with inline "0x" + x.hex() encodes scattered alongside. Introduce a single shared hex codec module exposing to_hex and from_hex, and route all three fixtures through it (audit finding FIX-03). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Unifies the duplicated 0x-prefixed hex codec helpers in the consensus test fixtures (audit finding FIX-03).
The same hex decode logic —
bytes.fromhex(s.removeprefix("0x"))— was copy-pasted under three different forms:_unhexingossipsub_handler.py_from_hexinnetworking_codec.pyssz.pyThe networking codec also carried its own
_to_hexinverse, with inline"0x" + x.hex()encodes scattered across the same three files.Change
test_fixtures/hex_codec.pyexposing two public functions:to_hex(data)andfrom_hex(hex_string).gossipsub_handler.py,networking_codec.py, andssz.pyall now route through it; the local_unhex/_from_hex/_to_hexfunctions and inline copies are removed.Net: one hex codec, not four copies (60 insertions / 65 deletions across 4 files).
Notes
hex_string(nothex_str) per the repo's no-abbreviation rule.sync.py,api_endpoint.py) also have inline"0x" + x.hex()encodes; FIX-03 named only the three files above, so they're left for a possible follow-up rather than widening this PR's scope.Verification
grepconfirms no_unhex/_from_hex/_to_hexdefinitions or call sites remain, and no inlineremoveprefix("0x")/"0x" + ...hex()patterns remain in the three target files.just checkpasses: ruff check, ruff format,ty, codespell, mdformat all clean.🤖 Generated with Claude Code