Skip to content

Commit

Permalink
Merge pull request #454 from libp2p/feat/autorelay
Browse files Browse the repository at this point in the history
autorelay
  • Loading branch information
vyzo authored Nov 6, 2018
2 parents 2b6c3a2 + a309f09 commit 3e2dc09
Show file tree
Hide file tree
Showing 10 changed files with 700 additions and 11 deletions.
44 changes: 41 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ 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"
metrics "github.com/libp2p/go-libp2p-metrics"
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"
Expand All @@ -31,6 +35,8 @@ type AddrsFactory = bhost.AddrsFactory
// NATManagerC is a NATManager constructor.
type NATManagerC func(inet.Network) bhost.NATManager

type RoutingC func(host.Host) (routing.PeerRouting, error)

// Config describes a set of settings for a libp2p node
//
// This is *not* a stable interface. Use the options defined in the root
Expand Down Expand Up @@ -58,6 +64,8 @@ type Config struct {
Reporter metrics.Reporter

DisablePing bool

Routing RoutingC
}

// NewNode constructs a new libp2p Host from the Config.
Expand Down Expand Up @@ -99,8 +107,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,
Expand Down Expand Up @@ -158,7 +166,37 @@ 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
}

crouter, ok := router.(routing.ContentRouting)
if ok {
if cfg.Relay {
discovery := discovery.NewRoutingDiscovery(crouter)

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
Expand Down
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 specify 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
Expand Down
23 changes: 15 additions & 8 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ type BasicHost struct {
ids *identify.IDService
pings *ping.PingService
natmgr NATManager
addrs AddrsFactory
maResolver *madns.Resolver
cmgr ifconnmgr.ConnManager

AddrsFactory AddrsFactory

negtimeout time.Duration

proc goprocess.Process
Expand Down Expand Up @@ -106,11 +107,11 @@ type HostOpts struct {
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, error) {
h := &BasicHost{
network: net,
mux: msmux.NewMultistreamMuxer(),
negtimeout: DefaultNegotiationTimeout,
addrs: DefaultAddrsFactory,
maResolver: madns.DefaultResolver,
network: net,
mux: msmux.NewMultistreamMuxer(),
negtimeout: DefaultNegotiationTimeout,
AddrsFactory: DefaultAddrsFactory,
maResolver: madns.DefaultResolver,
}

h.proc = goprocessctx.WithContextAndTeardown(ctx, func() error {
Expand All @@ -136,7 +137,7 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost,
}

if opts.AddrsFactory != nil {
h.addrs = opts.AddrsFactory
h.AddrsFactory = opts.AddrsFactory
}

if opts.NATManager != nil {
Expand Down Expand Up @@ -256,6 +257,12 @@ func (h *BasicHost) newStreamHandler(s inet.Stream) {
go handle(protoID, s)
}

// PushIdentify pushes an identify update through the identify push protocol
// Warning: this interface is unstable and may disappear in the future.
func (h *BasicHost) PushIdentify() {
h.ids.Push()
}

// ID returns the (local) peer.ID associated with this Host
func (h *BasicHost) ID() peer.ID {
return h.Network().LocalPeer()
Expand Down Expand Up @@ -474,7 +481,7 @@ func (h *BasicHost) ConnManager() ifconnmgr.ConnManager {
// Addrs returns listening addresses that are safe to announce to the network.
// The output is the same as AllAddrs, but processed by AddrsFactory.
func (h *BasicHost) Addrs() []ma.Multiaddr {
return h.addrs(h.AllAddrs())
return h.AddrsFactory(h.AllAddrs())
}

// mergeAddrs merges input address lists, leave only unique addresses
Expand Down
Loading

0 comments on commit 3e2dc09

Please sign in to comment.