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

implement relay v2 discovery #1368

Merged
merged 9 commits into from Apr 10, 2022
22 changes: 3 additions & 19 deletions config/config.go
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"

drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
Expand Down Expand Up @@ -101,8 +100,8 @@ type Config struct {
Routing RoutingC

EnableAutoRelay bool
AutoRelayOpts []autorelay.Option
AutoNATConfig
StaticRelayOpt autorelay.StaticRelayOption

EnableHolePunching bool
HolePunchingOptions []holepunch.Option
Expand Down Expand Up @@ -270,7 +269,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
}
}

// Note: h.AddrsFactory may be changed by AutoRelay, but non-relay version is
// Note: h.AddrsFactory may be changed by relayFinder, but non-relay version is
// used by AutoNAT below.
var ar *autorelay.AutoRelay
addrF := h.AddrsFactory
Expand All @@ -280,22 +279,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
}

var opts []autorelay.Option
if cfg.StaticRelayOpt != nil {
opts = append(opts, autorelay.Option(cfg.StaticRelayOpt))
} else {
if router == nil {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no routing for discovery")
}
crouter, ok := router.(routing.ContentRouting)
if !ok {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no suitable routing for discovery")
}
opts = append(opts, autorelay.WithDiscoverer(drouting.NewRoutingDiscovery(crouter)))
}
ar, err = autorelay.NewAutoRelay(h, router, opts...)
ar, err = autorelay.NewAutoRelay(h, cfg.AutoRelayOpts...)
if err != nil {
return nil, err
}
Expand Down
11 changes: 3 additions & 8 deletions options.go
Expand Up @@ -250,15 +250,10 @@ func EnableRelayService(opts ...relayv2.Option) Option {
//
// This subsystem performs automatic address rewriting to advertise relay addresses when it
// detects that the node is publicly unreachable (e.g. behind a NAT).
func EnableAutoRelay(opts ...autorelay.StaticRelayOption) Option {
func EnableAutoRelay(opts ...autorelay.Option) Option {
return func(cfg *Config) error {
if len(opts) > 0 {
if len(opts) > 1 {
return errors.New("only expected a single static relay configuration option")
}
cfg.StaticRelayOpt = opts[0]
}
cfg.EnableAutoRelay = true
cfg.AutoRelayOpts = opts
return nil
}
}
Expand All @@ -269,7 +264,7 @@ func EnableAutoRelay(opts ...autorelay.StaticRelayOption) Option {
// Deprecated: pass an autorelay.WithStaticRelays option to EnableAutoRelay.
func StaticRelays(relays []peer.AddrInfo) Option {
return func(cfg *Config) error {
cfg.StaticRelayOpt = autorelay.WithStaticRelays(relays)
cfg.AutoRelayOpts = append(cfg.AutoRelayOpts, autorelay.WithStaticRelays(relays))
return nil
}
}
Expand Down