-
Notifications
You must be signed in to change notification settings - Fork 175
✨ Feat: add Thin Waist address validation utilities and integrate into echo example #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
seetadev
merged 33 commits into
libp2p:main
from
yashksaini-coder:feat/804-add-thin-waist-address
Aug 25, 2025
Merged
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
b840eaa
Implement advanced network discovery example and address validation u…
fa17423
Refactor echo example to use optimal binding address
59a898c
Add tests for echo example and address validation utilities
09cd8b3
Merge branch 'main' into feat/804-add-thin-waist-address
yashksaini-coder a14c42e
Merge branch 'main' into feat/804-add-thin-waist-address
yashksaini-coder 9a0f224
Merge branch 'libp2p:main' into feat/804-add-thin-waist-address
yashksaini-coder b363d1d
fix: update listening address handling to use all available interfaces
yashksaini-coder a2fcf33
refactor: migrate echo example test to use Trio for process handling
yashksaini-coder 9378490
fix: ensure loopback addresses are included in available interfaces
yashksaini-coder fe71c47
Merge branch 'main' into feat/804-add-thin-waist-address
seetadev 05b372b
Fix linting and type checking issues for Thin Waist feature
acul71 7f6469d
Merge remote-tracking branch 'acul71/feat/804-add-thin-waist-address'…
yashksaini-coder 55dd883
Merge branch 'main' into feat/804-add-thin-waist-address
yashksaini-coder a1b1624
fix: correct listening address variable in echo example and streamlin…
yashksaini-coder 3ff5728
Merge branch 'feat/804-add-thin-waist-address' of
yashksaini-coder 69d5274
fix: update listening address parameter in echo example to accept a list
yashksaini-coder 905f3a5
Merge branch 'main' into feat/804-add-thin-waist-address
yashksaini-coder 8a2d1f7
Merge branch 'main' into feat/804-add-thin-waist-address
yashksaini-coder c2c91b8
refactor: Improve comment formatting in test_echo_thin_waist.py for c…
yashksaini-coder 5b9bec8
fix: Enhance error handling in echo stream handler to manage stream c…
yashksaini-coder ed2716c
feat: Enhance echo example to dynamically find free ports and improve…
yashksaini-coder b6cbd78
Fix multi-address listening bug in swarm.listen()
acul71 3bd6d1f
doc: add newsfragment
acul71 b38d504
Merge pull request #1 from acul71/fix/multi-address-listening-bug
yashksaini-coder 88a1f0a
cherry pick https://github.com/acul71/py-libp2p-fork/blob/7a1198c8c6e…
yashksaini-coder cf48d2e
chore(app): Add 811.internal.rst
yashksaini-coder 75ffb79
fix: Ensure newline at end of file in address_validation.py and updat…
yashksaini-coder ed91ee0
refactor(app): 804 refactored find_free_port() in address_validation.py
yashksaini-coder 63a8458
add import to __init__
yashksaini-coder fde8c8f
Merge branch 'main' into feat/804-add-thin-waist-address
yashksaini-coder 6a0a7c2
chore(app): Add newsfragment for 811.feature.rst
yashksaini-coder 6c6adf7
chore(app): 804 Suggested changes - Remove the comment
yashksaini-coder c9795e3
Merge branch 'main' into feat/804-add-thin-waist-address
seetadev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Advanced demonstration of Thin Waist address handling. | ||
|
||
Run: | ||
python -m examples.advanced.network_discovery | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from multiaddr import Multiaddr | ||
|
||
try: | ||
from libp2p.utils.address_validation import ( | ||
expand_wildcard_address, | ||
get_available_interfaces, | ||
get_optimal_binding_address, | ||
) | ||
except ImportError: | ||
# Fallbacks if utilities are missing | ||
def get_available_interfaces(port: int, protocol: str = "tcp"): | ||
return [Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}")] | ||
|
||
def expand_wildcard_address(addr: Multiaddr, port: int | None = None): | ||
if port is None: | ||
return [addr] | ||
addr_str = str(addr).rsplit("/", 1)[0] | ||
return [Multiaddr(addr_str + f"/{port}")] | ||
|
||
def get_optimal_binding_address(port: int, protocol: str = "tcp"): | ||
return Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}") | ||
|
||
|
||
def main() -> None: | ||
port = 8080 | ||
interfaces = get_available_interfaces(port) | ||
print(f"Discovered interfaces for port {port}:") | ||
for a in interfaces: | ||
print(f" - {a}") | ||
|
||
wildcard_v4 = Multiaddr(f"/ip4/0.0.0.0/tcp/{port}") | ||
expanded_v4 = expand_wildcard_address(wildcard_v4) | ||
print("\nExpanded IPv4 wildcard:") | ||
for a in expanded_v4: | ||
print(f" - {a}") | ||
|
||
wildcard_v6 = Multiaddr(f"/ip6/::/tcp/{port}") | ||
expanded_v6 = expand_wildcard_address(wildcard_v6) | ||
print("\nExpanded IPv6 wildcard:") | ||
for a in expanded_v6: | ||
print(f" - {a}") | ||
|
||
print("\nOptimal binding address heuristic result:") | ||
print(f" -> {get_optimal_binding_address(port)}") | ||
|
||
override_port = 9000 | ||
overridden = expand_wildcard_address(wildcard_v4, port=override_port) | ||
print(f"\nPort override expansion to {override_port}:") | ||
for a in overridden: | ||
print(f" - {a}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
from __future__ import annotations | ||
|
||
from multiaddr import Multiaddr | ||
|
||
try: | ||
from multiaddr.utils import ( # type: ignore | ||
get_network_addrs, | ||
get_thin_waist_addresses, | ||
) | ||
|
||
_HAS_THIN_WAIST = True | ||
except ImportError: # pragma: no cover - only executed in older environments | ||
_HAS_THIN_WAIST = False | ||
get_thin_waist_addresses = None # type: ignore | ||
get_network_addrs = None # type: ignore | ||
|
||
|
||
def _safe_get_network_addrs(ip_version: int) -> list[str]: | ||
""" | ||
Internal safe wrapper. Returns a list of IP addresses for the requested IP version. | ||
Falls back to minimal defaults when Thin Waist helpers are missing. | ||
|
||
:param ip_version: 4 or 6 | ||
""" | ||
if _HAS_THIN_WAIST and get_network_addrs: | ||
try: | ||
return get_network_addrs(ip_version) or [] | ||
except Exception: # pragma: no cover - defensive | ||
return [] | ||
# Fallback behavior (very conservative) | ||
if ip_version == 4: | ||
return ["127.0.0.1"] | ||
if ip_version == 6: | ||
return ["::1"] | ||
return [] | ||
|
||
|
||
def _safe_expand(addr: Multiaddr, port: int | None = None) -> list[Multiaddr]: | ||
""" | ||
Internal safe expansion wrapper. Returns a list of Multiaddr objects. | ||
If Thin Waist isn't available, returns [addr] (identity). | ||
""" | ||
if _HAS_THIN_WAIST and get_thin_waist_addresses: | ||
try: | ||
if port is not None: | ||
return get_thin_waist_addresses(addr, port=port) or [] | ||
return get_thin_waist_addresses(addr) or [] | ||
except Exception: # pragma: no cover - defensive | ||
return [addr] | ||
return [addr] | ||
|
||
|
||
def get_available_interfaces(port: int, protocol: str = "tcp") -> list[Multiaddr]: | ||
""" | ||
Discover available network interfaces (IPv4 + IPv6 if supported) for binding. | ||
|
||
:param port: Port number to bind to. | ||
:param protocol: Transport protocol (e.g., "tcp" or "udp"). | ||
:return: List of Multiaddr objects representing candidate interface addresses. | ||
""" | ||
addrs: list[Multiaddr] = [] | ||
|
||
# IPv4 enumeration | ||
seen_v4: set[str] = set() | ||
|
||
for ip in _safe_get_network_addrs(4): | ||
seen_v4.add(ip) | ||
addrs.append(Multiaddr(f"/ip4/{ip}/{protocol}/{port}")) | ||
|
||
# Ensure IPv4 loopback is always included when IPv4 interfaces are discovered | ||
if seen_v4 and "127.0.0.1" not in seen_v4: | ||
addrs.append(Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}")) | ||
|
||
# TODO: IPv6 support temporarily disabled due to libp2p handshake issues | ||
# IPv6 connections fail during protocol negotiation (SecurityUpgradeFailure) | ||
# Re-enable IPv6 support once the following issues are resolved: | ||
# - libp2p security handshake over IPv6 | ||
# - multiselect protocol over IPv6 | ||
# - connection establishment over IPv6 | ||
# | ||
# seen_v6: set[str] = set() | ||
# for ip in _safe_get_network_addrs(6): | ||
# seen_v6.add(ip) | ||
# addrs.append(Multiaddr(f"/ip6/{ip}/{protocol}/{port}")) | ||
# | ||
# # Always include IPv6 loopback for testing purposes when IPv6 is available | ||
# # This ensures IPv6 functionality can be tested even without global IPv6 addresses | ||
# if "::1" not in seen_v6: | ||
# addrs.append(Multiaddr(f"/ip6/::1/{protocol}/{port}")) | ||
|
||
# Fallback if nothing discovered | ||
if not addrs: | ||
addrs.append(Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}")) | ||
|
||
return addrs | ||
|
||
|
||
def expand_wildcard_address( | ||
addr: Multiaddr, port: int | None = None | ||
) -> list[Multiaddr]: | ||
""" | ||
Expand a wildcard (e.g. /ip4/0.0.0.0/tcp/0) into all concrete interfaces. | ||
|
||
:param addr: Multiaddr to expand. | ||
:param port: Optional override for port selection. | ||
:return: List of concrete Multiaddr instances. | ||
""" | ||
expanded = _safe_expand(addr, port=port) | ||
if not expanded: # Safety fallback | ||
return [addr] | ||
return expanded | ||
|
||
|
||
def get_optimal_binding_address(port: int, protocol: str = "tcp") -> Multiaddr: | ||
""" | ||
Choose an optimal address for an example to bind to: | ||
- Prefer non-loopback IPv4 | ||
- Then non-loopback IPv6 | ||
- Fallback to loopback | ||
- Fallback to wildcard | ||
|
||
:param port: Port number. | ||
:param protocol: Transport protocol. | ||
:return: A single Multiaddr chosen heuristically. | ||
""" | ||
candidates = get_available_interfaces(port, protocol) | ||
|
||
def is_non_loopback(ma: Multiaddr) -> bool: | ||
s = str(ma) | ||
return not ("/ip4/127." in s or "/ip6/::1" in s) | ||
|
||
for c in candidates: | ||
if "/ip4/" in str(c) and is_non_loopback(c): | ||
return c | ||
for c in candidates: | ||
if "/ip6/" in str(c) and is_non_loopback(c): | ||
return c | ||
for c in candidates: | ||
if "/ip4/127." in str(c) or "/ip6/::1" in str(c): | ||
return c | ||
|
||
# As a final fallback, produce a wildcard | ||
return Multiaddr(f"/ip4/0.0.0.0/{protocol}/{port}") | ||
|
||
|
||
__all__ = [ | ||
"get_available_interfaces", | ||
"get_optimal_binding_address", | ||
"expand_wildcard_address", | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Add Thin Waist address validation utilities and integrate into echo example | ||
|
||
- Add ``libp2p/utils/address_validation.py`` with dynamic interface discovery | ||
- Implement ``get_available_interfaces()``, ``get_optimal_binding_address()``, and ``expand_wildcard_address()`` | ||
- Update echo example to use dynamic address discovery instead of hardcoded wildcard | ||
- Add safe fallbacks for environments lacking Thin Waist support | ||
- Temporarily disable IPv6 support due to libp2p handshake issues (TODO: re-enable when resolved) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Fix multi-address listening bug in swarm.listen() | ||
|
||
- Fix early return in swarm.listen() that prevented listening on all addresses | ||
- Add comprehensive tests for multi-address listening functionality | ||
- Ensure all available interfaces are properly bound and connectable |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.