go-libp2p-kad-dht/lookup.go

123 lines
2.9 KiB
Go
Raw Permalink Normal View History

package dht
import (
2016-09-30 10:24:03 -07:00
"context"
"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
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
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"
)
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
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))
}
2019-12-17 17:25:11 -05:00
return fmt.Sprintf("/%s/%s", proto, encStr), nil
}
func loggableKey(k string) logging.LoggableMap {
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 {
k = newKey
}
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),
}
}
// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
// to the given key
//
// If the context is canceled, this function will return the context error along
// with the closest K peers it has found so far.
func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan peer.ID, error) {
//TODO: I can break the interface! return []peer.ID
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()
2020-03-24 02:21:47 -04:00
lookupRes, err := dht.runLookupWithFollowup(ctx, dht.d, key,
2020-01-17 07:00:42 -08:00
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())
2020-01-17 07:00:42 -08:00
// For DHT query command
routing.PublishQueryEvent(ctx, &routing.QueryEvent{
Type: routing.PeerResponse,
ID: p,
Responses: peers,
})
2020-01-17 07:00:42 -08:00
return peers, err
},
func() bool { return false },
2020-01-17 07:00:42 -08:00
)
if err != nil {
return nil, err
}
2020-01-17 07:00:42 -08:00
out := make(chan peer.ID, dht.bucketSize)
defer close(out)
for _, p := range lookupRes.peers {
2020-01-17 07:00:42 -08:00
out <- p
}
if ctx.Err() == nil && lookupRes.completed {
2020-01-17 07:00:42 -08:00
// refresh the cpl for this key as the query was successful
dht.routingTable.ResetCplRefreshedAtForID(kb.ConvertKey(key), time.Now())
2020-01-17 07:00:42 -08:00
}
return out, ctx.Err()
}