dht.go 14.3 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2
package dht

3
import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	"bytes"
5
	"crypto/rand"
6
	"errors"
7
	"fmt"
8 9
	"sync"
	"time"
10

11
	inet "github.com/jbenet/go-ipfs/net"
12
	msg "github.com/jbenet/go-ipfs/net/message"
13
	peer "github.com/jbenet/go-ipfs/peer"
Jeromy's avatar
Jeromy committed
14
	kb "github.com/jbenet/go-ipfs/routing/kbucket"
15
	u "github.com/jbenet/go-ipfs/util"
16

17
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
18
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
19
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Jeromy's avatar
Jeromy committed
20

21
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
22 23
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
var log = u.Logger("dht")
25

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28 29 30
// 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 {
31 32
	// Array of routing tables for differently distanced nodes
	// NOTE: (currently, only a single table is used)
33
	routingTables []*kb.RoutingTable
34

35
	// the network interface. service
36
	network inet.Network
37
	sender  inet.Sender
38

Jeromy's avatar
Jeromy committed
39 40 41
	// Local peer (yourself)
	self *peer.Peer

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43 44
	// Other peers
	peerstore peer.Peerstore

Jeromy's avatar
Jeromy committed
45 46
	// Local data
	datastore ds.Datastore
47
	dslock    sync.Mutex
48

49
	providers *ProviderManager
50

51 52
	// When this peer started up
	birth time.Time
53 54 55

	//lock to make diagnostics work better
	diaglock sync.Mutex
56 57

	ctx context.Context
58 59
}

Jeromy's avatar
Jeromy committed
60
// NewDHT creates a new DHT object with the given peer as the 'local' host
61
func NewDHT(ctx context.Context, p *peer.Peer, ps peer.Peerstore, net inet.Network, sender inet.Sender, dstore ds.Datastore) *IpfsDHT {
62
	dht := new(IpfsDHT)
Jeromy's avatar
Jeromy committed
63
	dht.network = net
64
	dht.sender = sender
Jeromy's avatar
Jeromy committed
65
	dht.datastore = dstore
66
	dht.self = p
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
	dht.peerstore = ps
68
	dht.ctx = ctx
69

70
	dht.providers = NewProviderManager(p.ID)
71

72
	dht.routingTables = make([]*kb.RoutingTable, 3)
73 74
	dht.routingTables[0] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Millisecond*1000)
	dht.routingTables[1] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Millisecond*1000)
75
	dht.routingTables[2] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Hour)
76
	dht.birth = time.Now()
77 78

	go dht.PingRoutine(time.Second * 10)
Jeromy's avatar
Jeromy committed
79
	return dht
80 81
}

82
// Connect to a new peer at the given address, ping and add to the routing table
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
func (dht *IpfsDHT) Connect(ctx context.Context, npeer *peer.Peer) (*peer.Peer, error) {
84
	log.Debug("Connect to new peer: %s", npeer)
85 86 87 88 89 90 91 92 93 94 95

	// TODO(jbenet,whyrusleeping)
	//
	// Connect should take in a Peer (with ID). In a sense, we shouldn't be
	// allowing connections to random multiaddrs without knowing who we're
	// speaking to (i.e. peer.ID). In terms of moving around simple addresses
	// -- instead of an (ID, Addr) pair -- we can use:
	//
	//   /ip4/10.20.30.40/tcp/1234/ipfs/Qxhxxchxzcncxnzcnxzcxzm
	//
	err := dht.network.DialPeer(npeer)
96
	if err != nil {
97
		return nil, err
98 99
	}

Jeromy's avatar
Jeromy committed
100 101
	// Ping new peer to register in their routing table
	// NOTE: this should be done better...
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102
	err = dht.Ping(ctx, npeer)
Jeromy's avatar
Jeromy committed
103
	if err != nil {
104
		return nil, fmt.Errorf("failed to ping newly connected peer: %s\n", err)
Jeromy's avatar
Jeromy committed
105 106
	}

107 108
	dht.Update(npeer)

109
	return npeer, nil
Jeromy's avatar
Jeromy committed
110 111
}

