2014-11-20 10:46:19 -08:00
|
|
|
// Package dht implements a distributed hash table that satisfies the ipfs routing
|
2014-11-08 13:43:43 -08:00
|
|
|
// interface. This DHT is modeled after kademlia with Coral and S/Kademlia modifications.
|
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"
|
2014-09-04 20:32:46 +00:00
|
|
|
"crypto/rand"
|
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
|
|
|
|
2015-01-01 12:45:39 -08:00
|
|
|
host "github.com/jbenet/go-ipfs/p2p/host"
|
2014-12-29 05:43:56 -08:00
|
|
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
2015-01-01 12:45:39 -08:00
|
|
|
protocol "github.com/jbenet/go-ipfs/p2p/protocol"
|
2014-10-28 02:17:46 -07:00
|
|
|
routing "github.com/jbenet/go-ipfs/routing"
|
2014-10-25 04:13:28 -07:00
|
|
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
2014-08-09 22:28:46 -07:00
|
|
|
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
2015-01-18 12:31:12 -08:00
|
|
|
"github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
2014-08-08 18:09:21 -07:00
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
2014-07-31 17:43:48 -07:00
|
|
|
|
2014-09-16 00:56:40 -07:00
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
2014-09-09 22:39:42 -07:00
|
|
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
2014-12-16 08:55:46 -08:00
|
|
|
ctxgroup "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
|
|
|
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
2014-07-28 22:14:27 -07:00
|
|
|
)
|
|
|
|
|
2014-11-16 04:01:02 -08:00
|
|
|
var log = eventlog.Logger("dht")
|
2014-09-25 15:10:57 -07:00
|
|
|
|
2015-01-01 12:45:39 -08:00
|
|
|
var ProtocolDHT protocol.ID = "/ipfs/dht"
|
|
|
|
|
2014-10-25 14:50:22 -07:00
|
|
|
const doPinging = false
|
2014-10-17 17:54:47 -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
|
|
|
|
|
2014-07-23 04:48:30 -07:00
|
|
|
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
|
|
|
|
|
|
|
|
// IpfsDHT is an implementation of Kademlia with Coral and S/Kademlia modifications.
|
|
|
|
// It is used to implement the base IpfsRouting module.
|
|
|
|
type IpfsDHT struct {
|
2015-01-01 12:45:39 -08:00
|
|
|
host host.Host // the network services we need
|
2014-12-19 12:19:56 -08:00
|
|
|
self peer.ID // Local peer (yourself)
|
|
|
|
peerstore peer.Peerstore // Peer Registry
|
2014-07-29 14:50:33 -07:00
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
datastore ds.ThreadSafeDatastore // 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
|
|
|
|
providers *ProviderManager
|
2014-08-07 14:16:24 -07:00
|
|
|
|
2014-12-16 08:55:46 -08:00
|
|
|
birth time.Time // When this peer started up
|
|
|
|
diaglock sync.Mutex // lock to make diagnostics work better
|
2014-10-15 12:30:52 -07:00
|
|
|
|
2014-11-09 23:45:16 -08:00
|
|
|
// record validator funcs
|
|
|
|
Validators map[string]ValidatorFunc
|
|
|
|
|
2014-12-16 08:55:46 -08:00
|
|
|
ctxgroup.ContextGroup
|
2014-07-28 22:14:27 -07:00
|
|
|
}
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
// NewDHT creates a new DHT object with the given peer as the 'local' host
|
2015-01-01 12:45:39 -08:00
|
|
|
func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *IpfsDHT {
|
2014-08-01 13:21:51 -07:00
|
|
|
dht := new(IpfsDHT)
|
2014-08-26 14:24:51 -07:00
|
|
|
dht.datastore = dstore
|
2015-01-01 12:45:39 -08:00
|
|
|
dht.self = h.ID()
|
|
|
|
dht.peerstore = h.Peerstore()
|
2014-12-16 08:55:46 -08:00
|
|
|
dht.ContextGroup = ctxgroup.WithContext(ctx)
|
2015-01-01 12:45:39 -08:00
|
|
|
dht.host = h
|
2014-09-16 00:56:40 -07:00
|
|
|
|
2015-01-12 12:21:04 -08:00
|
|
|
// sanity check. this should **never** happen
|
|
|
|
if len(dht.peerstore.Addresses(dht.self)) < 1 {
|
|
|
|
panic("attempt to initialize dht without addresses for self")
|
|
|
|
}
|
|
|
|
|
|
|
|
h.SetStreamHandler(ProtocolDHT, dht.handleNewStream)
|
2015-01-01 12:45:39 -08:00
|
|
|
dht.providers = NewProviderManager(dht.Context(), dht.self)
|
2014-12-16 08:55:46 -08:00
|
|
|
dht.AddChildGroup(dht.providers)
|
2014-08-11 20:11:23 -07:00
|
|
|
|
2015-01-01 12:45:39 -08:00
|
|
|
dht.routingTable = kb.NewRoutingTable(20, kb.ConvertPeerID(dht.self), time.Minute, dht.peerstore)
|
2014-08-06 18:37:45 -07:00
|
|
|
dht.birth = time.Now()
|
2014-11-10 15:48:49 -08:00
|
|
|
|
2014-11-09 23:45:16 -08:00
|
|
|
dht.Validators = make(map[string]ValidatorFunc)
|
2014-11-10 15:48:49 -08:00
|
|
|
dht.Validators["pk"] = ValidatePublicKeyRecord
|
2014-10-15 12:30:52 -07:00
|
|
|
|
2014-10-17 17:54:47 -07:00
|
|
|
if doPinging {
|
2014-10-25 07:12:01 -07:00
|
|
|
dht.Children().Add(1)
|
2014-10-17 17:54:47 -07:00
|
|
|
go dht.PingRoutine(time.Second * 10)
|
|
|
|
}
|
2014-08-10 21:40:17 -07:00
|
|
|
return dht
|
2014-07-31 17:43:48 -07:00
|
|
|
}
|
|
|
|
|
2014-12-18 12:23:09 -08:00
|
|
|
// LocalPeer returns the peer.Peer of the dht.
|
|
|
|
func (dht *IpfsDHT) LocalPeer() peer.ID {
|
|
|
|
return dht.self
|
|
|
|
}
|
|
|
|
|
2015-01-05 04:35:54 -08:00
|
|
|
// log returns the dht's logger
|
|
|
|
func (dht *IpfsDHT) log() eventlog.EventLogger {
|
|
|
|
return log.Prefix("dht(%s)", dht.self)
|
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
// Connect to a new peer at the given address, ping and add to the routing table
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) Connect(ctx context.Context, npeer peer.ID) error {
|
2015-01-01 12:45:39 -08:00
|
|
|
// TODO: change interface to accept a PeerInfo as well.
|
|
|
|
if err := dht.host.Connect(ctx, peer.PeerInfo{ID: npeer}); err != nil {
|
2014-12-08 04:07:52 -08:00
|
|
|
return err
|
2014-07-31 17:43:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 10:02:53 -07:00
|
|
|
// Ping new peer to register in their routing table
|
|
|
|
// NOTE: this should be done better...
|
2015-01-09 19:04:13 +00:00
|
|
|
if _, err := dht.Ping(ctx, npeer); err != nil {
|
|
|
|
return fmt.Errorf("failed to ping newly connected peer: %s", err)
|
2014-08-06 10:02:53 -07:00
|
|
|
}
|
2014-11-25 04:17:37 -08:00
|
|
|
log.Event(ctx, "connect", dht.self, npeer)
|
2014-11-22 20:02:45 -08:00
|
|
|
dht.Update(ctx, npeer)
|
2014-12-08 04:07:52 -08:00
|
|
|
return nil
|
2014-07-30 20:16:34 -07:00
|
|
|
}
|
|
|
|
|
2014-12-28 23:46:25 +00:00
|
|
|
// putValueToPeer stores the given key/value pair at the peer 'p'
|
|
|
|
func (dht *IpfsDHT) putValueToPeer(ctx context.Context, p peer.ID,
|
2014-12-14 00:50:49 +00:00
|
|
|
key u.Key, rec *pb.Record) error {
|
2014-09-19 08:07:56 -07:00
|
|
|
|
2014-10-25 04:13:28 -07:00
|
|
|
pmes := pb.NewMessage(pb.Message_PUT_VALUE, string(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 {
|
|
|
|
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) {
|
2014-09-19 08:07:56 -07:00
|
|
|
return errors.New("value not put correctly")
|
|
|
|
}
|
|
|
|
return nil
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
2014-11-03 03:02:56 +00:00
|
|
|
// putProvider sends a message to peer 'p' saying that the local node
|
|
|
|
// can provide the value of 'key'
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) putProvider(ctx context.Context, p peer.ID, key string) error {
|
2014-09-17 07:19:40 -07:00
|
|
|
|
2014-10-11 06:33:57 -07:00
|
|
|
// add self as the provider
|
2014-12-19 12:19:56 -08:00
|
|
|
pi := dht.peerstore.PeerInfo(dht.self)
|
2015-01-12 12:21:04 -08:00
|
|
|
// // only share WAN-friendly addresses ??
|
|
|
|
// pi.Addrs = addrutil.WANShareableAddrs(pi.Addrs)
|
|
|
|
if len(pi.Addrs) < 1 {
|
|
|
|
log.Errorf("%s putProvider: %s for %s error: no wan-friendly addresses", dht.self, p, u.Key(key), pi.Addrs)
|
|
|
|
return fmt.Errorf("no known addresses for self. cannot put provider.")
|
|
|
|
}
|
2014-10-11 06:33:57 -07:00
|
|
|
|
2015-01-12 12:21:04 -08:00
|
|
|
pmes := pb.NewMessage(pb.Message_ADD_PROVIDER, string(key), 0)
|
|
|
|
pmes.ProviderPeers = pb.PeerInfosToPBPeers(dht.host.Network(), []peer.PeerInfo{pi})
|
2014-12-16 04:01:15 +00:00
|
|
|
err := dht.sendMessage(ctx, p, pmes)
|
2014-09-17 07:19:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-19 14:31:10 -07:00
|
|
|
|
2014-12-22 23:52:21 -08:00
|
|
|
log.Debugf("%s putProvider: %s for %s (%s)", dht.self, p, u.Key(key), pi.Addrs)
|
2014-09-19 14:31:10 -07:00
|
|
|
return nil
|
2014-09-17 07:19:40 -07:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
// NOTE: it will update the dht's peerstore with any new addresses
|
|
|
|
// it finds for the given peer.
|
|
|
|
func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p peer.ID,
|
|
|
|
key u.Key) ([]byte, []peer.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
|
|
|
}
|
|
|
|
|
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
|
2014-09-28 00:13:07 -07:00
|
|
|
log.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.
|
|
|
|
err = dht.verifyRecordOnline(ctx, record)
|
2014-11-09 23:45:16 -08:00
|
|
|
if err != nil {
|
2014-11-14 11:00:45 -08:00
|
|
|
log.Error("Received invalid record!")
|
2014-11-09 23:45:16 -08:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return record.GetValue(), nil, nil
|
2014-08-16 23:03:36 -07:00
|
|
|
}
|
2014-08-15 09:39:38 -07:00
|
|
|
|
2014-09-16 05:05:32 -07:00
|
|
|
// Perhaps we were given closer peers
|
2014-12-19 12:19:56 -08:00
|
|
|
peers := pb.PBPeersToPeerInfos(pmes.GetCloserPeers())
|
2014-09-16 05:05:32 -07:00
|
|
|
if len(peers) > 0 {
|
2014-09-29 17:47:13 +00:00
|
|
|
log.Debug("getValueOrPeers: peers")
|
2014-09-16 05:05:32 -07:00
|
|
|
return nil, peers, nil
|
|
|
|
}
|
|
|
|
|
2014-10-28 02:17:46 -07:00
|
|
|
log.Warning("getValueOrPeers: routing.ErrNotFound")
|
|
|
|
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
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) getValueSingle(ctx context.Context, p peer.ID,
|
2014-12-11 06:08:53 +00:00
|
|
|
key u.Key) (*pb.Message, error) {
|
2015-01-15 04:45:34 +00:00
|
|
|
defer log.EventBegin(ctx, "getValueSingle", p, &key).Done()
|
2014-09-16 02:43:11 -07:00
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
pmes := pb.NewMessage(pb.Message_GET_VALUE, string(key), 0)
|
2014-09-16 02:43:11 -07:00
|
|
|
return dht.sendRequest(ctx, p, pmes)
|
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
|
2014-08-16 23:03:36 -07:00
|
|
|
func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
|
2014-12-19 12:19:56 -08:00
|
|
|
|
2014-11-11 16:28:20 -08:00
|
|
|
log.Debug("getLocal %s", key)
|
2014-10-03 15:34:08 -07:00
|
|
|
v, err := dht.datastore.Get(key.DsKey())
|
2014-08-07 21:52:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-11 16:28:20 -08:00
|
|
|
log.Debug("found in db")
|
2014-09-16 02:26:46 -07:00
|
|
|
|
|
|
|
byt, ok := v.([]byte)
|
|
|
|
if !ok {
|
2014-10-15 12:30:52 -07:00
|
|
|
return nil, errors.New("value stored in datastore not []byte")
|
2014-09-16 02:26:46 -07:00
|
|
|
}
|
2014-11-09 23:45:16 -08:00
|
|
|
rec := new(pb.Record)
|
|
|
|
err = proto.Unmarshal(byt, rec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: 'if paranoid'
|
|
|
|
if u.Debug {
|
2014-12-19 12:19:56 -08:00
|
|
|
err = dht.verifyRecordLocally(rec)
|
2014-11-09 23:45:16 -08:00
|
|
|
if err != nil {
|
2014-11-11 16:28:20 -08:00
|
|
|
log.Errorf("local record verify failed: %s", err)
|
2014-11-09 23:45:16 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec.GetValue(), nil
|
2014-08-07 21:52:11 -07:00
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// putLocal stores the key value pair in the datastore
|
2014-08-16 23:03:36 -07:00
|
|
|
func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
|
2014-11-09 23:45:16 -08:00
|
|
|
rec, err := dht.makePutRecord(key, value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := proto.Marshal(rec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dht.datastore.Put(key.DsKey(), 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) {
|
2014-11-22 20:02:45 -08:00
|
|
|
log.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.
|
2014-12-28 23:46:25 +00:00
|
|
|
func (dht *IpfsDHT) FindLocal(id peer.ID) peer.PeerInfo {
|
2014-12-11 06:08:53 +00:00
|
|
|
p := dht.routingTable.Find(id)
|
2014-12-19 12:19:56 -08:00
|
|
|
if p != "" {
|
2014-12-28 23:46:25 +00:00
|
|
|
return dht.peerstore.PeerInfo(p)
|
2014-08-09 22:28:46 -07:00
|
|
|
}
|
2014-12-28 23:46:25 +00:00
|
|
|
return peer.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) {
|
2015-01-15 04:45:34 +00:00
|
|
|
defer log.EventBegin(ctx, "findPeerSingle", p, id).Done()
|
2015-01-15 04:17:17 +00:00
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
pmes := pb.NewMessage(pb.Message_FIND_NODE, string(id), 0)
|
2014-09-16 07:17:55 -07:00
|
|
|
return dht.sendRequest(ctx, p, pmes)
|
2014-08-10 21:02:05 -07:00
|
|
|
}
|
2014-08-11 20:11:23 -07:00
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p peer.ID, key u.Key) (*pb.Message, error) {
|
2015-01-15 04:45:34 +00:00
|
|
|
defer log.EventBegin(ctx, "findProvidersSingle", p, &key).Done()
|
2015-01-15 04:17:17 +00:00
|
|
|
|
2014-12-11 06:08:53 +00:00
|
|
|
pmes := pb.NewMessage(pb.Message_GET_PROVIDERS, string(key), 0)
|
2014-09-16 07:17:55 -07:00
|
|
|
return dht.sendRequest(ctx, p, pmes)
|
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 {
|
2014-09-16 01:54:53 -07:00
|
|
|
key := u.Key(pmes.GetKey())
|
2014-12-11 06:08:53 +00:00
|
|
|
closer := dht.routingTable.NearestPeers(kb.ConvertKey(key), count)
|
2014-09-16 01:54:53 -07:00
|
|
|
return closer
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// betterPeerToQuery returns nearestPeersToQuery, but iff 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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// == to self? thats bad
|
|
|
|
for _, p := range closer {
|
2014-12-19 12:19:56 -08:00
|
|
|
if p == dht.self {
|
2014-10-12 23:07:30 -07:00
|
|
|
log.Error("Attempted to return self! this shouldnt happen...")
|
|
|
|
return nil
|
|
|
|
}
|
2014-09-16 01:54:53 -07:00
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
var filtered []peer.ID
|
2014-12-14 00:50:49 +00:00
|
|
|
for _, clp := range closer {
|
|
|
|
// Dont send a peer back themselves
|
|
|
|
if p == clp {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-12 23:07:30 -07:00
|
|
|
// must all be closer than self
|
|
|
|
key := u.Key(pmes.GetKey())
|
2014-12-14 00:50:49 +00:00
|
|
|
if !kb.Closer(dht.self, clp, key) {
|
|
|
|
filtered = append(filtered, clp)
|
2014-10-12 23:07:30 -07:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:56 -08:00
|
|
|
func (dht *IpfsDHT) ensureConnectedToPeer(ctx context.Context, p peer.ID) error {
|
|
|
|
if p == dht.self {
|
|
|
|
return errors.New("attempting to ensure connection to self")
|
2014-08-26 14:24:51 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 06:33:51 -07:00
|
|
|
// dial connection
|
2015-01-01 12:45:39 -08:00
|
|
|
return dht.host.Connect(ctx, peer.PeerInfo{ID: p})
|
2014-08-26 14:24:51 -07:00
|
|
|
}
|
2014-09-04 20:32:46 +00:00
|
|
|
|
2014-10-19 23:40:14 -07:00
|
|
|
// PingRoutine periodically pings nearest neighbors.
|
2014-10-15 12:30:52 -07:00
|
|
|
func (dht *IpfsDHT) PingRoutine(t time.Duration) {
|
2014-10-25 07:12:01 -07:00
|
|
|
defer dht.Children().Done()
|
|
|
|
|
2014-10-15 12:30:52 -07:00
|
|
|
tick := time.Tick(t)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick:
|
|
|
|
id := make([]byte, 16)
|
|
|
|
rand.Read(id)
|
2014-12-11 06:08:53 +00:00
|
|
|
peers := dht.routingTable.NearestPeers(kb.ConvertKey(u.Key(id)), 5)
|
2014-10-15 12:30:52 -07:00
|
|
|
for _, p := range peers {
|
2014-10-25 07:12:01 -07:00
|
|
|
ctx, _ := context.WithTimeout(dht.Context(), time.Second*5)
|
2015-01-09 19:04:13 +00:00
|
|
|
_, err := dht.Ping(ctx, p)
|
2014-10-15 12:30:52 -07:00
|
|
|
if err != nil {
|
2014-10-25 03:17:14 -07:00
|
|
|
log.Errorf("Ping error: %s", err)
|
2014-10-15 12:30:52 -07:00
|
|
|
}
|
|
|
|
}
|
2014-10-25 07:12:01 -07:00
|
|
|
case <-dht.Closing():
|
2014-10-15 12:30:52 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 07:17:55 -07:00
|
|
|
// Bootstrap builds up list of peers by requesting random peer IDs
|
2014-12-08 18:21:57 -08:00
|
|
|
func (dht *IpfsDHT) Bootstrap(ctx context.Context, queries int) error {
|
2014-12-09 12:04:25 -08:00
|
|
|
var merr u.MultiErr
|
2014-12-19 12:19:56 -08:00
|
|
|
|
2014-12-09 11:22:00 -08:00
|
|
|
randomID := func() peer.ID {
|
|
|
|
// 16 random bytes is not a valid peer id. it may be fine becuase
|
|
|
|
// the dht will rehash to its own keyspace anyway.
|
2014-12-23 23:12:22 -08:00
|
|
|
id := make([]byte, 16)
|
|
|
|
rand.Read(id)
|
2014-12-09 11:22:00 -08:00
|
|
|
return peer.ID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bootstrap sequentially, as results will compound
|
2014-12-09 12:04:25 -08:00
|
|
|
runQuery := func(ctx context.Context, id peer.ID) {
|
2014-12-09 11:22:00 -08:00
|
|
|
p, err := dht.FindPeer(ctx, id)
|
2014-12-23 23:12:22 -08:00
|
|
|
if err == routing.ErrNotFound {
|
|
|
|
// this isn't an error. this is precisely what we expect.
|
|
|
|
} else if err != nil {
|
2014-12-09 12:04:25 -08:00
|
|
|
merr = append(merr, err)
|
2014-12-23 23:12:22 -08:00
|
|
|
} else {
|
2014-12-09 12:04:25 -08:00
|
|
|
// woah, actually found a peer with that ID? this shouldn't happen normally
|
|
|
|
// (as the ID we use is not a real ID). this is an odd error worth logging.
|
|
|
|
err := fmt.Errorf("Bootstrap peer error: Actually FOUND peer. (%s, %s)", id, p)
|
|
|
|
log.Errorf("%s", err)
|
|
|
|
merr = append(merr, err)
|
2014-12-23 23:12:22 -08:00
|
|
|
}
|
2014-10-17 17:54:47 -07:00
|
|
|
}
|
2014-12-09 12:04:25 -08:00
|
|
|
|
|
|
|
sequential := true
|
|
|
|
if sequential {
|
|
|
|
// these should be parallel normally. but can make them sequential for debugging.
|
|
|
|
// note that the core/bootstrap context deadline should be extended too for that.
|
|
|
|
for i := 0; i < queries; i++ {
|
|
|
|
id := randomID()
|
|
|
|
log.Debugf("Bootstrapping query (%d/%d) to random ID: %s", i+1, queries, id)
|
|
|
|
runQuery(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// note on parallelism here: the context is passed in to the queries, so they
|
|
|
|
// **should** exit when it exceeds, making this function exit on ctx cancel.
|
|
|
|
// normally, we should be selecting on ctx.Done() here too, but this gets
|
|
|
|
// complicated to do with WaitGroup, and doesnt wait for the children to exit.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < queries; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
id := randomID()
|
|
|
|
log.Debugf("Bootstrapping query (%d/%d) to random ID: %s", i+1, queries, id)
|
|
|
|
runQuery(ctx, id)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(merr) > 0 {
|
|
|
|
return merr
|
|
|
|
}
|
2014-12-08 18:21:57 -08:00
|
|
|
return nil
|
2014-09-04 20:32:46 +00:00
|
|
|
}
|