Skip to content

refactor(webrtc): deduplicate _varint.py β€” use libp2p.utils.varint#1379

Open
yashksaini-coder wants to merge 2 commits into
libp2p:mainfrom
yashksaini-coder:refactor/webrtc-deduplicate-varint-1355
Open

refactor(webrtc): deduplicate _varint.py β€” use libp2p.utils.varint#1379
yashksaini-coder wants to merge 2 commits into
libp2p:mainfrom
yashksaini-coder:refactor/webrtc-deduplicate-varint-1355

Conversation

@yashksaini-coder

@yashksaini-coder yashksaini-coder commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #1355.

PR #1309 added libp2p/transport/webrtc/_varint.py with encode_uvarint and decode_uvarint for WebRTC data-channel and signaling framing. The same unsigned LEB128 logic already exists in libp2p/utils/varint.py, used by mplex, msgio, identify, and others.

This PR:

  • Deletes libp2p/transport/webrtc/_varint.py
  • stream.py: imports encode_uvarint + decode_varint_with_size from libp2p.utils.varint β€” the latter returns the same (value, consumed) tuple shape, making it a 1:1 replacement
  • signaling.py: imports encode_uvarint + decode_uvarint_from_stream from libp2p.utils.varint β€” replaces the private _read_uvarint helper (functionally identical byte-by-byte reader); error wrapping in read_signaling_message preserves WebRTCSignalingError at call sites
  • Three test files updated to import from libp2p.utils.varint directly

No wire-format or protocol change. Pure refactor.

Why it's safe

_varint.py libp2p.utils.varint
encode_uvarint(v) encode_uvarint(v) β€” identical algorithm (>0x7F ≑ >=0x80)
decode_uvarint(data) β†’ (value, consumed) decode_varint_with_size(data) β†’ (value, consumed) β€” same tuple
_read_uvarint(stream) decode_uvarint_from_stream(reader) β€” same byte-by-byte loop; INetStream is a Reader

Test plan

  • All 154 WebRTC tests pass (tests/core/transport/webrtc/)
  • No change to on-wire framing β€” encode/decode logic is identical
  • Newsfragment 1355.internal.rst added

Deletes libp2p/transport/webrtc/_varint.py and migrates stream.py and
signaling.py to import from the shared libp2p.utils.varint utilities:

- encode_uvarint (identical algorithm, both files)
- decode_varint_with_size (same (value, consumed) tuple, replaces
  decode_uvarint in stream.py)
- decode_uvarint_from_stream (byte-by-byte async reader, replaces the
  private _read_uvarint in signaling.py; error wrapping in
  read_signaling_message preserves WebRTCSignalingError at call sites)

Updates three test files to import the canonical utilities directly.
No wire-format or protocol change.

Closes libp2p#1355.
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.

refactor(webrtc): deduplicate _varint.py β€” use libp2p.utils.varint

1 participant