github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/p2p/host.go (about)

     1  package p2p
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/libp2p/go-libp2p"
     8  	p2pconfig "github.com/libp2p/go-libp2p/config"
     9  	"github.com/libp2p/go-libp2p/core/connmgr"
    10  	"github.com/libp2p/go-libp2p/core/crypto"
    11  	hst "github.com/libp2p/go-libp2p/core/host"
    12  	"github.com/libp2p/go-libp2p/core/metrics"
    13  	"github.com/libp2p/go-libp2p/core/network"
    14  	"github.com/libp2p/go-libp2p/core/peer"
    15  	"github.com/libp2p/go-libp2p/core/peerstore"
    16  	"github.com/libp2p/go-libp2p/core/routing"
    17  	routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
    18  	"github.com/libp2p/go-libp2p/p2p/net/conngater"
    19  	"github.com/prometheus/client_golang/prometheus"
    20  	"go.uber.org/fx"
    21  
    22  	"github.com/celestiaorg/celestia-node/nodebuilder/node"
    23  )
    24  
    25  // routedHost constructs a wrapped Host that may fallback to address discovery,
    26  // if any top-level operation on the Host is provided with PeerID(Hash(PbK)) only.
    27  func routedHost(base HostBase, r routing.PeerRouting) hst.Host {
    28  	return routedhost.Wrap(base, r)
    29  }
    30  
    31  // host returns constructor for Host.
    32  func host(params hostParams) (HostBase, error) {
    33  	opts := []libp2p.Option{
    34  		libp2p.NoListenAddrs, // do not listen automatically
    35  		libp2p.AddrsFactory(params.AddrF),
    36  		libp2p.Identity(params.Key),
    37  		libp2p.Peerstore(params.PStore),
    38  		libp2p.ConnectionManager(params.ConnMngr),
    39  		libp2p.ConnectionGater(params.ConnGater),
    40  		libp2p.UserAgent(fmt.Sprintf("celestia-%s", params.Net)),
    41  		libp2p.NATPortMap(), // enables upnp
    42  		libp2p.DisableRelay(),
    43  		libp2p.BandwidthReporter(params.Bandwidth),
    44  		libp2p.ResourceManager(params.ResourceManager),
    45  		// to clearly define what defaults we rely upon
    46  		libp2p.DefaultSecurity,
    47  		libp2p.DefaultTransports,
    48  		libp2p.DefaultMuxers,
    49  	}
    50  
    51  	if params.Registry != nil {
    52  		opts = append(opts, libp2p.PrometheusRegisterer(params.Registry))
    53  	} else {
    54  		opts = append(opts, libp2p.DisableMetrics())
    55  	}
    56  
    57  	// All node types except light (bridge, full) will enable NATService
    58  	if params.Tp != node.Light {
    59  		opts = append(opts, libp2p.EnableNATService())
    60  	}
    61  
    62  	h, err := libp2p.NewWithoutDefaults(opts...)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	params.Lc.Append(fx.Hook{OnStop: func(context.Context) error {
    68  		return h.Close()
    69  	}})
    70  
    71  	return h, nil
    72  }
    73  
    74  type HostBase hst.Host
    75  
    76  type hostParams struct {
    77  	fx.In
    78  
    79  	Net             Network
    80  	Lc              fx.Lifecycle
    81  	ID              peer.ID
    82  	Key             crypto.PrivKey
    83  	AddrF           p2pconfig.AddrsFactory
    84  	PStore          peerstore.Peerstore
    85  	ConnMngr        connmgr.ConnManager
    86  	ConnGater       *conngater.BasicConnectionGater
    87  	Bandwidth       *metrics.BandwidthCounter
    88  	ResourceManager network.ResourceManager
    89  	Registry        prometheus.Registerer `optional:"true"`
    90  
    91  	Tp node.Type
    92  }