Skip to content

Agent Mesh

sdk/python: dial a peer at the addresses given, whatever the peerstore holds

Antonio Ojeab4bdd4a20721
diff --git a/sdk/python/src/agent_mesh/host.py b/sdk/python/src/agent_mesh/host.py
index b82a0eb4..c68b0f46 100644
--- a/sdk/python/src/agent_mesh/host.py
+++ b/sdk/python/src/agent_mesh/host.py
@@ -34,6 +34,7 @@ from libp2p.crypto.ed25519 import create_new_key_pair
from libp2p.crypto.x25519 import create_new_key_pair as create_new_x25519_key_pair
from libp2p.custom_types import TProtocol
from libp2p.network.config import ConnectionConfig
+from libp2p.network.swarm import Swarm
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addr
from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID
@@ -135,7 +136,19 @@ HANGUP_GRACE = 1.0
async def dial(host: IHost, info: PeerInfo) -> None:
- """host.connect, bounded by DIAL_TIMEOUT."""
+ """host.connect at the addresses given, bounded by DIAL_TIMEOUT. What the
+ host remembers of the peer is dropped first. py-libp2p dials one address
+ per transport, the first its peerstore holds, and host.connect only
+ appends to that list: a router or provider that came back on another
+ address, a pod rescheduled with the same key, would be dialed at the old
+ one on every retry, whatever the caller had just resolved. The swarm's
+ negative cache is keyed by peer, not by address, and would refuse the new
+ address for a minute after the old one failed, so the peer is taken out
+ of it as well."""
+ host.get_peerstore().clear_addrs(info.peer_id)
+ network = host.get_network()
+ if isinstance(network, Swarm):
+ network.unblock_peer(info.peer_id)
try:
with trio.fail_after(DIAL_TIMEOUT):
await host.connect(info)
diff --git a/sdk/python/tests/test_discovery.py b/sdk/python/tests/test_discovery.py
index c2bd092c..b8b22ff9 100644
--- a/sdk/python/tests/test_discovery.py
+++ b/sdk/python/tests/test_discovery.py
@@ -22,6 +22,7 @@ import trio
import trio.testing
from libp2p.kad_dht.pb import kademlia_pb2 as kad
from libp2p.peer.id import ID
+from libp2p.peer.peerstore import PeerStore
from libp2p.utils.varint import encode_varint_prefixed, read_varint_prefixed_bytes
from agent_mesh.discovery import DHT_PROTOCOL, _QUERY_TIMEOUT, find_peer, find_providers
@@ -79,10 +80,17 @@ class _Host:
self._answers = answers
self.connected = set(answers)
self.dials = []
+ self.peerstore = PeerStore()
def get_id(self):
return SELF
+ def get_peerstore(self):
+ return self.peerstore
+
+ def get_network(self):
+ return None
+
def get_connected_peers(self):
return list(self.connected)
diff --git a/sdk/python/tests/test_host.py b/sdk/python/tests/test_host.py
index 80301896..fd6ab311 100644
--- a/sdk/python/tests/test_host.py
+++ b/sdk/python/tests/test_host.py
@@ -18,6 +18,7 @@ address through its TXT records and keep the transports this host has
stream must do: come back after the 256th one on a connection (py-libp2p
0.7 leaks its yamux backlog slots), or fail at a deadline."""
+import socket
import ssl
import struct
@@ -28,6 +29,7 @@ import trio.testing
from libp2p.custom_types import TProtocol
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addr
+from libp2p.peer.peerstore import PeerStore
from libp2p.stream_muxer.yamux.yamux import FLAG_SYN, YAMUX_HEADER_FORMAT
from libp2p.transport.websocket.transport import WebsocketTransport
from multiaddr.resolvers import DNSResolver
@@ -171,6 +173,12 @@ def test_a_dial_nobody_answers_ends_at_the_timeout():
unanswered. The dial ends at DIAL_TIMEOUT, not at the kernel's."""
class Host:
+ def get_peerstore(self):
+ return PeerStore()
+
+ def get_network(self):
+ return None
+
async def connect(self, info):
await trio.sleep_forever()
@@ -183,6 +191,34 @@ def test_a_dial_nobody_answers_ends_at_the_timeout():
trio.run(main, clock=trio.testing.MockClock(autojump_threshold=0))
+def test_a_peer_that_came_back_on_another_address_is_dialed_there():
+ """A router pod rescheduled keeps its key and gets a new IP; the member
+ re-resolves its name and dials again. py-libp2p dials one address per
+ transport, the first its peerstore holds, and host.connect appends: on
+ its own it would dial the old address on every retry, for as long as the
+ pod is gone. Its negative cache, keyed by peer, would then refuse the new
+ address for a minute after the old one failed."""
+
+ async def main():
+ server, server_listen = create_mesh_host(Identity.generate(), ["/ip4/127.0.0.1/tcp/0"])
+ client, _ = create_mesh_host(Identity.generate())
+ # Where the peer was: a port nobody listens on now.
+ with socket.socket() as s:
+ s.bind(("127.0.0.1", 0))
+ stale = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{s.getsockname()[1]}")
+ async with server.run(listen_addrs=server_listen), client.run(listen_addrs=[]):
+ peer = server.get_id()
+ # The peerstore holds the old address, and the swarm a failed dial to it.
+ with trio.fail_after(10), pytest.raises(Exception, match="no addresses established"):
+ await client.connect(PeerInfo(peer, [stale]))
+ with trio.fail_after(10):
+ await dial(client, await peer_info(server.get_addrs()[0]))
+ assert peer in client.get_connected_peers()
+ assert stale not in client.get_peerstore().addrs(peer)
+
+ trio.run(main)
+
+
ECHO = TProtocol("/test/echo/1.0.0")
diff --git a/sdk/python/tests/test_session.py b/sdk/python/tests/test_session.py
index d7584119..174a24e8 100644
--- a/sdk/python/tests/test_session.py
+++ b/sdk/python/tests/test_session.py
@@ -31,6 +31,7 @@ from libp2p.crypto.ed25519 import create_new_key_pair
from libp2p.custom_types import TProtocol
from libp2p.peer.id import ID
from libp2p.peer.peerinfo import info_from_p2p_addr
+from libp2p.peer.peerstore import PeerStore
from libp2p.security.tls.transport import PROTOCOL_ID as TLS_PROTOCOL_ID
from libp2p.security.tls.transport import TLSTransport
from libp2p.stream_muxer.yamux.yamux import PROTOCOL_ID as YAMUX_PROTOCOL_ID
@@ -145,9 +146,9 @@ def hop_handler(relay_addr: str, grants, ttl: int = 3600, events=None):
return handle
-async def start_router(nursery, role=ROLE_ROUTER, trusted=(CP_KEY,), grants=True, ttl=3600, events=None):
+async def start_router(nursery, role=ROLE_ROUTER, trusted=(CP_KEY,), grants=True, ttl=3600, events=None, identity=None):
"""events, when given, records ("auth", peer) per handshake and ("reserve", peer) per RESERVE."""
- identity = Identity.generate()
+ identity = identity or Identity.generate()
router = libp2p_host(identity)
router_biscuit = mint(identity.peer_id, role)
@@ -322,6 +323,12 @@ def test_a_router_nobody_answers_costs_join_one_dial_timeout(monkeypatch):
reserved = []
class Host:
+ def get_peerstore(self):
+ return PeerStore()
+
+ def get_network(self):
+ return None
+
async def connect(self, info):
if str(info.addrs[0]).startswith("/ip4/203.0.113.7/"):
await trio.sleep_forever()
@@ -525,6 +532,68 @@ def test_a_dropped_router_connection_is_reserved_again_before_the_ttl():
trio.run(with_timeout, 60, main)
+def test_a_router_that_came_back_on_another_address_is_reserved_on_again(monkeypatch):
+ """A router pod rescheduled keeps its key and gets a new IP; the name the
+ control plane hands out resolves to it. The member's connection went with
+ the old pod; the reservation loop dials the router by name again and must
+ land on the new address, not on the one the peerstore remembers from the
+ old pod, and advertise the relayed address the new pod lists."""
+ from multiaddr.resolvers import DNSResolver
+
+ class TXT:
+ def __init__(self, strings):
+ self.strings = strings
+
+ def __iter__(self):
+ for s in self.strings:
+ yield type("TXT", (), {"strings": [s.encode()]})()
+
+ def __len__(self):
+ return len(self.strings)
+
+ records: dict[str, list[str]] = {}
+
+ class FakeDNS:
+ async def resolve(self, name, rdtype):
+ return TXT(records.get(str(name).rstrip("."), []))
+
+ monkeypatch.setattr(DNSResolver, "__init__", lambda self: setattr(self, "_resolver", FakeDNS()))
+
+ async def wait_for(predicate):
+ while not predicate():
+ await trio.sleep(0.1)
+
+ async def main():
+ async with trio.open_nursery() as nursery:
+ identity = Identity.generate()
+ before_events, after_events = [], []
+ before, before_addr = await start_router(nursery, events=before_events, identity=identity)
+ after, after_addr = await start_router(nursery, events=after_events, identity=identity)
+ name = f"/dnsaddr/router.test/p2p/{identity.peer_id}"
+ records["_dnsaddr.router.test"] = [f"dnsaddr={before_addr}"]
+
+ mesh = AgentMesh.enroll("http://127.0.0.1:1", bootstrap_token="sbt", transport=fake_control_plane([name]))
+ async with mesh.join(refresh_lead=0, refresh_retry=0.5, reservation_check_interval=0.5) as session:
+ member = session.host.get_id()
+ assert before_events == [("auth", mesh.peer_id), ("reserve", mesh.peer_id)]
+ assert session.relay_addresses == [f"{before_addr}/p2p-circuit/p2p/{mesh.peer_id}"]
+
+ # The pod goes: its connection with it, and the name now resolves to the new one.
+ records["_dnsaddr.router.test"] = [f"dnsaddr={after_addr}"]
+ await before.disconnect(member)
+ await wait_for(lambda: member not in before.get_connected_peers())
+
+ reserve = ("reserve", mesh.peer_id)
+ await wait_for(lambda: before_events.count(reserve) + after_events.count(reserve) >= 2)
+ assert before_events.count(reserve) == 1, "dialed the old pod's address again"
+ assert after_events == [("auth", mesh.peer_id), reserve]
+ assert member in after.get_connected_peers()
+ assert session.relay_addresses == [f"{after_addr}/p2p-circuit/p2p/{mesh.peer_id}"]
+ nursery.cancel_scope.cancel()
+
+ trio.run(with_timeout, 60, main)
+
+
def test_join_renews_the_reservation_and_reauthenticates_after_a_disconnect():
"""go-libp2p's relay drops a reservation when its TTL passes and grants one
only to a peer authenticated on the current connection. The member renews