2015-01-22 07:59:57 +00:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2016-09-30 10:24:03 -07:00
|
|
|
"context"
|
|
|
|
|
2017-07-27 13:24:08 +02:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2016-09-30 10:13:57 -07:00
|
|
|
logging "github.com/ipfs/go-log"
|
2016-09-02 20:21:23 +01:00
|
|
|
kb "github.com/libp2p/go-libp2p-kbucket"
|
2016-10-05 12:34:28 -07:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
2016-09-02 20:21:23 +01:00
|
|
|
notif "github.com/libp2p/go-libp2p-routing/notifications"
|
2015-01-22 07:59:57 +00:00
|
|
|
)
|
|
|
|
|
2015-01-22 18:18:41 +00:00
|
|
|
// Required in order for proper JSON marshaling
|
2016-06-01 15:51:39 -07:00
|
|
|
func pointerizePeerInfos(pis []pstore.PeerInfo) []*pstore.PeerInfo {
|
|
|
|
out := make([]*pstore.PeerInfo, len(pis))
|
2015-01-22 07:59:57 +00:00
|
|
|
for i, p := range pis {
|
|
|
|
np := p
|
|
|
|
out[i] = &np
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
func toPeerInfos(ps []peer.ID) []*pstore.PeerInfo {
|
|
|
|
out := make([]*pstore.PeerInfo, len(ps))
|
|
|
|
for i, p := range ps {
|
|
|
|
out[i] = &pstore.PeerInfo{ID: p}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
func loggableKey(k string) logging.LoggableMap {
|
2017-07-27 13:24:08 +02:00
|
|
|
if cid, err := cid.Cast([]byte(k)); err == nil {
|
|
|
|
k = cid.String()
|
|
|
|
}
|
2016-09-30 10:13:57 -07:00
|
|
|
return logging.LoggableMap{
|
|
|
|
"key": k,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
e := log.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
|
|
|
}
|
|
|
|
|
|
|
|
out := make(chan peer.ID, KValue)
|
|
|
|
|
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
|
|
|
|
|
|
|
closer, err := dht.closerPeersSingle(ctx, key, p)
|
|
|
|
if err != nil {
|
2015-01-26 19:12:12 -08:00
|
|
|
log.Debugf("error getting closer peers: %s", err)
|
2015-01-22 07:59:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
peerinfos := toPeerInfos(closer)
|
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,
|
2017-03-05 12:08:20 -08:00
|
|
|
Responses: peerinfos, // todo: remove need for this pointerize thing
|
2015-01-22 18:18:41 +00:00
|
|
|
})
|
2015-01-22 07:59:57 +00:00
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
return &dhtQueryResult{closerPeers: peerinfos}, nil
|
2015-01-22 07:59:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
defer e.Done()
|
|
|
|
// run it!
|
2017-03-05 12:08:20 -08:00
|
|
|
res, err := query.Run(ctx, tablepeers)
|
2015-01-22 07:59:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("closestPeers query run error: %s", err)
|
|
|
|
}
|
2017-03-05 12:08:20 -08:00
|
|
|
|
|
|
|
if res != nil && res.finalSet != nil {
|
|
|
|
sorted := kb.SortClosestPeers(res.finalSet.Peers(), kb.ConvertKey(key))
|
|
|
|
if len(sorted) > KValue {
|
|
|
|
sorted = sorted[:KValue]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range sorted {
|
|
|
|
out <- p
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 07:59:57 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) closerPeersSingle(ctx context.Context, key string, p peer.ID) ([]peer.ID, error) {
|
2015-01-22 07:59:57 +00:00
|
|
|
pmes, err := dht.findPeerSingle(ctx, p, peer.ID(key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []peer.ID
|
|
|
|
for _, pbp := range pmes.GetCloserPeers() {
|
|
|
|
pid := peer.ID(pbp.GetId())
|
|
|
|
if pid != dht.self { // dont add self
|
2016-06-01 15:51:39 -07:00
|
|
|
dht.peerstore.AddAddrs(pid, pbp.Addresses(), pstore.TempAddrTTL)
|
2015-01-22 07:59:57 +00:00
|
|
|
out = append(out, pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|