Skip to content

Agent Mesh

Add regression test for the revoked-peer discovery skip

Tomas Tormoed744e00b22a
diff --git a/internal/node/node.go b/internal/node/node.go
index 792d1214..9fe5c597 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -1536,9 +1536,7 @@ func (n *SamNode) startDiscovery(ctx context.Context, meshID string, interval ti
continue
}
for p := range peers {
- // Revoked peers keep advertising the mesh rendezvous; without this
- // every tick re-dials them and the gater denies it.
- if p.ID == n.Host.ID() || n.peerIsRevoked(p.ID) {
+ if !n.shouldDialDiscovered(p.ID) {
continue
}
@@ -1577,6 +1575,13 @@ func (n *SamNode) startDiscovery(ctx context.Context, meshID string, interval ti
}
}
+// shouldDialDiscovered filters the DHT discovery results. Revoked peers keep
+// advertising the mesh rendezvous, so without this every tick re-dials them
+// and the connection gater denies it.
+func (n *SamNode) shouldDialDiscovered(p peer.ID) bool {
+ return p != n.Host.ID() && !n.peerIsRevoked(p)
+}
+
// awaitRoutingTable blocks until the DHT routing table has a peer, ctx ends
// or bound elapses. DHT.Bootstrap only triggers a refresh: the router enters
// the table after an asynchronous FIND_NODE liveliness check that nothing
diff --git a/internal/node/node_test.go b/internal/node/node_test.go
index 2352db3f..997560cf 100644
--- a/internal/node/node_test.go
+++ b/internal/node/node_test.go
@@ -761,3 +761,46 @@ func TestNewSamNode_DHTOptions(t *testing.T) {
t.Errorf("expected DiscoveryConcurrency to be 5, got %d", node.config.DiscoveryConcurrency)
}
}
+
+func TestShouldDialDiscovered(t *testing.T) {
+ host, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = host.Close() }()
+
+ revokedCache, err := lru.New[string, int64](10)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node := &SamNode{Host: host, revokedPeers: revokedCache}
+
+ _, revokedPub, err := crypto.GenerateEd25519Key(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ revoked, err := peer.IDFromPublicKey(revokedPub)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node.revokedPeers.Add(revoked.String(), time.Now().UnixMilli())
+
+ _, healthyPub, err := crypto.GenerateEd25519Key(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ healthy, err := peer.IDFromPublicKey(healthyPub)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if node.shouldDialDiscovered(revoked) {
+ t.Error("revoked peer should not be dialled from DHT discovery")
+ }
+ if node.shouldDialDiscovered(host.ID()) {
+ t.Error("self should not be dialled from DHT discovery")
+ }
+ if !node.shouldDialDiscovered(healthy) {
+ t.Error("healthy peer should be dialled from DHT discovery")
+ }
+}