@@ -23,11 +23,13 @@ import (
2323 "github.com/libp2p/go-libp2p/core/sec"
2424 "github.com/libp2p/go-libp2p/core/sec/insecure"
2525 "github.com/libp2p/go-libp2p/core/transport"
26+ logging "github.com/libp2p/go-libp2p/gologshim"
2627 "github.com/libp2p/go-libp2p/p2p/host/autonat"
2728 "github.com/libp2p/go-libp2p/p2p/host/autorelay"
2829 bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
2930 blankhost "github.com/libp2p/go-libp2p/p2p/host/blank"
3031 "github.com/libp2p/go-libp2p/p2p/host/eventbus"
32+ "github.com/libp2p/go-libp2p/p2p/host/observedaddrs"
3133 "github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
3234 rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
3335 routed "github.com/libp2p/go-libp2p/p2p/host/routed"
@@ -50,6 +52,8 @@ import (
5052 "go.uber.org/fx/fxevent"
5153)
5254
55+ var log = logging .Logger ("p2p-config" )
56+
5357// AddrsFactory is a function that takes a set of multiaddrs we're listening on and
5458// returns the set of multiaddrs we should advertise to the network.
5559type AddrsFactory = bhost.AddrsFactory
@@ -158,9 +162,7 @@ func (cfg *Config) makeSwarm(eventBus event.Bus, enableMetrics bool) (*swarm.Swa
158162
159163 // Check this early. Prevents us from even *starting* without verifying this.
160164 if pnet .ForcePrivateNetwork && len (cfg .PSK ) == 0 {
161- log .Error ("tried to create a libp2p node with no Private" +
162- " Network Protector but usage of Private Networks" +
163- " is forced by the environment" )
165+ log .Error ("tried to create a libp2p node with no Private Network Protector but usage of Private Networks is forced by the environment" )
164166 // Note: This is *also* checked the upgrader itself, so it'll be
165167 // enforced even *if* you don't use the libp2p constructor.
166168 return nil , pnet .ErrNotInPrivateNetwork
@@ -287,7 +289,11 @@ func (cfg *Config) makeAutoNATV2Host() (host.Host, error) {
287289
288290func (cfg * Config ) addTransports () ([]fx.Option , error ) {
289291 fxopts := []fx.Option {
290- fx .WithLogger (func () fxevent.Logger { return getFXLogger () }),
292+ fx .WithLogger (func () fxevent.Logger {
293+ return & fxevent.SlogLogger {
294+ Logger : log .With ("system" , "fx" ),
295+ }
296+ }),
291297 fx .Provide (fx .Annotate (tptu .New , fx .ParamTags (`name:"security"` ))),
292298 fx .Supply (cfg .Muxers ),
293299 fx .Provide (func () connmgr.ConnectionGater { return cfg .ConnectionGater }),
@@ -434,23 +440,23 @@ func (cfg *Config) addTransports() ([]fx.Option, error) {
434440 return fxopts , nil
435441}
436442
437- func (cfg * Config ) newBasicHost (swrm * swarm.Swarm , eventBus event.Bus , an * autonatv2.AutoNAT ) (* bhost.BasicHost , error ) {
443+ func (cfg * Config ) newBasicHost (swrm * swarm.Swarm , eventBus event.Bus , an * autonatv2.AutoNAT , o bhost. ObservedAddrsManager ) (* bhost.BasicHost , error ) {
438444 h , err := bhost .NewHost (swrm , & bhost.HostOpts {
439- EventBus : eventBus ,
440- ConnManager : cfg .ConnManager ,
441- AddrsFactory : cfg .AddrsFactory ,
442- NATManager : cfg .NATManager ,
443- EnablePing : ! cfg .DisablePing ,
444- UserAgent : cfg .UserAgent ,
445- ProtocolVersion : cfg .ProtocolVersion ,
446- EnableHolePunching : cfg .EnableHolePunching ,
447- HolePunchingOptions : cfg .HolePunchingOptions ,
448- EnableRelayService : cfg .EnableRelayService ,
449- RelayServiceOpts : cfg .RelayServiceOpts ,
450- EnableMetrics : ! cfg .DisableMetrics ,
451- PrometheusRegisterer : cfg .PrometheusRegisterer ,
452- DisableIdentifyAddressDiscovery : cfg . DisableIdentifyAddressDiscovery ,
453- AutoNATv2 : an ,
445+ EventBus : eventBus ,
446+ ConnManager : cfg .ConnManager ,
447+ AddrsFactory : cfg .AddrsFactory ,
448+ NATManager : cfg .NATManager ,
449+ EnablePing : ! cfg .DisablePing ,
450+ UserAgent : cfg .UserAgent ,
451+ ProtocolVersion : cfg .ProtocolVersion ,
452+ EnableHolePunching : cfg .EnableHolePunching ,
453+ HolePunchingOptions : cfg .HolePunchingOptions ,
454+ EnableRelayService : cfg .EnableRelayService ,
455+ RelayServiceOpts : cfg .RelayServiceOpts ,
456+ EnableMetrics : ! cfg .DisableMetrics ,
457+ PrometheusRegisterer : cfg .PrometheusRegisterer ,
458+ AutoNATv2 : an ,
459+ ObservedAddrsManager : o ,
454460 })
455461 if err != nil {
456462 return nil , err
@@ -467,7 +473,7 @@ func (cfg *Config) validate() error {
467473 if l , ok := cfg .ResourceManager .(connmgr.GetConnLimiter ); ok {
468474 err := cfg .ConnManager .CheckLimit (l )
469475 if err != nil {
470- log .Warn (fmt . Sprintf ( "rcmgr limit conflicts with connmgr limit: %v" , err ) )
476+ log .Warn ("rcmgr limit conflicts with connmgr limit" , "err" , err )
471477 }
472478 }
473479
@@ -529,6 +535,25 @@ func (cfg *Config) NewNode() (host.Host, error) {
529535 })
530536 return sw , nil
531537 }),
538+ fx .Provide (func (eventBus event.Bus , s * swarm.Swarm , lifecycle fx.Lifecycle ) (bhost.ObservedAddrsManager , error ) {
539+ if cfg .DisableIdentifyAddressDiscovery {
540+ return nil , nil
541+ }
542+ o , err := observedaddrs .NewManager (eventBus , s )
543+ if err != nil {
544+ return nil , err
545+ }
546+ lifecycle .Append (fx.Hook {
547+ OnStart : func (context.Context ) error {
548+ o .Start (s )
549+ return nil
550+ },
551+ OnStop : func (context.Context ) error {
552+ return o .Close ()
553+ },
554+ })
555+ return o , nil
556+ }),
532557 fx .Provide (func () (* autonatv2.AutoNAT , error ) {
533558 if ! cfg .EnableAutoNATv2 {
534559 return nil , nil
0 commit comments