Skip to content

Commit

Permalink
config: fix AddrFactory for AutoNAT
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jul 8, 2024
1 parent 2212800 commit 803e846
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
40 changes: 23 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,26 +486,33 @@ func (cfg *Config) NewNode() (host.Host, error) {
)
}

// Note: h.AddrsFactory may be changed by relayFinder, but non-relay version is
// used by AutoNAT below.
if cfg.EnableAutoRelay {
if !cfg.DisableMetrics {
mt := autorelay.WithMetricsTracer(
autorelay.NewMetricsTracer(autorelay.WithRegisterer(cfg.PrometheusRegisterer)))
mtOpts := []autorelay.Option{mt}
cfg.AutoRelayOpts = append(mtOpts, cfg.AutoRelayOpts...)
}
fxopts = append(fxopts,
fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) (*autorelay.AutoRelay, error) {
// oldAddrFactory is the AddrFactory before it's modified by autorelay
// we need this for checking reachability via autonat
oldAddrFactory := func(addrs []ma.Multiaddr) []ma.Multiaddr {
return addrs
}

fxopts = append(fxopts,
fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) (*autorelay.AutoRelay, error) {
oldAddrFactory = h.AddrsFactory
if cfg.EnableAutoRelay {
if !cfg.DisableMetrics {
mt := autorelay.WithMetricsTracer(
autorelay.NewMetricsTracer(autorelay.WithRegisterer(cfg.PrometheusRegisterer)))
mtOpts := []autorelay.Option{mt}
cfg.AutoRelayOpts = append(mtOpts, cfg.AutoRelayOpts...)
}

ar, err := autorelay.NewAutoRelay(h, cfg.AutoRelayOpts...)
if err != nil {
return nil, err
}
lifecycle.Append(fx.StartStopHook(ar.Start, ar.Close))
return ar, nil
}),
)
}
}
return nil, nil
}),
)

var bh *bhost.BasicHost
fxopts = append(fxopts, fx.Invoke(func(bho *bhost.BasicHost) { bh = bho }))
Expand All @@ -523,7 +530,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
return nil, err
}

if err := cfg.addAutoNAT(bh); err != nil {
if err := cfg.addAutoNAT(bh, oldAddrFactory); err != nil {
app.Stop(context.Background())
if cfg.Routing != nil {
rh.Close()
Expand All @@ -539,8 +546,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
return &closableBasicHost{App: app, BasicHost: bh}, nil
}

func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error {
addrF := h.AddrsFactory
func (cfg *Config) addAutoNAT(h *bhost.BasicHost, addrF AddrsFactory) error {
autonatOpts := []autonat.Option{
autonat.UsingAddresses(func() []ma.Multiaddr {
return addrF(h.AllAddrs())
Expand Down
5 changes: 4 additions & 1 deletion libp2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,10 @@ func TestRoutedHost(t *testing.T) {
}

func TestAutoNATService(t *testing.T) {
h, err := New(EnableNATService())
// h, err := New(EnableNATService())
// require.NoError(t, err)
// h.Close()
h, err := New(EnableNATService(), EnableAutoRelayWithStaticRelays([]peer.AddrInfo{{ID: peer.ID("hello")}}))
require.NoError(t, err)
h.Close()
}
Expand Down

0 comments on commit 803e846

Please sign in to comment.