112
// HandleMessage implements the inet.Handler interface.
113
func (dht *IpfsDHT) HandleMessage(ctx context.Context, mes msg.NetMessage) msg.NetMessage {
114 115 116

	mData := mes.Data()
	if mData == nil {
117
		log.Error("Message contained nil data.")
118
		return nil
119 120 121 122
	}

	mPeer := mes.Peer()
	if mPeer == nil {
123
		log.Error("Message contained nil peer.")
124
		return nil
125 126 127 128 129 130
	}

	// deserialize msg
	pmes := new(Message)
	err := proto.Unmarshal(mData, pmes)
	if err != nil {
131
		log.Error("Error unmarshaling data")
132
		return nil
133 134 135 136 137 138
	}

	// update the peer (on valid msgs only)
	dht.Update(mPeer)

	// Print out diagnostic
Jeromy's avatar
Jeromy committed
139
	log.Debug("[peer: %s] Got message type: '%s' [from = %s]\n",
140
		dht.self, Message_MessageType_name[int32(pmes.GetType())], mPeer)
141 142 143 144

	// get handler for this msg type.
	handler := dht.handlerForMsgType(pmes.GetType())
	if handler == nil {
145
		log.Error("got back nil handler from handlerForMsgType")
146
		return nil
147 148 149 150 151
	}

	// dispatch handler.
	rpmes, err := handler(mPeer, pmes)
	if err != nil {
152
		log.Error("handle message error: %s", err)
153
		return nil
154 155
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156 157
	// if nil response, return it before serializing
	if rpmes == nil {
158
		log.Warning("Got back nil response from request.")
159
		return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
160 161
	}

162 163 164
	// serialize response msg
	rmes, err := msg.FromObject(mPeer, rpmes)
	if err != nil {
165
		log.Error("serialze response error: %s", err)
166
		return nil
167 168
	}

169
	return rmes
170 171
}

172 173 174 175 176 177 178 179 180 181 182
// sendRequest sends out a request using dht.sender, but also makes sure to
// measure the RTT for latency measurements.
func (dht *IpfsDHT) sendRequest(ctx context.Context, p *peer.Peer, pmes *Message) (*Message, error) {

	mes, err := msg.FromObject(p, pmes)
	if err != nil {
		return nil, err
	}

	start := time.Now()

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
183
	// Print out diagnostic
184
	log.Debug("Sent message type: '%s' [to = %s]",
185
		Message_MessageType_name[int32(pmes.GetType())], p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
186

187 188 189 190
	rmes, err := dht.sender.SendRequest(ctx, mes)
	if err != nil {
		return nil, err
	}
191 192 193
	if rmes == nil {
		return nil, errors.New("no response to request")
	}
194 195 196 197 198 199 200 201 202 203 204 205

	rtt := time.Since(start)
	rmes.Peer().SetLatency(rtt)

	rpmes := new(Message)
	if err := proto.Unmarshal(rmes.Data(), rpmes); err != nil {
		return nil, err
	}

	return rpmes, nil
}

206
// putValueToNetwork stores the given key/value pair at the peer 'p'
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
207 208 209
func (dht *IpfsDHT) putValueToNetwork(ctx context.Context, p *peer.Peer,
	key string, value []byte) error {

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
210 211
	pmes := newMessage(Message_PUT_VALUE, string(key), 0)
	pmes.Value = value
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
212
	rpmes, err := dht.sendRequest(ctx, p, pmes)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
213 214 215
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
216 217 218 219 220

	if !bytes.Equal(rpmes.Value, pmes.Value) {
		return errors.New("value not put correctly")
	}
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
221 222 223 224
}

func (dht *IpfsDHT) putProvider(ctx context.Context, p *peer.Peer, key string) error {

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
225
	pmes := newMessage(Message_ADD_PROVIDER, string(key), 0)
226 227 228 229

	// add self as the provider
	pmes.ProviderPeers = peersToPBPeers([]*peer.Peer{dht.self})

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
230
	rpmes, err := dht.sendRequest(ctx, p, pmes)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
231 232 233
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
234

235
	log.Debug("%s putProvider: %s for %s", dht.self, p, key)
236
	if rpmes.GetKey() != pmes.GetKey() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237 238 239 240
		return errors.New("provider not added correctly")
	}

	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
241 242
}

243 244 245 246
func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p *peer.Peer,
	key u.Key, level int) ([]byte, []*peer.Peer, error) {

	pmes, err := dht.getValueSingle(ctx, p, key, level)
247
	if err != nil {
248
		return nil, nil, err
249 250
	}

Jeromy's avatar
Jeromy committed
251
	log.Debug("pmes.GetValue() %v", pmes.GetValue())
252
	if value := pmes.GetValue(); value != nil {
253
		// Success! We were given the value
Jeromy's avatar
Jeromy committed
254
		log.Debug("getValueOrPeers: got value")
255
		return value, nil, nil
256
	}
257

258
	// TODO decide on providers. This probably shouldn't be happening.
259 260 261 262 263
	if prv := pmes.GetProviderPeers(); prv != nil && len(prv) > 0 {
		val, err := dht.getFromPeerList(ctx, key, prv, level)
		if err != nil {
			return nil, nil, err
		}
Jeromy's avatar
Jeromy committed
264
		log.Debug("getValueOrPeers: get from providers")
265 266
		return val, nil, nil
	}
267 268

	// Perhaps we were given closer peers
269
	var peers []*peer.Peer
270
	for _, pb := range pmes.GetCloserPeers() {
271
		pr, err := dht.addPeer(pb)
272
		if err != nil {
273
			log.Error("%s", err)
274 275
			continue
		}
276
		peers = append(peers, pr)
277
	}
278 279

	if len(peers) > 0 {
280
		log.Debug("getValueOrPeers: peers")
281 282 283
		return nil, peers, nil
	}

Jeromy's avatar
Jeromy committed
284
	log.Warning("getValueOrPeers: u.ErrNotFound")
285
	return nil, nil, u.ErrNotFound
286 287
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
func (dht *IpfsDHT) addPeer(pb *Message_Peer) (*peer.Peer, error) {
	if peer.ID(pb.GetId()).Equal(dht.self.ID) {
		return nil, errors.New("cannot add self as peer")
	}

	addr, err := ma.NewMultiaddr(pb.GetAddr())
	if err != nil {
		return nil, err
	}

	// check if we already have this peer.
	pr, _ := dht.peerstore.Get(peer.ID(pb.GetId()))
	if pr == nil {
		pr = &peer.Peer{ID: peer.ID(pb.GetId())}
		dht.peerstore.Put(pr)
	}
	pr.AddAddress(addr) // idempotent

	return pr, nil
}

309
// getValueSingle simply performs the get value RPC with the given parameters
310 311 312
func (dht *IpfsDHT) getValueSingle(ctx context.Context, p *peer.Peer,
	key u.Key, level int) (*Message, error) {

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
313
	pmes := newMessage(Message_GET_VALUE, string(key), level)
314
	return dht.sendRequest(ctx, p, pmes)
Jeromy's avatar
Jeromy committed
315 316
}

317 318 319 320
// TODO: Im not certain on this implementation, we get a list of peers/providers
// from someone what do we do with it? Connect to each of them? randomly pick
// one to get the value from? Or just connect to one at a time until we get a
// successful connection and request the value from it?
321 322
func (dht *IpfsDHT) getFromPeerList(ctx context.Context, key u.Key,
	peerlist []*Message_Peer, level int) ([]byte, error) {
323

324
	for _, pinfo := range peerlist {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
325
		p, err := dht.ensureConnectedToPeer(pinfo)
326
		if err != nil {
Jeromy's avatar
Jeromy committed
327
			log.Error("getFromPeers error: %s", err)
328
			continue
Jeromy's avatar
Jeromy committed
329
		}
330 331

		pmes, err := dht.getValueSingle(ctx, p, key, level)
Jeromy's avatar
Jeromy committed
332
		if err != nil {
Jeromy's avatar
Jeromy committed
333
			log.Error("getFromPeers error: %s\n", err)
Jeromy's avatar
Jeromy committed
334 335 336
			continue
		}

337 338 339 340
		if value := pmes.GetValue(); value != nil {
			// Success! We were given the value
			dht.providers.AddProvider(key, p)
			return value, nil
341
		}
Jeromy's avatar
Jeromy committed
342 343 344 345
	}
	return nil, u.ErrNotFound
}

346
// getLocal attempts to retrieve the value from the datastore
347
func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
348 349
	dht.dslock.Lock()
	defer dht.dslock.Unlock()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
350
	v, err := dht.datastore.Get(key.DsKey())
351 352 353
	if err != nil {
		return nil, err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
354 355 356

	byt, ok := v.([]byte)
	if !ok {
357
		return nil, errors.New("value stored in datastore not []byte")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
358 359
	}
	return byt, nil
360 361
}

362
// putLocal stores the key value pair in the datastore
363
func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
364
	return dht.datastore.Put(key.DsKey(), value)
365
}
366

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
367 368
// Update signals to all routingTables to Update their last-seen status
// on the given peer.
369
func (dht *IpfsDHT) Update(p *peer.Peer) {
370
	log.Debug("updating peer: %s latency = %f\n", p, p.GetLatency().Seconds())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
371
	removedCount := 0
372
	for _, route := range dht.routingTables {
373
		removed := route.Update(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
374
		// Only close the connection if no tables refer to this peer
375
		if removed != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
376
			removedCount++
377
		}
378
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
379 380 381 382 383 384 385 386

	// Only close the connection if no tables refer to this peer
	// if removedCount == len(dht.routingTables) {
	// 	dht.network.ClosePeer(p)
	// }
	// ACTUALLY, no, let's not just close the connection. it may be connected
	// due to other things. it seems that we just need connection timeouts
	// after some deadline of inactivity.
387
}
Jeromy's avatar
Jeromy committed
388

Jeromy's avatar
Jeromy committed
389 390
// FindLocal looks for a peer with a given ID connected to this dht and returns the peer and the table it was found in.
func (dht *IpfsDHT) FindLocal(id peer.ID) (*peer.Peer, *kb.RoutingTable) {
391
	for _, table := range dht.routingTables {
Jeromy's avatar
Jeromy committed
392 393 394 395 396 397 398
		p := table.Find(id)
		if p != nil {
			return p, table
		}
	}
	return nil, nil
}
399

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
400 401 402
func (dht *IpfsDHT) findPeerSingle(ctx context.Context, p *peer.Peer, id peer.ID, level int) (*Message, error) {
	pmes := newMessage(Message_FIND_NODE, string(id), level)
	return dht.sendRequest(ctx, p, pmes)
403
}
404

405 406
func (dht *IpfsDHT) printTables() {
	for _, route := range dht.routingTables {
407 408 409
		route.Print()
	}
}
Jeromy's avatar
Jeromy committed
410

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
411 412 413
func (dht *IpfsDHT) findProvidersSingle(ctx context.Context, p *peer.Peer, key u.Key, level int) (*Message, error) {
	pmes := newMessage(Message_GET_PROVIDERS, string(key), level)
	return dht.sendRequest(ctx, p, pmes)
Jeromy's avatar
Jeromy committed
414 415
}

416
// TODO: Could be done async
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
417
func (dht *IpfsDHT) addProviders(key u.Key, peers []*Message_Peer) []*peer.Peer {
418
	var provArr []*peer.Peer
Jeromy's avatar
Jeromy committed
419
	for _, prov := range peers {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
420 421
		p, err := dht.peerFromInfo(prov)
		if err != nil {
422
			log.Error("error getting peer from info: %v", err)
Jeromy's avatar
Jeromy committed
423 424
			continue
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
425

426
		log.Debug("%s adding provider: %s for %s", dht.self, p, key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
427

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
428 429 430
		// Dont add outselves to the list
		if p.ID.Equal(dht.self.ID) {
			continue
Jeromy's avatar
Jeromy committed
431
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
432 433

		// TODO(jbenet) ensure providers is idempotent
434
		dht.providers.AddProvider(key, p)
435
		provArr = append(provArr, p)
Jeromy's avatar
Jeromy committed
436
	}
437
	return provArr
Jeromy's avatar
Jeromy committed
438
}
Jeromy's avatar
Jeromy committed
439

440 441
// nearestPeersToQuery returns the routing tables closest peers.
func (dht *IpfsDHT) nearestPeersToQuery(pmes *Message, count int) []*peer.Peer {
442 443 444 445
	level := pmes.GetClusterLevel()
	cluster := dht.routingTables[level]

	key := u.Key(pmes.GetKey())
446
	closer := cluster.NearestPeers(kb.ConvertKey(key), count)
447 448 449
	return closer
}

450 451 452
// betterPeerToQuery returns nearestPeersToQuery, but iff closer than self.
func (dht *IpfsDHT) betterPeersToQuery(pmes *Message, count int) []*peer.Peer {
	closer := dht.nearestPeersToQuery(pmes, count)
453 454 455 456 457 458

	// no node? nil
	if closer == nil {
		return nil
	}

459 460 461 462 463 464
	// == to self? thats bad
	for _, p := range closer {
		if p.ID.Equal(dht.self.ID) {
			log.Error("Attempted to return self! this shouldnt happen...")
			return nil
		}
465 466
	}

467 468 469 470 471 472 473
	var filtered []*peer.Peer
	for _, p := range closer {
		// must all be closer than self
		key := u.Key(pmes.GetKey())
		if !kb.Closer(dht.self.ID, p.ID, key) {
			filtered = append(filtered, p)
		}
474 475
	}

476 477
	// ok seems like closer nodes
	return filtered
478 479
}

480 481 482
func (dht *IpfsDHT) peerFromInfo(pbp *Message_Peer) (*peer.Peer, error) {

	id := peer.ID(pbp.GetId())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
483 484 485 486 487 488

	// continue if it's ourselves
	if id.Equal(dht.self.ID) {
		return nil, errors.New("found self")
	}

489 490
	p, _ := dht.peerstore.Get(id)
	if p == nil {
Jeromy's avatar
Jeromy committed
491
		p, _ = dht.FindLocal(id)
492 493 494 495 496 497
		if p != nil {
			panic("somehow peer not getting into peerstore")
		}
	}

	if p == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
498
		maddr, err := pbp.Address()
499 500 501 502 503
		if err != nil {
			return nil, err
		}

		// create new Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
504
		p = &peer.Peer{ID: id}
505
		p.AddAddress(maddr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
506
		dht.peerstore.Put(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
507
		log.Info("dht found new peer: %s %s", p, maddr)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
508 509 510 511 512 513 514 515
	}
	return p, nil
}

func (dht *IpfsDHT) ensureConnectedToPeer(pbp *Message_Peer) (*peer.Peer, error) {
	p, err := dht.peerFromInfo(pbp)
	if err != nil {
		return nil, err
Jeromy's avatar
Jeromy committed
516 517
	}

518
	// dial connection
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
519
	err = dht.network.DialPeer(p)
520
	return p, err
Jeromy's avatar
Jeromy committed
521
}
522

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
523
//TODO: this should be smarter about which keys it selects.
524
func (dht *IpfsDHT) loadProvidableKeys() error {
525 526 527 528
	kl, err := dht.datastore.KeyList()
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
529 530 531 532 533 534 535
	for _, dsk := range kl {
		k := u.KeyFromDsKey(dsk)
		if len(k) == 0 {
			log.Error("loadProvidableKeys error: %v", dsk)
		}

		dht.providers.AddProvider(k, dht.self)
536 537 538 539
	}
	return nil
}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
func (dht *IpfsDHT) PingRoutine(t time.Duration) {
	tick := time.Tick(t)
	for {
		select {
		case <-tick:
			id := make([]byte, 16)
			rand.Read(id)
			peers := dht.routingTables[0].NearestPeers(kb.ConvertKey(u.Key(id)), 5)
			for _, p := range peers {
				ctx, _ := context.WithTimeout(dht.ctx, time.Second*5)
				err := dht.Ping(ctx, p)
				if err != nil {
					log.Error("Ping error: %s", err)
				}
			}
		case <-dht.ctx.Done():
			return
		}
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
561
// Bootstrap builds up list of peers by requesting random peer IDs
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
562
func (dht *IpfsDHT) Bootstrap(ctx context.Context) {
563 564
	id := make([]byte, 16)
	rand.Read(id)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
565
	dht.FindPeer(ctx, peer.ID(id))
566
}