/
setup.go
487 lines (411 loc) · 13 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
package main
import (
"context"
crand "crypto/rand"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"time"
"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/bitswap"
wl "github.com/ipfs/boxo/bitswap/client/wantlist"
bsmspb "github.com/ipfs/boxo/bitswap/message/pb"
bsnet "github.com/ipfs/boxo/bitswap/network"
bsserver "github.com/ipfs/boxo/bitswap/server"
"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/exchange"
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"
delay "github.com/ipfs/go-ipfs-delay"
metri "github.com/ipfs/go-metrics-interface"
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"
type DHTRouting string
const (
DHTAccelerated DHTRouting = "accelerated"
DHTStandard DHTRouting = "standard"
DHTOff DHTRouting = "off"
)
func init() {
// Lets us discover our own public address with a single observation
identify.ActivationThresh = 1
}
type Node struct {
vs routing.ValueStore
host host.Host
dataDir string
datastore datastore.Batching
blockstore blockstore.Blockstore
bs *bitswap.Bitswap
bsrv blockservice.BlockService
resolver resolver.Resolver
ns namesys.NameSystem
denylistSubs []*nopfs.HTTPSubscriber
}
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
DHTRouting DHTRouting
DHTSharedHost bool
IpnsMaxCacheTTL time.Duration
DenylistSubs []string
Peering []peer.AddrInfo
PeeringCache bool
Seed string
SeedIndex int
SeedPeering bool
SeedPeeringMaxIndex int
GCInterval time.Duration
GCThreshold float64
}
func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cachedDNS) (*Node, error) {
var err error
cfg.DataDir, err = filepath.Abs(cfg.DataDir)
if err != nil {
return nil, err
}
ds, err := setupDatastore(cfg)
if err != nil {
return nil, err
}
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
}))
}
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 = blockstore.NewIdStore(blkst)
var (
cr routing.ContentRouting
pr routing.PeerRouting
vs routing.ValueStore
)
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
}
bswap := setupBitswap(ctx, cfg, h, cr, blkst)
err = os.Mkdir(filepath.Join(cfg.DataDir, "denylists"), 0755)
if err != nil && !errors.Is(err, fs.ErrExist) {
return 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, err
}
denylists = append(denylists, s)
}
files, err := nopfs.GetDenylistFilesInDir(filepath.Join(cfg.DataDir, "denylists"))
if err != nil {
return nil, err
}
blocker, err := nopfs.NewBlocker(files)
if err != nil {
return nil, err
}
bsrv := blockservice.New(blkst, &noNotifyExchange{bswap},
// 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(),
)
bsrv = nopfsipfs.WrapBlockService(bsrv, blocker)
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)
fetcherCfg := bsfetcher.NewFetcherConfig(bsrv)
fetcherCfg.PrototypeChooser = dagpb.AddSupportToChooser(bsfetcher.DefaultPrototypeChooser)
fetcher := fetcherCfg.WithReifier(unixfsnode.Reify)
r := resolver.NewBasicResolver(fetcher)
r = nopfsipfs.WrapResolver(r, blocker)
return &Node{
host: h,
blockstore: blkst,
dataDir: cfg.DataDir,
datastore: ds,
bs: bswap,
ns: ns,
vs: vs,
bsrv: bsrv,
resolver: r,
denylistSubs: denylists,
}, 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 "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 setupBitswap(ctx context.Context, cfg Config, h host.Host, cr routing.ContentRouting, bstore blockstore.Blockstore) *bitswap.Bitswap {
var (
peerBlockRequestFilter bsserver.PeerBlockRequestFilter
)
if cfg.PeeringCache && len(cfg.Peering) > 0 {
peers := make(map[peer.ID]struct{}, len(cfg.Peering))
for _, a := range cfg.Peering {
peers[a.ID] = struct{}{}
}
peerBlockRequestFilter = func(p peer.ID, c cid.Cid) bool {
_, ok := peers[p]
return ok
}
} else {
peerBlockRequestFilter = func(p peer.ID, c cid.Cid) bool {
return false
}
}
bsctx := metri.CtxScope(ctx, "ipfs_bitswap")
bn := bsnet.NewFromIpfsHost(h, cr)
bswap := bitswap.New(bsctx, bn, bstore,
// --- Client Options
// default is 1 minute to search for a random live-want (1
// CID). I think we want to search for random live-wants more
// often although probably it overlaps with general
// rebroadcasts.
bitswap.RebroadcastDelay(delay.Fixed(10*time.Second)),
// ProviderSearchDelay: default is 1 second.
bitswap.ProviderSearchDelay(time.Second),
bitswap.WithoutDuplicatedBlockStats(),
// ---- Server Options
bitswap.WithPeerBlockRequestFilter(peerBlockRequestFilter),
bitswap.ProvideEnabled(false),
// Do not keep track of other peer's wantlists, we only want to reply if we
// have a block. If we get it later, it's no longer relevant.
bitswap.WithPeerLedger(&noopPeerLedger{}),
// When we don't have a block, don't reply. This reduces processment.
bitswap.SetSendDontHaves(false),
)
bn.Start(bswap)
return bswap
}
type noopPeerLedger struct{}
func (*noopPeerLedger) Wants(p peer.ID, e wl.Entry) {}
func (*noopPeerLedger) CancelWant(p peer.ID, k cid.Cid) bool {
return false
}
func (*noopPeerLedger) CancelWantWithType(p peer.ID, k cid.Cid, typ bsmspb.Message_Wantlist_WantType) {
}
func (*noopPeerLedger) Peers(k cid.Cid) []bsserver.PeerEntry {
return nil
}
func (*noopPeerLedger) CollectPeerIDs() []peer.ID {
return nil
}
func (*noopPeerLedger) WantlistSizeForPeer(p peer.ID) int {
return 0
}
func (*noopPeerLedger) WantlistForPeer(p peer.ID) []wl.Entry {
return nil
}
func (*noopPeerLedger) ClearPeerWantlist(p peer.ID) {}
func (*noopPeerLedger) PeerDisconnected(p peer.ID) {}
type noNotifyExchange struct {
exchange.Interface
}
func (e *noNotifyExchange) NotifyNewBlocks(ctx context.Context, blocks ...blocks.Block) error {
// Rainbow does not notify when we get new blocks in our Blockservice.
return nil
}