From 2dfee68f20fadd32ebc85bfeca4fdcc2d5aa3a4e Mon Sep 17 00:00:00 2001 From: "sumanjeet0012@gmail.com" Date: Thu, 10 Jul 2025 19:24:09 +0530 Subject: [PATCH] Refactor bootstrap discovery to use async methods and update bootstrap peers list --- examples/bootstrap/bootstrap.py | 7 ++++++- libp2p/discovery/bootstrap/bootstrap.py | 26 +++++++++++++++++++------ libp2p/host/basic_host.py | 2 +- pyproject.toml | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/examples/bootstrap/bootstrap.py b/examples/bootstrap/bootstrap.py index 2ab24d54..dce8d4a3 100644 --- a/examples/bootstrap/bootstrap.py +++ b/examples/bootstrap/bootstrap.py @@ -29,8 +29,13 @@ def on_peer_discovery(peer_info: PeerInfo) -> None: logger.info(f" Addresses: {[str(addr) for addr in peer_info.addrs]}") -# Example bootstrap peers ( valid IPFS bootstrap nodes) +# Example bootstrap peers BOOTSTRAP_PEERS = [ + "/dnsaddr/github.com/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/dnsaddr/cloudflare.com/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/dnsaddr/google.com/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", + "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb", "/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", "/ip6/2604:a880:1:20::203:d001/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM", "/ip4/128.199.219.111/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", diff --git a/libp2p/discovery/bootstrap/bootstrap.py b/libp2p/discovery/bootstrap/bootstrap.py index 5844eb53..ef09e3b5 100644 --- a/libp2p/discovery/bootstrap/bootstrap.py +++ b/libp2p/discovery/bootstrap/bootstrap.py @@ -22,7 +22,7 @@ class BootstrapDiscovery: self.bootstrap_addrs = bootstrap_addrs or [] self.discovered_peers: set[str] = set() - def start(self) -> None: + async def start(self) -> None: """Process bootstrap addresses and emit peer discovery events.""" logger.debug( f"Starting bootstrap discovery with " @@ -34,7 +34,7 @@ class BootstrapDiscovery: for addr_str in self.bootstrap_addrs: try: - self._process_bootstrap_addr(addr_str) + await self._process_bootstrap_addr(addr_str) except Exception as e: logger.debug(f"Failed to process bootstrap address {addr_str}: {e}") @@ -43,19 +43,33 @@ class BootstrapDiscovery: logger.debug("Stopping bootstrap discovery") self.discovered_peers.clear() - def _process_bootstrap_addr(self, addr_str: str) -> None: + async def _process_bootstrap_addr(self, addr_str: str) -> None: """Convert string address to PeerInfo and add to peerstore.""" try: multiaddr = Multiaddr(addr_str) except Exception as e: logger.debug(f"Invalid multiaddr format '{addr_str}': {e}") return + if (self.is_dns_addr(multiaddr)): + resolved_addrs = await multiaddr.resolve() + for resolved_addr in resolved_addrs: + if resolved_addr == multiaddr: + return + self.add_addr(Multiaddr(resolved_addr)) + + self.add_addr(multiaddr) + def is_dns_addr(self, addr: Multiaddr) -> bool: + """Check if the address is a DNS address.""" + return any(protocol.name == "dnsaddr" for protocol in addr.protocols()) + + def add_addr(self, addr: Multiaddr) -> None: + """Add a peer to the peerstore and emit discovery event.""" # Extract peer info from multiaddr try: - peer_info = info_from_p2p_addr(multiaddr) + peer_info = info_from_p2p_addr(addr) except Exception as e: - logger.debug(f"Failed to extract peer info from '{addr_str}': {e}") + logger.debug(f"Failed to extract peer info from '{addr}': {e}") return # Skip if it's our own peer @@ -75,4 +89,4 @@ class BootstrapDiscovery: self.discovered_peers.add(str(peer_info.peer_id)) # Emit peer discovery event - peerDiscovery.emit_peer_discovered(peer_info) + peerDiscovery.emit_peer_discovered(peer_info) \ No newline at end of file diff --git a/libp2p/host/basic_host.py b/libp2p/host/basic_host.py index b3b0ecc7..70e41953 100644 --- a/libp2p/host/basic_host.py +++ b/libp2p/host/basic_host.py @@ -178,7 +178,7 @@ class BasicHost(IHost): self.mDNS.start() if hasattr(self, "bootstrap") and self.bootstrap is not None: logger.debug("Starting Bootstrap Discovery") - self.bootstrap.start() + await self.bootstrap.start() try: yield finally: diff --git a/pyproject.toml b/pyproject.toml index 604949fb..1cc9bfc8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ dependencies = [ "exceptiongroup>=1.2.0; python_version < '3.11'", "grpcio>=1.41.0", "lru-dict>=1.1.6", - "multiaddr>=0.0.9", + "multiaddr @ git+https://github.com/multiformats/py-multiaddr.git", "mypy-protobuf>=3.0.0", "noiseprotocol>=0.3.0", "protobuf>=4.21.0,<5.0.0",