/
options.go
183 lines (164 loc) · 4.72 KB
/
options.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
package opts
import (
"fmt"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"
dht "github.com/libp2p/go-libp2p-kad-dht"
kbucket "github.com/libp2p/go-libp2p-kbucket"
"github.com/libp2p/hydra-booster/idgen"
"github.com/multiformats/go-multiaddr"
)
// Options are Hydra Head options
type Options struct {
Datastore ds.Batching
Peerstore peerstore.Peerstore
RoutingTable *kbucket.RoutingTable
EnableRelay bool
Addrs []multiaddr.Multiaddr
ProtocolPrefix protocol.ID
BucketSize int
Limiter chan struct{}
BootstrapPeers []multiaddr.Multiaddr
IDGenerator idgen.IdentityGenerator
DisableProvGC bool
DisableProviders bool
DisableValues bool
}
// Option is the Hydra Head option type.
type Option func(*Options) error
// Apply applies the given options to this Option.
func (o *Options) Apply(opts ...Option) error {
for i, opt := range opts {
if err := opt(o); err != nil {
return fmt.Errorf("hydra node option %d failed: %s", i, err)
}
}
return nil
}
// Defaults are the default Hydra Head options. This option will be automatically
// prepended to any options you pass to the Hydra Head constructor.
var Defaults = func(o *Options) error {
o.Datastore = dssync.MutexWrap(ds.NewMapDatastore())
tcpAddr, _ := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/0")
quicAddr, _ := multiaddr.NewMultiaddr("/ip4/0.0.0.0/udp/0/quic")
o.Addrs = []multiaddr.Multiaddr{tcpAddr, quicAddr}
o.ProtocolPrefix = dht.DefaultPrefix
o.BucketSize = 20
o.BootstrapPeers = dht.DefaultBootstrapPeers
o.IDGenerator = idgen.HydraIdentityGenerator
return nil
}
// Datastore configures the Hydra Head to use the specified datastore.
// Defaults to an in-memory (temporary) map.
func Datastore(ds ds.Batching) Option {
return func(o *Options) error {
o.Datastore = ds
return nil
}
}
// Peerstore configures the Hydra Head to use the specified peerstore.
// Defaults to an in-memory (temporary) map.
func Peerstore(ps peerstore.Peerstore) Option {
return func(o *Options) error {
o.Peerstore = ps
return nil
}
}
// RoutingTable configures the Hydra Head to use the specified routing table.
// Defaults to the routing table provided by IpfsDHT.
func RoutingTable(rt *kbucket.RoutingTable) Option {
return func(o *Options) error {
o.RoutingTable = rt
return nil
}
}
// EnableRelay configures whether this node acts as a relay node.
// The default value is false.
func EnableRelay() Option {
return func(o *Options) error {
o.EnableRelay = true
return nil
}
}
// Addrs configures the swarm addresses for this Hydra node.
// The default value is /ip4/0.0.0.0/tcp/0 and /ip4/0.0.0.0/udp/0/quic.
func Addrs(addrs []multiaddr.Multiaddr) Option {
return func(o *Options) error {
o.Addrs = addrs
return nil
}
}
// ProtocolPrefix configures the application specific prefix attached to all DHT protocols by default.
// The default value is "/ipfs".
func ProtocolPrefix(pfx protocol.ID) Option {
return func(o *Options) error {
if pfx != "" {
o.ProtocolPrefix = pfx
}
return nil
}
}
// BucketSize configures the bucket size of the routing table.
// The default value is 20.
func BucketSize(bucketSize int) Option {
return func(o *Options) error {
if bucketSize != 0 {
o.BucketSize = bucketSize
}
return nil
}
}
// Limiter configures ???.
// The default value is nil.
func Limiter(l chan struct{}) Option {
return func(o *Options) error {
o.Limiter = l
return nil
}
}
// BootstrapPeers configures the set of bootstrap peers that should be randomly selected from.
// The default value is `dht.DefaultBootstrapPeers`.
func BootstrapPeers(addrs []multiaddr.Multiaddr) Option {
return func(o *Options) error {
if len(addrs) > 0 {
o.BootstrapPeers = addrs
}
return nil
}
}
// IDGenerator configures the identity generator.
// The default value is `idgen.HydraIdentityGenerator`.
func IDGenerator(g idgen.IdentityGenerator) Option {
return func(o *Options) error {
if g != nil {
o.IDGenerator = g
}
return nil
}
}
// DisableProvGC disables garbage collections of provider records from the shared datastore.
// The default value is false.
func DisableProvGC() Option {
return func(o *Options) error {
o.DisableProvGC = true
return nil
}
}
// DisableProviders disables storing and retrieving provider records.
// The default value is false.
func DisableProviders() Option {
return func(o *Options) error {
o.DisableProviders = true
return nil
}
}
// DisableValues disables storing and retrieving value records (including public keys).
// The default value is false.
func DisableValues() Option {
return func(o *Options) error {
o.DisableValues = true
return nil
}
}