update libp2p.New constructor to construct relayed/routed hosts
This commit is contained in:
parent
5c623f269f
commit
d24fe6ae91
|
@ -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
|
||||
|
|
11
options.go
11
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
|
||||
|
|
Loading…
Reference in New Issue