Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autorelay #454

Merged
merged 36 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4ea04c2
identify: implement identify push protocol
vyzo Oct 17, 2018
a89e74a
basic host: export AddrsFactory, provide method to push identify
vyzo Oct 17, 2018
a585891
autorelay hosts
vyzo Oct 17, 2018
9795a01
configurable boot delay for autorelay
vyzo Oct 17, 2018
5c623f2
name the autorelay logger as such
vyzo Oct 17, 2018
d24fe6a
update libp2p.New constructor to construct relayed/routed hosts
vyzo Oct 17, 2018
6bdfcac
use AllAddrs as the address factory in autonat
vyzo Oct 17, 2018
0ff7393
fix announced relay address
vyzo Oct 18, 2018
477c19a
reduce boot delay to 60s
vyzo Oct 18, 2018
7fabe39
parallel identify push
vyzo Oct 18, 2018
c6d1eeb
autorelay test
vyzo Oct 18, 2018
f6c38c9
filter unspecific relay address
vyzo Oct 18, 2018
8d108dd
import go-libp2p-discovery and go-libp2p-autonat
vyzo Oct 18, 2018
8faf540
fix typo
vyzo Oct 19, 2018
b62c83b
better limit adjustment for relay discovery
vyzo Oct 19, 2018
ee69383
extend autorelay test to verify connectivity
vyzo Oct 20, 2018
dcb8fd2
fix inverted address selection logic in test
vyzo Oct 20, 2018
89aef89
don't adveretise unspecific relay addrs in RelayHost
vyzo Oct 20, 2018
a7e1bf0
call the routing interface BasicRouting, alias to top level type
vyzo Oct 22, 2018
2993fd9
add autorelay documentation
vyzo Oct 23, 2018
cc2dd22
make randezvous key a constant
vyzo Oct 24, 2018
67aba4d
move relay selection strategy out of line
vyzo Oct 24, 2018
4f90393
add a comment
vyzo Oct 24, 2018
84400e2
fix typo
vyzo Oct 24, 2018
e96605d
use /p2p multiaddr
vyzo Oct 24, 2018
4be7ada
tag relay connections
vyzo Oct 24, 2018
fdfa224
document doUpdateAddrs
vyzo Oct 24, 2018
292b8a1
update gx deps
vyzo Oct 25, 2018
c2d846a
fix go vet issue
vyzo Oct 25, 2018
7b324b1
remove BasicRouting interface; use PeerRouting and upcast for discovery
vyzo Oct 26, 2018
0dfca3b
increase test AutoNATIdentifyDelay to 100ms
vyzo Oct 26, 2018
69144bd
extend autorelay test to verify pushing of relay addrs
vyzo Oct 26, 2018
5d8988f
add comment about the unstable nature of BasicHost.PushIdentify
vyzo Oct 30, 2018
5a1c09c
use advertised addrs for autonat dial back, not all addrs
vyzo Nov 2, 2018
4cb5d00
gx update
vyzo Nov 4, 2018
a309f09
Add note to relay docs about internal interface instability
vyzo Nov 6, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment saying that this is an unstable interface. Ideally, we'd have an address manager, a protocol muxer, etc. that all emit events. Then, the identify service would subscribe to these events to determine when and if to push information to peers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will add a comment.

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