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

import (
Jeromy's avatar
Jeromy committed
4
	"context"
Steven Allen's avatar
Steven Allen committed
5 6
	"fmt"
	"strings"
Michael Avila's avatar
Michael Avila committed
7
	"time"
Jeromy's avatar
Jeromy committed
8

9
	"github.com/libp2p/go-libp2p-core/peer"
10
	"github.com/libp2p/go-libp2p-core/routing"
11

12
	"github.com/ipfs/go-cid"
13
	logging "github.com/ipfs/go-log"
14
	pb "github.com/libp2p/go-libp2p-kad-dht/pb"
George Antoniadis's avatar
George Antoniadis committed
15
	kb "github.com/libp2p/go-libp2p-kbucket"
Adin Schmahmann's avatar
Adin Schmahmann committed
16 17
	"github.com/multiformats/go-base32"
	"github.com/multiformats/go-multihash"
18 19
)

Steven Allen's avatar
Steven Allen committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
38
	var encStr string
Steven Allen's avatar
Steven Allen committed
39
	c, err := cid.Cast([]byte(cstr))
Adin Schmahmann's avatar
Adin Schmahmann committed
40 41 42 43
	if err == nil {
		encStr = c.String()
	} else {
		encStr = base32.RawStdEncoding.EncodeToString([]byte(cstr))
Steven Allen's avatar
Steven Allen committed
44
	}
Adin Schmahmann's avatar
Adin Schmahmann committed
45 46

	return fmt.Sprintf("/%s/%s", proto, encStr), nil
Steven Allen's avatar
Steven Allen committed
47 48
}

49
func loggableKey(k string) logging.LoggableMap {
Steven Allen's avatar
Steven Allen committed
50
	newKey, err := tryFormatLoggableKey(k)
Tomas Virgl's avatar
Tomas Virgl committed
51
	if err != nil {
Matt Joiner's avatar
Matt Joiner committed
52
		logger.Debug(err)
Tomas Virgl's avatar
Tomas Virgl committed
53
	} else {
Steven Allen's avatar
Steven Allen committed
54
		k = newKey
Tomas Virgl's avatar
Tomas Virgl committed
55
	}
Steven Allen's avatar
Steven Allen committed
56

57 58 59 60 61
	return logging.LoggableMap{
		"key": k,
	}
}

Adin Schmahmann's avatar
Adin Schmahmann committed
62 63 64 65 66 67
func multihashLoggableKey(mh multihash.Multihash) logging.LoggableMap {
	return logging.LoggableMap{
		"multihash": base32.RawStdEncoding.EncodeToString(mh),
	}
}

68 69
// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
// to the given key
70 71 72
//
// If the context is canceled, this function will return the context error along
// with the closest K peers it has found so far.
73
func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan peer.ID, error) {
74
	//TODO: I can break the interface! return []peer.ID
Matt Joiner's avatar
Matt Joiner committed
75
	e := logger.EventBegin(ctx, "getClosestPeers", loggableKey(key))
Adin Schmahmann's avatar
Adin Schmahmann committed
76 77
	defer e.Done()

Adin Schmahmann's avatar
Adin Schmahmann committed
78
	lookupRes, err := dht.runLookupWithFollowup(ctx, dht.d, key,
Adin Schmahmann's avatar
Adin Schmahmann committed
79 80 81 82 83 84 85 86 87 88 89 90 91
		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())
92

Adin Schmahmann's avatar
Adin Schmahmann committed
93 94 95 96 97 98
			// For DHT query command
			routing.PublishQueryEvent(ctx, &routing.QueryEvent{
				Type:      routing.PeerResponse,
				ID:        p,
				Responses: peers,
			})
99

Adin Schmahmann's avatar
Adin Schmahmann committed
100 101
			return peers, err
		},
Adin Schmahmann's avatar
Adin Schmahmann committed
102
		func() bool { return false },
Adin Schmahmann's avatar
Adin Schmahmann committed
103
	)
Jeromy's avatar
Jeromy committed
104

105 106 107 108
	if err != nil {
		return nil, err
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
109 110
	out := make(chan peer.ID, dht.bucketSize)
	defer close(out)
111

Adin Schmahmann's avatar
Adin Schmahmann committed
112
	for _, p := range lookupRes.peers {
Adin Schmahmann's avatar
Adin Schmahmann committed
113 114 115
		out <- p
	}

Adin Schmahmann's avatar
Adin Schmahmann committed
116
	if ctx.Err() == nil && lookupRes.completed {
Adin Schmahmann's avatar
Adin Schmahmann committed
117
		// refresh the cpl for this key as the query was successful
Adin Schmahmann's avatar
Adin Schmahmann committed
118
		dht.routingTable.ResetCplRefreshedAtForID(kb.ConvertKey(key), time.Now())
Adin Schmahmann's avatar
Adin Schmahmann committed
119
	}
120

121
	return out, ctx.Err()
122
}