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

import (
4
	"bytes"
5
	"fmt"
Jeromy's avatar
Jeromy committed
6
	"sync"
Jeromy's avatar
Jeromy committed
7
	"time"
8

9
	key "github.com/ipfs/go-ipfs/blocks/key"
10 11 12 13 14
	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"
15
	pset "github.com/ipfs/go-ipfs/thirdparty/peerset"
Jeromy's avatar
Jeromy committed
16

17 18 19
	pstore "gx/ipfs/QmQdnfvZQuhdT93LNc5bos52wAmdr3G2p6G8teLJMEN32P/go-libp2p-peerstore"
	peer "gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer"
	inet "gx/ipfs/QmZ8bCZpMWDbFSh6h2zgTYwrhnjrGM5c9WCzw72SU8p63b/go-libp2p/p2p/net"
Jeromy's avatar
Jeromy committed
20
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22
)

23 24 25 26 27 28
// 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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29 30 31 32 33
// This file implements the Routing interface for the IpfsDHT struct.

// Basic Put/Get

// PutValue adds value corresponding to given Key.
34
// This is the top level "Store" operation of the DHT
35
func (dht *IpfsDHT) PutValue(ctx context.Context, key key.Key, value []byte) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
	log.Debugf("PutValue %s", key)
Jeromy's avatar
Jeromy committed
37
	sk, err := dht.getOwnPrivateKey()
Jeromy's avatar
Jeromy committed
38 39 40
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41

42 43 44 45 46
	sign, err := dht.Validator.IsSigned(key)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
47
	rec, err := record.MakePutRecord(sk, key, value, sign)
Jeromy's avatar
Jeromy committed
48
	if err != nil {
49
		log.Debug("creation of record failed!")
Jeromy's avatar
Jeromy committed
50 51 52
		return err
	}

Jeromy's avatar
Jeromy committed
53
	err = dht.putLocal(key, rec)
54 55 56 57
	if err != nil {
		return err
	}

58
	pchan, err := dht.GetClosestPeers(ctx, key)
59 60 61
	if err != nil {
		return err
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62

63 64 65 66
	wg := sync.WaitGroup{}
	for p := range pchan {
		wg.Add(1)
		go func(p peer.ID) {
Jeromy's avatar
Jeromy committed
67 68
			ctx, cancel := context.WithCancel(ctx)
			defer cancel()
69
			defer wg.Done()
Jeromy's avatar
Jeromy committed
70 71 72 73 74
			notif.PublishQueryEvent(ctx, &notif.QueryEvent{
				Type: notif.Value,
				ID:   p,
			})

Jeromy's avatar
Jeromy committed
75
			err := dht.putValueToPeer(ctx, p, key, rec)
76
			if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
				log.Debugf("failed putting value to peer: %s", err)
78 79 80 81 82
			}
		}(p)
	}
	wg.Wait()
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83 84 85
}

// GetValue searches for the value corresponding to given Key.
86
func (dht *IpfsDHT) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
Jeromy's avatar
Jeromy committed
87 88 89 90
	ctx, cancel := context.WithTimeout(ctx, time.Minute)
	defer cancel()

	vals, err := dht.GetValues(ctx, key, 16)
91 92 93 94 95 96
	if err != nil {
		return nil, err
	}

	var recs [][]byte
	for _, v := range vals {
97 98 99
		if v.Val != nil {
			recs = append(recs, v.Val)
		}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	}

	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) {
125 126 127 128 129 130 131
				if v.From == dht.self {
					err := dht.putLocal(key, fixupRec)
					if err != nil {
						log.Error("Error correcting local dht entry:", err)
					}
					return
				}
Jeromy's avatar
Jeromy committed
132 133
				ctx, cancel := context.WithTimeout(dht.Context(), time.Second*30)
				defer cancel()
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
				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

Jeromy's avatar
Jeromy committed
149
	// If we have it local, dont bother doing an RPC!
150
	lrec, err := dht.getLocal(key)
Jeromy's avatar
Jeromy committed
151
	if err == nil {
152 153
		// TODO: this is tricky, we dont always want to trust our own value
		// what if the authoritative source updated it?
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
154
		log.Debug("have it locally")
155 156 157 158 159 160 161 162 163 164
		vals = append(vals, routing.RecvdVal{
			Val:  lrec.GetValue(),
			From: dht.self,
		})

		if nvals <= 1 {
			return vals, nil
		}
	} else if nvals == 0 {
		return nil, err
Jeromy's avatar
Jeromy committed
165 166
	}

167
	// get closest peers in the routing table
Jeromy's avatar
Jeromy committed
168
	rtp := dht.routingTable.NearestPeers(kb.ConvertKey(key), KValue)
Jeromy's avatar
Jeromy committed
169
	log.Debugf("peers in rt: %s", len(rtp), rtp)
170
	if len(rtp) == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
171
		log.Warning("No peers from routing table!")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172
		return nil, kb.ErrLookupFailure
173 174
	}

