refactor(ssz): consolidate to 4 files, modernize idioms, tighten tests#782
Merged
Merged
Conversation
Source (src/lean_spec/subspecs/ssz/): - Delete pack.py and utils.py; inline their helpers as private members of hash.py and merkleization.py - Collapse the 4 pack functions into a single _pack_bytes using bytes.ljust - Rewrite _pack_bits using int.to_bytes instead of bytearray bit-twiddling - Inline hash_nodes at its 5 call sites - Remove dead code in _merkleize_efficient (unreachable second loop and zero-tree fallback); add an assertion that documents the invariant - Standardize ceiling division on math.ceil everywhere - Rewrite docstrings per .claude/rules/documentation.md (no backticks, no identifier names in prose, one sentence per line) - Document constants with WHY, not what - Add ASCII diagrams to merkleize and _pack_bits Tests (tests/lean_spec/subspecs/ssz/): - Delete fork-specific tests that were misplaced in the SSZ folder (test_block, test_state, test_signed_attestation) - Delete tests redundant with the public-surface coverage (test_pack, test_utils_ssz, test_boundary_values, test_edge_cases, test_malformed_data, test_nested_structures) - Rewrite test_hash.py with parametric coverage of every dispatch arm built from first-principles expected values - Rewrite test_merkleization.py absorbing _next_pow2 coverage - Every test function has a one-line docstring Result: 100% line + branch coverage on all 4 SSZ source files (107 tests). Source goes from 6 files / 401 lines to 4 files / 332 lines. Tests go from 9 files / 2,585 lines to 2 files / 877 lines. Co-Authored-By: Claude Opus 4.7 (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
Cleanup pass on
src/lean_spec/subspecs/ssz/andtests/lean_spec/subspecs/ssz/to bring the SSZ submodule closer to a spec-quality reference implementation: fewer files, more modern Python, sharper tests.__init__,constants,hash,merkleization)Source changes
pack.pyandutils.py— their helpers are now private members ofhash.py/merkleization.py._pack_bytesusingbytes.ljust._pack_bitsusingint.to_bytesinstead of bytearray bit-twiddling.hash_nodesat its 5 call sites._merkleize_efficient(unreachable second loop and fallback); added anassertthat documents the invariant.math.ceileverywhere..claude/rules/documentation.md— no backticks, no identifier names in prose, one sentence per line.constants.pywith the WHY of each value.merkleizeand_pack_bits.Test changes
test_block,test_state,test_signed_attestation).test_pack,test_utils_ssz,test_boundary_values,test_edge_cases,test_malformed_data,test_nested_structures).test_hash.pywith parametric coverage of everyhash_tree_rootdispatch arm; expected values are built from first principles (sha256 + chunking + zero-tree table), not by re-running the system under test.test_merkleization.py, absorbing_next_pow2coverage; deleted two tests that probed the now-removed dead code.What is not in this PR (deliberate out-of-scope items)
These were surfaced during the analysis but are spec-policy decisions, not cleanup:
hash_tree_rootstill silently acceptsbytes/bytearray/memoryview/Fpwithout explicit spec backing.verify_merkle_proof,compute_merkle_proof, generalized indices.Happy to do either in a follow-up if desired.
Test plan
uv run pytest tests/lean_spec/subspecs/ssz/— 107 passeduv run pytest tests/lean_spec/subspecs/ssz/ --cov=src/lean_spec/subspecs/ssz --cov-branch— 100% line + branch on all 4 filesuv run pytest(full suite) — 3012 passed (run after the source refactor)just lint— cleanjust format-check— clean🤖 Generated with Claude Code