lookup.go 2.83 KB
Newer Older
1 2 3
package dht

import (
Jeromy's avatar
Jeromy committed
4 5
	"context"

6
	logging "github.com/ipfs/go-log"
George Antoniadis's avatar
George Antoniadis committed
7
	kb "github.com/libp2p/go-libp2p-kbucket"
8 9 10
	peer "github.com/libp2p/go-libp2p-peer"
	pset "github.com/libp2p/go-libp2p-peer/peerset"
	pstore "github.com/libp2p/go-libp2p-peerstore"
George Antoniadis's avatar
George Antoniadis committed
11
	notif "github.com/libp2p/go-libp2p-routing/notifications"
12 13
)

14
// Required in order for proper JSON marshaling
Jeromy's avatar
Jeromy committed
15 16
func pointerizePeerInfos(pis []pstore.PeerInfo) []*pstore.PeerInfo {
	out := make([]*pstore.PeerInfo, len(pis))
17 18 19 20 21 22 23
	for i, p := range pis {
		np := p
		out[i] = &np
	}
	return out
}

24 25 26 27 28 29
func loggableKey(k string) logging.LoggableMap {
	return logging.LoggableMap{
		"key": k,
	}
}

30 31
// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
// to the given key
32 33
func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan peer.ID, error) {
	e := log.EventBegin(ctx, "getClosestPeers", loggableKey(key))
34
	tablepeers := dht.routingTable.NearestPeers(kb.ConvertKey(key), AlphaValue)
35
	if len(tablepeers) == 0 {
36
		return nil, kb.ErrLookupFailure
37 38 39 40 41 42 43 44 45 46 47 48 49 50
	}

	out := make(chan peer.ID, KValue)
	peerset := pset.NewLimited(KValue)

	for _, p := range tablepeers {
		select {
		case out <- p:
		case <-ctx.Done():
			return nil, ctx.Err()
		}
		peerset.Add(p)
	}

Jeromy's avatar
Jeromy committed
51 52 53
	// since the query doesnt actually pass our context down
	// we have to hack this here. whyrusleeping isnt a huge fan of goprocess
	parent := ctx
54 55
	query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
		// For DHT query command
Jeromy's avatar
Jeromy committed
56
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
57
			Type: notif.SendingQuery,
58
			ID:   p,
59
		})
60 61 62

		closer, err := dht.closerPeersSingle(ctx, key, p)
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63
			log.Debugf("error getting closer peers: %s", err)
64 65 66
			return nil, err
		}

Jeromy's avatar
Jeromy committed
67
		var filtered []pstore.PeerInfo
68
		for _, clp := range closer {
69
			if peerset.TryAdd(clp) {
70 71 72 73 74 75 76 77 78 79
				select {
				case out <- clp:
				case <-ctx.Done():
					return nil, ctx.Err()
				}
				filtered = append(filtered, dht.peerstore.PeerInfo(clp))
			}
		}

		// For DHT query command
Jeromy's avatar
Jeromy committed
80
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
81
			Type:      notif.PeerResponse,
82 83
			ID:        p,
			Responses: pointerizePeerInfos(filtered),
84
		})
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

		return &dhtQueryResult{closerPeers: filtered}, nil
	})

	go func() {
		defer close(out)
		defer e.Done()
		// run it!
		_, err := query.Run(ctx, tablepeers)
		if err != nil {
			log.Debugf("closestPeers query run error: %s", err)
		}
	}()

	return out, nil
}

102
func (dht *IpfsDHT) closerPeersSingle(ctx context.Context, key string, p peer.ID) ([]peer.ID, error) {
103 104 105 106 107 108 109 110 111
	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
Jeromy's avatar
Jeromy committed
112
			dht.peerstore.AddAddrs(pid, pbp.Addresses(), pstore.TempAddrTTL)
113 114 115 116 117
			out = append(out, pid)
		}
	}
	return out, nil
}