2018-07-04 10:51:47 +00:00
|
|
|
package libp2p
|
|
|
|
|
|
|
|
// This file contains all the default configuration options.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
|
|
|
|
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
|
2022-08-19 16:34:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/security/noise"
|
|
|
|
tls "github.com/libp2p/go-libp2p/p2p/security/tls"
|
|
|
|
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
|
|
|
ws "github.com/libp2p/go-libp2p/p2p/transport/websocket"
|
2023-02-22 21:58:17 +00:00
|
|
|
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
|
2023-04-07 18:23:07 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-08-19 16:34:07 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2022-11-04 13:57:20 +00:00
|
|
|
madns "github.com/multiformats/go-multiaddr-dns"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultSecurity is the default security option.
|
|
|
|
//
|
|
|
|
// Useful when you want to extend, but not replace, the supported transport
|
|
|
|
// security protocols.
|
2021-06-16 20:19:45 +00:00
|
|
|
var DefaultSecurity = ChainOptions(
|
|
|
|
Security(tls.ID, tls.New),
|
2024-06-05 20:10:03 +00:00
|
|
|
Security(noise.ID, noise.New),
|
2021-06-16 20:19:45 +00:00
|
|
|
)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// DefaultMuxers configures libp2p to use the stream connection multiplexers.
|
2018-07-04 10:51:47 +00:00
|
|
|
//
|
|
|
|
// Use this option when you want to *extend* the set of multiplexers used by
|
|
|
|
// libp2p instead of replacing them.
|
2023-02-22 21:58:17 +00:00
|
|
|
var DefaultMuxers = Muxer(yamux.ID, yamux.DefaultTransport)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// DefaultTransports are the default libp2p transports.
|
|
|
|
//
|
2019-06-09 07:24:20 +00:00
|
|
|
// Use this option when you want to *extend* the set of transports used by
|
2018-07-04 10:51:47 +00:00
|
|
|
// libp2p instead of replacing them.
|
|
|
|
var DefaultTransports = ChainOptions(
|
|
|
|
Transport(tcp.NewTCPTransport),
|
2022-04-01 16:16:46 +00:00
|
|
|
Transport(quic.NewTransport),
|
2018-07-04 10:51:47 +00:00
|
|
|
Transport(ws.New),
|
2023-02-22 21:58:17 +00:00
|
|
|
Transport(webtransport.New),
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultPrivateTransports are the default libp2p transports when a PSK is supplied.
|
|
|
|
//
|
|
|
|
// Use this option when you want to *extend* the set of transports used by
|
|
|
|
// libp2p instead of replacing them.
|
|
|
|
var DefaultPrivateTransports = ChainOptions(
|
|
|
|
Transport(tcp.NewTCPTransport),
|
|
|
|
Transport(ws.New),
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultPeerstore configures libp2p to use the default peerstore.
|
|
|
|
var DefaultPeerstore Option = func(cfg *Config) error {
|
2022-04-01 16:16:46 +00:00
|
|
|
ps, err := pstoremem.NewPeerstore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cfg.Apply(Peerstore(ps))
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// RandomIdentity generates a random identity. (default behaviour)
|
2018-07-04 10:51:47 +00:00
|
|
|
var RandomIdentity = func(cfg *Config) error {
|
2022-11-04 13:57:20 +00:00
|
|
|
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cfg.Apply(Identity(priv))
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// DefaultListenAddrs configures libp2p to use default listen address.
|
2019-06-09 07:24:20 +00:00
|
|
|
var DefaultListenAddrs = func(cfg *Config) error {
|
2023-02-22 21:58:17 +00:00
|
|
|
addrs := []string{
|
|
|
|
"/ip4/0.0.0.0/tcp/0",
|
|
|
|
"/ip4/0.0.0.0/udp/0/quic-v1",
|
|
|
|
"/ip4/0.0.0.0/udp/0/quic-v1/webtransport",
|
|
|
|
"/ip6/::/tcp/0",
|
|
|
|
"/ip6/::/udp/0/quic-v1",
|
|
|
|
"/ip6/::/udp/0/quic-v1/webtransport",
|
2022-11-04 13:57:20 +00:00
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
listenAddrs := make([]multiaddr.Multiaddr, 0, len(addrs))
|
|
|
|
for _, s := range addrs {
|
|
|
|
addr, err := multiaddr.NewMultiaddr(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
listenAddrs = append(listenAddrs, addr)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
return cfg.Apply(ListenAddrs(listenAddrs...))
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// DefaultEnableRelay enables relay dialing and listening by default.
|
2019-06-09 07:24:20 +00:00
|
|
|
var DefaultEnableRelay = func(cfg *Config) error {
|
|
|
|
return cfg.Apply(EnableRelay())
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
var DefaultResourceManager = func(cfg *Config) error {
|
|
|
|
// Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
|
2022-08-19 16:34:07 +00:00
|
|
|
limits := rcmgr.DefaultLimits
|
|
|
|
SetDefaultServiceLimits(&limits)
|
|
|
|
mgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(limits.AutoScale()))
|
2022-04-01 16:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.Apply(ResourceManager(mgr))
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// DefaultConnectionManager creates a default connection manager
|
2022-04-01 16:16:46 +00:00
|
|
|
var DefaultConnectionManager = func(cfg *Config) error {
|
|
|
|
mgr, err := connmgr.NewConnManager(160, 192)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.Apply(ConnectionManager(mgr))
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
// DefaultMultiaddrResolver creates a default connection manager
|
|
|
|
var DefaultMultiaddrResolver = func(cfg *Config) error {
|
|
|
|
return cfg.Apply(MultiaddrResolver(madns.DefaultResolver))
|
|
|
|
}
|
|
|
|
|
2023-04-07 18:23:07 +00:00
|
|
|
// DefaultPrometheusRegisterer configures libp2p to use the default registerer
|
|
|
|
var DefaultPrometheusRegisterer = func(cfg *Config) error {
|
|
|
|
return cfg.Apply(PrometheusRegisterer(prometheus.DefaultRegisterer))
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
// Complete list of default options and when to fallback on them.
|
|
|
|
//
|
|
|
|
// Please *DON'T* specify default options any other way. Putting this all here
|
|
|
|
// makes tracking defaults *much* easier.
|
|
|
|
var defaults = []struct {
|
|
|
|
fallback func(cfg *Config) bool
|
|
|
|
opt Option
|
|
|
|
}{
|
2019-06-09 07:24:20 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
|
|
|
|
opt: DefaultListenAddrs,
|
|
|
|
},
|
2018-07-04 10:51:47 +00:00
|
|
|
{
|
2023-02-22 21:58:17 +00:00
|
|
|
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.PSK == nil },
|
2018-07-04 10:51:47 +00:00
|
|
|
opt: DefaultTransports,
|
|
|
|
},
|
2023-02-22 21:58:17 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.PSK != nil },
|
|
|
|
opt: DefaultPrivateTransports,
|
|
|
|
},
|
2018-07-04 10:51:47 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Muxers == nil },
|
|
|
|
opt: DefaultMuxers,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return !cfg.Insecure && cfg.SecurityTransports == nil },
|
|
|
|
opt: DefaultSecurity,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.PeerKey == nil },
|
|
|
|
opt: RandomIdentity,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.Peerstore == nil },
|
|
|
|
opt: DefaultPeerstore,
|
|
|
|
},
|
2019-06-09 07:24:20 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return !cfg.RelayCustom },
|
|
|
|
opt: DefaultEnableRelay,
|
|
|
|
},
|
2022-04-01 16:16:46 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.ResourceManager == nil },
|
|
|
|
opt: DefaultResourceManager,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.ConnManager == nil },
|
|
|
|
opt: DefaultConnectionManager,
|
|
|
|
},
|
2022-11-04 13:57:20 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return cfg.MultiaddrResolver == nil },
|
|
|
|
opt: DefaultMultiaddrResolver,
|
|
|
|
},
|
2023-04-07 18:23:07 +00:00
|
|
|
{
|
|
|
|
fallback: func(cfg *Config) bool { return !cfg.DisableMetrics && cfg.PrometheusRegisterer == nil },
|
|
|
|
opt: DefaultPrometheusRegisterer,
|
|
|
|
},
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Defaults configures libp2p to use the default options. Can be combined with
|
|
|
|
// other options to *extend* the default options.
|
|
|
|
var Defaults Option = func(cfg *Config) error {
|
|
|
|
for _, def := range defaults {
|
|
|
|
if err := cfg.Apply(def.opt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FallbackDefaults applies default options to the libp2p node if and only if no
|
2019-10-04 15:21:24 +00:00
|
|
|
// other relevant options have been applied. will be appended to the options
|
2018-07-04 10:51:47 +00:00
|
|
|
// passed into New.
|
|
|
|
var FallbackDefaults Option = func(cfg *Config) error {
|
|
|
|
for _, def := range defaults {
|
|
|
|
if !def.fallback(cfg) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := cfg.Apply(def.opt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|