2017-11-04 19:06:13 +00:00
|
|
|
package libp2p
|
|
|
|
|
|
|
|
import (
|
2021-09-17 16:48:22 +00:00
|
|
|
"github.com/libp2p/go-libp2p/config"
|
2018-03-10 01:56:02 +00:00
|
|
|
|
2019-05-26 21:55:46 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
2017-11-04 19:06:13 +00:00
|
|
|
)
|
|
|
|
|
2020-03-03 14:33:32 +00:00
|
|
|
// Config describes a set of settings for a libp2p node.
|
2018-03-10 01:56:02 +00:00
|
|
|
type Config = config.Config
|
2017-12-14 23:40:24 +00:00
|
|
|
|
2018-03-10 01:56:02 +00:00
|
|
|
// Option is a libp2p config option that can be given to the libp2p constructor
|
|
|
|
// (`libp2p.New`).
|
|
|
|
type Option = config.Option
|
2017-12-14 23:40:24 +00:00
|
|
|
|
2018-03-10 01:56:02 +00:00
|
|
|
// ChainOptions chains multiple options into a single option.
|
|
|
|
func ChainOptions(opts ...Option) Option {
|
2017-12-14 23:40:24 +00:00
|
|
|
return func(cfg *Config) error {
|
2018-03-10 01:56:02 +00:00
|
|
|
for _, opt := range opts {
|
2019-04-17 23:20:02 +00:00
|
|
|
if opt == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-10 01:56:02 +00:00
|
|
|
if err := opt(cfg); err != nil {
|
2017-12-14 23:40:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-12-16 00:02:30 +00:00
|
|
|
}
|
2017-12-14 23:40:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-05 15:28:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 17:28:00 +00:00
|
|
|
// New constructs a new libp2p node with the given options, falling back on
|
|
|
|
// reasonable defaults. The defaults are:
|
|
|
|
//
|
|
|
|
// - If no transport and listen addresses are provided, the node listens to
|
|
|
|
// the multiaddresses "/ip4/0.0.0.0/tcp/0" and "/ip6/::/tcp/0";
|
|
|
|
//
|
2021-12-04 13:52:56 +00:00
|
|
|
// - If no transport options are provided, the node uses TCP, websocket and QUIC
|
2018-08-05 17:28:00 +00:00
|
|
|
// transport protocols;
|
|
|
|
//
|
|
|
|
// - If no multiplexer configuration is provided, the node is configured by
|
|
|
|
// default to use the "yamux/1.0.0" and "mplux/6.7.0" stream connection
|
|
|
|
// multiplexers;
|
|
|
|
//
|
2020-06-24 17:28:07 +00:00
|
|
|
// - If no security transport is provided, the host uses the go-libp2p's noise
|
|
|
|
// and/or tls encrypted transport to encrypt all traffic;
|
2018-08-05 17:28:00 +00:00
|
|
|
//
|
2020-02-21 17:35:36 +00:00
|
|
|
// - If no peer identity is provided, it generates a random RSA 2048 key-pair
|
2018-08-05 17:28:00 +00:00
|
|
|
// and derives a new identity from it;
|
|
|
|
//
|
|
|
|
// - If no peerstore is provided, the host is initialized with an empty
|
|
|
|
// peerstore.
|
2018-03-10 01:56:02 +00:00
|
|
|
//
|
2021-01-19 05:27:34 +00:00
|
|
|
// To stop/shutdown the returned libp2p node, the user needs to cancel the passed context and call `Close` on the returned Host.
|
2021-09-17 16:48:22 +00:00
|
|
|
func New(opts ...Option) (host.Host, error) {
|
|
|
|
return NewWithoutDefaults(append(opts, FallbackDefaults)...)
|
2017-12-16 00:02:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 01:56:02 +00:00
|
|
|
// NewWithoutDefaults constructs a new libp2p node with the given options but
|
|
|
|
// *without* falling back on reasonable defaults.
|
|
|
|
//
|
|
|
|
// Warning: This function should not be considered a stable interface. We may
|
|
|
|
// choose to add required services at any time and, by using this function, you
|
|
|
|
// opt-out of any defaults we may provide.
|
2021-09-17 16:48:22 +00:00
|
|
|
func NewWithoutDefaults(opts ...Option) (host.Host, error) {
|
2018-03-10 01:56:02 +00:00
|
|
|
var cfg Config
|
|
|
|
if err := cfg.Apply(opts...); err != nil {
|
2017-11-04 19:06:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-17 16:48:22 +00:00
|
|
|
return cfg.NewNode()
|
2017-11-04 19:06:13 +00:00
|
|
|
}
|