Skip to content

refactor(testing): unify duplicated hex codec helpers - #905

Merged
tcoratger merged 1 commit into
leanEthereum:mainfrom
tcoratger:fix/unify-hex-codec
Jun 10, 2026
Merged

refactor(testing): unify duplicated hex codec helpers#905
tcoratger merged 1 commit into
leanEthereum:mainfrom
tcoratger:fix/unify-hex-codec

Conversation

@tcoratger

Copy link
Copy Markdown
Collaborator

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:

  • _unhex in gossipsub_handler.py
  • _from_hex in networking_codec.py
  • an inline copy in ssz.py

The networking codec also carried its own _to_hex inverse, with inline "0x" + x.hex() encodes scattered across the same three files.

Change

  • New shared module test_fixtures/hex_codec.py exposing two public functions: to_hex(data) and from_hex(hex_string).
  • gossipsub_handler.py, networking_codec.py, and ssz.py all now route through it; the local _unhex / _from_hex / _to_hex functions and inline copies are removed.

Net: one hex codec, not four copies (60 insertions / 65 deletions across 4 files).

Notes

  • Parameter is spelled hex_string (not hex_str) per the repo's no-abbreviation rule.
  • Two other fixtures (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

  • grep confirms no _unhex / _from_hex / _to_hex definitions or call sites remain, and no inline removeprefix("0x") / "0x" + ...hex() patterns remain in the three target files.
  • just check passes: ruff check, ruff format, ty, codespell, mdformat all clean.

🤖 Generated with Claude Code

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>
@tcoratger
tcoratger merged commit 71a72e0 into leanEthereum:main Jun 10, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant