-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
172 lines (153 loc) · 4.84 KB
/
Copy pathconfig.go
File metadata and controls
172 lines (153 loc) · 4.84 KB
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
package ipfs
import (
"context"
"fmt"
"time"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-verifcid"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
type Config struct {
OfflineMode bool
SecretKey crypto.PrivKey
ListenAddrs []multiaddr.Multiaddr
Bootstrappers []peer.AddrInfo
MinConnections int
MaxConnections int
ConnectionGracePeriod time.Duration
Datastore datastore.Batching
Blockstore blockstore.Blockstore
ReprovideInterval time.Duration
CidBuilder cid.Builder
ConnectionLogging bool
}
func fillConfigDefaults(ctx context.Context, c Config) (Config, error) {
if c.SecretKey == nil {
sk, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
return Config{}, err
}
c.SecretKey = sk
}
if len(c.ListenAddrs) == 0 {
for _, addr := range []string{
"/ip4/0.0.0.0/tcp/0",
"/ip4/0.0.0.0/udp/0/quic",
"/ip6/::/tcp/0",
"/ip6/::/udp/0/quic",
} {
maddr, err := multiaddr.NewMultiaddr(addr)
if err != nil {
return Config{}, err
}
c.ListenAddrs = append(c.ListenAddrs, maddr)
}
}
if len(c.Bootstrappers) == 0 {
bootstrappers, err := defaultBootstrapPeers()
if err != nil {
return Config{}, err
}
c.Bootstrappers = bootstrappers
}
if c.MinConnections == 0 {
c.MinConnections = 100
}
if c.MaxConnections == 0 {
c.MaxConnections = 600
}
if c.ConnectionGracePeriod == 0 {
c.ConnectionGracePeriod = time.Minute
}
if c.Datastore == nil {
c.Datastore = dssync.MutexWrap(datastore.NewMapDatastore())
}
if c.Blockstore == nil {
bs, err := blockstore.CachedBlockstore(
ctx,
&VerifBS{
Blockstore: blockstore.NewBlockstore(c.Datastore),
},
blockstore.DefaultCacheOpts(),
)
if err != nil {
return Config{}, err
}
c.Blockstore = blockstore.NewIdStore(bs)
}
if c.ReprovideInterval == 0 {
c.ReprovideInterval = 12 * time.Hour
}
if c.CidBuilder == nil {
c.CidBuilder = merkledag.V1CidPrefix()
}
return c, nil
}
// The following code is copied from "github.com/ipfs/kubo/config" to avoid
// dependency on kubo package which introduces conflicts on libp2p-core.
// DefaultBootstrapAddresses are the hardcoded bootstrap addresses
// for IPFS. they are nodes run by the IPFS team. docs on these later.
// As with all p2p networks, bootstrap is an important security concern.
var defaultBootstrapAddresses = []string{
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
"/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
}
// defaultBootstrapPeers returns the (parsed) set of default bootstrap peers.
// if it fails, it returns a meaningful error for the user.
// This is here (and not inside cmd/ipfs/init) because of module dependency problems.
func defaultBootstrapPeers() ([]peer.AddrInfo, error) {
ps, err := parseBootstrapPeers(defaultBootstrapAddresses)
if err != nil {
return nil, fmt.Errorf(`failed to parse hardcoded bootstrap peers: %s
This is a problem with the ipfs codebase. Please report it to the dev team`, err)
}
return ps, nil
}
// ParseBootstrapPeer parses a bootstrap list into a list of AddrInfos.
func parseBootstrapPeers(addrs []string) ([]peer.AddrInfo, error) {
maddrs := make([]multiaddr.Multiaddr, len(addrs))
for i, addr := range addrs {
var err error
maddrs[i], err = multiaddr.NewMultiaddr(addr)
if err != nil {
return nil, err
}
}
return peer.AddrInfosFromP2pAddrs(maddrs...)
}
// Copied from github.com/ipfs/kubo/thirdparty/verifbs to avoid dependency on
// Kubo, which introduces version conflicts for libp2p-core
type VerifBS struct {
blockstore.Blockstore
}
func (bs *VerifBS) Put(ctx context.Context, b blocks.Block) error {
if err := verifcid.ValidateCid(b.Cid()); err != nil {
return err
}
return bs.Blockstore.Put(ctx, b)
}
func (bs *VerifBS) PutMany(ctx context.Context, blks []blocks.Block) error {
for _, b := range blks {
if err := verifcid.ValidateCid(b.Cid()); err != nil {
return err
}
}
return bs.Blockstore.PutMany(ctx, blks)
}
func (bs *VerifBS) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
if err := verifcid.ValidateCid(c); err != nil {
return nil, err
}
return bs.Blockstore.Get(ctx, c)
}