175
	// setup the Query
Jeromy's avatar
Jeromy committed
176
	parent := ctx
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
177
	query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
Jeromy's avatar
Jeromy committed
178
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
Jeromy's avatar
Jeromy committed
179 180 181 182
			Type: notif.SendingQuery,
			ID:   p,
		})

183
		rec, peers, err := dht.getValueOrPeers(ctx, p, key)
184 185 186 187 188 189 190 191 192
		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, &notif.QueryEvent{
				Type: notif.PeerResponse,
				ID:   p,
			})
193
			return nil, err
194 195 196 197 198
		default:
			return nil, err

		case nil, errInvalidRecord:
			// in either of these cases, we want to keep going
199
		}
200

201 202
		res := &dhtQueryResult{closerPeers: peers}

203
		if rec.GetValue() != nil || err == errInvalidRecord {
204 205 206 207 208 209 210 211 212 213 214 215
			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()
216 217
		}

Jeromy's avatar
Jeromy committed
218
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
Jeromy's avatar
Jeromy committed
219 220 221 222 223
			Type:      notif.PeerResponse,
			ID:        p,
			Responses: pointerizePeerInfos(peers),
		})

224 225
		return res, nil
	})
226

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
227
	// run it!
228 229 230 231 232
	_, err = query.Run(ctx, rtp)
	if len(vals) == 0 {
		if err != nil {
			return nil, err
		}
233 234
	}

235
	return vals, nil
236

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237 238 239 240 241
}

// Value provider layer of indirection.
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.

242
// Provide makes this node announce that it can provide a value for the given key
243
func (dht *IpfsDHT) Provide(ctx context.Context, key key.Key) error {
Jeromy's avatar
Jeromy committed
244
	defer log.EventBegin(ctx, "provide", &key).Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
245 246

	// add self locally
247
	dht.providers.AddProvider(ctx, key, dht.self)
248

249
	peers, err := dht.GetClosestPeers(ctx, key)
250 251
	if err != nil {
		return err
252 253
	}

254 255 256 257 258
	mes, err := dht.makeProvRecord(key)
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
259
	wg := sync.WaitGroup{}
260
	for p := range peers {
Jeromy's avatar
Jeromy committed
261 262 263
		wg.Add(1)
		go func(p peer.ID) {
			defer wg.Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
264
			log.Debugf("putProvider(%s, %s)", key, p)
265
			err := dht.sendMessage(ctx, p, mes)
Jeromy's avatar
Jeromy committed
266
			if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
267
				log.Debug(err)
Jeromy's avatar
Jeromy committed
268 269
			}
		}(p)
270
	}
Jeromy's avatar
Jeromy committed
271
	wg.Wait()
272
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
273
}
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
func (dht *IpfsDHT) makeProvRecord(skey key.Key) (*pb.Message, error) {
	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.")
	}

	pmes := pb.NewMessage(pb.Message_ADD_PROVIDER, string(skey), 0)
	pmes.ProviderPeers = pb.RawPeerInfosToPBPeers([]pstore.PeerInfo{pi})
	return pmes, nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
290

Brian Tiger Chow's avatar
Brian Tiger Chow committed
291
// FindProviders searches until the context expires.
Jeromy's avatar
Jeromy committed
292 293
func (dht *IpfsDHT) FindProviders(ctx context.Context, key key.Key) ([]pstore.PeerInfo, error) {
	var providers []pstore.PeerInfo
Jeromy's avatar
Jeromy committed
294
	for p := range dht.FindProvidersAsync(ctx, key, KValue) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
295 296 297 298 299
		providers = append(providers, p)
	}
	return providers, nil
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
300 301 302
// 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.
Jeromy's avatar
Jeromy committed
303
func (dht *IpfsDHT) FindProvidersAsync(ctx context.Context, key key.Key, count int) <-chan pstore.PeerInfo {
304
	log.Event(ctx, "findProviders", &key)
Jeromy's avatar
Jeromy committed
305
	peerOut := make(chan pstore.PeerInfo, count)
Jeromy's avatar
Jeromy committed
306 307 308 309
	go dht.findProvidersAsyncRoutine(ctx, key, count, peerOut)
	return peerOut
}

