Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
node: try the routers that admitted us when a peer announced no circuit
Antonio Ojead0af1948553c
diff --git a/internal/node/mcp.go b/internal/node/mcp.goindex cc02756a..db9ff545 100644--- a/internal/node/mcp.go+++ b/internal/node/mcp.go@@ -20,6 +20,7 @@ import ("errors""fmt""net/http"+ "slices""sort""strings""time"@@ -477,7 +478,7 @@ func (n *AgentMeshNode) preparePeerAddrs(ctx context.Context, targetPeer peer.ID}}}- logger.Debugf("[Discovery] preparePeerAddrs for %s: found %d addrs in peerstore", targetPeer, len(addrs))+ logger.Debugf("[Discovery] preparePeerAddrs for %s: found %d addrs in peerstore: %v", targetPeer, len(addrs), addrs)var validAddrs []multiaddr.Multiaddrseen := make(map[string]struct{})@@ -548,11 +549,12 @@ func (n *AgentMeshNode) preparePeerAddrs(ctx context.Context, targetPeer peer.ID}}- // A peer nothing announced an address for is still reachable through the- // routers that admitted both of us: an SDK agent publishes no service and- // no record, and is named by peer ID alone. Every router relays for the- // peers it authenticated, so each is a path worth trying.- if len(validAddrs) == 0 {+ // A peer that announced no circuit is still reachable through the+ // routers that admitted both of us: an SDK agent publishes no address at+ // all, and a node's direct addresses may be on a network this one cannot+ // reach. Every router relays for the peers it authenticated, so each is+ // a path worth trying; libp2p dials direct addresses first and these after.+ if !slices.ContainsFunc(validAddrs, func(ma multiaddr.Multiaddr) bool { return circuitRelay(ma) != "" }) {for _, router := range n.authenticatedRouterIDs() {circuitAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s/p2p-circuit", router.String()))if err == nil && addUnique(circuitAddr) {diff --git a/internal/node/mcp_test.go b/internal/node/mcp_test.goindex a0814120..d4f34eea 100644--- a/internal/node/mcp_test.go+++ b/internal/node/mcp_test.go@@ -19,6 +19,8 @@ import ("fmt""net/http""net/http/httptest"+ "slices"+ "sort""testing""time"@@ -159,7 +161,9 @@ func TestResolveRelayAddresses(t *testing.T) {// A peer named by ID alone, with no address anywhere (an SDK agent publishes// no record), is given a circuit through every router that admitted this-// node, and through nothing else.+// node, and through nothing else. A peer known by direct addresses only is+// given the same circuits besides them: those addresses may be on a network+// this node cannot reach. A peer that announced a circuit is not.func TestPreparePeerAddrsFallsBackToAuthenticatedRouters(t *testing.T) {ctx := context.Background()mn := mocknet.New()@@ -195,15 +199,47 @@ func TestPreparePeerAddrsFallsBackToAuthenticatedRouters(t *testing.T) {BiscuitTimeout: 500 * time.Millisecond,authenticatedRouters: map[peer.ID]bool{admitted.ID(): true, stale.ID(): true},}+ circuit := fmt.Sprintf("/p2p/%s/p2p-circuit", admitted.ID())+ addrsOf := func(p peer.ID) []string {+ var got []string+ for _, a := range localHost.Peerstore().Addrs(p) {+ got = append(got, a.String())+ }+ sort.Strings(got)+ return got+ }+node.preparePeerAddrs(ctx, unknown.ID())+ if got := addrsOf(unknown.ID()); len(got) != 1 || got[0] != circuit {+ t.Fatalf("addresses for an unknown peer: got %v, want only a circuit through the connected router %v", got, circuit)+ }++ directOnly, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }+ direct := multiaddr.StringCast("/ip4/172.18.0.6/tcp/5002")+ localHost.Peerstore().AddAddr(directOnly.ID(), direct, peerstore.PermanentAddrTTL)+ node.preparePeerAddrs(ctx, directOnly.ID())+ want := []string{direct.String(), circuit}+ sort.Strings(want)+ if got := addrsOf(directOnly.ID()); !slices.Equal(got, want) {+ t.Fatalf("addresses for a peer with direct addresses only: got %v, want %v", got, want)+ }- var got []string- for _, a := range localHost.Peerstore().Addrs(unknown.ID()) {- got = append(got, a.String())+ announced, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)+ }+ other, err := mn.GenPeer()+ if err != nil {+ t.Fatal(err)}- want := []string{fmt.Sprintf("/p2p/%s/p2p-circuit", admitted.ID())}- if len(got) != 1 || got[0] != want[0] {- t.Fatalf("addresses for an unknown peer: got %v, want only a circuit through the connected router %v", got, want)+ ownCircuit := multiaddr.StringCast(fmt.Sprintf("/ip4/10.0.0.2/tcp/4501/p2p/%s/p2p-circuit", other.ID()))+ localHost.Peerstore().AddAddr(announced.ID(), ownCircuit, peerstore.PermanentAddrTTL)+ node.preparePeerAddrs(ctx, announced.ID())+ if got := addrsOf(announced.ID()); slices.Contains(got, circuit) {+ t.Fatalf("a peer that announced a circuit was also given ours: %v", got)}}