p2p.go 2.21 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1
package p2p
2 3 4 5

import (
	"context"

tavit ohanian's avatar
tavit ohanian committed
6
	config "gitlab.dms3.io/p2p/go-p2p/config"
Steven Allen's avatar
Steven Allen committed
7

tavit ohanian's avatar
tavit ohanian committed
8
	"gitlab.dms3.io/p2p/go-p2p-core/host"
9 10
)

tavit ohanian's avatar
tavit ohanian committed
11
// Config describes a set of settings for a p2p node.
Steven Allen's avatar
Steven Allen committed
12
type Config = config.Config
Jeromy's avatar
Jeromy committed
13

tavit ohanian's avatar
tavit ohanian committed
14 15
// Option is a p2p config option that can be given to the p2p constructor
// (`p2p.New`).
Steven Allen's avatar
Steven Allen committed
16
type Option = config.Option
Jeromy's avatar
Jeromy committed
17

Steven Allen's avatar
Steven Allen committed
18 19
// ChainOptions chains multiple options into a single option.
func ChainOptions(opts ...Option) Option {
Jeromy's avatar
Jeromy committed
20
	return func(cfg *Config) error {
Steven Allen's avatar
Steven Allen committed
21
		for _, opt := range opts {
Steven Allen's avatar
Steven Allen committed
22 23 24
			if opt == nil {
				continue
			}
Steven Allen's avatar
Steven Allen committed
25
			if err := opt(cfg); err != nil {
Jeromy's avatar
Jeromy committed
26 27
				return err
			}
Jeromy's avatar
Jeromy committed
28
		}
Jeromy's avatar
Jeromy committed
29 30
		return nil
	}
Jeromy's avatar
Jeromy committed
31 32
}

tavit ohanian's avatar
tavit ohanian committed
33
// New constructs a new p2p node with the given options, falling back on
gpestana's avatar
gpestana committed
34 35 36 37 38
// 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";
//
Can ZHANG's avatar
Can ZHANG committed
39
// - If no transport options are provided, the node uses TCP and websocket
gpestana's avatar
gpestana committed
40 41 42 43 44 45
// 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;
//
tavit ohanian's avatar
tavit ohanian committed
46
// - If no security transport is provided, the host uses the go-p2p's noise
47
// and/or tls encrypted transport to encrypt all traffic;
gpestana's avatar
gpestana committed
48
//
49
// - If no peer identity is provided, it generates a random RSA 2048 key-pair
gpestana's avatar
gpestana committed
50 51 52 53
// and derives a new identity from it;
//
// - If no peerstore is provided, the host is initialized with an empty
// peerstore.
Steven Allen's avatar
Steven Allen committed
54
//
tavit ohanian's avatar
tavit ohanian committed
55
// To stop/shutdown the returned p2p node, the user needs to cancel the passed context and call `Close` on the returned Host.
Jeromy's avatar
Jeromy committed
56
func New(ctx context.Context, opts ...Option) (host.Host, error) {
Steven Allen's avatar
Steven Allen committed
57
	return NewWithoutDefaults(ctx, append(opts, FallbackDefaults)...)
Jeromy's avatar
Jeromy committed
58 59
}

tavit ohanian's avatar
tavit ohanian committed
60
// NewWithoutDefaults constructs a new p2p node with the given options but
Steven Allen's avatar
Steven Allen committed
61 62 63 64 65 66 67 68
// *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.
func NewWithoutDefaults(ctx context.Context, opts ...Option) (host.Host, error) {
	var cfg Config
	if err := cfg.Apply(opts...); err != nil {
69 70
		return nil, err
	}
Steven Allen's avatar
Steven Allen committed
71
	return cfg.NewNode(ctx)
72
}