Skip to content

Commit

Permalink
fix: 'ipfs routing findpeer' explicitly fails when searching for self (
Browse files Browse the repository at this point in the history
  • Loading branch information
susarlanikhilesh authored Jun 1, 2023
1 parent dfd2448 commit 6eef0b4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions core/commands/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions test/cli/routing_dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6eef0b4

Please sign in to comment.