2014-07-23 04:48:30 -07:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2015-09-18 10:27:55 -07:00
|
|
|
"bytes"
|
2014-10-11 10:43:54 -07:00
|
|
|
"sync"
|
2015-09-30 10:59:12 -07:00
|
|
|
"time"
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2015-06-01 16:10:08 -07:00
|
|
|
key "github.com/ipfs/go-ipfs/blocks/key"
|
2015-03-30 20:04:32 -07:00
|
|
|
notif "github.com/ipfs/go-ipfs/notifications"
|
|
|
|
"github.com/ipfs/go-ipfs/routing"
|
|
|
|
pb "github.com/ipfs/go-ipfs/routing/dht/pb"
|
|
|
|
kb "github.com/ipfs/go-ipfs/routing/kbucket"
|
|
|
|
record "github.com/ipfs/go-ipfs/routing/record"
|
2016-02-09 10:56:19 -08:00
|
|
|
pset "github.com/ipfs/go-ipfs/thirdparty/peerset"
|
2016-05-30 22:14:21 -07:00
|
|
|
inet "gx/ipfs/QmRW2xiYTpDLWTHb822ZYbPBoh3dGLJwaXLGS9tnPyWZpq/go-libp2p/p2p/net"
|
2016-01-28 09:43:06 -08:00
|
|
|
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
|
2016-05-17 10:23:10 -07:00
|
|
|
peer "gx/ipfs/QmbyvM8zRFDkbFdYyt1MnevUMJ62SiSGbfDFZ3Z8nkrzr4/go-libp2p-peer"
|
2014-07-23 04:48:30 -07:00
|
|
|
)
|
|
|
|
|
2014-12-08 21:55:51 -08:00
|
|
|
// asyncQueryBuffer is the size of buffered channels in async queries. This
|
|
|
|
// buffer allows multiple queries to execute simultaneously, return their
|
|
|
|
// results and continue querying closer peers. Note that different query
|
|
|
|
// results will wait for the channel to drain.
|
|
|
|
var asyncQueryBuffer = 10
|
|
|
|
|
2014-07-23 04:48:30 -07:00
|
|
|
// This file implements the Routing interface for the IpfsDHT struct.
|
|
|
|
|
|
|
|
// Basic Put/Get
|
|
|
|
|
|
|
|
// PutValue adds value corresponding to given Key.
|
2014-08-07 18:06:50 -07:00
|
|
|
// This is the top level "Store" operation of the DHT
|
2015-06-01 16:10:08 -07:00
|
|
|
func (dht *IpfsDHT) PutValue(ctx context.Context, key key.Key, value []byte) error {
|
2014-10-25 03:17:14 -07:00
|
|
|
log.Debugf("PutValue %s", key)
|
2015-02-23 00:25:20 -08:00
|
|
|
sk, err := dht.getOwnPrivateKey()
|
2014-09-12 17:34:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2015-02-25 14:56:26 -08:00
|
|
|
sign, err := dht.Validator.IsSigned(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:25:20 -08:00
|
|
|
rec, err := record.MakePutRecord(sk, key, value, sign)
|
2015-01-16 22:46:44 +00:00
|
|
|
if err != nil {
|
2015-02-23 00:25:20 -08:00
|
|
|
log.Debug("Creation of record failed!")
|
2015-01-16 22:46:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-23 00:25:20 -08:00
|
|
|
err = dht.putLocal(key, rec)
|
2014-11-09 23:45:16 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-22 18:18:41 +00:00
|
|
|
pchan, err := dht.GetClosestPeers(ctx, key)
|
2014-12-14 00:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2014-12-14 00:50:49 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for p := range pchan {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(p peer.ID) {
|
2015-09-30 10:59:12 -07:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
2014-12-14 00:50:49 +00:00
|
|
|
defer wg.Done()
|
2015-02-21 16:20:28 -08:00
|
|
|
notif.PublishQueryEvent(ctx, ¬if.QueryEvent{
|
|
|
|
Type: notif.Value,
|
|
|
|
ID: p,
|
|
|
|
})
|
|
|
|
|
2014-12-28 23:46:25 +00:00
|
|
|
err := dht.putValueToPeer(ctx, p, key, rec)
|
2014-12-14 00:50:49 +00:00
|
|
|
if err != nil {
|
2015-01-26 19:12:12 -08:00
|
|
|
log.Debugf("failed putting value to peer: %s", err)
|
2014-12-14 00:50:49 +00:00
|
|
|
}
|
|
|
|
}(p)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue searches for the value corresponding to given Key.
|
2015-06-01 16:10:08 -07:00
|
|
|
func (dht *IpfsDHT) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
|
2015-09-30 10:59:12 -07:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
vals, err := dht.GetValues(ctx, key, 16)
|
2015-09-18 10:27:55 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var recs [][]byte
|
|
|
|
for _, v := range vals {
|
2015-11-19 11:24:59 -08:00
|
|
|
if v.Val != nil {
|
|
|
|
recs = append(recs, v.Val)
|
|
|
|
}
|
2015-09-18 10:27:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
i, err := dht.Selector.BestRecord(key, recs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
best := recs[i]
|
|
|
|
log.Debugf("GetValue %v %v", key, best)
|
|
|
|
if best == nil {
|
|
|
|
log.Errorf("GetValue yielded correct record with nil value.")
|
|
|
|
return nil, routing.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
fixupRec, err := record.MakePutRecord(dht.peerstore.PrivKey(dht.self), key, best, true)
|
|
|
|
if err != nil {
|
|
|
|
// probably shouldnt actually 'error' here as we have found a value we like,
|
|
|
|
// but this call failing probably isnt something we want to ignore
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range vals {
|
|
|
|
// if someone sent us a different 'less-valid' record, lets correct them
|
|
|
|
if !bytes.Equal(v.Val, best) {
|
|
|
|
go func(v routing.RecvdVal) {
|
2015-09-30 10:59:12 -07:00
|
|
|
ctx, cancel := context.WithTimeout(dht.Context(), time.Second*30)
|
|
|
|
defer cancel()
|
2015-09-18 10:27:55 -07:00
|
|
|
err := dht.putValueToPeer(ctx, v.From, key, fixupRec)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error correcting DHT entry: ", err)
|
|
|
|
}
|
|
|
|
}(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) GetValues(ctx context.Context, key key.Key, nvals int) ([]routing.RecvdVal, error) {
|
|
|
|
var vals []routing.RecvdVal
|
|
|
|
var valslock sync.Mutex
|
|
|
|
|
2014-08-12 22:10:44 -07:00
|
|
|
// If we have it local, dont bother doing an RPC!
|
2015-09-18 10:27:55 -07:00
|
|
|
lrec, err := dht.getLocal(key)
|
2014-08-14 08:32:17 -07:00
|
|
|
if err == nil {
|
2015-09-18 10:27:55 -07:00
|
|
|
// TODO: this is tricky, we dont always want to trust our own value
|
|
|
|
// what if the authoritative source updated it?
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debug("have it locally")
|
2015-09-18 10:27:55 -07:00
|
|
|
vals = append(vals, routing.RecvdVal{
|
|
|
|
Val: lrec.GetValue(),
|
|
|
|
From: dht.self,
|
|
|
|
})
|
|
|
|
|
|
|
|
if nvals <= 1 {
|
|
|
|
return vals, nil
|
|
|
|
}
|
|
|
|
} else if nvals == 0 {
|
|
|
|
return nil, err
|
2014-08-12 22:10:44 -07:00
|
|
|
}
|
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
// get closest peers in the routing table
|
2015-09-21 09:55:25 -07:00
|
|
|
rtp := dht.routingTable.NearestPeers(kb.ConvertKey(key), KValue)
|
2015-02-23 11:22:45 -08:00
|
|
|
log.Debugf("peers in rt: %s", len(rtp), rtp)
|
2015-01-18 01:00:25 -08:00
|
|
|
if len(rtp) == 0 {
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Warning("No peers from routing table!")
|
2015-04-20 00:15:34 -07:00
|
|
|
return nil, kb.ErrLookupFailure
|
2014-08-10 21:02:05 -07:00
|
|
|
}
|
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
// setup the Query
|
2015-08-12 16:08:57 -07:00
|
|
|
parent := ctx
|
2015-01-01 12:45:39 -08:00
|
|
|
query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-02-21 16:20:28 -08:00
|
|
|
Type: notif.SendingQuery,
|
|
|
|
ID: p,
|
|
|
|
})
|
|
|
|
|
2015-09-18 10:27:55 -07:00
|
|
|
rec, peers, err := dht.getValueOrPeers(ctx, p, key)
|
2015-11-20 11:12:14 -08:00
|
|
|
switch err {
|
|
|
|
case routing.ErrNotFound:
|
|
|
|
// in this case, they responded with nothing,
|
|
|
|
// still send a notification so listeners can know the
|
|
|
|
// request has completed 'successfully'
|
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
|
|
|
Type: notif.PeerResponse,
|
|
|
|
ID: p,
|
|
|
|
})
|
2014-09-18 19:30:04 -07:00
|
|
|
return nil, err
|
2015-11-20 11:12:14 -08:00
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
case nil, errInvalidRecord:
|
|
|
|
// in either of these cases, we want to keep going
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
|
2015-09-18 10:27:55 -07:00
|
|
|
res := &dhtQueryResult{closerPeers: peers}
|
|
|
|
|
2015-11-20 11:12:14 -08:00
|
|
|
if rec.GetValue() != nil || err == errInvalidRecord {
|
2015-09-18 10:27:55 -07:00
|
|
|
rv := routing.RecvdVal{
|
|
|
|
Val: rec.GetValue(),
|
|
|
|
From: p,
|
|
|
|
}
|
|
|
|
valslock.Lock()
|
|
|
|
vals = append(vals, rv)
|
|
|
|
|
|
|
|
// If weve collected enough records, we're done
|
|
|
|
if len(vals) >= nvals {
|
|
|
|
res.success = true
|
|
|
|
}
|
|
|
|
valslock.Unlock()
|
2014-09-18 19:30:04 -07:00
|
|
|
}
|
|
|
|
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-02-21 16:20:28 -08:00
|
|
|
Type: notif.PeerResponse,
|
|
|
|
ID: p,
|
|
|
|
Responses: pointerizePeerInfos(peers),
|
|
|
|
})
|
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
return res, nil
|
|
|
|
})
|
2014-08-15 09:39:38 -07:00
|
|
|
|
2014-09-17 07:19:40 -07:00
|
|
|
// run it!
|
2015-09-18 10:27:55 -07:00
|
|
|
_, err = query.Run(ctx, rtp)
|
|
|
|
if len(vals) == 0 {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-15 09:39:38 -07:00
|
|
|
}
|
|
|
|
|
2015-09-18 10:27:55 -07:00
|
|
|
return vals, nil
|
2014-09-18 19:30:04 -07:00
|
|
|
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value provider layer of indirection.
|
|
|
|
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
|
|
|
|
|
2014-08-16 23:03:36 -07:00
|
|
|
// Provide makes this node announce that it can provide a value for the given key
|
2015-06-01 16:10:08 -07:00
|
|
|
func (dht *IpfsDHT) Provide(ctx context.Context, key key.Key) error {
|
2015-01-15 04:45:34 +00:00
|
|
|
defer log.EventBegin(ctx, "provide", &key).Done()
|
2015-01-05 04:35:54 -08:00
|
|
|
|
|
|
|
// add self locally
|
2015-02-26 16:44:36 -08:00
|
|
|
dht.providers.AddProvider(ctx, key, dht.self)
|
2014-12-14 00:50:49 +00:00
|
|
|
|
2015-01-22 18:18:41 +00:00
|
|
|
peers, err := dht.GetClosestPeers(ctx, key)
|
2014-12-14 00:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
|
|
|
|
2014-12-28 23:46:25 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2014-12-14 00:50:49 +00:00
|
|
|
for p := range peers {
|
2014-12-28 23:46:25 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func(p peer.ID) {
|
|
|
|
defer wg.Done()
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debugf("putProvider(%s, %s)", key, p)
|
2014-12-28 23:46:25 +00:00
|
|
|
err := dht.putProvider(ctx, p, string(key))
|
|
|
|
if err != nil {
|
2015-01-26 19:12:12 -08:00
|
|
|
log.Debug(err)
|
2014-12-28 23:46:25 +00:00
|
|
|
}
|
|
|
|
}(p)
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
2014-12-28 23:46:25 +00:00
|
|
|
wg.Wait()
|
2014-08-03 21:46:01 -07:00
|
|
|
return nil
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
2014-12-17 10:02:19 -08:00
|
|
|
// FindProviders searches until the context expires.
|
2015-06-01 16:10:08 -07:00
|
|
|
func (dht *IpfsDHT) FindProviders(ctx context.Context, key key.Key) ([]peer.PeerInfo, error) {
|
2014-12-17 10:02:19 -08:00
|
|
|
var providers []peer.PeerInfo
|
2015-02-21 19:26:58 -08:00
|
|
|
for p := range dht.FindProvidersAsync(ctx, key, KValue) {
|
2014-12-17 10:02:19 -08:00
|
|
|
providers = append(providers, p)
|
|
|
|
}
|
|
|
|
return providers, nil
|
|
|
|
}
|
|
|
|
|
2014-11-20 10:46:19 -08:00
|
|
|
// FindProvidersAsync is the same thing as FindProviders, but returns a channel.
|
|
|
|
// Peers will be returned on the channel as soon as they are found, even before
|
|
|
|
// the search query completes.
|
2015-06-01 16:10:08 -07:00
|
|
|
func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key key.Key, count int) <-chan peer.PeerInfo {
|
2014-12-03 19:51:17 +00:00
|
|
|
log.Event(ctx, "findProviders", &key)
|
2014-12-19 12:19:56 -08:00
|
|
|
peerOut := make(chan peer.PeerInfo, count)
|
2014-12-11 05:08:39 +00:00
|
|
|
go dht.findProvidersAsyncRoutine(ctx, key, count, peerOut)
|
|
|
|
return peerOut
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:10:08 -07:00
|
|
|
func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key key.Key, count int, peerOut chan peer.PeerInfo) {
|
2015-01-15 04:45:34 +00:00
|
|
|
defer log.EventBegin(ctx, "findProvidersAsync", &key).Done()
|
2014-12-11 05:08:39 +00:00
|
|
|
defer close(peerOut)
|
|
|
|
|
2014-12-17 19:27:41 +00:00
|
|
|
ps := pset.NewLimited(count)
|
2014-12-11 05:08:39 +00:00
|
|
|
provs := dht.providers.GetProviders(ctx, key)
|
|
|
|
for _, p := range provs {
|
2016-04-29 16:57:19 -04:00
|
|
|
// NOTE: Assuming that this list of peers is unique
|
2014-12-16 18:33:36 +00:00
|
|
|
if ps.TryAdd(p) {
|
2014-12-11 05:42:05 +00:00
|
|
|
select {
|
2014-12-19 12:19:56 -08:00
|
|
|
case peerOut <- dht.peerstore.PeerInfo(p):
|
2014-12-11 05:42:05 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
2014-12-11 05:08:39 +00:00
|
|
|
}
|
2014-12-11 05:42:05 +00:00
|
|
|
|
|
|
|
// If we have enough peers locally, dont bother with remote RPC
|
|
|
|
if ps.Size() >= count {
|
2014-12-11 05:08:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the Query
|
2015-08-12 16:08:57 -07:00
|
|
|
parent := ctx
|
2015-01-01 12:45:39 -08:00
|
|
|
query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-01-25 01:41:06 +00:00
|
|
|
Type: notif.SendingQuery,
|
|
|
|
ID: p,
|
|
|
|
})
|
2014-12-11 06:08:53 +00:00
|
|
|
pmes, err := dht.findProvidersSingle(ctx, p, key)
|
2014-12-11 05:08:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debugf("%d provider entries", len(pmes.GetProviderPeers()))
|
2014-12-19 12:19:56 -08:00
|
|
|
provs := pb.PBPeersToPeerInfos(pmes.GetProviderPeers())
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debugf("%d provider entries decoded", len(provs))
|
2014-12-11 05:08:39 +00:00
|
|
|
|
|
|
|
// Add unique providers from request, up to 'count'
|
|
|
|
for _, prov := range provs {
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debugf("got provider: %s", prov)
|
2014-12-19 12:19:56 -08:00
|
|
|
if ps.TryAdd(prov.ID) {
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debugf("using provider: %s", prov)
|
2014-12-11 05:42:05 +00:00
|
|
|
select {
|
|
|
|
case peerOut <- prov:
|
|
|
|
case <-ctx.Done():
|
2015-01-26 19:12:12 -08:00
|
|
|
log.Debug("Context timed out sending more providers")
|
2014-12-11 05:42:05 +00:00
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2014-12-02 00:19:22 -08:00
|
|
|
}
|
2014-12-11 05:08:39 +00:00
|
|
|
if ps.Size() >= count {
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debugf("got enough providers (%d/%d)", ps.Size(), count)
|
2014-12-11 05:08:39 +00:00
|
|
|
return &dhtQueryResult{success: true}, nil
|
2014-08-25 09:44:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 05:08:39 +00:00
|
|
|
// Give closer peers back to the query to be queried
|
|
|
|
closer := pmes.GetCloserPeers()
|
2014-12-19 12:19:56 -08:00
|
|
|
clpeers := pb.PBPeersToPeerInfos(closer)
|
2015-01-05 04:35:54 -08:00
|
|
|
log.Debugf("got closer peers: %d %s", len(clpeers), clpeers)
|
2015-01-25 01:41:06 +00:00
|
|
|
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-01-25 01:41:06 +00:00
|
|
|
Type: notif.PeerResponse,
|
|
|
|
ID: p,
|
|
|
|
Responses: pointerizePeerInfos(clpeers),
|
|
|
|
})
|
2014-12-11 05:08:39 +00:00
|
|
|
return &dhtQueryResult{closerPeers: clpeers}, nil
|
|
|
|
})
|
|
|
|
|
2015-09-21 09:55:25 -07:00
|
|
|
peers := dht.routingTable.NearestPeers(kb.ConvertKey(key), KValue)
|
2014-12-11 05:08:39 +00:00
|
|
|
_, err := query.Run(ctx, peers)
|
|
|
|
if err != nil {
|
2015-01-26 19:12:12 -08:00
|
|
|
log.Debugf("Query error: %s", err)
|
2015-01-24 00:59:47 +00:00
|
|
|
notif.PublishQueryEvent(ctx, ¬if.QueryEvent{
|
|
|
|
Type: notif.QueryError,
|
|
|
|
Extra: err.Error(),
|
|
|
|
})
|
2014-12-11 05:08:39 +00:00
|
|
|
}
|
2014-08-25 09:44:42 -07:00
|
|
|
}
|
|
|
|
|
2014-07-23 04:48:30 -07:00
|
|
|
// FindPeer searches for a peer with given ID.
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (peer.PeerInfo, error) {
|
2015-01-15 04:45:34 +00:00
|
|
|
defer log.EventBegin(ctx, "FindPeer", id).Done()
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2014-08-12 15:37:26 -07:00
|
|
|
// Check if were already connected to them
|
2014-12-28 23:46:25 +00:00
|
|
|
if pi := dht.FindLocal(id); pi.ID != "" {
|
2014-12-19 12:19:56 -08:00
|
|
|
return pi, nil
|
2014-08-12 15:37:26 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 09:55:25 -07:00
|
|
|
peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), KValue)
|
2015-01-18 01:00:25 -08:00
|
|
|
if len(peers) == 0 {
|
2015-04-20 00:15:34 -07:00
|
|
|
return peer.PeerInfo{}, kb.ErrLookupFailure
|
2014-08-12 15:37:26 -07:00
|
|
|
}
|
2014-08-03 21:46:01 -07:00
|
|
|
|
2014-10-24 18:32:28 -07:00
|
|
|
// Sanity...
|
2015-01-18 01:00:25 -08:00
|
|
|
for _, p := range peers {
|
2014-12-19 12:19:56 -08:00
|
|
|
if p == id {
|
2015-01-26 19:12:12 -08:00
|
|
|
log.Debug("Found target peer in list of closest peers...")
|
2014-12-19 12:19:56 -08:00
|
|
|
return dht.peerstore.PeerInfo(p), nil
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
2014-09-01 16:09:03 -07:00
|
|
|
}
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2014-10-24 18:32:28 -07:00
|
|
|
// setup the Query
|
2015-08-12 16:08:57 -07:00
|
|
|
parent := ctx
|
2015-06-01 16:10:08 -07:00
|
|
|
query := dht.newQuery(key.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-01-24 00:59:47 +00:00
|
|
|
Type: notif.SendingQuery,
|
|
|
|
ID: p,
|
|
|
|
})
|
2014-10-24 18:32:28 -07:00
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
pmes, err := dht.findPeerSingle(ctx, p, id)
|
2014-09-17 07:19:40 -07:00
|
|
|
if err != nil {
|
2014-09-18 19:30:04 -07:00
|
|
|
return nil, err
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
2014-09-01 16:09:03 -07:00
|
|
|
|
2014-10-24 18:32:28 -07:00
|
|
|
closer := pmes.GetCloserPeers()
|
2014-12-19 12:19:56 -08:00
|
|
|
clpeerInfos := pb.PBPeersToPeerInfos(closer)
|
2014-11-20 10:46:56 -08:00
|
|
|
|
2014-11-21 08:03:11 -08:00
|
|
|
// see it we got the peer here
|
2014-12-19 12:19:56 -08:00
|
|
|
for _, npi := range clpeerInfos {
|
|
|
|
if npi.ID == id {
|
2014-10-24 18:32:28 -07:00
|
|
|
return &dhtQueryResult{
|
2014-12-19 12:19:56 -08:00
|
|
|
peer: npi,
|
2014-10-24 18:32:28 -07:00
|
|
|
success: true,
|
|
|
|
}, nil
|
|
|
|
}
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
2015-08-12 16:08:57 -07:00
|
|
|
notif.PublishQueryEvent(parent, ¬if.QueryEvent{
|
2015-01-24 00:59:47 +00:00
|
|
|
Type: notif.PeerResponse,
|
|
|
|
Responses: pointerizePeerInfos(clpeerInfos),
|
|
|
|
})
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
return &dhtQueryResult{closerPeers: clpeerInfos}, nil
|
2014-09-18 19:30:04 -07:00
|
|
|
})
|
2014-09-01 16:09:03 -07:00
|
|
|
|
2014-10-24 18:32:28 -07:00
|
|
|
// run it!
|
2015-01-18 01:00:25 -08:00
|
|
|
result, err := query.Run(ctx, peers)
|
2014-09-17 07:19:40 -07:00
|
|
|
if err != nil {
|
2014-12-19 12:19:56 -08:00
|
|
|
return peer.PeerInfo{}, err
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
2014-10-30 06:35:29 -07:00
|
|
|
log.Debugf("FindPeer %v %v", id, result.success)
|
2014-12-19 12:19:56 -08:00
|
|
|
if result.peer.ID == "" {
|
|
|
|
return peer.PeerInfo{}, routing.ErrNotFound
|
2014-09-01 16:09:03 -07:00
|
|
|
}
|
2014-10-24 18:32:28 -07:00
|
|
|
|
2014-09-18 19:30:04 -07:00
|
|
|
return result.peer, nil
|
2014-09-01 16:09:03 -07:00
|
|
|
}
|
|
|
|
|
2014-11-24 14:58:51 -05:00
|
|
|
// FindPeersConnectedToPeer searches for peers directly connected to a given peer.
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<-chan peer.PeerInfo, error) {
|
2014-11-24 14:58:51 -05:00
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
peerchan := make(chan peer.PeerInfo, asyncQueryBuffer)
|
|
|
|
peersSeen := peer.Set{}
|
2014-11-24 14:58:51 -05:00
|
|
|
|
2015-09-21 09:55:25 -07:00
|
|
|
peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), KValue)
|
2015-01-18 01:00:25 -08:00
|
|
|
if len(peers) == 0 {
|
2015-04-20 00:15:34 -07:00
|
|
|
return nil, kb.ErrLookupFailure
|
2014-11-24 14:58:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup the Query
|
2015-06-01 16:10:08 -07:00
|
|
|
query := dht.newQuery(key.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
|
2014-11-24 14:58:51 -05:00
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
pmes, err := dht.findPeerSingle(ctx, p, id)
|
2014-11-24 14:58:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
var clpeers []peer.PeerInfo
|
2014-11-24 14:58:51 -05:00
|
|
|
closer := pmes.GetCloserPeers()
|
|
|
|
for _, pbp := range closer {
|
2014-12-19 12:19:56 -08:00
|
|
|
pi := pb.PBPeerToPeerInfo(pbp)
|
2014-11-24 14:58:51 -05:00
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
// skip peers already seen
|
|
|
|
if _, found := peersSeen[pi.ID]; found {
|
2014-11-24 14:58:51 -05:00
|
|
|
continue
|
|
|
|
}
|
2014-12-19 12:19:56 -08:00
|
|
|
peersSeen[pi.ID] = struct{}{}
|
2014-11-24 14:58:51 -05:00
|
|
|
|
|
|
|
// if peer is connected, send it to our client.
|
|
|
|
if pb.Connectedness(*pbp.Connection) == inet.Connected {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
2014-12-19 12:19:56 -08:00
|
|
|
case peerchan <- pi:
|
2014-11-24 14:58:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if peer is the peer we're looking for, don't bother querying it.
|
2014-12-19 12:19:56 -08:00
|
|
|
// TODO maybe query it?
|
2014-11-24 14:58:51 -05:00
|
|
|
if pb.Connectedness(*pbp.Connection) != inet.Connected {
|
2014-12-19 12:19:56 -08:00
|
|
|
clpeers = append(clpeers, pi)
|
2014-11-24 14:58:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dhtQueryResult{closerPeers: clpeers}, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// run it! run it asynchronously to gen peers as results are found.
|
|
|
|
// this does no error checking
|
|
|
|
go func() {
|
2015-01-18 01:00:25 -08:00
|
|
|
if _, err := query.Run(ctx, peers); err != nil {
|
2015-01-26 19:12:12 -08:00
|
|
|
log.Debug(err)
|
2014-11-24 14:58:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// close the peerchan channel when done.
|
|
|
|
close(peerchan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return peerchan, nil
|
|
|
|
}
|