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-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"
|
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"
|
|
|
|
notif "github.com/libp2p/go-libp2p-routing/notifications"
|
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))
|
2017-03-03 23:31:43 -08:00
|
|
|
tablepeers := dht.routingTable.NearestPeers(kb.ConvertKey(key), AlphaValue)
|
2015-01-22 07:59:57 +00:00
|
|
|
if len(tablepeers) == 0 {
|
2015-03-17 21:03:37 -07:00
|
|
|
return nil, kb.ErrLookupFailure
|
2015-01-22 07:59:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 22:12:00 +09:00
|
|
|
out := make(chan peer.ID, dht.bucketSize)
|
2015-01-22 07:59:57 +00:00
|
|
|
|
2015-08-12 16:08:57 -07:00
|
|
|
// since the query doesnt actually pass our context down
|
|
|
|
// we have to hack this here. whyrusleeping isnt a huge fan of goprocess
|
|
|
|
parent := ctx
|
2015-01-22 07:59:57 +00:00
|
|
|
query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
|
|
|
|
// For DHT query command
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-01-22 18:18:41 +00:00
|
|
|
Type: notif.SendingQuery,
|
2015-01-22 07:59:57 +00:00
|
|
|
ID: p,
|
2015-01-22 18:18:41 +00:00
|
|
|
})
|
2015-01-22 07:59:57 +00:00
|
|
|
|
2018-02-19 19:25:44 -05:00
|
|
|
pmes, err := dht.findPeerSingle(ctx, p, peer.ID(key))
|
2015-01-22 07:59:57 +00:00
|
|
|
if err != nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debugf("error getting closer peers: %s", err)
|
2015-01-22 07:59:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-19 19:25:44 -05:00
|
|
|
peers := pb.PBPeersToPeerInfos(pmes.GetCloserPeers())
|
2015-01-22 07:59:57 +00:00
|
|
|
|
|
|
|
// For DHT query command
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-01-22 18:18:41 +00:00
|
|
|
Type: notif.PeerResponse,
|
2015-01-22 07:59:57 +00:00
|
|
|
ID: p,
|
2018-02-19 19:25:44 -05:00
|
|
|
Responses: peers,
|
2015-01-22 18:18:41 +00:00
|
|
|
})
|
2015-01-22 07:59:57 +00:00
|
|
|
|
2018-02-19 19:25:44 -05:00
|
|
|
return &dhtQueryResult{closerPeers: peers}, nil
|
2015-01-22 07:59:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
defer e.Done()
|
2019-06-14 15:55:36 -07:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
defer cancel()
|
2015-01-22 07:59:57 +00:00
|
|
|
// run it!
|
2019-06-14 15:55:36 -07:00
|
|
|
res, err := query.Run(timedCtx, tablepeers)
|
2015-01-22 07:59:57 +00:00
|
|
|
if err != nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debugf("closestPeers query run error: %s", err)
|
2015-01-22 07:59:57 +00:00
|
|
|
}
|
2017-03-05 12:08:20 -08:00
|
|
|
|
2018-06-06 09:49:03 -07:00
|
|
|
if res != nil && res.queriedSet != nil {
|
2019-12-17 01:25:57 +08:00
|
|
|
// refresh the cpl for this key as the query was successful
|
|
|
|
dht.routingTable.ResetCplRefreshedAtForID(kb.ConvertKey(key), time.Now())
|
2019-08-30 16:18:50 +08:00
|
|
|
|
2018-06-06 09:49:03 -07:00
|
|
|
sorted := kb.SortClosestPeers(res.queriedSet.Peers(), kb.ConvertKey(key))
|
2019-10-04 22:12:00 +09:00
|
|
|
l := len(sorted)
|
|
|
|
if l > dht.bucketSize {
|
|
|
|
sorted = sorted[:dht.bucketSize]
|
2017-03-05 12:08:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range sorted {
|
|
|
|
out <- p
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 07:59:57 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|