@cryptotaxi247 / kubo / commits / b14907b79

use libp2p.Routing in constructor for routing host construction and autorelay

License: MIT Signed-off-by: vyzo <vyzo@hackzen.org>

vyzo committed Nov 19, 2018 at 15:59 UTC b14907b794e6cd4e1208e566d07db38ece70bbc1
1 file changed +20 -9
core/core.go
+20 -9
@@ -259,12 +259,21 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
259 libp2pOpts = append(libp2pOpts, libp2p.Transport(quic.NewTransport))
260 }
261
262 + // enable routing and autorelay
263 + libp2pOpts = append(libp2pOpts, libp2p.Routing(func(h p2phost.Host) (routing.PeerRouting, error) {
264 + r, err := routingOption(ctx, h, n.Repo.Datastore(), n.RecordValidator)
265 + n.Routing = r
266 + return r, err
267 + }))
268 +
269 peerhost, err := hostOption(ctx, n.Identity, n.Peerstore, libp2pOpts...)
270
271 if err != nil {
272 return err
273 }
274
275 + n.PeerHost = peerhost
276 +
277 if err := n.startOnlineServicesWithHost(ctx, peerhost, routingOption, pubsub, ipnsps); err != nil {
278 return err
279 }
@@ -496,12 +505,17 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
505 n.PubSub = service
506 }
507
499 - // setup routing service
500 - r, err := routingOption(ctx, host, n.Repo.Datastore(), n.RecordValidator)
501 - if err != nil {
502 - return err
508 + // sadly, this code is necessary just for tests:
509 + // it is necessary for mock network constructions that ignore the libp2p options
510 + // that actually construct the routing!
511 + if n.Routing == nil {
512 + r, err := routingOption(ctx, host, n.Repo.Datastore(), n.RecordValidator)
513 + if err != nil {
514 + return err
515 + }
516 + n.Routing = r
517 + n.PeerHost = rhost.Wrap(host, n.Routing)
518 }
504 - n.Routing = r
519
520 // TODO: I'm not a fan of type assertions like this but the
521 // `RoutingOption` system doesn't currently provide access to the
@@ -516,7 +530,7 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
530 // PSRouter case below.
531 // 3. Introduce some kind of service manager? (my personal favorite but
532 // that requires a fair amount of work).
519 - if dht, ok := r.(*dht.IpfsDHT); ok {
533 + if dht, ok := n.Routing.(*dht.IpfsDHT); ok {
534 n.DHT = dht
535 }
536
@@ -543,9 +557,6 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
557 }
558 }
559
546 - // Wrap standard peer host with routing system to allow unknown peer lookups
547 - n.PeerHost = rhost.Wrap(host, n.Routing)
548 -
560 // setup exchange service
561 bitswapNetwork := bsnet.NewFromIpfsHost(n.PeerHost, n.Routing)
562 n.Exchange = bitswap.New(ctx, bitswapNetwork, n.Blockstore)