2014-07-23 04:48:30 -07:00
|
|
|
package dht
|
|
|
|
|
2014-07-28 22:14:27 -07:00
|
|
|
import (
|
2014-09-19 08:07:56 -07:00
|
|
|
"bytes"
|
2016-09-30 10:24:03 -07:00
|
|
|
"context"
|
2014-09-16 00:56:40 -07:00
|
|
|
"errors"
|
2014-08-23 22:21:20 -07:00
|
|
|
"fmt"
|
2014-08-08 18:09:21 -07:00
|
|
|
"sync"
|
|
|
|
"time"
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2019-02-12 16:38:06 +10:00
|
|
|
opts "github.com/libp2p/go-libp2p-kad-dht/v5/opts"
|
|
|
|
pb "github.com/libp2p/go-libp2p-kad-dht/v5/pb"
|
|
|
|
providers "github.com/libp2p/go-libp2p-kad-dht/v5/providers"
|
2016-09-30 10:13:57 -07:00
|
|
|
|
2016-08-21 17:18:58 +01:00
|
|
|
proto "github.com/gogo/protobuf/proto"
|
2016-09-30 10:13:57 -07:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2016-08-21 17:18:58 +01:00
|
|
|
ds "github.com/ipfs/go-datastore"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
goprocess "github.com/jbenet/goprocess"
|
|
|
|
goprocessctx "github.com/jbenet/goprocess/context"
|
2016-10-05 12:34:28 -07:00
|
|
|
ci "github.com/libp2p/go-libp2p-crypto"
|
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
2016-09-02 20:21:23 +01:00
|
|
|
kb "github.com/libp2p/go-libp2p-kbucket"
|
2018-06-14 16:47:34 -07:00
|
|
|
inet "github.com/libp2p/go-libp2p-net"
|
2016-10-05 12:34:28 -07:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
pstore "github.com/libp2p/go-libp2p-peerstore"
|
|
|
|
protocol "github.com/libp2p/go-libp2p-protocol"
|
2016-09-02 20:21:23 +01:00
|
|
|
record "github.com/libp2p/go-libp2p-record"
|
2016-09-03 20:35:59 +01:00
|
|
|
recpb "github.com/libp2p/go-libp2p-record/pb"
|
2018-05-15 19:00:16 +01:00
|
|
|
routing "github.com/libp2p/go-libp2p-routing"
|
2016-09-30 10:13:57 -07:00
|
|
|
base32 "github.com/whyrusleeping/base32"
|
2014-07-28 22:14:27 -07:00
|
|
|
)
|
|
|
|
|
2019-02-01 17:46:46 +11:00
|
|
|
var logger = logging.Logger("dht")
|
2014-09-25 15:10:57 -07:00
|
|
|
|
2014-12-23 22:04:21 -08:00
|
|
|
// NumBootstrapQueries defines the number of random dht queries to do to
|
|
|
|
// collect members of the routing table.
|
|
|
|
const NumBootstrapQueries = 5
|
|
|
|
|
2018-03-28 03:44:49 +02:00
|
|
|
// IpfsDHT is an implementation of Kademlia with S/Kademlia modifications.
|
2014-07-23 04:48:30 -07:00
|
|
|
// It is used to implement the base IpfsRouting module.
|
|
|
|
type IpfsDHT struct {
|
2016-06-01 15:51:39 -07:00
|
|
|
host host.Host // the network services we need
|
|
|
|
self peer.ID // Local peer (yourself)
|
|
|
|
peerstore pstore.Peerstore // Peer Registry
|
2014-07-29 14:50:33 -07:00
|
|
|
|
2015-05-20 08:50:36 -07:00
|
|
|
datastore ds.Datastore // Local data
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-12-16 08:55:46 -08:00
|
|
|
routingTable *kb.RoutingTable // Array of routing tables for differently distanced nodes
|
2016-05-16 17:01:00 -07:00
|
|
|
providers *providers.ProviderManager
|
2014-08-07 14:16:24 -07:00
|
|
|
|
2016-11-21 20:10:14 -08:00
|
|
|
birth time.Time // When this peer started up
|
2014-10-15 12:30:52 -07:00
|
|
|
|
2018-05-03 16:36:48 -07:00
|
|
|
Validator record.Validator
|
2014-11-09 23:45:16 -08:00
|
|
|
|
2015-06-18 17:17:38 +07:00
|
|
|
ctx context.Context
|
|
|
|
proc goprocess.Process
|
2016-06-06 17:35:56 -07:00
|
|
|
|
|
|
|
strmap map[peer.ID]*messageSender
|
|
|
|
smlk sync.Mutex
|
2017-07-26 13:18:54 -07:00
|
|
|
|
2018-01-06 17:08:24 -08:00
|
|
|
plk sync.Mutex
|
2018-06-01 13:45:34 -07:00
|
|
|
|
|
|
|
protocols []protocol.ID // DHT protocols
|
2014-07-28 22:14:27 -07:00
|
|
|
}
|
|
|
|
|
2019-01-31 10:06:56 +11:00
|
|
|
// Assert that IPFS assumptions about interfaces aren't broken. These aren't a
|
|
|
|
// guarantee, but we can use them to aid refactoring.
|
|
|
|
var (
|
|
|
|
_ routing.ContentRouting = (*IpfsDHT)(nil)
|
|
|
|
_ routing.IpfsRouting = (*IpfsDHT)(nil)
|
|
|
|
_ routing.PeerRouting = (*IpfsDHT)(nil)
|
|
|
|
_ routing.PubKeyFetcher = (*IpfsDHT)(nil)
|
|
|
|
_ routing.ValueStore = (*IpfsDHT)(nil)
|
|
|
|
)
|
|
|
|
|
2018-05-15 19:00:16 +01:00
|
|
|
// New creates a new DHT with the specified host and options.
|
|
|
|
func New(ctx context.Context, h host.Host, options ...opts.Option) (*IpfsDHT, error) {
|
|
|
|
var cfg opts.Options
|
|
|
|
if err := cfg.Apply(append([]opts.Option{opts.Defaults}, options...)...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-01 13:45:34 -07:00
|
|
|
dht := makeDHT(ctx, h, cfg.Datastore, cfg.Protocols)
|
2016-09-23 23:45:35 -07:00
|
|
|
|
|
|
|
// register for network notifs.
|
|
|
|
dht.host.Network().Notify((*netNotifiee)(dht))
|
|
|
|
|
|
|
|
dht.proc = goprocessctx.WithContextAndTeardown(ctx, func() error {
|
|
|
|
// remove ourselves from network notifs.
|
|
|
|
dht.host.Network().StopNotify((*netNotifiee)(dht))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
dht.proc.AddChild(dht.providers.Process())
|
2018-05-15 19:00:16 +01:00
|
|
|
dht.Validator = cfg.Validator
|
|
|
|
|
|
|
|
if !cfg.Client {
|
2018-06-01 13:45:34 -07:00
|
|
|
for _, p := range cfg.Protocols {
|
|
|
|
h.SetStreamHandler(p, dht.handleNewStream)
|
|
|
|
}
|
2018-05-15 19:00:16 +01:00
|
|
|
}
|
|
|
|
return dht, nil
|
|
|
|
}
|
2016-09-23 23:45:35 -07:00
|
|
|
|
2018-05-15 19:00:16 +01:00
|
|
|
// NewDHT creates a new DHT object with the given peer as the 'local' host.
|
|
|
|
// IpfsDHT's initialized with this function will respond to DHT requests,
|
|
|
|
// whereas IpfsDHT's initialized with NewDHTClient will not.
|
|
|
|
func NewDHT(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT {
|
|
|
|
dht, err := New(ctx, h, opts.Datastore(dstore))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return dht
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDHTClient creates a new DHT object with the given peer as the 'local'
|
|
|
|
// host. IpfsDHT clients initialized with this function will not respond to DHT
|
|
|
|
// requests. If you need a peer to respond to DHT requests, use NewDHT instead.
|
|
|
|
// NewDHTClient creates a new DHT object with the given peer as the 'local' host
|
|
|
|
func NewDHTClient(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT {
|
|
|
|
dht, err := New(ctx, h, opts.Datastore(dstore), opts.Client(true))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-09-23 23:45:35 -07:00
|
|
|
return dht
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:45:34 -07:00
|
|
|
func makeDHT(ctx context.Context, h host.Host, dstore ds.Batching, protocols []protocol.ID) *IpfsDHT {
|
2017-10-08 08:40:19 -07:00
|
|
|
rt := kb.NewRoutingTable(KValue, kb.ConvertPeerID(h.ID()), time.Minute, h.Peerstore())
|
|
|
|
|
|
|
|
cmgr := h.ConnManager()
|
|
|
|
rt.PeerAdded = func(p peer.ID) {
|
|
|
|
cmgr.TagPeer(p, "kbucket", 5)
|
|
|
|
}
|
|
|
|
rt.PeerRemoved = func(p peer.ID) {
|
|
|
|
cmgr.UntagPeer(p, "kbucket")
|
|
|
|
}
|
|
|
|
|
2016-09-23 04:38:05 -07:00
|
|
|
return &IpfsDHT{
|
|
|
|
datastore: dstore,
|
|
|
|
self: h.ID(),
|
|
|
|
peerstore: h.Peerstore(),
|
|
|
|
host: h,
|
|
|
|
strmap: make(map[peer.ID]*messageSender),
|
|
|
|
ctx: ctx,
|
|
|
|
providers: providers.NewProviderManager(ctx, h.ID(), dstore),
|
|
|
|
birth: time.Now(),
|
2017-10-08 08:40:19 -07:00
|
|
|
routingTable: rt,
|
2018-06-01 13:45:34 -07:00
|
|
|
protocols: protocols,
|
2016-09-23 04:38:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-28 23:46:25 +00:00
|
|
|
// putValueToPeer stores the given key/value pair at the peer 'p'
|
2018-08-07 17:12:59 -07:00
|
|
|
func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID, rec *recpb.Record) error {
|
2014-09-19 08:07:56 -07:00
|
|
|
|
2018-08-07 17:12:59 -07:00
|
|
|
pmes := pb.NewMessage(pb.Message_PUT_VALUE, rec.Key, 0)
|
2014-11-09 23:45:16 -08:00
|
|
|
pmes.Record = rec
|
2014-09-19 08:07:56 -07:00
|
|
|
rpmes, err := dht.sendRequest(ctx, p, pmes)
|
2014-09-17 07:19:40 -07:00
|
|
|
if err != nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debugf("putValueToPeer: %v. (peer: %s, key: %s)", err, p.Pretty(), loggableKey(string(rec.Key)))
|
2014-09-17 07:19:40 -07:00
|
|
|
return err
|
|
|
|
}
|
2014-09-19 08:07:56 -07:00
|
|
|
|
2014-11-09 23:45:16 -08:00
|
|
|
if !bytes.Equal(rpmes.GetRecord().Value, pmes.GetRecord().Value) {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warningf("putValueToPeer: value not put correctly. (%v != %v)", pmes, rpmes)
|
2014-09-19 08:07:56 -07:00
|
|
|
return errors.New("value not put correctly")
|
|
|
|
}
|
2018-08-02 17:22:59 +03:00
|
|
|
|
2014-09-19 08:07:56 -07:00
|
|
|
return nil
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
2015-11-20 11:12:14 -08:00
|
|
|
var errInvalidRecord = errors.New("received invalid record")
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
// getValueOrPeers queries a particular peer p for the value for
|
|
|
|
// key. It returns either the value or a list of closer peers.
|
2016-04-29 16:57:19 -04:00
|
|
|
// NOTE: It will update the dht's peerstore with any new addresses
|
2014-12-19 12:19:56 -08:00
|
|
|
// it finds for the given peer.
|
2017-03-05 12:08:20 -08:00
|
|
|
func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID, key string) (*recpb.Record, []*pstore.PeerInfo, error) {
|
2014-09-16 02:43:11 -07:00
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
pmes, err := dht.getValueSingle(ctx, p, key)
|
2014-08-15 09:39:38 -07:00
|
|
|
if err != nil {
|
2014-08-15 22:37:53 -07:00
|
|
|
return nil, nil, err
|
2014-08-15 09:39:38 -07:00
|
|
|
}
|
|
|
|
|
2015-09-18 10:27:55 -07:00
|
|
|
// Perhaps we were given closer peers
|
|
|
|
peers := pb.PBPeersToPeerInfos(pmes.GetCloserPeers())
|
|
|
|
|
2014-11-09 23:45:16 -08:00
|
|
|
if record := pmes.GetRecord(); record != nil {
|
2014-08-15 09:39:38 -07:00
|
|
|
// Success! We were given the value
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debug("getValueOrPeers: got value")
|
2014-11-09 23:45:16 -08:00
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
// make sure record is valid.
|
2018-08-07 17:12:59 -07:00
|
|
|
err = dht.Validator.Validate(string(record.GetKey()), record.GetValue())
|
2014-11-09 23:45:16 -08:00
|
|
|
if err != nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Info("Received invalid record! (discarded)")
|
2015-11-20 11:12:14 -08:00
|
|
|
// return a sentinal to signify an invalid record was received
|
|
|
|
err = errInvalidRecord
|
2016-09-03 20:35:59 +01:00
|
|
|
record = new(recpb.Record)
|
2014-11-09 23:45:16 -08:00
|
|
|
}
|
2015-11-20 11:12:14 -08:00
|
|
|
return record, peers, err
|
2014-08-16 23:03:36 -07:00
|
|
|
}
|
2014-08-15 09:39:38 -07:00
|
|
|
|
2014-09-16 05:05:32 -07:00
|
|
|
if len(peers) > 0 {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debug("getValueOrPeers: peers")
|
2014-09-16 05:05:32 -07:00
|
|
|
return nil, peers, nil
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warning("getValueOrPeers: routing.ErrNotFound")
|
2014-10-28 02:17:46 -07:00
|
|
|
return nil, nil, routing.ErrNotFound
|
2014-08-15 09:39:38 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
// getValueSingle simply performs the get value RPC with the given parameters
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID, key string) (*pb.Message, error) {
|
|
|
|
meta := logging.LoggableMap{
|
|
|
|
"key": key,
|
|
|
|
"peer": p,
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:46:46 +11:00
|
|
|
eip := logger.EventBegin(ctx, "getValueSingle", meta)
|
2017-12-11 13:08:29 -08:00
|
|
|
defer eip.Done()
|
2014-09-16 02:43:11 -07:00
|
|
|
|
2018-08-07 17:12:59 -07:00
|
|
|
pmes := pb.NewMessage(pb.Message_GET_VALUE, []byte(key), 0)
|
2016-06-17 11:01:35 -07:00
|
|
|
resp, err := dht.sendRequest(ctx, p, pmes)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return resp, nil
|
|
|
|
case ErrReadTimeout:
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warningf("getValueSingle: read timeout %s %s", p.Pretty(), key)
|
2016-06-17 11:01:35 -07:00
|
|
|
fallthrough
|
|
|
|
default:
|
2017-12-11 13:08:29 -08:00
|
|
|
eip.SetError(err)
|
2016-06-17 11:01:35 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-09 22:28:46 -07:00
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// getLocal attempts to retrieve the value from the datastore
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) getLocal(key string) (*recpb.Record, error) {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debugf("getLocal %s", key)
|
2018-06-13 16:43:19 -07:00
|
|
|
rec, err := dht.getRecordFromDatastore(mkDsKey(key))
|
2014-08-07 21:52:11 -07:00
|
|
|
if err != nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warningf("getLocal: %s", err)
|
2014-08-07 21:52:11 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-16 02:26:46 -07:00
|
|
|
|
2018-06-13 16:43:19 -07:00
|
|
|
// Double check the key. Can't hurt.
|
2018-08-07 17:12:59 -07:00
|
|
|
if rec != nil && string(rec.GetKey()) != key {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Errorf("BUG getLocal: found a DHT record that didn't match it's key: %s != %s", rec.GetKey(), key)
|
2018-06-13 19:42:26 -07:00
|
|
|
return nil, nil
|
2014-11-09 23:45:16 -08:00
|
|
|
|
|
|
|
}
|
2015-09-18 10:27:55 -07:00
|
|
|
return rec, nil
|
2014-08-07 21:52:11 -07:00
|
|
|
}
|
|
|
|
|
2015-01-16 23:53:56 +00:00
|
|
|
// getOwnPrivateKey attempts to load the local peers private
|
|
|
|
// key from the peerstore.
|
2015-01-16 22:46:44 +00:00
|
|
|
func (dht *IpfsDHT) getOwnPrivateKey() (ci.PrivKey, error) {
|
|
|
|
sk := dht.peerstore.PrivKey(dht.self)
|
|
|
|
if sk == nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warningf("%s dht cannot get own private key!", dht.self)
|
2015-01-16 22:46:44 +00:00
|
|
|
return nil, fmt.Errorf("cannot get private key to sign record!")
|
|
|
|
}
|
|
|
|
return sk, nil
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// putLocal stores the key value pair in the datastore
|
2016-09-30 10:13:57 -07:00
|
|
|
func (dht *IpfsDHT) putLocal(key string, rec *recpb.Record) error {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Debugf("putLocal: %v %v", key, rec)
|
2014-11-09 23:45:16 -08:00
|
|
|
data, err := proto.Marshal(rec)
|
|
|
|
if err != nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warningf("putLocal: %s", err)
|
2014-11-09 23:45:16 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
return dht.datastore.Put(mkDsKey(key), data)
|
2014-08-07 21:52:11 -07:00
|
|
|
}
|
2014-08-08 18:09:21 -07:00
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
// Update signals the routingTable to Update its last-seen status
|
2014-09-16 06:40:17 -07:00
|
|
|
// on the given peer.
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) Update(ctx context.Context, p peer.ID) {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Event(ctx, "updatePeer", p)
|
2014-12-11 06:08:53 +00:00
|
|
|
dht.routingTable.Update(p)
|
2014-08-08 18:09:21 -07:00
|
|
|
}
|
2014-08-09 22:28:46 -07:00
|
|
|
|
2014-10-11 10:43:54 -07:00
|
|
|
// FindLocal looks for a peer with a given ID connected to this dht and returns the peer and the table it was found in.
|
2016-06-01 15:51:39 -07:00
|
|
|
func (dht *IpfsDHT) FindLocal(id peer.ID) pstore.PeerInfo {
|
2018-06-14 16:47:34 -07:00
|
|
|
switch dht.host.Network().Connectedness(id) {
|
|
|
|
case inet.Connected, inet.CanConnect:
|
|
|
|
return dht.peerstore.PeerInfo(id)
|
|
|
|
default:
|
|
|
|
return pstore.PeerInfo{}
|
2014-08-09 22:28:46 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
|
2014-11-03 03:02:56 +00:00
|
|
|
// findPeerSingle asks peer 'p' if they know where the peer with id 'id' is
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) findPeerSingle(ctx context.Context, p peer.ID, id peer.ID) (*pb.Message, error) {
|
2019-02-01 17:46:46 +11:00
|
|
|
eip := logger.EventBegin(ctx, "findPeerSingle",
|
2018-03-26 17:30:45 -07:00
|
|
|
logging.LoggableMap{
|
|
|
|
"peer": p,
|
|
|
|
"target": id,
|
|
|
|
})
|
2017-12-11 13:08:29 -08:00
|
|
|
defer eip.Done()
|
2015-01-15 04:17:17 +00:00
|
|
|
|
2018-08-07 17:12:59 -07:00
|
|
|
pmes := pb.NewMessage(pb.Message_FIND_NODE, []byte(id), 0)
|
2016-06-17 11:01:35 -07:00
|
|
|
resp, err := dht.sendRequest(ctx, p, pmes)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return resp, nil
|
|
|
|
case ErrReadTimeout:
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warningf("read timeout: %s %s", p.Pretty(), id)
|
2016-06-17 11:01:35 -07:00
|
|
|
fallthrough
|
|
|
|
default:
|
2017-12-11 13:08:29 -08:00
|
|
|
eip.SetError(err)
|
2016-06-17 11:01:35 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
}
|
2014-08-11 20:11:23 -07:00
|
|
|
|
2018-09-05 03:02:31 -04:00
|
|
|
func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key cid.Cid) (*pb.Message, error) {
|
2019-02-01 17:46:46 +11:00
|
|
|
eip := logger.EventBegin(ctx, "findProvidersSingle", p, key)
|
2017-12-11 13:08:29 -08:00
|
|
|
defer eip.Done()
|
2015-01-15 04:17:17 +00:00
|
|
|
|
2018-08-07 17:12:59 -07:00
|
|
|
pmes := pb.NewMessage(pb.Message_GET_PROVIDERS, key.Bytes(), 0)
|
2016-06-17 11:01:35 -07:00
|
|
|
resp, err := dht.sendRequest(ctx, p, pmes)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return resp, nil
|
|
|
|
case ErrReadTimeout:
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warningf("read timeout: %s %s", p.Pretty(), key)
|
2016-06-17 11:01:35 -07:00
|
|
|
fallthrough
|
|
|
|
default:
|
2017-12-11 13:08:29 -08:00
|
|
|
eip.SetError(err)
|
2016-06-17 11:01:35 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-14 08:32:17 -07:00
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// nearestPeersToQuery returns the routing tables closest peers.
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) nearestPeersToQuery(pmes *pb.Message, count int) []peer.ID {
|
2018-08-07 17:12:59 -07:00
|
|
|
closer := dht.routingTable.NearestPeers(kb.ConvertKey(string(pmes.GetKey())), count)
|
2014-09-16 01:54:53 -07:00
|
|
|
return closer
|
|
|
|
}
|
|
|
|
|
2018-11-15 12:25:38 +01:00
|
|
|
// betterPeersToQuery returns nearestPeersToQuery, but if and only if closer than self.
|
2014-12-14 00:50:49 +00:00
|
|
|
func (dht *IpfsDHT) betterPeersToQuery(pmes *pb.Message, p peer.ID, count int) []peer.ID {
|
2014-10-12 23:07:30 -07:00
|
|
|
closer := dht.nearestPeersToQuery(pmes, count)
|
2014-09-16 01:54:53 -07:00
|
|
|
|
|
|
|
// no node? nil
|
|
|
|
if closer == nil {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Warning("betterPeersToQuery: no closer peers to send:", p)
|
2014-09-16 01:54:53 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-05 11:31:34 -08:00
|
|
|
filtered := make([]peer.ID, 0, len(closer))
|
2017-03-03 23:31:12 -08:00
|
|
|
for _, clp := range closer {
|
|
|
|
|
|
|
|
// == to self? thats bad
|
2017-03-05 12:08:20 -08:00
|
|
|
if clp == dht.self {
|
2019-02-01 17:46:46 +11:00
|
|
|
logger.Error("BUG betterPeersToQuery: attempted to return self! this shouldn't happen...")
|
2014-10-12 23:07:30 -07:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-14 00:50:49 +00:00
|
|
|
// Dont send a peer back themselves
|
2017-03-05 12:08:20 -08:00
|
|
|
if clp == p {
|
2014-12-14 00:50:49 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-21 09:55:25 -07:00
|
|
|
filtered = append(filtered, clp)
|
2014-09-16 01:54:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// ok seems like closer nodes
|
|
|
|
return filtered
|
2014-09-16 01:54:53 -07:00
|
|
|
}
|
|
|
|
|
2015-06-18 17:17:38 +07:00
|
|
|
// Context return dht's context
|
|
|
|
func (dht *IpfsDHT) Context() context.Context {
|
|
|
|
return dht.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process return dht's process
|
|
|
|
func (dht *IpfsDHT) Process() goprocess.Process {
|
|
|
|
return dht.proc
|
|
|
|
}
|
|
|
|
|
2019-01-22 11:46:41 +08:00
|
|
|
// RoutingTable return dht's routingTable
|
|
|
|
func (dht *IpfsDHT) RoutingTable() *kb.RoutingTable {
|
|
|
|
return dht.routingTable
|
|
|
|
}
|
|
|
|
|
2015-06-18 17:17:38 +07:00
|
|
|
// Close calls Process Close
|
|
|
|
func (dht *IpfsDHT) Close() error {
|
|
|
|
return dht.proc.Close()
|
|
|
|
}
|
2016-09-30 10:13:57 -07:00
|
|
|
|
2018-06-01 13:45:34 -07:00
|
|
|
func (dht *IpfsDHT) protocolStrs() []string {
|
|
|
|
pstrs := make([]string, len(dht.protocols))
|
2018-06-01 15:57:22 -07:00
|
|
|
for idx, proto := range dht.protocols {
|
|
|
|
pstrs[idx] = string(proto)
|
2018-06-01 13:45:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return pstrs
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:13:57 -07:00
|
|
|
func mkDsKey(s string) ds.Key {
|
|
|
|
return ds.NewKey(base32.RawStdEncoding.EncodeToString([]byte(s)))
|
|
|
|
}
|