diff --git a/config/config.go b/config/config.go index 8f1c210827..cc689baf7b 100644 --- a/config/config.go +++ b/config/config.go @@ -372,12 +372,11 @@ func (cfg *Config) NewNode() (host.Host, error) { return sw }), fx.Provide(cfg.newBasicHost), - fx.Provide(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) host.Host { - lifecycle.Append(fx.StartHook(h.Start)) - return h + fx.Provide(func(bh *bhost.BasicHost) host.Host { + return bh }), - fx.Provide(func(h host.Host) peer.ID { return h.ID() }), - fx.Provide(func(h host.Host) crypto.PrivKey { return h.Peerstore().PrivKey(h.ID()) }), + fx.Provide(func(h *swarm.Swarm) peer.ID { return h.LocalPeer() }), + fx.Provide(func(h *swarm.Swarm) crypto.PrivKey { return h.Peerstore().PrivKey(h.LocalPeer()) }), } transportOpts, err := cfg.addTransports() if err != nil { @@ -418,6 +417,9 @@ func (cfg *Config) NewNode() (host.Host, error) { var bh *bhost.BasicHost fxopts = append(fxopts, fx.Invoke(func(bho *bhost.BasicHost) { bh = bho })) + fxopts = append(fxopts, fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) { + lifecycle.Append(fx.StartHook(h.Start)) + })) var rh *routed.RoutedHost if cfg.Routing != nil { @@ -430,7 +432,12 @@ func (cfg *Config) NewNode() (host.Host, error) { } if err := cfg.addAutoNAT(bh); err != nil { - rh.Close() + app.Stop(context.Background()) + if cfg.Routing != nil { + rh.Close() + } else { + bh.Close() + } return nil, err } @@ -505,6 +512,7 @@ func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error { return dialer, err }), + fx.Provide(func(s *swarm.Swarm) peer.ID { return s.LocalPeer() }), fx.Provide(func() crypto.PrivKey { return autonatPrivKey }), ) app := fx.New(fxopts...) diff --git a/libp2p_test.go b/libp2p_test.go index 343681be85..1c21146bf9 100644 --- a/libp2p_test.go +++ b/libp2p_test.go @@ -376,6 +376,21 @@ func TestAutoNATService(t *testing.T) { h.Close() } +func TestInsecureConstructor(t *testing.T) { + h, err := New( + EnableNATService(), + NoSecurity, + ) + require.NoError(t, err) + h.Close() + + h, err = New( + NoSecurity, + ) + require.NoError(t, err) + h.Close() +} + func TestDisableIdentifyAddressDiscovery(t *testing.T) { h, err := New(DisableIdentifyAddressDiscovery()) require.NoError(t, err)