2014-07-23 04:48:30 -07:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2015-09-18 10:27:55 -07:00
|
|
|
"bytes"
|
2016-09-30 10:24:03 -07:00
|
|
|
"context"
|
2016-06-11 17:08:34 -07:00
|
|
|
"fmt"
|
2016-08-03 18:19:47 -07:00
|
|
|
"runtime"
|
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
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2017-05-16 18:23:18 -07:00
|
|
|
logging "github.com/ipfs/go-log"
|
2016-09-02 20:21:23 +01:00
|
|
|
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
|
|
|
|
kb "github.com/libp2p/go-libp2p-kbucket"
|
2016-10-05 12:34:28 -07:00
|
|
|
inet "github.com/libp2p/go-libp2p-net"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
pset "github.com/libp2p/go-libp2p-peer/peerset"
|
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
2016-09-02 20:21:23 +01:00
|
|
|
record "github.com/libp2p/go-libp2p-record"
|
|
|
|
routing "github.com/libp2p/go-libp2p-routing"
|
|
|
|
notif "github.com/libp2p/go-libp2p-routing/notifications"
|
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
|
2017-12-08 14:12:02 -08:00
|
|
|
func (dht *IpfsDHT) PutValue(ctx context.Context, key string, value []byte) (err error) {
|
|
|
|
eip := log.EventBegin(ctx, "PutValue")
|
|
|
|
defer func() {
|
|
|
|
eip.Append(loggableKey(key))
|
|
|
|
if err != nil {
|
|
|
|
eip.SetError(err)
|
|
|
|
}
|
|
|
|
eip.Done()
|
|
|
|
}()
|
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 {
|
2016-06-14 12:20:21 +01: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.
|
2017-12-08 14:12:02 -08:00
|
|
|
func (dht *IpfsDHT) GetValue(ctx context.Context, key string) (_ []byte, err error) {
|
|
|
|
eip := log.EventBegin(ctx, "GetValue")
|
|
|
|
defer func() {
|
|
|
|
eip.Append(loggableKey(key))
|
|
|
|
if err != nil {
|
|
|
|
eip.SetError(err)
|
|
|
|
}
|
|
|
|
eip.Done()
|
|
|
|
}()
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-05 11:31:34 -08:00
|
|
|
recs := make([][]byte, 0, len(vals))
|
2015-09-18 10:27:55 -07:00
|
|
|
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) {
|
2016-07-05 12:19:54 -07:00
|
|
|
if v.From == dht.self {
|
|
|
|
err := dht.putLocal(key, fixupRec)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error correcting local dht entry:", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-08 14:12:02 -08:00
|
|
|
func (dht *IpfsDHT) GetValues(ctx context.Context, key string, nvals int) (_ []routing.RecvdVal, err error) {
|
|
|
|
eip := log.EventBegin(ctx, "GetValues")
|
|
|
|
defer func() {
|
|
|
|
eip.Append(loggableKey(key))
|
|
|
|
if err != nil {
|
|
|
|
eip.SetError(err)
|
|
|
|
}
|
|
|
|
eip.Done()
|
|
|
|
}()
|
2017-12-05 11:31:34 -08:00
|
|
|
vals := make([]routing.RecvdVal, 0, nvals)
|
2015-09-18 10:27:55 -07:00
|
|
|
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
|
2017-03-05 12:08:20 -08:00
|
|
|
rtp := dht.routingTable.NearestPeers(kb.ConvertKey(key), AlphaValue)
|
2016-09-30 10:13:57 -07:00
|
|
|
log.Debugf("peers in rt: %d %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,
|
2017-03-05 12:08:20 -08:00
|
|
|
Responses: peers,
|
2015-02-21 16:20:28 -08:00
|
|
|
})
|
|
|
|
|
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-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
|
2017-12-08 14:12:02 -08:00
|
|
|
func (dht *IpfsDHT) Provide(ctx context.Context, key *cid.Cid, brdcst bool) (err error) {
|
|
|
|
eip := log.EventBegin(ctx, "Provide", key, logging.LoggableMap{"broadcast": brdcst})
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
eip.SetError(err)
|
|
|
|
}
|
|
|
|
eip.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)
|
2017-05-16 18:23:18 -07:00
|
|
|
if !brdcst {
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-14 00:50:49 +00:00
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
peers, err := dht.GetClosestPeers(ctx, key.KeyString())
|
2014-12-14 00:50:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
|
|
|
|
2016-06-11 17:08:34 -07:00
|
|
|
mes, err := dht.makeProvRecord(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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)
|
2016-06-11 17:08:34 -07:00
|
|
|
err := dht.sendMessage(ctx, p, mes)
|
2014-12-28 23:46:25 +00:00
|
|
|
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
|
|
|
}
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) makeProvRecord(skey *cid.Cid) (*pb.Message, error) {
|
2016-06-11 17:08:34 -07:00
|
|
|
pi := pstore.PeerInfo{
|
|
|
|
ID: dht.self,
|
|
|
|
Addrs: dht.host.Addrs(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// // only share WAN-friendly addresses ??
|
|
|
|
// pi.Addrs = addrutil.WANShareableAddrs(pi.Addrs)
|
|
|
|
if len(pi.Addrs) < 1 {
|
|
|
|
return nil, fmt.Errorf("no known addresses for self. cannot put provider.")
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
pmes := pb.NewMessage(pb.Message_ADD_PROVIDER, skey.KeyString(), 0)
|
2016-06-11 17:08:34 -07:00
|
|
|
pmes.ProviderPeers = pb.RawPeerInfosToPBPeers([]pstore.PeerInfo{pi})
|
|
|
|
return pmes, nil
|
|
|
|
}
|
2014-07-23 04:48:30 -07:00
|
|
|
|
2014-12-17 10:02:19 -08:00
|
|
|
// FindProviders searches until the context expires.
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) FindProviders(ctx context.Context, c *cid.Cid) ([]pstore.PeerInfo, error) {
|
2016-06-01 15:51:39 -07:00
|
|
|
var providers []pstore.PeerInfo
|
2016-09-30 10:13:57 -07:00
|
|
|
for p := range dht.FindProvidersAsync(ctx, c, 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.
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key *cid.Cid, count int) <-chan pstore.PeerInfo {
|
|
|
|
log.Event(ctx, "findProviders", key)
|
2016-06-01 15:51:39 -07:00
|
|
|
peerOut := make(chan pstore.PeerInfo, count)
|
2014-12-11 05:08:39 +00:00
|
|
|
go dht.findProvidersAsyncRoutine(ctx, key, count, peerOut)
|
|
|
|
return peerOut
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key *cid.Cid, count int, peerOut chan pstore.PeerInfo) {
|
|
|
|
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) {
|
2017-03-05 12:08:20 -08:00
|
|
|
pi := dht.peerstore.PeerInfo(p)
|
2014-12-11 05:42:05 +00:00
|
|
|
select {
|
2017-03-05 12:08:20 -08:00
|
|
|
case peerOut <- pi:
|
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
|
2017-03-05 12:08:20 -08:00
|
|
|
// TODO: is this a DOS vector?
|
2014-12-11 05:42:05 +00:00
|
|
|
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
|
2016-09-30 10:13:57 -07:00
|
|
|
query := dht.newQuery(key.KeyString(), 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 {
|
2017-03-24 10:28:37 -07:00
|
|
|
if prov.ID != dht.self {
|
|
|
|
dht.peerstore.AddAddrs(prov.ID, prov.Addrs, pstore.TempAddrTTL)
|
|
|
|
}
|
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 {
|
2017-03-05 12:08:20 -08:00
|
|
|
case peerOut <- *prov:
|
2014-12-11 05:42:05 +00:00
|
|
|
case <-ctx.Done():
|
2016-06-14 12:20:21 +01: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,
|
2017-03-05 12:08:20 -08:00
|
|
|
Responses: clpeers,
|
2015-01-25 01:41:06 +00:00
|
|
|
})
|
2014-12-11 05:08:39 +00:00
|
|
|
return &dhtQueryResult{closerPeers: clpeers}, nil
|
|
|
|
})
|
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
peers := dht.routingTable.NearestPeers(kb.ConvertKey(key.KeyString()), AlphaValue)
|
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)
|
2016-08-03 18:19:47 -07:00
|
|
|
// Special handling for issue: https://github.com/ipfs/go-ipfs/issues/3032
|
|
|
|
if fmt.Sprint(err) == "<nil>" {
|
|
|
|
log.Error("reproduced bug 3032:")
|
|
|
|
log.Errorf("Errors type information: %#v", err)
|
|
|
|
log.Errorf("go version: %s", runtime.Version())
|
|
|
|
log.Error("please report this information to: https://github.com/ipfs/go-ipfs/issues/3032")
|
|
|
|
|
|
|
|
// replace problematic error with something that won't crash the daemon
|
|
|
|
err = fmt.Errorf("<nil>")
|
|
|
|
}
|
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.
|
2017-12-08 14:12:02 -08:00
|
|
|
func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (_ pstore.PeerInfo, err error) {
|
|
|
|
eip := log.EventBegin(ctx, "FindPeer", id)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
eip.SetError(err)
|
|
|
|
}
|
|
|
|
eip.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
|
|
|
}
|
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), AlphaValue)
|
2015-01-18 01:00:25 -08:00
|
|
|
if len(peers) == 0 {
|
2016-06-01 15:51:39 -07:00
|
|
|
return pstore.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 {
|
2016-06-14 12:20:21 +01: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
|
2016-09-30 10:13:57 -07:00
|
|
|
query := dht.newQuery(string(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
|
|
|
|
2017-07-14 22:08:13 -04:00
|
|
|
// see if 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,
|
2017-07-14 22:08:13 -04:00
|
|
|
ID: p,
|
2017-03-05 12:08:20 -08:00
|
|
|
Responses: clpeerInfos,
|
2015-01-24 00:59:47 +00:00
|
|
|
})
|
|
|
|
|
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 {
|
2016-06-01 15:51:39 -07:00
|
|
|
return pstore.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 == "" {
|
2016-06-01 15:51:39 -07:00
|
|
|
return pstore.PeerInfo{}, routing.ErrNotFound
|
2014-09-01 16:09:03 -07:00
|
|
|
}
|
2014-10-24 18:32:28 -07:00
|
|
|
|
2017-03-05 12:08:20 -08: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.
|
2017-03-05 12:08:20 -08:00
|
|
|
func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<-chan *pstore.PeerInfo, error) {
|
2014-11-24 14:58:51 -05:00
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
peerchan := make(chan *pstore.PeerInfo, asyncQueryBuffer)
|
2016-06-01 15:51:39 -07:00
|
|
|
peersSeen := make(map[peer.ID]struct{})
|
2014-11-24 14:58:51 -05:00
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), AlphaValue)
|
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
|
2016-09-30 10:13:57 -07:00
|
|
|
query := dht.newQuery(string(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
|
|
|
|
}
|
|
|
|
|
2017-03-05 12:08:20 -08:00
|
|
|
var clpeers []*pstore.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
|
|
|
|
}
|