Jeromy's avatar
Jeromy committed
310
func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key key.Key, count int, peerOut chan pstore.PeerInfo) {
Jeromy's avatar
Jeromy committed
311
	defer log.EventBegin(ctx, "findProvidersAsync", &key).Done()
Jeromy's avatar
Jeromy committed
312 313
	defer close(peerOut)

Jeromy's avatar
Jeromy committed
314
	ps := pset.NewLimited(count)
Jeromy's avatar
Jeromy committed
315 316
	provs := dht.providers.GetProviders(ctx, key)
	for _, p := range provs {
317
		// NOTE: Assuming that this list of peers is unique
Jeromy's avatar
Jeromy committed
318
		if ps.TryAdd(p) {
Jeromy's avatar
Jeromy committed
319
			select {
320
			case peerOut <- dht.peerstore.PeerInfo(p):
Jeromy's avatar
Jeromy committed
321 322 323
			case <-ctx.Done():
				return
			}
Jeromy's avatar
Jeromy committed
324
		}
Jeromy's avatar
Jeromy committed
325 326 327

		// If we have enough peers locally, dont bother with remote RPC
		if ps.Size() >= count {
Jeromy's avatar
Jeromy committed
328 329 330 331 332
			return
		}
	}

	// setup the Query
Jeromy's avatar
Jeromy committed
333
	parent := ctx
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
334
	query := dht.newQuery(key, func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
Jeromy's avatar
Jeromy committed
335
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
336 337 338
			Type: notif.SendingQuery,
			ID:   p,
		})
339
		pmes, err := dht.findProvidersSingle(ctx, p, key)
Jeromy's avatar
Jeromy committed
340 341 342 343
		if err != nil {
			return nil, err
		}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
344
		log.Debugf("%d provider entries", len(pmes.GetProviderPeers()))
345
		provs := pb.PBPeersToPeerInfos(pmes.GetProviderPeers())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
346
		log.Debugf("%d provider entries decoded", len(provs))
Jeromy's avatar
Jeromy committed
347 348 349

		// Add unique providers from request, up to 'count'
		for _, prov := range provs {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
350
			log.Debugf("got provider: %s", prov)
351
			if ps.TryAdd(prov.ID) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
352
				log.Debugf("using provider: %s", prov)
Jeromy's avatar
Jeromy committed
353 354 355
				select {
				case peerOut <- prov:
				case <-ctx.Done():
356
					log.Debug("context timed out sending more providers")
Jeromy's avatar
Jeromy committed
357 358
					return nil, ctx.Err()
				}
359
			}
Jeromy's avatar
Jeromy committed
360
			if ps.Size() >= count {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
361
				log.Debugf("got enough providers (%d/%d)", ps.Size(), count)
Jeromy's avatar
Jeromy committed
362
				return &dhtQueryResult{success: true}, nil
363 364 365
			}
		}

Jeromy's avatar
Jeromy committed
366 367
		// Give closer peers back to the query to be queried
		closer := pmes.GetCloserPeers()
368
		clpeers := pb.PBPeersToPeerInfos(closer)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
369
		log.Debugf("got closer peers: %d %s", len(clpeers), clpeers)
370

Jeromy's avatar
Jeromy committed
371
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
372 373 374 375
			Type:      notif.PeerResponse,
			ID:        p,
			Responses: pointerizePeerInfos(clpeers),
		})
Jeromy's avatar
Jeromy committed
376 377 378
		return &dhtQueryResult{closerPeers: clpeers}, nil
	})

Jeromy's avatar
Jeromy committed
379
	peers := dht.routingTable.NearestPeers(kb.ConvertKey(key), KValue)
Jeromy's avatar
Jeromy committed
380 381
	_, err := query.Run(ctx, peers)
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
382
		log.Debugf("Query error: %s", err)
383 384 385 386
		notif.PublishQueryEvent(ctx, &notif.QueryEvent{
			Type:  notif.QueryError,
			Extra: err.Error(),
		})
Jeromy's avatar
Jeromy committed
387
	}
388 389
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
390
// FindPeer searches for a peer with given ID.
Jeromy's avatar
Jeromy committed
391
func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (pstore.PeerInfo, error) {
Jeromy's avatar
Jeromy committed
392
	defer log.EventBegin(ctx, "FindPeer", id).Done()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
393

394
	// Check if were already connected to them
Jeromy's avatar
Jeromy committed
395
	if pi := dht.FindLocal(id); pi.ID != "" {
396
		return pi, nil
397 398
	}

Jeromy's avatar
Jeromy committed
399
	peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), KValue)
