2015-01-22 07:59:57 +00:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2016-09-30 10:24:03 -07:00
|
|
|
"context"
|
2017-12-11 15:54:32 -08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-06-14 15:55:36 -07:00
|
|
|
"time"
|
2016-09-30 10:24:03 -07:00
|
|
|
|
2019-05-26 23:33:15 +01:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-12-26 19:53:52 -05:00
|
|
|
"github.com/libp2p/go-libp2p-core/routing"
|
2019-05-26 23:33:15 +01:00
|
|
|
|
2019-08-30 16:18:50 +08:00
|
|
|
"github.com/ipfs/go-cid"
|
2016-09-30 10:13:57 -07:00
|
|
|
logging "github.com/ipfs/go-log"
|
2020-01-17 07:00:42 -08:00
|
|
|
"github.com/libp2p/go-libp2p-kad-dht/kpeerset"
|
2019-02-12 14:12:53 +00:00
|
|
|
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
|
2016-09-02 20:21:23 +01:00
|
|
|
kb "github.com/libp2p/go-libp2p-kbucket"
|
2019-12-17 17:25:11 -05:00
|
|
|
"github.com/multiformats/go-base32"
|
|
|
|
"github.com/multiformats/go-multihash"
|
2015-01-22 07:59:57 +00:00
|
|
|
)
|
|
|
|
|
2017-12-11 15:54:32 -08:00
|
|
|
func tryFormatLoggableKey(k string) (string, error) {
|
|
|
|
if len(k) == 0 {
|
|
|
|
return "", fmt.Errorf("loggableKey is empty")
|
|
|
|
}
|
|
|
|
var proto, cstr string
|
|
|
|
if k[0] == '/' {
|
|
|
|
// it's a path (probably)
|
|
|
|
protoEnd := strings.IndexByte(k[1:], '/')
|
|
|
|
if protoEnd < 0 {
|
|
|
|
return k, fmt.Errorf("loggableKey starts with '/' but is not a path: %x", k)
|
|
|
|
}
|
|
|
|
proto = k[1 : protoEnd+1]
|
|
|
|
cstr = k[protoEnd+2:]
|
|
|
|
} else {
|
|
|
|
proto = "provider"
|
|
|
|
cstr = k
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:25:11 -05:00
|
|
|
var encStr string
|
2017-12-11 15:54:32 -08:00
|
|
|
c, err := cid.Cast([]byte(cstr))
|
2019-12-17 17:25:11 -05:00
|
|
|
if err == nil {
|
|
|
|
encStr = c.String()
|
|
|
|
} else {
|
|
|
|
encStr = base32.RawStdEncoding.EncodeToString([]byte(cstr))
|
2017-12-11 15:54:32 -08:00
|
|
|
}
|
2019-12-17 17:25:11 -05:00
|
|
|
|
|
|
|
return fmt.Sprintf("/%s/%s", proto, encStr), nil
|
2017-12-11 15:54:32 -08:00
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
func loggableKey(k string) logging.LoggableMap {
|
2017-12-11 15:54:32 -08:00
|
|
|
newKey, err := tryFormatLoggableKey(k)
|
2017-07-27 16:49:44 +02:00
|
|
|
if err != nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debug(err)
|
2017-07-27 16:49:44 +02:00
|
|
|
} else {
|
2017-12-11 15:54:32 -08:00
|
|
|
k = newKey
|
2017-07-27 13:24:08 +02:00
|
|
|
}
|
2017-12-11 15:54:32 -08:00
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
return logging.LoggableMap{
|
|
|
|
"key": k,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 17:25:11 -05:00
|
|
|
func multihashLoggableKey(mh multihash.Multihash) logging.LoggableMap {
|
|
|
|
return logging.LoggableMap{
|
|
|
|
"multihash": base32.RawStdEncoding.EncodeToString(mh),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 07:59:57 +00:00
|
|
|
// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
|
|
|
|
// to the given key
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan peer.ID, error) {
|
2019-02-01 17:46:46 +11:00
|
|
|
e := logger.EventBegin(ctx, "getClosestPeers", loggableKey(key))
|
2020-01-17 07:00:42 -08:00
|
|
|
defer e.Done()
|
|
|
|
|
|
|
|
queries := dht.runDisjointQueries(ctx, dht.d, key,
|
|
|
|
func(ctx context.Context, p peer.ID) ([]*peer.AddrInfo, error) {
|
|
|
|
// For DHT query command
|
|
|
|
routing.PublishQueryEvent(ctx, &routing.QueryEvent{
|
|
|
|
Type: routing.SendingQuery,
|
|
|
|
ID: p,
|
|
|
|
})
|
|
|
|
|
|
|
|
pmes, err := dht.findPeerSingle(ctx, p, peer.ID(key))
|
|
|
|
if err != nil {
|
|
|
|
logger.Debugf("error getting closer peers: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peers := pb.PBPeersToPeerInfos(pmes.GetCloserPeers())
|
2015-01-22 07:59:57 +00:00
|
|
|
|
2020-01-17 07:00:42 -08:00
|
|
|
// For DHT query command
|
|
|
|
routing.PublishQueryEvent(ctx, &routing.QueryEvent{
|
|
|
|
Type: routing.PeerResponse,
|
|
|
|
ID: p,
|
|
|
|
Responses: peers,
|
|
|
|
})
|
2015-01-22 07:59:57 +00:00
|
|
|
|
2020-01-17 07:00:42 -08:00
|
|
|
return peers, err
|
|
|
|
},
|
|
|
|
func(peerset *kpeerset.SortedPeerset) bool { return false },
|
|
|
|
)
|
2017-03-05 12:08:20 -08:00
|
|
|
|
2020-01-17 07:00:42 -08:00
|
|
|
out := make(chan peer.ID, dht.bucketSize)
|
|
|
|
defer close(out)
|
2019-08-30 16:18:50 +08:00
|
|
|
|
2020-01-17 07:00:42 -08:00
|
|
|
allPeers := make([]peer.ID, 0, dht.bucketSize*dht.d)
|
|
|
|
for _, q := range queries {
|
|
|
|
allPeers = append(allPeers, q.localPeers.TopK()...)
|
|
|
|
}
|
2017-03-05 12:08:20 -08:00
|
|
|
|
2020-01-17 07:00:42 -08:00
|
|
|
kadID := kb.ConvertKey(key)
|
|
|
|
allPeers = kb.SortClosestPeers(allPeers, kadID)
|
|
|
|
for i, p := range allPeers {
|
|
|
|
if i == dht.bucketSize {
|
|
|
|
break
|
2017-03-05 12:08:20 -08:00
|
|
|
}
|
2020-01-17 07:00:42 -08:00
|
|
|
out <- p
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
// refresh the cpl for this key as the query was successful
|
|
|
|
dht.routingTable.ResetCplRefreshedAtForID(kadID, time.Now())
|
|
|
|
}
|
2015-01-22 07:59:57 +00:00
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|