Skip to content

refactor(rlp): tighten encode/decode, unify length-prefix logic, lift docs#766

Merged
tcoratger merged 5 commits into
leanEthereum:mainfrom
tcoratger:refactor/rlp-compact-and-document
May 24, 2026
Merged

refactor(rlp): tighten encode/decode, unify length-prefix logic, lift docs#766
tcoratger merged 5 commits into
leanEthereum:mainfrom
tcoratger:refactor/rlp-compact-and-document

Conversation

@tcoratger
Copy link
Copy Markdown
Collaborator

Summary

  • Collapse encode bytes + encode list + encode length into one shared length-prefix helper parameterised by base (0x80 for strings, 0xC0 for lists). Same idea on the decode side: four near-identical short/long string/list branches fold into a single length helper that returns the payload range.
  • Remove a defensive bounds check in the decode dispatcher that turns out to be unreachable from either call path (top-level rejects empty input, recursive call is gated by the parent list's payload end which is bounded by the buffer length).
  • Rewrite every docstring and inline comment to match the bitfields module style: one sentence per line, no backticks, role-based prose (no function/variable names in doc text), Phase N labels with concrete hex walkthroughs at each algorithm step.
  • Inline the magic numbers (0x80, 0xB7, 0xC0, 0xF7, 55) since they are the lingua franca of the RLP spec; previously named module constants moved into the test file as locals.
  • Add tests for three previously uncovered error paths:
    • leading-zero length bytes rejected for long strings
    • leading-zero length bytes rejected for long lists
    • inner item overshooting parent list boundary

Result

  • Source: 302 -> 333 lines (executable code shrinks from ~130 to ~75 lines; the rest is now richer documentation).
  • Coverage: RLP module now at 100% line and branch coverage (77/77, 38/38).

Test plan

  • uv run pytest tests/lean_spec/types/test_rlp.py — 352 passed
  • uv run pytest tests/lean_spec/test_cli.py tests/lean_spec/subspecs/networking/enr/test_enr.py — consumers still green
  • just check — ruff, format, ty, codespell, mdformat all pass
  • 100% line + branch coverage on src/lean_spec/types/rlp.py

tcoratger and others added 5 commits May 24, 2026 00:38
… docs

Collapses encode_bytes + encode_list + encode_length into one shared
length-prefix helper parameterised by base, and folds the four decode
branches (short/long string, short/long list) into a single length helper
with shared range parsing. Removes a defensive bounds check in the decode
dispatcher that is unreachable from both call paths.

Rewrites all docstrings and inline comments to match the bitfields module
style: one sentence per line, no backticks, role-based prose, Phase N
labels with concrete hex walkthroughs at every algorithm step.

Adds tests for three previously uncovered error paths (leading-zero length
bytes for long strings and long lists, inner item overshooting parent list
boundary). RLP module now at 100% line and branch coverage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces the inlined hex literals with three Final-typed module constants
(STRING_BASE, LIST_BASE, SHORT_FORM_MAX) carrying friendly docstrings that
explain the why behind each value. Anchors every pytest.raises match
pattern with ^...$ against the full error message, replacing partial
substring and .* patterns that could silently pass on regressions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ork-agnostic facade

The architectural test test_subspecs_do_not_import_concrete_fork forbids
subspec modules from importing under lean_spec.forks.lstar. The top-level
lean_spec.forks package already re-exports Block, BlockBody, and
AggregatedAttestations, so the fix is a one-line consolidation of the
three import lines into one fork-agnostic import.

No runtime behavior change.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Module-level constants get docstrings per the project code style.
Each docstring explains the why behind the value (boundary role, short/long
form switchover, range layout) instead of restating the value.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@tcoratger tcoratger merged commit 653262f into leanEthereum:main May 24, 2026
12 of 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