400
	if len(peers) == 0 {
Jeromy's avatar
Jeromy committed
401
		return pstore.PeerInfo{}, kb.ErrLookupFailure
402
	}
403

Jeromy's avatar
Jeromy committed
404
	// Sanity...
405
	for _, p := range peers {
406
		if p == id {
407
			log.Debug("found target peer in list of closest peers...")
408
			return dht.peerstore.PeerInfo(p), nil
409
		}
410
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
411

Jeromy's avatar
Jeromy committed
412
	// setup the Query
Jeromy's avatar
Jeromy committed
413
	parent := ctx
414
	query := dht.newQuery(key.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
Jeromy's avatar
Jeromy committed
415
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
416 417 418
			Type: notif.SendingQuery,
			ID:   p,
		})
Jeromy's avatar
Jeromy committed
419

420
		pmes, err := dht.findPeerSingle(ctx, p, id)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
421
		if err != nil {
422
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
423
		}
424

Jeromy's avatar
Jeromy committed
425
		closer := pmes.GetCloserPeers()
426
		clpeerInfos := pb.PBPeersToPeerInfos(closer)
427

428
		// see it we got the peer here
429 430
		for _, npi := range clpeerInfos {
			if npi.ID == id {
Jeromy's avatar
Jeromy committed
431
				return &dhtQueryResult{
432
					peer:    npi,
Jeromy's avatar
Jeromy committed
433 434 435
					success: true,
				}, nil
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
436 437
		}

Jeromy's avatar
Jeromy committed
438
		notif.PublishQueryEvent(parent, &notif.QueryEvent{
439 440 441 442
			Type:      notif.PeerResponse,
			Responses: pointerizePeerInfos(clpeerInfos),
		})

443
		return &dhtQueryResult{closerPeers: clpeerInfos}, nil
444
	})
445

Jeromy's avatar
Jeromy committed
446
	// run it!
447
	result, err := query.Run(ctx, peers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
448
	if err != nil {
Jeromy's avatar
Jeromy committed
449
		return pstore.PeerInfo{}, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
450 451
	}

452
	log.Debugf("FindPeer %v %v", id, result.success)
453
	if result.peer.ID == "" {
Jeromy's avatar
Jeromy committed
454
		return pstore.PeerInfo{}, routing.ErrNotFound
455
	}
Jeromy's avatar
Jeromy committed
456

457
	return result.peer, nil
458 459
}

460
// FindPeersConnectedToPeer searches for peers directly connected to a given peer.
Jeromy's avatar
Jeromy committed
461
func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<-chan pstore.PeerInfo, error) {
462

Jeromy's avatar
Jeromy committed
463 464
	peerchan := make(chan pstore.PeerInfo, asyncQueryBuffer)
	peersSeen := make(map[peer.ID]struct{})
465

Jeromy's avatar
Jeromy committed
466
	peers := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), KValue)
467
	if len(peers) == 0 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
468
		return nil, kb.ErrLookupFailure
469 470 471
	}

	// setup the Query
472
	query := dht.newQuery(key.Key(id), func(ctx context.Context, p peer.ID) (*dhtQueryResult, error) {
473

474
		pmes, err := dht.findPeerSingle(ctx, p, id)
475 476 477 478
		if err != nil {
			return nil, err
		}

Jeromy's avatar
Jeromy committed
479
		var clpeers []pstore.PeerInfo
480 481
		closer := pmes.GetCloserPeers()
		for _, pbp := range closer {
482
			pi := pb.PBPeerToPeerInfo(pbp)
483

484 485
			// skip peers already seen
			if _, found := peersSeen[pi.ID]; found {
486 487
				continue
			}
488
			peersSeen[pi.ID] = struct{}{}
489 490 491 492 493 494

			// if peer is connected, send it to our client.
			if pb.Connectedness(*pbp.Connection) == inet.Connected {
				select {
				case <-ctx.Done():
					return nil, ctx.Err()
495
				case peerchan <- pi:
496 497 498 499
				}
			}

			// if peer is the peer we're looking for, don't bother querying it.
500
			// TODO maybe query it?
501
			if pb.Connectedness(*pbp.Connection) != inet.Connected {
502
				clpeers = append(clpeers, pi)
503 504 505 506 507 508 509 510 511
			}
		}

		return &dhtQueryResult{closerPeers: clpeers}, nil
	})

	// run it! run it asynchronously to gen peers as results are found.
	// this does no error checking
	go func() {
512
		if _, err := query.Run(ctx, peers); err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
513
			log.Debug(err)
514 515 516 517 518 519 520 521
		}

		// close the peerchan channel when done.
		close(peerchan)
	}()

	return peerchan, nil
}