-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup.go
602 lines (502 loc) · 16.3 KB
/
setup.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package main
import (
"context"
crand "crypto/rand"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"
"github.com/cockroachdb/pebble"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/badger/v4/options"
nopfs "github.com/ipfs-shipyard/nopfs"
nopfsipfs "github.com/ipfs-shipyard/nopfs/ipfs"
"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/exchange/offline"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
"github.com/ipfs/boxo/gateway"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path/resolver"
"github.com/ipfs/boxo/peering"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
badger4 "github.com/ipfs/go-ds-badger4"
flatfs "github.com/ipfs/go-ds-flatfs"
pebbleds "github.com/ipfs/go-ds-pebble"
mprome "github.com/ipfs/go-metrics-prometheus"
"github.com/ipfs/go-unixfsnode"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/multiformats/go-multiaddr"
)
func init() {
if err := mprome.Inject(); err != nil {
panic(err)
}
}
const cidContactEndpoint = "https://cid.contact"
var httpRoutersFilterProtocols = []string{"unknown", "transport-bitswap"} // IPIP-484
type DHTRouting string
const (
DHTAccelerated DHTRouting = "accelerated"
DHTStandard DHTRouting = "standard"
DHTOff DHTRouting = "off"
)
type RemoteBackendMode string
const (
RemoteBackendBlock RemoteBackendMode = "block"
RemoteBackendCAR RemoteBackendMode = "car"
)
func init() {
// Lets us discover our own public address with a single observation
identify.ActivationThresh = 1
}
type Node struct {
ns namesys.NameSystem
vs routing.ValueStore
dataDir string
bsrv blockservice.BlockService
denylistSubs []*nopfs.HTTPSubscriber
// Maybe not be set depending on the configuration:
host host.Host
datastore datastore.Batching
blockstore blockstore.Blockstore
resolver resolver.Resolver
}
type Config struct {
DataDir string
BlockstoreType string
ListenAddrs []string
AnnounceAddrs []string
ConnMgrLow int
ConnMgrHi int
ConnMgrGrace time.Duration
InMemBlockCache int64
MaxMemory uint64
MaxFD int
GatewayDomains []string
SubdomainGatewayDomains []string
TrustlessGatewayDomains []string
RoutingV1Endpoints []string
RoutingV1FilterProtocols []string
DHTRouting DHTRouting
DHTSharedHost bool
IpnsMaxCacheTTL time.Duration
Bitswap bool
// BitswapWantHaveReplaceSize tells the bitswap server to replace WantHave
// with WantBlock responses when the block size less then or equal to this
// value. Set to zero to disable replacement and avoid block size lookup
// when processing HaveWant requests.
BitswapWantHaveReplaceSize int
DenylistSubs []string
Peering []peer.AddrInfo
PeeringSharedCache bool
Seed string
SeedIndex int
SeedPeering bool
SeedPeeringMaxIndex int
RemoteBackends []string
RemoteBackendsIPNS bool
RemoteBackendMode RemoteBackendMode
GCInterval time.Duration
GCThreshold float64
TracingAuthToken string
disableMetrics bool // only meant to be used during testing
// Pebble config values
BytesPerSync int
DisableWAL bool
L0CompactionThreshold int
L0StopWritesThreshold int
LBaseMaxBytes int64
MemTableSize uint64
MemTableStopWritesThreshold int
WALBytesPerSync int
MaxConcurrentCompactions int
WALMinSyncInterval time.Duration
}
func SetupNoLibp2p(ctx context.Context, cfg Config, dnsCache *cachedDNS) (*Node, error) {
var err error
cfg.DataDir, err = filepath.Abs(cfg.DataDir)
if err != nil {
return nil, err
}
denylists, blocker, err := setupDenylists(cfg)
if err != nil {
return nil, err
}
// The stars aligned and Libp2p does not need to be turned on at all.
if len(cfg.RemoteBackends) == 0 {
return nil, errors.New("RAINBOW_REMOTE_BACKENDS must be set if RAINBOW_LIBP2P is disabled")
}
// Setup a Value Store composed of both the remote backends and the delegated
// routers, if they exist. This vs is only used for resolving IPNS Records.
vs, err := setupRoutingNoLibp2p(cfg, dnsCache)
if err != nil {
return nil, err
}
// Setup the remote blockstore if that's the mode we're using.
var bsrv blockservice.BlockService
if cfg.RemoteBackendMode == RemoteBackendBlock {
blkst, err := gateway.NewRemoteBlockstore(cfg.RemoteBackends, nil)
if err != nil {
return nil, err
}
bsrv = blockservice.New(blkst, offline.Exchange(blkst))
bsrv = nopfsipfs.WrapBlockService(bsrv, blocker)
}
ns, err := setupNamesys(cfg, vs, blocker)
if err != nil {
return nil, err
}
return &Node{
vs: vs,
ns: ns,
dataDir: cfg.DataDir,
denylistSubs: denylists,
bsrv: bsrv,
}, nil
}
func SetupWithLibp2p(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cachedDNS) (*Node, error) {
if !cfg.Bitswap && cfg.DHTRouting == DHTOff {
return nil, errors.New("libp2p is enabled, but not used: bitswap and dht are disabled")
}
var err error
cfg.DataDir, err = filepath.Abs(cfg.DataDir)
if err != nil {
return nil, err
}
denylists, blocker, err := setupDenylists(cfg)
if err != nil {
return nil, err
}
n := &Node{
dataDir: cfg.DataDir,
denylistSubs: denylists,
}
bwc := metrics.NewBandwidthCounter()
cmgr, err := connmgr.NewConnManager(cfg.ConnMgrLow, cfg.ConnMgrHi, connmgr.WithGracePeriod(cfg.ConnMgrGrace))
if err != nil {
return nil, err
}
bitswapRcMgr, dhtRcMgr, err := makeResourceMgrs(cfg.MaxMemory, cfg.MaxFD, cfg.ConnMgrHi, !cfg.DHTSharedHost)
if err != nil {
return nil, err
}
opts := []libp2p.Option{
libp2p.NATPortMap(),
libp2p.ConnectionManager(cmgr),
libp2p.Identity(key),
libp2p.UserAgent("rainbow/" + buildVersion()),
libp2p.BandwidthReporter(bwc),
libp2p.DefaultTransports,
libp2p.DefaultMuxers,
libp2p.ResourceManager(bitswapRcMgr),
libp2p.EnableHolePunching(),
}
if len(cfg.ListenAddrs) == 0 {
// Note: because the transports are set above we must also set the listen addresses
// We need to set listen addresses in order for hole punching to work
opts = append(opts, libp2p.DefaultListenAddrs)
} else {
opts = append(opts, libp2p.ListenAddrStrings(cfg.ListenAddrs...))
}
if len(cfg.AnnounceAddrs) > 0 {
var addrs []multiaddr.Multiaddr
for _, anna := range cfg.AnnounceAddrs {
a, err := multiaddr.NewMultiaddr(anna)
if err != nil {
return nil, fmt.Errorf("failed to parse announce addr: %w", err)
}
addrs = append(addrs, a)
}
opts = append(opts, libp2p.AddrsFactory(func([]multiaddr.Multiaddr) []multiaddr.Multiaddr {
return addrs
}))
}
ds, err := setupDatastore(cfg)
if err != nil {
return nil, err
}
var (
vs routing.ValueStore
cr routing.ContentRouting
pr routing.PeerRouting
)
opts = append(opts, libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
cr, pr, vs, err = setupRouting(ctx, cfg, h, ds, dhtRcMgr, bwc, dnsCache)
return pr, err
}))
h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
err = setupPeering(cfg, h)
if err != nil {
return nil, err
}
var bsrv blockservice.BlockService
if cfg.Bitswap {
blkst := blockstore.NewBlockstore(ds,
blockstore.NoPrefix(),
// Every Has() for every written block is a transaction with a
// seek onto LSM. If not in memory it will be a pain.
// We opt to write every block Put into the blockstore.
// See also comment in blockservice.
blockstore.WriteThrough(),
)
blkst = &switchingBlockstore{
baseBlockstore: blkst,
contextSwitchingKey: NoBlockcache{},
}
blkst = blockstore.NewIdStore(blkst)
n.blockstore = blkst
bsrv = blockservice.New(blkst, setupBitswapExchange(ctx, cfg, h, cr, blkst),
// if we are doing things right, our bitswap wantlists should
// not have blocks that we already have (see
// https://github.com/ipfs/boxo/blob/e0d4b3e9b91e9904066a10278e366c9a6d9645c7/blockservice/blockservice.go#L272). Thus
// we should not be writing many blocks that we already
// have. Thus, no point in checking whether we have a block
// before writing new blocks.
blockservice.WriteThrough(),
)
} else {
if len(cfg.RemoteBackends) == 0 || cfg.RemoteBackendMode != RemoteBackendBlock {
return nil, errors.New("remote backends in block mode must be set when disabling bitswap")
}
if cfg.PeeringSharedCache {
return nil, errors.New("disabling bitswap is incompatible with peering cache")
}
blkst, err := gateway.NewRemoteBlockstore(cfg.RemoteBackends, nil)
if err != nil {
return nil, err
}
bsrv = blockservice.New(blkst, offline.Exchange(blkst))
}
bsrv = nopfsipfs.WrapBlockService(bsrv, blocker)
fetcherCfg := bsfetcher.NewFetcherConfig(bsrv)
fetcherCfg.PrototypeChooser = dagpb.AddSupportToChooser(bsfetcher.DefaultPrototypeChooser)
fetcher := fetcherCfg.WithReifier(unixfsnode.Reify)
r := resolver.NewBasicResolver(fetcher)
r = nopfsipfs.WrapResolver(r, blocker)
n.host = h
n.datastore = ds
n.bsrv = bsrv
n.resolver = r
ns, err := setupNamesys(cfg, vs, blocker)
if err != nil {
return nil, err
}
n.vs = vs
n.ns = ns
return n, nil
}
func setupDatastore(cfg Config) (datastore.Batching, error) {
switch cfg.BlockstoreType {
case "flatfs":
return flatfs.CreateOrOpen(filepath.Join(cfg.DataDir, "flatfs"), flatfs.NextToLast(3), false)
case "pebble":
return pebbleds.NewDatastore(filepath.Join(cfg.DataDir, "pebbleds"),
pebbleds.WithCacheSize(cfg.InMemBlockCache),
pebbleds.WithPebbleOpts(getPebbleOpts(cfg)),
)
case "badger":
badgerOpts := badger.DefaultOptions("")
badgerOpts.CompactL0OnClose = false
// ValueThreshold: defaults to 1MB! For us that means everything goes
// into the LSM tree and that means more stuff in memory. We only
// put very small things on the LSM tree by default (i.e. a single
// CID).
badgerOpts.ValueThreshold = 256
// BlockCacheSize: instead of using blockstore, we cache things
// here. This only makes sense if using compression, according to
// docs.
badgerOpts.BlockCacheSize = cfg.InMemBlockCache // default 1 GiB.
// Compression: default. Trades reading less from disk for using more
// CPU. Given gateways are usually IO bound, I think we can make this
// trade.
if badgerOpts.BlockCacheSize == 0 {
badgerOpts.Compression = options.None
} else {
badgerOpts.Compression = options.Snappy
}
// If we write something twice, we do it with the same values so
// *shrugh*.
badgerOpts.DetectConflicts = false
// MemTableSize: Defaults to 64MiB which seems an ok amount to flush
// to disk from time to time.
badgerOpts.MemTableSize = 64 << 20
// NumMemtables: more means more memory, faster writes, but more to
// commit to disk if they get full. Default is 5.
badgerOpts.NumMemtables = 5
// IndexCacheSize: 0 means all in memory (default). All means indexes,
// bloom filters etc. Usually not huge amount of memory usage from
// this.
badgerOpts.IndexCacheSize = 0
opts := badger4.Options{
GcDiscardRatio: 0.3,
GcInterval: 20 * time.Minute,
GcSleep: 10 * time.Second,
Options: badgerOpts,
}
return badger4.NewDatastore(filepath.Join(cfg.DataDir, "badger4"), &opts)
default:
return nil, fmt.Errorf("unsupported blockstore type: %s", cfg.BlockstoreType)
}
}
func loadOrInitPeerKey(kf string) (crypto.PrivKey, error) {
data, err := os.ReadFile(kf)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
k, _, err := crypto.GenerateEd25519Key(crand.Reader)
if err != nil {
return nil, err
}
data, err := crypto.MarshalPrivateKey(k)
if err != nil {
return nil, err
}
if err := os.WriteFile(kf, data, 0600); err != nil {
return nil, err
}
return k, nil
}
return crypto.UnmarshalPrivateKey(data)
}
func setupPeering(cfg Config, h host.Host) error {
if len(cfg.Peering) == 0 && !cfg.SeedPeering {
return nil
}
ps := peering.NewPeeringService(h)
if err := ps.Start(); err != nil {
return err
}
for _, a := range cfg.Peering {
ps.AddPeer(a)
}
if !cfg.SeedPeering {
return nil
}
if cfg.SeedIndex < 0 {
return fmt.Errorf("seed index must be equal or greater than 0, it is %d", cfg.SeedIndex)
}
if cfg.SeedPeeringMaxIndex < 0 {
return fmt.Errorf("seed peering max index must be a positive number, it is %d", cfg.SeedPeeringMaxIndex)
}
pids, err := derivePeerIDs(cfg.Seed, cfg.SeedIndex, cfg.SeedPeeringMaxIndex)
if err != nil {
return err
}
for _, pid := range pids {
// The peering module will automatically perform lookups to find the
// addresses of the given peers.
ps.AddPeer(peer.AddrInfo{ID: pid})
}
return nil
}
func setupDenylists(cfg Config) ([]*nopfs.HTTPSubscriber, *nopfs.Blocker, error) {
err := os.Mkdir(filepath.Join(cfg.DataDir, "denylists"), 0755)
if err != nil && !errors.Is(err, fs.ErrExist) {
return nil, nil, err
}
var denylists []*nopfs.HTTPSubscriber
for _, dl := range cfg.DenylistSubs {
s, err := nopfs.NewHTTPSubscriber(dl, filepath.Join(cfg.DataDir, "denylists", filepath.Base(dl)), time.Minute)
if err != nil {
return nil, nil, err
}
denylists = append(denylists, s)
}
files, err := nopfs.GetDenylistFilesInDir(filepath.Join(cfg.DataDir, "denylists"))
if err != nil {
return nil, nil, err
}
blocker, err := nopfs.NewBlocker(files)
if err != nil {
return nil, nil, err
}
return denylists, blocker, nil
}
func setupNamesys(cfg Config, vs routing.ValueStore, blocker *nopfs.Blocker) (namesys.NameSystem, error) {
dns, err := gateway.NewDNSResolver(nil)
if err != nil {
return nil, err
}
nsOptions := []namesys.Option{namesys.WithDNSResolver(dns)}
if cfg.IpnsMaxCacheTTL > 0 {
nsOptions = append(nsOptions, namesys.WithMaxCacheTTL(cfg.IpnsMaxCacheTTL))
}
ns, err := namesys.NewNameSystem(vs, nsOptions...)
if err != nil {
return nil, err
}
ns = nopfsipfs.WrapNameSystem(ns, blocker)
return ns, nil
}
type switchingBlockstore struct {
baseBlockstore blockstore.Blockstore
contextSwitchingKey any
}
func (s *switchingBlockstore) getBlockstore(ctx context.Context) blockstore.Blockstore {
alternativeBlockstore, ok := ctx.Value(s.contextSwitchingKey).(blockstore.Blockstore)
if ok {
return alternativeBlockstore
}
return s.baseBlockstore
}
func (s *switchingBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
return s.getBlockstore(ctx).DeleteBlock(ctx, c)
}
func (s *switchingBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
return s.getBlockstore(ctx).Has(ctx, c)
}
func (s *switchingBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
return s.getBlockstore(ctx).Get(ctx, c)
}
func (s *switchingBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
return s.getBlockstore(ctx).GetSize(ctx, c)
}
func (s *switchingBlockstore) Put(ctx context.Context, block blocks.Block) error {
return s.getBlockstore(ctx).Put(ctx, block)
}
func (s *switchingBlockstore) PutMany(ctx context.Context, blocks []blocks.Block) error {
return s.getBlockstore(ctx).PutMany(ctx, blocks)
}
func (s *switchingBlockstore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return s.getBlockstore(ctx).AllKeysChan(ctx)
}
func (s *switchingBlockstore) HashOnRead(enabled bool) {
s.baseBlockstore.HashOnRead(enabled)
}
var _ blockstore.Blockstore = (*switchingBlockstore)(nil)
func getPebbleOpts(cfg Config) *pebble.Options {
opts := &pebble.Options{
BytesPerSync: cfg.BytesPerSync,
DisableWAL: cfg.DisableWAL,
L0CompactionThreshold: cfg.L0CompactionThreshold,
L0StopWritesThreshold: cfg.L0StopWritesThreshold,
LBaseMaxBytes: cfg.LBaseMaxBytes,
MemTableSize: cfg.MemTableSize,
MemTableStopWritesThreshold: cfg.MemTableStopWritesThreshold,
WALBytesPerSync: cfg.WALBytesPerSync,
}
if cfg.MaxConcurrentCompactions != 0 {
opts.MaxConcurrentCompactions = func() int { return cfg.MaxConcurrentCompactions }
}
if cfg.WALMinSyncInterval != 0 {
opts.WALMinSyncInterval = func() time.Duration { return cfg.WALMinSyncInterval }
}
return opts
}