refactor(varint): parametrize the canonical LEB128 codec with max_bytes#798
Merged
tcoratger merged 1 commit intoMay 29, 2026
Merged
Conversation
The networking and snappy modules each carried a self-contained LEB128 implementation. The two algorithms were identical, differing only in byte cap (10 vs 5). One implementation was a slow drift away from the other waiting to happen. Keep the networking varint as the single source of truth and give both encode and decode a max_bytes parameter (default 10 = uint64 cap). Snappy now imports the canonical codec and passes 5 via a new SNAPPY_VARINT_MAX_BYTES constant. Encode now also enforces the cap, so per-cap semantics are symmetric. The snappy length-prefix tests run against the canonical codec, plus an integration test that asserts an oversize prefix surfaces as a SnappyDecompressionError. 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
The networking and snappy modules each carried a self-contained LEB128 implementation. The two algorithms were identical, differing only in the byte cap (10 vs 5). One implementation was a slow drift away from the other waiting to happen.
This PR keeps the networking varint as the single source of truth and gives both encode and decode a
max_bytesparameter (default10= uint64 cap). Snappy now imports the canonical codec and passes5via a newSNAPPY_VARINT_MAX_BYTESconstant.Changes
src/lean_spec/node/networking/varint.py— the canonical home:encode_varint(value, max_bytes=10)anddecode_varint(data, offset=0, max_bytes=10)both take an explicit cap.max_bytes * 7bits) — symmetric with decode.src/lean_spec/node/snappy/— caller migration:encoding.py: 159-lineencode_varint32/decode_varint32deleted; module now contains only tag-encoding logic.compress.py: importsencode_varintfrom networking and passesmax_bytes=SNAPPY_VARINT_MAX_BYTES.decompress.py: importsdecode_varintand catchesVarintError(the oldValueErrorpath is gone).constants.py: staleVARINT_CONTINUATION_BIT/VARINT_DATA_MASKremoved; replaced bySNAPPY_VARINT_MAX_BYTES = 5.Tests
tests/lean_spec/node/networking/test_varint.py: newTestMaxBytesParameterclass covers the 5-byte cap boundary — success at the largest value that fits, error at one bit over, truncation, and oversize input.tests/lean_spec/node/snappy/test_snappy.py:TestVarintEncodingrenamed toTestSnappyLengthPrefix. Tests now exercise the canonical codec through the snappy cap, plus an integration test that asserts an oversize prefix surfaces asSnappyDecompressionError.tests/lean_spec/node/networking/test_reqresp.py: the error-message match updated to the new formatted message.Test plan
just check— ruff, format, ty, codespell, mdformat passuv run pytest tests/lean_spec/node/{networking/test_varint,networking/test_reqresp,snappy/test_snappy,snappy/test_framing}.py— 229 tests passNet change: +126/−201 across 8 files. No backward-compatibility shims, no re-exports.
🤖 Generated with Claude Code