From d24fe6ae9161e8e6ce475fe8e7554d2e2c9fdbcb Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 17 Oct 2018 18:24:54 +0300 Subject: [PATCH] update libp2p.New constructor to construct relayed/routed hosts --- config/config.go | 46 +++++++++++++++++++++++++++++++++++++++++++--- options.go | 11 +++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index dd3d399..aa06705 100644 --- a/config/config.go +++ b/config/config.go @@ -5,10 +5,13 @@ import ( "fmt" bhost "github.com/libp2p/go-libp2p/p2p/host/basic" + relay "github.com/libp2p/go-libp2p/p2p/host/relay" + routed "github.com/libp2p/go-libp2p/p2p/host/routed" logging "github.com/ipfs/go-log" circuit "github.com/libp2p/go-libp2p-circuit" crypto "github.com/libp2p/go-libp2p-crypto" + discovery "github.com/libp2p/go-libp2p-discovery" host "github.com/libp2p/go-libp2p-host" ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr" pnet "github.com/libp2p/go-libp2p-interface-pnet" @@ -16,6 +19,7 @@ import ( inet "github.com/libp2p/go-libp2p-net" peer "github.com/libp2p/go-libp2p-peer" pstore "github.com/libp2p/go-libp2p-peerstore" + routing "github.com/libp2p/go-libp2p-routing" swarm "github.com/libp2p/go-libp2p-swarm" tptu "github.com/libp2p/go-libp2p-transport-upgrader" filter "github.com/libp2p/go-maddr-filter" @@ -31,6 +35,13 @@ type AddrsFactory = bhost.AddrsFactory // NATManagerC is a NATManager constructor. type NATManagerC func(inet.Network) bhost.NATManager +type Routing interface { + routing.ContentRouting + routing.PeerRouting +} + +type RoutingC func(host.Host) (Routing, error) + // Config describes a set of settings for a libp2p node // // This is *not* a stable interface. Use the options defined in the root @@ -58,6 +69,8 @@ type Config struct { Reporter metrics.Reporter DisablePing bool + + Routing RoutingC } // NewNode constructs a new libp2p Host from the Config. @@ -99,8 +112,8 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { swrm.Filters = cfg.Filters } - // TODO: make host implementation configurable. - h, err := bhost.NewHost(ctx, swrm, &bhost.HostOpts{ + var h host.Host + h, err = bhost.NewHost(ctx, swrm, &bhost.HostOpts{ ConnManager: cfg.ConnManager, AddrsFactory: cfg.AddrsFactory, NATManager: cfg.NATManager, @@ -158,7 +171,34 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) { return nil, err } - // TODO: Configure routing (it's a pain to setup). + if cfg.Routing != nil { + router, err := cfg.Routing(h) + if err != nil { + h.Close() + return nil, err + } + + if cfg.Relay { + discovery := discovery.NewRoutingDiscovery(router) + + hop := false + for _, opt := range cfg.RelayOpts { + if opt == circuit.OptHop { + hop = true + break + } + } + + if hop { + h = relay.NewRelayHost(swrm.Context(), h.(*bhost.BasicHost), discovery) + } else { + h = relay.NewAutoRelayHost(swrm.Context(), h.(*bhost.BasicHost), discovery) + } + } + + h = routed.Wrap(h, router) + } + // TODO: Bootstrapping. return h, nil diff --git a/options.go b/options.go index 4ab4f39..23c9f7d 100644 --- a/options.go +++ b/options.go @@ -260,6 +260,17 @@ func Ping(enable bool) Option { } } +// Routing will configure libp2p to use routing. +func Routing(rt config.RoutingC) Option { + return func(cfg *Config) error { + if cfg.Routing != nil { + return fmt.Errorf("cannot specified multiple routing options") + } + cfg.Routing = rt + return nil + } +} + // NoListenAddrs will configure libp2p to not listen by default. // // This will both clear any configured listen addrs and prevent libp2p from -- GitLab