lookup.go 2.71 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
	pb "github.com/libp2p/go-libp2p-kad-dht/pb"
George Antoniadis's avatar
George Antoniadis committed
14
	kb "github.com/libp2p/go-libp2p-kbucket"
Adin Schmahmann's avatar
Adin Schmahmann committed
15
	"github.com/multiformats/go-base32"
16 17
)

Steven Allen's avatar
Steven Allen committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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
36
	var encStr string
Steven Allen's avatar
Steven Allen committed
37
	c, err := cid.Cast([]byte(cstr))
Adin Schmahmann's avatar
Adin Schmahmann committed
38 39 40 41
	if err == nil {
		encStr = c.String()
	} else {
		encStr = base32.RawStdEncoding.EncodeToString([]byte(cstr))
Steven Allen's avatar
Steven Allen committed
42
	}
Adin Schmahmann's avatar
Adin Schmahmann committed
43 44

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

Steven Allen's avatar
Steven Allen committed
47
type loggableKeyBytes []byte
Steven Allen's avatar
Steven Allen committed
48

Steven Allen's avatar
Steven Allen committed
49 50 51 52 53
func (lk loggableKeyString) String() string {
	k := string(lk)
	newKey, err := tryFormatLoggableKey(k)
	if err == nil {
		return newKey
54
	}
Steven Allen's avatar
Steven Allen committed
55
	return k
56 57
}

Steven Allen's avatar
Steven Allen committed
58 59 60 61 62 63 64
type loggableKeyString string

func (lk loggableKeyBytes) String() string {
	k := string(lk)
	newKey, err := tryFormatLoggableKey(k)
	if err == nil {
		return newKey
Adin Schmahmann's avatar
Adin Schmahmann committed
65
	}
Steven Allen's avatar
Steven Allen committed
66
	return k
Adin Schmahmann's avatar
Adin Schmahmann committed
67 68
}

69 70
// Kademlia 'node lookup' operation. Returns a channel of the K closest peers
// to the given key
71 72 73
//
// If the context is canceled, this function will return the context error along
// with the closest K peers it has found so far.
74
func (dht *IpfsDHT) GetClosestPeers(ctx context.Context, key string) (<-chan peer.ID, error) {
75
	//TODO: I can break the interface! return []peer.ID
Aarsh Shah's avatar
Aarsh Shah committed
76
	lookupRes, err := dht.runLookupWithFollowup(ctx, key,
Adin Schmahmann's avatar
Adin Schmahmann committed
77 78 79 80 81 82 83 84 85 86 87 88 89
		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())
90

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

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

103 104 105 106
	if err != nil {
		return nil, err
	}

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

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

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

119
	return out, ctx.Err()
120
}