fix: 'ipfs routing findpeer' explicitly fails when searching for self (#9903)

This commit is contained in:
Nikhilesh Susarla 2023-06-01 15:49:29 +05:30 committed by GitHub
parent dfd244816a
commit 6eef0b4eef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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)
}