forked from libp2p/go-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 3
/
defaults.go
220 lines (199 loc) · 6.47 KB
/
defaults.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package libp2p
// This file contains all the default configuration options.
import (
"crypto/rand"
"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"
"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"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
"github.com/prometheus/client_golang/prometheus"
"github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
)
// DefaultSecurity is the default security option.
//
// Useful when you want to extend, but not replace, the supported transport
// security protocols.
var DefaultSecurity = ChainOptions(
Security(noise.ID, noise.New),
Security(tls.ID, tls.New),
)
// DefaultMuxers configures libp2p to use the stream connection multiplexers.
//
// Use this option when you want to *extend* the set of multiplexers used by
// libp2p instead of replacing them.
var DefaultMuxers = Muxer(yamux.ID, yamux.DefaultTransport)
// DefaultTransports are the default libp2p transports.
//
// Use this option when you want to *extend* the set of transports used by
// libp2p instead of replacing them.
var DefaultTransports = ChainOptions(
Transport(tcp.NewTCPTransport),
Transport(quic.NewTransport),
Transport(ws.New),
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),
)
// DefaultPeerstore configures libp2p to use the default peerstore.
var DefaultPeerstore Option = func(cfg *Config) error {
ps, err := pstoremem.NewPeerstore()
if err != nil {
return err
}
return cfg.Apply(Peerstore(ps))
}
// RandomIdentity generates a random identity. (default behaviour)
var RandomIdentity = func(cfg *Config) error {
priv, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil {
return err
}
return cfg.Apply(Identity(priv))
}
// DefaultListenAddrs configures libp2p to use default listen address.
var DefaultListenAddrs = func(cfg *Config) error {
addrs := []string{
"/ip4/0.0.0.0/tcp/0",
"/ip4/0.0.0.0/udp/0/quic",
"/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",
"/ip6/::/udp/0/quic-v1",
"/ip6/::/udp/0/quic-v1/webtransport",
}
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)
}
return cfg.Apply(ListenAddrs(listenAddrs...))
}
// DefaultEnableRelay enables relay dialing and listening by default.
var DefaultEnableRelay = func(cfg *Config) error {
return cfg.Apply(EnableRelay())
}
var DefaultResourceManager = func(cfg *Config) error {
// Default memory limit: 1/8th of total memory, minimum 128MB, maximum 1GB
limits := rcmgr.DefaultLimits
SetDefaultServiceLimits(&limits)
mgr, err := rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(limits.AutoScale()))
if err != nil {
return err
}
return cfg.Apply(ResourceManager(mgr))
}
// DefaultConnectionManager creates a default connection manager
var DefaultConnectionManager = func(cfg *Config) error {
mgr, err := connmgr.NewConnManager(160, 192)
if err != nil {
return err
}
return cfg.Apply(ConnectionManager(mgr))
}
// DefaultMultiaddrResolver creates a default connection manager
var DefaultMultiaddrResolver = func(cfg *Config) error {
return cfg.Apply(MultiaddrResolver(madns.DefaultResolver))
}
// DefaultPrometheusRegisterer configures libp2p to use the default registerer
var DefaultPrometheusRegisterer = func(cfg *Config) error {
return cfg.Apply(PrometheusRegisterer(prometheus.DefaultRegisterer))
}
// 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
}{
{
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
opt: DefaultListenAddrs,
},
{
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.PSK == nil },
opt: DefaultTransports,
},
{
fallback: func(cfg *Config) bool { return cfg.Transports == nil && cfg.PSK != nil },
opt: DefaultPrivateTransports,
},
{
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,
},
{
fallback: func(cfg *Config) bool { return !cfg.RelayCustom },
opt: DefaultEnableRelay,
},
{
fallback: func(cfg *Config) bool { return cfg.ResourceManager == nil },
opt: DefaultResourceManager,
},
{
fallback: func(cfg *Config) bool { return cfg.ConnManager == nil },
opt: DefaultConnectionManager,
},
{
fallback: func(cfg *Config) bool { return cfg.MultiaddrResolver == nil },
opt: DefaultMultiaddrResolver,
},
{
fallback: func(cfg *Config) bool { return !cfg.DisableMetrics && cfg.PrometheusRegisterer == nil },
opt: DefaultPrometheusRegisterer,
},
}
// 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
// other relevant options have been applied. will be appended to the options
// 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
}