Skip to content

Agent Mesh

Add regression tests for the revoked-peer skips

Tomas Tormo8182c1048f36
diff --git a/internal/node/node_test.go b/internal/node/node_test.go
index 2352db3f..c86337ae 100644
--- a/internal/node/node_test.go
+++ b/internal/node/node_test.go
@@ -29,8 +29,13 @@ import (
"github.com/google/sam/internal/ratelimit"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/libp2p/go-libp2p"
+ dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/crypto"
+ "github.com/libp2p/go-libp2p/core/host"
+ "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
+ "github.com/libp2p/go-libp2p/p2p/discovery/routing"
+ "github.com/libp2p/go-libp2p/p2p/discovery/util"
"github.com/libp2p/go-msgio"
"github.com/multiformats/go-multiaddr"
"google.golang.org/protobuf/proto"
@@ -761,3 +766,126 @@ func TestNewSamNode_DHTOptions(t *testing.T) {
t.Errorf("expected DiscoveryConcurrency to be 5, got %d", node.config.DiscoveryConcurrency)
}
}
+
+// newDHTHost returns a host wired to a DHT on the /sam prefix. Client mode
+// keeps the host out of its peers' routing tables, so nothing dials it except
+// the code under test.
+func newDHTHost(t *testing.T, mode dht.ModeOpt) (host.Host, *dht.IpfsDHT) {
+ t.Helper()
+ h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = h.Close() })
+ d, err := dht.New(h, dht.Mode(mode), dht.ProtocolPrefix("/sam"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = d.Close() })
+ return h, d
+}
+
+// waitForRoutingTable polls until the DHT has learned about a peer.
+func waitForRoutingTable(t *testing.T, d *dht.IpfsDHT) {
+ t.Helper()
+ deadline := time.Now().Add(5 * time.Second)
+ for d.RoutingTable().Size() == 0 {
+ if time.Now().After(deadline) {
+ t.Fatalf("routing table still empty after 5s")
+ }
+ time.Sleep(20 * time.Millisecond)
+ }
+}
+
+func TestFindProvidersSkipsRevoked(t *testing.T) {
+ ctx := context.Background()
+
+ hostA, dhtA := newDHTHost(t, dht.ModeServer)
+ hostB, dhtB := newDHTHost(t, dht.ModeServer)
+ if err := hostA.Connect(ctx, peer.AddrInfo{ID: hostB.ID(), Addrs: hostB.Addrs()}); err != nil {
+ t.Fatal(err)
+ }
+ waitForRoutingTable(t, dhtA)
+ waitForRoutingTable(t, dhtB)
+
+ c, err := serviceNameToCID(api.ServiceType_SERVICE_TYPE_MCP, "revoked-svc")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := dhtB.Provide(ctx, c, true); err != nil {
+ t.Fatal(err)
+ }
+
+ revokedCache, err := lru.New[string, int64](10)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node := &SamNode{Host: hostA, DHT: dhtA, revokedPeers: revokedCache}
+
+ // Positive control: the provider record is findable before the ban, so a
+ // later empty result is the guard and not a lookup that never worked.
+ providers, err := node.FindProvidersByName(ctx, api.ServiceType_SERVICE_TYPE_MCP, "revoked-svc")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(providers) != 1 || providers[0].ID != hostB.ID() {
+ t.Fatalf("expected to find %s as provider, got %v", hostB.ID(), providers)
+ }
+
+ node.revokedPeers.Add(hostB.ID().String(), time.Now().UnixMilli())
+
+ providers, err = node.FindProvidersByName(ctx, api.ServiceType_SERVICE_TYPE_MCP, "revoked-svc")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(providers) != 0 {
+ t.Errorf("revoked peer returned as provider: %v", providers)
+ }
+}
+
+func TestStartDiscoverySkipsRevoked(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ const meshID = "test-mesh"
+
+ // Only the bootstrap host runs a server DHT: the others are clients, so
+ // nothing dials A and every connection A opens comes from startDiscovery.
+ bootstrapHost, _ := newDHTHost(t, dht.ModeServer)
+ hostA, dhtA := newDHTHost(t, dht.ModeClient)
+ revokedHost, revokedDHT := newDHTHost(t, dht.ModeClient)
+ healthyHost, healthyDHT := newDHTHost(t, dht.ModeClient)
+
+ for _, h := range []host.Host{hostA, revokedHost, healthyHost} {
+ if err := h.Connect(ctx, peer.AddrInfo{ID: bootstrapHost.ID(), Addrs: bootstrapHost.Addrs()}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, d := range []*dht.IpfsDHT{dhtA, revokedDHT, healthyDHT} {
+ waitForRoutingTable(t, d)
+ util.Advertise(ctx, routing.NewRoutingDiscovery(d), meshID)
+ }
+
+ revokedCache, err := lru.New[string, int64](10)
+ if err != nil {
+ t.Fatal(err)
+ }
+ revokedCache.Add(revokedHost.ID().String(), time.Now().UnixMilli())
+ node := &SamNode{Host: hostA, DHT: dhtA, revokedPeers: revokedCache}
+
+ go node.startDiscovery(ctx, meshID, 200*time.Millisecond)
+
+ // Both advertisers come back in the same FindPeers result, so the healthy
+ // one connecting proves the revoked one was offered and skipped.
+ deadline := time.Now().Add(20 * time.Second)
+ for hostA.Network().Connectedness(healthyHost.ID()) != network.Connected {
+ if time.Now().After(deadline) {
+ t.Fatalf("healthy peer never connected; discovery did not run")
+ }
+ time.Sleep(50 * time.Millisecond)
+ }
+
+ if c := hostA.Network().Connectedness(revokedHost.ID()); c == network.Connected {
+ t.Errorf("revoked peer was dialled from discovery (connectedness %s)", c)
+ }
+}