-
Notifications
You must be signed in to change notification settings - Fork 0
/
dep2p.go
68 lines (62 loc) · 2.09 KB
/
dep2p.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package dep2p
import (
"github.com/bpfs/dep2p/config"
"github.com/bpfs/dep2p/core/host"
)
// Config describes a set of settings for a dep2p node.
type Config = config.Config
// Option is a dep2p config option that can be given to the dep2p constructor
// (`dep2p.New`).
type Option = config.Option
// ChainOptions chains multiple options into a single option.
func ChainOptions(opts ...Option) Option {
return func(cfg *Config) error {
for _, opt := range opts {
if opt == nil {
continue
}
if err := opt(cfg); err != nil {
return err
}
}
return nil
}
}
// New constructs a new dep2p 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";
//
// - If no transport options are provided, the node uses TCP, websocket and QUIC
// transport protocols;
//
// - If no multiplexer configuration is provided, the node is configured by
// default to use yamux;
//
// - If no security transport is provided, the host uses the dep2p's noise
// and/or tls encrypted transport to encrypt all traffic;
//
// - If no peer identity is provided, it generates a random Ed25519 key-pair
// and derives a new identity from it;
//
// - If no peerstore is provided, the host is initialized with an empty
// peerstore.
//
// To stop/shutdown the returned dep2p node, the user needs to cancel the passed context and call `Close` on the returned Host.
func New(opts ...Option) (host.Host, error) {
return NewWithoutDefaults(append(opts, FallbackDefaults)...)
}
// NewWithoutDefaults constructs a new dep2p 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.
func NewWithoutDefaults(opts ...Option) (host.Host, error) {
var cfg Config
if err := cfg.Apply(opts...); err != nil {
return nil, err
}
return cfg.NewNode()
}