Skip to content

refactor: migrate all encode/decode to stream-based APIs#66

Merged
b00f merged 3 commits into
mainfrom
refactor/stream-based-encoding
Jul 2, 2026
Merged

refactor: migrate all encode/decode to stream-based APIs#66
b00f merged 3 commits into
mainfrom
refactor/stream-based-encoding

Conversation

@b00f

@b00f b00f commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Replace the functional bytes-based encoding approach with a stream-based
API using io.Writer (write) and io.BufferedReader (read).

Why

  • Eliminates O(n²) byte concatenation from repeated buf += data on immutable bytes
  • Simplifies call sites: val, buf = read_uint8(buf)val = read_uint8(buf)
  • Centralized EOF handling in a single _read() helper
  • More idiomatic Python

What changed

  • All encode() methods now take io.Writer and return None
  • All decode() classmethods now take io.BufferedReader and return the value directly (no more tuple unwrapping)
  • Updated abstract base classes (Signature, PublicKey)
  • Updated crypto types (Hash, Address, BLS/Ed25519/Secp256k1 sig/pub)
  • Updated simple types (Height, Round, Amount)
  • Updated transaction payloads
  • Updated Transaction, Block, Header, Certificate
  • Updated all tests to match the new API
  • Added encode/decode tests for Amount
  • All 49 tests pass, zero lint errors

b00f added 3 commits July 2, 2026 19:57
…eredReader API

Replace the functional bytes-based encoding approach with a stream-based
API using io.Writer (write) and io.BufferedReader (read). This eliminates
O(n^2) byte concatenation and simplifies call sites.

Changes:
- All encode() methods now take io.Writer and return None
- All decode() classmethods now take io.BufferedReader and return the value directly (no more tuple unwrapping)
- Updated abstract base classes (Signature, PublicKey)
- Updated crypto types (Hash, Address, BLS/Ed25519/Secp256k1 sig/pub)
- Updated simple types (Height, Round, Amount)
- Updated transaction payloads
- Updated Transaction, Block, Header, Certificate
- Updated all tests to match the new API
- Fixed lint issues with ruff
Block.decode was the only remaining decode() method that took raw bytes
and wrapped them internally. Now it takes io.BufferedReader directly,
consistent with every other decode() in the codebase.
@b00f
b00f requested a review from themantre July 2, 2026 15:27
@b00f b00f changed the title refactor: migrate all encode/decode to stream-based io.Writer/io.BufferedReader API refactor: migrate all encode/decode to stream-based APIs Jul 2, 2026
@b00f
b00f merged commit fc5ab04 into main Jul 2, 2026
3 checks passed
@b00f
b00f deleted the refactor/stream-based-encoding branch July 2, 2026 15:28
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.

2 participants