fix: 'ipfs routing findpeer' explicitly fails when searching for self (#9903)
Nikhilesh Susarla committed
Jun 1, 2023 at 15:49 UTC
6eef0b4eefd6813dd6169d5a78ae57b9aa1cd90b
3 files changed
+21
core/commands/root.go
+1
@@ -17,6 +17,7 @@ import (
17
var log = logging.Logger("core/commands")
18
19
var ErrNotOnline = errors.New("this command must be run in online mode. Try running 'ipfs daemon' first")
20
+var ErrSelfUnsupported = errors.New("finding your own node in the DHT is currently not supported")
21
22
const (
23
RepoDirOption = "repo-dir"
core/commands/routing.go
+4
@@ -301,6 +301,10 @@ var findPeerRoutingCmd = &cmds.Command{
301
return err
302
}
303
304
+ if pid == nd.Identity {
305
+ return ErrSelfUnsupported
306
+ }
307
+
308
ctx, cancel := context.WithCancel(req.Context)
309
ctx, events := routing.RegisterForQueryEvents(ctx)
310
test/cli/routing_dht_test.go
+16
@@ -117,7 +117,23 @@ func testRoutingDHT(t *testing.T, enablePubsub bool) {
117
})
118
}
119
120
+func testSelfFindDHT(t *testing.T) {
121
+ t.Run("ipfs routing findpeer fails for self", func(t *testing.T) {
122
+ t.Parallel()
123
+ nodes := harness.NewT(t).NewNodes(1).Init()
124
+ nodes.ForEachPar(func(node *harness.Node) {
125
+ node.IPFS("config", "Routing.Type", "dht")
126
+ })
127
+
128
+ nodes.StartDaemons()
129
+
130
+ res := nodes[0].RunIPFS("dht", "findpeer", nodes[0].PeerID().String())
131
+ assert.Equal(t, 1, res.ExitCode())
132
+ })
133
+}
134
+
135
func TestRoutingDHT(t *testing.T) {
136
testRoutingDHT(t, false)
137
testRoutingDHT(t, true)
138
+ testSelfFindDHT(t)
139
}