-
Notifications
You must be signed in to change notification settings - Fork 33
/
config.go
409 lines (340 loc) · 10.8 KB
/
config.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
package types
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"strings"
"text/template"
"time"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/spf13/viper"
randutil "github.com/sentinel-official/dvpn-node/utils/rand"
)
const (
MinPeers = 1
MaxPeers = 250
MinMonikerLength = 4
MaxMonikerLength = 32
MinIntervalSetSessions = 10 * time.Second
MaxIntervalSetSessions = 10 * time.Minute
MinIntervalUpdateSessions = (2 * time.Hour) / 2
MaxIntervalUpdateSessions = (2 * time.Hour) - (5 * time.Minute)
MinIntervalUpdateStatus = (1 * time.Hour) / 2
MaxIntervalUpdateStatus = (1 * time.Hour) - (5 * time.Minute)
)
var (
ct = strings.TrimSpace(`
[chain]
# Gas adjustment factor
gas_adjustment = {{ .Chain.GasAdjustment }}
# Gas limit to set per transaction
gas = {{ .Chain.Gas }}
# Gas prices to determine the transaction fee
gas_prices = "{{ .Chain.GasPrices }}"
# The network chain ID
id = "{{ .Chain.ID }}"
# Tendermint RPC interface for the chain
rpc_address = "{{ .Chain.RPCAddress }}"
# Calculate the transaction fee by simulating it
simulate_and_execute = {{ .Chain.SimulateAndExecute }}
[handshake]
# Enable Handshake DNS resolver
enable = {{ .Handshake.Enable }}
# Number of peers
peers = {{ .Handshake.Peers }}
[keyring]
# Underlying storage mechanism for keys
backend = "{{ .Keyring.Backend }}"
# Name of the key with which to sign
from = "{{ .Keyring.From }}"
[node]
# Time interval between each set_sessions operation
interval_set_sessions = "{{ .Node.IntervalSetSessions }}"
# Time interval between each update_sessions transaction
interval_update_sessions = "{{ .Node.IntervalUpdateSessions }}"
# Time interval between each set_status transaction
interval_update_status = "{{ .Node.IntervalUpdateStatus }}"
# API listen-address
listen_on = "{{ .Node.ListenOn }}"
# Name of the node
moniker = "{{ .Node.Moniker }}"
# Per Gigabyte price to charge against the provided bandwidth
price = "{{ .Node.Price }}"
# Address of the provider the node wants to operate under
provider = "{{ .Node.Provider }}"
# Public URL of the node
remote_url = "{{ .Node.RemoteURL }}"
[qos]
# Limit max number of concurrent peers
max_peers = {{ .QOS.MaxPeers }}
`)
t = func() *template.Template {
t, err := template.New("").Parse(ct)
if err != nil {
panic(err)
}
return t
}()
)
type ChainConfig struct {
GasAdjustment float64 `json:"gas_adjustment" mapstructure:"gas_adjustment"`
GasPrices string `json:"gas_prices" mapstructure:"gas_prices"`
Gas uint64 `json:"gas" mapstructure:"gas"`
ID string `json:"id" mapstructure:"id"`
RPCAddress string `json:"rpc_address" mapstructure:"rpc_address"`
SimulateAndExecute bool `json:"simulate_and_execute" mapstructure:"simulate_and_execute"`
}
func NewChainConfig() *ChainConfig {
return &ChainConfig{}
}
func (c *ChainConfig) Validate() error {
if c.GasAdjustment <= 0 {
return errors.New("gas_adjustment must be positive")
}
if _, err := sdk.ParseCoinsNormalized(c.GasPrices); err != nil {
return errors.Wrap(err, "invalid gas_prices")
}
if c.Gas <= 0 {
return errors.New("gas must be positive")
}
if c.ID == "" {
return errors.New("id cannot be empty")
}
if c.RPCAddress == "" {
return errors.New("rpc_address cannot be empty")
}
rpcAddress, err := url.ParseRequestURI(c.RPCAddress)
if err != nil {
return errors.Wrap(err, "invalid rpc_address")
}
if rpcAddress.Scheme != "http" && rpcAddress.Scheme != "https" {
return errors.New("rpc_address scheme must be either http or https")
}
if rpcAddress.Port() == "" {
return errors.New("rpc_address port cannot be empty")
}
return nil
}
func (c *ChainConfig) WithDefaultValues() *ChainConfig {
c.GasAdjustment = 1.05
c.GasPrices = "0.1udvpn"
c.Gas = 200000
c.ID = "sentinelhub-2"
c.RPCAddress = "https://rpc.sentinel.co:443"
c.SimulateAndExecute = true
return c
}
type HandshakeConfig struct {
Enable bool `json:"enable" mapstructure:"enable"`
Peers uint64 `json:"peers" mapstructure:"peers"`
}
func NewHandshakeConfig() *HandshakeConfig {
return &HandshakeConfig{}
}
func (c *HandshakeConfig) Validate() error {
if c.Enable {
if c.Peers <= 0 {
return errors.New("peers must be positive")
}
}
return nil
}
func (c *HandshakeConfig) WithDefaultValues() *HandshakeConfig {
c.Enable = true
c.Peers = 8
return c
}
type KeyringConfig struct {
Backend string `json:"backend" mapstructure:"backend"`
From string `json:"from" mapstructure:"from"`
}
func NewKeyringConfig() *KeyringConfig {
return &KeyringConfig{}
}
func (c *KeyringConfig) Validate() error {
if c.Backend == "" {
return errors.New("backend cannot be empty")
}
if c.Backend != keyring.BackendFile && c.Backend != keyring.BackendTest {
return fmt.Errorf("backend must be either %s or %s", keyring.BackendFile, keyring.BackendTest)
}
if c.From == "" {
return errors.New("from cannot be empty")
}
return nil
}
func (c *KeyringConfig) WithDefaultValues() *KeyringConfig {
c.Backend = keyring.BackendFile
return c
}
type NodeConfig struct {
IntervalSetSessions time.Duration `json:"interval_set_sessions" mapstructure:"interval_set_sessions"`
IntervalUpdateSessions time.Duration `json:"interval_update_sessions" mapstructure:"interval_update_sessions"`
IntervalUpdateStatus time.Duration `json:"interval_update_status" mapstructure:"interval_update_status"`
ListenOn string `json:"listen_on" mapstructure:"listen_on"`
Moniker string `json:"moniker" mapstructure:"moniker"`
Price string `json:"price" mapstructure:"price"`
Provider string `json:"provider" mapstructure:"provider"`
RemoteURL string `json:"remote_url" mapstructure:"remote_url"`
}
func NewNodeConfig() *NodeConfig {
return &NodeConfig{}
}
func (c *NodeConfig) Validate() error {
if c.IntervalSetSessions < MinIntervalSetSessions {
return fmt.Errorf("interval_set_sessions cannot be less than %s", MinIntervalSetSessions)
}
if c.IntervalSetSessions > MaxIntervalSetSessions {
return fmt.Errorf("interval_set_sessions cannot be greater than %s", MaxIntervalSetSessions)
}
if c.IntervalUpdateSessions < MinIntervalUpdateSessions {
return fmt.Errorf("interval_update_sessions cannot be less than %s", MinIntervalUpdateSessions)
}
if c.IntervalUpdateSessions > MaxIntervalUpdateSessions {
return fmt.Errorf("interval_update_sessions cannot be greater than %s", MaxIntervalUpdateSessions)
}
if c.IntervalUpdateStatus < MinIntervalUpdateStatus {
return fmt.Errorf("interval_set_sessions cannot be less than %s", MinIntervalUpdateStatus)
}
if c.IntervalUpdateStatus > MaxIntervalUpdateStatus {
return fmt.Errorf("interval_set_sessions cannot be greater than %s", MaxIntervalUpdateStatus)
}
if c.ListenOn == "" {
return errors.New("listen_on cannot be empty")
}
if c.Moniker == "" {
return errors.New("moniker cannot be empty")
}
if len(c.Moniker) < MinMonikerLength {
return fmt.Errorf("moniker length cannot be less than %d", MinMonikerLength)
}
if len(c.Moniker) > MaxMonikerLength {
return fmt.Errorf("moniker length cannot be greater than %d", MaxMonikerLength)
}
if c.Price == "" && c.Provider == "" {
return errors.New("both price and provider cannot be empty")
}
if c.Price != "" && c.Provider != "" {
return errors.New("either price or provider must be empty")
}
if c.Price != "" {
if _, err := sdk.ParseCoinsNormalized(c.Price); err != nil {
return errors.Wrap(err, "invalid price")
}
}
if c.Provider != "" {
if _, err := hubtypes.ProvAddressFromBech32(c.Provider); err != nil {
return errors.Wrap(err, "invalid provider")
}
}
if c.RemoteURL == "" {
return errors.New("remote_url cannot be empty")
}
remoteURL, err := url.ParseRequestURI(c.RemoteURL)
if err != nil {
return errors.Wrap(err, "invalid remote_url")
}
if remoteURL.Scheme != "https" {
return errors.New("remote_url scheme must be https")
}
if remoteURL.Port() == "" {
return errors.New("remote_url port cannot be empty")
}
return nil
}
func (c *NodeConfig) WithDefaultValues() *NodeConfig {
c.IntervalSetSessions = 2 * time.Minute
c.IntervalUpdateSessions = MaxIntervalUpdateSessions
c.IntervalUpdateStatus = MaxIntervalUpdateStatus
c.ListenOn = fmt.Sprintf("0.0.0.0:%d", randutil.RandomPort())
return c
}
type QOSConfig struct {
MaxPeers int `json:"max_peers" mapstructure:"max_peers"`
}
func NewQOSConfig() *QOSConfig {
return &QOSConfig{}
}
func (c *QOSConfig) Validate() error {
if c.MaxPeers < MinPeers {
return fmt.Errorf("max_peers cannot be less than %d", MinPeers)
}
if c.MaxPeers > MaxPeers {
return fmt.Errorf("max_peers cannot be greater than %d", MaxPeers)
}
return nil
}
func (c *QOSConfig) WithDefaultValues() *QOSConfig {
c.MaxPeers = MaxPeers
return c
}
type Config struct {
Chain *ChainConfig `json:"chain" mapstructure:"chain"`
Handshake *HandshakeConfig `json:"handshake" mapstructure:"handshake"`
Keyring *KeyringConfig `json:"keyring" mapstructure:"keyring"`
Node *NodeConfig `json:"node" mapstructure:"node"`
QOS *QOSConfig `json:"qos" mapstructure:"qos"`
}
func NewConfig() *Config {
return &Config{
Chain: NewChainConfig(),
Handshake: NewHandshakeConfig(),
Keyring: NewKeyringConfig(),
Node: NewNodeConfig(),
QOS: NewQOSConfig(),
}
}
func (c *Config) Validate() error {
if err := c.Chain.Validate(); err != nil {
return errors.Wrapf(err, "invalid section chain")
}
if err := c.Handshake.Validate(); err != nil {
return errors.Wrapf(err, "invalid section handshake")
}
if err := c.Keyring.Validate(); err != nil {
return errors.Wrapf(err, "invalid section keyring")
}
if err := c.Node.Validate(); err != nil {
return errors.Wrapf(err, "invalid section node")
}
if err := c.QOS.Validate(); err != nil {
return errors.Wrapf(err, "invalid section qos")
}
return nil
}
func (c *Config) WithDefaultValues() *Config {
c.Chain = c.Chain.WithDefaultValues()
c.Handshake = c.Handshake.WithDefaultValues()
c.Keyring = c.Keyring.WithDefaultValues()
c.Node = c.Node.WithDefaultValues()
c.QOS = c.QOS.WithDefaultValues()
return c
}
func (c *Config) SaveToPath(path string) error {
var buffer bytes.Buffer
if err := t.Execute(&buffer, c); err != nil {
return err
}
return ioutil.WriteFile(path, buffer.Bytes(), 0600)
}
func (c *Config) String() string {
var buffer bytes.Buffer
if err := t.Execute(&buffer, c); err != nil {
panic(err)
}
return buffer.String()
}
func ReadInConfig(v *viper.Viper) (*Config, error) {
cfg := NewConfig().WithDefaultValues()
if err := v.ReadInConfig(); err != nil {
return nil, err
}
if err := v.Unmarshal(cfg); err != nil {
return nil, err
}
return cfg, nil
}