-
Notifications
You must be signed in to change notification settings - Fork 174
✨ 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
Draft
yashksaini-coder
wants to merge
5
commits into
libp2p:main
Choose a base branch
from
yashksaini-coder:feat/804-add-thin-waist-address
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 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 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,51 @@ | ||
import asyncio | ||
import contextlib | ||
import subprocess | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
# This test is intentionally lightweight and can be marked as 'integration'. | ||
# It ensures the echo example runs and prints the new Thin Waist lines. | ||
|
||
EXAMPLES_DIR = Path(__file__).parent.parent.parent / "examples" / "echo" | ||
|
||
|
||
@pytest.mark.timeout(20) | ||
def test_echo_example_starts_and_prints_thin_waist(monkeypatch, tmp_path): | ||
# We run: python examples/echo/echo.py -p 0 | ||
cmd = [sys.executable, str(EXAMPLES_DIR / "echo.py"), "-p", "0"] | ||
proc = subprocess.Popen( | ||
cmd, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True, | ||
) | ||
assert proc.stdout is not None | ||
|
||
found_selected = False | ||
found_interfaces = False | ||
start = time.time() | ||
|
||
try: | ||
while time.time() - start < 10: | ||
line = proc.stdout.readline() | ||
if not line: | ||
time.sleep(0.1) | ||
continue | ||
if "Selected binding address:" in line: | ||
found_selected = True | ||
if "Available candidate interfaces:" in line: | ||
found_interfaces = True | ||
if "Waiting for incoming connections..." in line: | ||
break | ||
finally: | ||
with contextlib.suppress(ProcessLookupError): | ||
proc.terminate() | ||
with contextlib.suppress(ProcessLookupError): | ||
proc.kill() | ||
|
||
assert found_selected, "Did not capture Thin Waist binding log line" | ||
assert found_interfaces, "Did not capture Thin Waist interfaces log line" |
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,56 @@ | ||
import os | ||
|
||
import pytest | ||
from multiaddr import Multiaddr | ||
|
||
from libp2p.utils.address_validation import ( | ||
get_available_interfaces, | ||
get_optimal_binding_address, | ||
expand_wildcard_address, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("proto", ["tcp"]) | ||
def test_get_available_interfaces(proto: str) -> None: | ||
interfaces = get_available_interfaces(0, protocol=proto) | ||
assert len(interfaces) > 0 | ||
for addr in interfaces: | ||
assert isinstance(addr, Multiaddr) | ||
assert f"/{proto}/" in str(addr) | ||
|
||
|
||
def test_get_optimal_binding_address() -> None: | ||
addr = get_optimal_binding_address(0) | ||
assert isinstance(addr, Multiaddr) | ||
# At least IPv4 or IPv6 prefix present | ||
s = str(addr) | ||
assert ("/ip4/" in s) or ("/ip6/" in s) | ||
|
||
|
||
def test_expand_wildcard_address_ipv4() -> None: | ||
wildcard = Multiaddr("/ip4/0.0.0.0/tcp/0") | ||
expanded = expand_wildcard_address(wildcard) | ||
assert len(expanded) > 0 | ||
for e in expanded: | ||
assert isinstance(e, Multiaddr) | ||
assert "/tcp/" in str(e) | ||
|
||
|
||
def test_expand_wildcard_address_port_override() -> None: | ||
wildcard = Multiaddr("/ip4/0.0.0.0/tcp/7000") | ||
overridden = expand_wildcard_address(wildcard, port=9001) | ||
assert len(overridden) > 0 | ||
for e in overridden: | ||
assert str(e).endswith("/tcp/9001") | ||
|
||
|
||
@pytest.mark.skipif( | ||
os.environ.get("NO_IPV6") == "1", | ||
reason="Environment disallows IPv6", | ||
) | ||
def test_expand_wildcard_address_ipv6() -> None: | ||
wildcard = Multiaddr("/ip6/::/tcp/0") | ||
expanded = expand_wildcard_address(wildcard) | ||
assert len(expanded) > 0 | ||
for e in expanded: | ||
assert "/ip6/" in str(e) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Trio we don't use asyncio (for uniformity in all code base)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okh got it