From 6eef0b4eefd6813dd6169d5a78ae57b9aa1cd90b Mon Sep 17 00:00:00 2001 From: Nikhilesh Susarla Date: Thu, 1 Jun 2023 15:49:29 +0530 Subject: [PATCH] fix: 'ipfs routing findpeer' explicitly fails when searching for self (#9903) --- core/commands/root.go | 1 + core/commands/routing.go | 4 ++++ test/cli/routing_dht_test.go | 16 ++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/core/commands/root.go b/core/commands/root.go index cfa2708e642..76e110e2b9b 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -17,6 +17,7 @@ import ( var log = logging.Logger("core/commands") var ErrNotOnline = errors.New("this command must be run in online mode. Try running 'ipfs daemon' first") +var ErrSelfUnsupported = errors.New("finding your own node in the DHT is currently not supported") const ( RepoDirOption = "repo-dir" diff --git a/core/commands/routing.go b/core/commands/routing.go index 76672c6f2fa..44ca2e29f1d 100644 --- a/core/commands/routing.go +++ b/core/commands/routing.go @@ -301,6 +301,10 @@ var findPeerRoutingCmd = &cmds.Command{ return err } + if pid == nd.Identity { + return ErrSelfUnsupported + } + ctx, cancel := context.WithCancel(req.Context) ctx, events := routing.RegisterForQueryEvents(ctx) diff --git a/test/cli/routing_dht_test.go b/test/cli/routing_dht_test.go index 7dc0ddfcb45..3a3adc51c51 100644 --- a/test/cli/routing_dht_test.go +++ b/test/cli/routing_dht_test.go @@ -117,7 +117,23 @@ func testRoutingDHT(t *testing.T, enablePubsub bool) { }) } +func testSelfFindDHT(t *testing.T) { + t.Run("ipfs routing findpeer fails for self", func(t *testing.T) { + t.Parallel() + nodes := harness.NewT(t).NewNodes(1).Init() + nodes.ForEachPar(func(node *harness.Node) { + node.IPFS("config", "Routing.Type", "dht") + }) + + nodes.StartDaemons() + + res := nodes[0].RunIPFS("dht", "findpeer", nodes[0].PeerID().String()) + assert.Equal(t, 1, res.ExitCode()) + }) +} + func TestRoutingDHT(t *testing.T) { testRoutingDHT(t, false) testRoutingDHT(t, true) + testSelfFindDHT(t) }