Merge branch 'main' into todo/handletimeout

This commit is contained in:
Manu Sheel Gupta
2025-07-26 20:54:53 +05:30
committed by GitHub
14 changed files with 413 additions and 4 deletions

View File

@ -0,0 +1 @@
"""Discovery tests for py-libp2p."""

View File

@ -0,0 +1 @@
"""Bootstrap discovery tests for py-libp2p."""

View File

@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""
Test the full bootstrap discovery integration
"""
import secrets
import pytest
from libp2p import new_host
from libp2p.crypto.secp256k1 import create_new_key_pair
from libp2p.host.basic_host import BasicHost
@pytest.mark.trio
async def test_bootstrap_integration():
"""Test bootstrap integration with new_host"""
# Test bootstrap addresses
bootstrap_addrs = [
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SznbYGzPwp8qDrq",
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
]
# Generate key pair
secret = secrets.token_bytes(32)
key_pair = create_new_key_pair(secret)
# Create host with bootstrap
host = new_host(key_pair=key_pair, bootstrap=bootstrap_addrs)
# Verify bootstrap discovery is set up (cast to BasicHost for type checking)
assert isinstance(host, BasicHost), "Host should be a BasicHost instance"
assert hasattr(host, "bootstrap"), "Host should have bootstrap attribute"
assert host.bootstrap is not None, "Bootstrap discovery should be initialized"
assert len(host.bootstrap.bootstrap_addrs) == len(bootstrap_addrs), (
"Bootstrap addresses should match"
)
def test_bootstrap_no_addresses():
"""Test that bootstrap is not initialized when no addresses provided"""
secret = secrets.token_bytes(32)
key_pair = create_new_key_pair(secret)
# Create host without bootstrap
host = new_host(key_pair=key_pair)
# Verify bootstrap is not initialized
assert isinstance(host, BasicHost)
assert not hasattr(host, "bootstrap") or host.bootstrap is None, (
"Bootstrap should not be initialized when no addresses provided"
)

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
"""
Test bootstrap address validation
"""
from libp2p.discovery.bootstrap.utils import (
parse_bootstrap_peer_info,
validate_bootstrap_addresses,
)
def test_validate_addresses():
"""Test validation with a mix of valid and invalid addresses in one list."""
addresses = [
# Valid - using proper peer IDs
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
# Invalid
"invalid-address",
"/ip4/192.168.1.1/tcp/4001", # Missing p2p part
"", # Empty
"/ip4/127.0.0.1/tcp/4001/p2p/InvalidPeerID", # Bad peer ID
]
valid_expected = [
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM",
]
validated = validate_bootstrap_addresses(addresses)
assert validated == valid_expected, (
f"Expected only valid addresses, got: {validated}"
)
for addr in addresses:
peer_info = parse_bootstrap_peer_info(addr)
if addr in valid_expected:
assert peer_info is not None and peer_info.peer_id is not None, (
f"Should parse valid address: {addr}"
)
else:
assert peer_info is None, f"Should not parse invalid address: {addr}"