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

import (
4
	key "github.com/ipfs/go-ipfs/blocks/key"
5 6
	notif "github.com/ipfs/go-ipfs/notifications"
	kb "github.com/ipfs/go-ipfs/routing/kbucket"
7
	pset "github.com/ipfs/go-ipfs/thirdparty/peerset"
Jeromy's avatar
Jeromy committed
8

9 10 11
	peer "github.com/ipfs/go-libp2p-peer"
	pstore "github.com/ipfs/go-libp2p-peerstore"
	context "golang.org/x/net/context"
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 24 25
	for i, p := range pis {
		np := p
		out[i] = &np
	}
	return out
}

// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
// to the given key
26
func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key key.Key) (<-chan peer.ID, error) {
27
	e := log.EventBegin(ctx, "getClosestPeers", &key)
Jeromy's avatar
Jeromy committed
28
	tablepeers := dht.routingTable.NearestPeers(kb.ConvertKey(key), KValue)
29
	if len(tablepeers) == 0 {
30
		return nil, kb.ErrLookupFailure
31 32 33 34 35 36 37 38 39 40 41 42 43 44
	}

	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
45 46 47
	// since the query doesnt actually pass our context down
	// we have to hack this here. whyrusleeping isnt a huge fan of goprocess
	parent := ctx
48 49
	query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
		// For DHT query command
Jeromy's avatar
Jeromy committed
50
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
51
			Type: notif.SendingQuery,
52
			ID:   p,
53
		})
54 55 56

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

Jeromy's avatar
Jeromy committed
61
		var filtered []pstore.PeerInfo
62 63 64 65 66 67 68 69 70 71 72 73
		for _, clp := range closer {
			if kb.Closer(clp, dht.self, key) && peerset.TryAdd(clp) {
				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
74
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
75
			Type:      notif.PeerResponse,
76 77
			ID:        p,
			Responses: pointerizePeerInfos(filtered),
78
		})
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

		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
}

96
func (dht *IpfsDHT) closerPeersSingle(ctx context.Context, key key.Key, p peer.ID) ([]peer.ID, error) {
97 98 99 100 101 102 103 104 105
	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
106
			dht.peerstore.AddAddrs(pid, pbp.Addresses(), pstore.TempAddrTTL)
107 108 109 110 111
			out = append(out, pid)
		}
	}
	return out, nil
}