/
config.go
603 lines (505 loc) · 14.9 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
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
603
package types
import (
"regexp"
"github.com/pelletier/go-toml"
"github.com/rs/zerolog"
)
var (
// Cfg represents the configuration to be used during the execution
Cfg Config
HTTPProtocols = regexp.MustCompile("https?://")
)
// ConfigParser represents a function that allows to parse a file contents as a Config object
type ConfigParser = func(fileContents []byte) (Config, error)
type configToml struct {
RPC *rpcConfig `toml:"rpc"`
Grpc *grpcConfig `toml:"grpc"`
Cosmos *cosmosConfig `toml:"cosmos"`
Database *databaseConfig `toml:"database"`
Logging *loggingConfig `toml:"logging"`
Parsing *parsingConfig `toml:"parsing"`
Pruning *pruningConfig `toml:"pruning"`
Telemetry *telemetryConfig `toml:"telemetry"`
}
// DefaultConfigParser attempts to read and parse a Juno config from the given string bytes.
// An error reading or parsing the config results in a panic.
func DefaultConfigParser(configData []byte) (Config, error) {
var cfg configToml
err := toml.Unmarshal(configData, &cfg)
return NewConfig(
cfg.RPC,
cfg.Grpc,
cfg.Cosmos,
cfg.Database,
cfg.Logging,
cfg.Parsing,
cfg.Pruning,
cfg.Telemetry,
), err
}
// ---------------------------------------------------------------------------------------------------------------------
// Config represents the configuration to run Juno
type Config interface {
GetRPCConfig() RPCConfig
GetGrpcConfig() GrpcConfig
GetCosmosConfig() CosmosConfig
GetDatabaseConfig() DatabaseConfig
GetLoggingConfig() LoggingConfig
GetParsingConfig() ParsingConfig
GetPruningConfig() PruningConfig
GetTelemetryConfig() TelemetryConfig
}
var _ Config = &config{}
// Config defines all necessary juno configuration parameters.
type config struct {
RPC RPCConfig `toml:"rpc"`
Grpc GrpcConfig `toml:"grpc"`
Cosmos CosmosConfig `toml:"cosmos"`
Database DatabaseConfig `toml:"database"`
Logging LoggingConfig `toml:"logging"`
Parsing ParsingConfig `toml:"parsing"`
Pruning PruningConfig `toml:"pruning"`
Telemetry TelemetryConfig `toml:"telemetry"`
}
// NewConfig builds a new Config instance
func NewConfig(
rpcConfig RPCConfig, grpConfig GrpcConfig,
cosmosConfig CosmosConfig, dbConfig DatabaseConfig,
loggingConfig LoggingConfig, parsingConfig ParsingConfig,
pruningConfig PruningConfig, telemetryConfig TelemetryConfig,
) Config {
return &config{
RPC: rpcConfig,
Grpc: grpConfig,
Cosmos: cosmosConfig,
Database: dbConfig,
Logging: loggingConfig,
Parsing: parsingConfig,
Pruning: pruningConfig,
Telemetry: telemetryConfig,
}
}
// GetRPCConfig implements Config
func (c *config) GetRPCConfig() RPCConfig {
if c.RPC == nil {
return DefaultRPCConfig()
}
return c.RPC
}
// GetGrpcConfig implements Config
func (c *config) GetGrpcConfig() GrpcConfig {
if c.Grpc == nil {
return DefaultGrpcConfig()
}
return c.Grpc
}
// GetCosmosConfig implements Config
func (c *config) GetCosmosConfig() CosmosConfig {
if c.Cosmos == nil {
return DefaultCosmosConfig()
}
return c.Cosmos
}
// GetDatabaseConfig implements Config
func (c *config) GetDatabaseConfig() DatabaseConfig {
if c.Database == nil {
return DefaultDatabaseConfig()
}
return c.Database
}
// GetLoggingConfig implements Config
func (c *config) GetLoggingConfig() LoggingConfig {
if c.Logging == nil {
return DefaultLoggingConfig()
}
return c.Logging
}
// GetParsingConfig implements Config
func (c *config) GetParsingConfig() ParsingConfig {
if c.Parsing == nil {
return DefaultParsingConfig()
}
return c.Parsing
}
// GetPruningConfig implements Config
func (c *config) GetPruningConfig() PruningConfig {
if c.Pruning == nil {
return DefaultPruningConfig()
}
return c.Pruning
}
// GetTelemetryConfig implements Config
func (c *config) GetTelemetryConfig() TelemetryConfig {
if c.Telemetry == nil {
return DefaultTelemetryConfig()
}
return c.Telemetry
}
// ---------------------------------------------------------------------------------------------------------------------
// RPCConfig contains the configuration of the RPC endpoint
type RPCConfig interface {
GetClientName() string
GetAddress() string
GetMaxConnections() int
}
var _ RPCConfig = &rpcConfig{}
const (
DefaultMaxConnections = 20
)
type rpcConfig struct {
ClientName string `toml:"client_name"`
Address string `toml:"address"`
MaxConnections int `toml:"max_connections"`
}
// NewRPCConfig allows to build a new RPCConfig instance
func NewRPCConfig(clientName, address string, maxConnections int) RPCConfig {
return &rpcConfig{
ClientName: clientName,
Address: address,
MaxConnections: maxConnections,
}
}
// DefaultRPCConfig returns the default instance of RPCConfig
func DefaultRPCConfig() RPCConfig {
return NewRPCConfig("juno", "http://localhost:26657", DefaultMaxConnections)
}
// GetClientName implements RPCConfig
func (r *rpcConfig) GetClientName() string {
return r.ClientName
}
// GetAddress implements RPCConfig
func (r *rpcConfig) GetAddress() string {
return r.Address
}
// GetMaxConnections implements RPCConfig
func (r *rpcConfig) GetMaxConnections() int {
if r.MaxConnections <= 0 {
return DefaultMaxConnections
}
return r.MaxConnections
}
// ---------------------------------------------------------------------------------------------------------------------
// GrpcConfig contains the configuration of the gRPC endpoint
type GrpcConfig interface {
GetAddress() string
IsInsecure() bool
}
var _ GrpcConfig = &grpcConfig{}
type grpcConfig struct {
Address string `toml:"address"`
Insecure bool `toml:"insecure"`
}
// NewGrpcConfig allows to build a new GrpcConfig instance
func NewGrpcConfig(address string, insecure bool) GrpcConfig {
return &grpcConfig{
Address: address,
Insecure: insecure,
}
}
// DefaultGrpcConfig returns the default instance of a GrpcConfig
func DefaultGrpcConfig() GrpcConfig {
return NewGrpcConfig("localhost:9090", true)
}
// GetAddress implements GrpcConfig
func (g *grpcConfig) GetAddress() string {
return HTTPProtocols.ReplaceAllString(g.Address, "")
}
// IsInsecure implements GrpcConfig
func (g *grpcConfig) IsInsecure() bool {
return g.Insecure
}
// ---------------------------------------------------------------------------------------------------------------------
// CosmosConfig contains the data to configure the CosmosConfig SDK
type CosmosConfig interface {
GetPrefix() string
GetModules() []string
}
var _ CosmosConfig = &cosmosConfig{}
type cosmosConfig struct {
Prefix string `toml:"prefix"`
Modules []string `toml:"modules"`
}
// NewCosmosConfig returns a new CosmosConfig instance
func NewCosmosConfig(prefix string, modules []string) CosmosConfig {
return &cosmosConfig{
Prefix: prefix,
Modules: modules,
}
}
// DefaultCosmosConfig returns the default instance of CosmosConfig
func DefaultCosmosConfig() CosmosConfig {
return NewCosmosConfig("cosmos", nil)
}
// GetPrefix implements CosmosConfig
func (c *cosmosConfig) GetPrefix() string {
return c.Prefix
}
// GetModules implements CosmosConfig
func (c *cosmosConfig) GetModules() []string {
return c.Modules
}
// ---------------------------------------------------------------------------------------------------------------------
// DatabaseConfig represents a generic database configuration
type DatabaseConfig interface {
GetName() string
GetHost() string
GetPort() int64
GetUser() string
GetPassword() string
GetSSLMode() string
GetSchema() string
GetMaxOpenConnections() int
GetMaxIdleConnections() int
}
var _ DatabaseConfig = &databaseConfig{}
type databaseConfig struct {
Name string `toml:"name"`
Host string `toml:"host"`
Port int64 `toml:"port"`
User string `toml:"user"`
Password string `toml:"password"`
SSLMode string `toml:"ssl_mode"`
Schema string `toml:"schema"`
MaxOpenConnections int `toml:"max_open_connections"`
MaxIdleConnections int `toml:"max_idle_connections"`
}
func NewDatabaseConfig(
name, host string, port int64, user string, password string,
sslMode string, schema string,
maxOpenConnections int, maxIdleConnections int,
) DatabaseConfig {
return &databaseConfig{
Name: name,
Host: host,
Port: port,
User: user,
Password: password,
SSLMode: sslMode,
Schema: schema,
MaxOpenConnections: maxOpenConnections,
MaxIdleConnections: maxIdleConnections,
}
}
// DefaultDatabaseConfig returns the default instance of DatabaseConfig
func DefaultDatabaseConfig() DatabaseConfig {
return NewDatabaseConfig(
"database-name",
"localhost",
5432,
"user",
"password",
"",
"public",
1,
1,
)
}
// GetName implements DatabaseConfig
func (d *databaseConfig) GetName() string {
return d.Name
}
// GetHost implements DatabaseConfig
func (d *databaseConfig) GetHost() string {
return d.Host
}
// GetPort implements DatabaseConfig
func (d *databaseConfig) GetPort() int64 {
return d.Port
}
// GetUser implements DatabaseConfig
func (d *databaseConfig) GetUser() string {
return d.User
}
// GetPassword implements DatabaseConfig
func (d *databaseConfig) GetPassword() string {
return d.Password
}
// GetSSLMode implements DatabaseConfig
func (d *databaseConfig) GetSSLMode() string {
return d.SSLMode
}
// GetSchema implements DatabaseConfig
func (d *databaseConfig) GetSchema() string {
return d.Schema
}
// GetMaxOpenConnections implements DatabaseConfig
func (d *databaseConfig) GetMaxOpenConnections() int {
return d.MaxOpenConnections
}
// GetMaxIdleConnections implements DatabaseConfig
func (d *databaseConfig) GetMaxIdleConnections() int {
return d.MaxIdleConnections
}
// ---------------------------------------------------------------------------------------------------------------------
// LoggingConfig represents the configuration for the logging part
type LoggingConfig interface {
GetLogLevel() string
GetLogFormat() string
}
var _ LoggingConfig = &loggingConfig{}
type loggingConfig struct {
LogLevel string `toml:"level"`
LogFormat string `toml:"format"`
}
// NewLoggingConfig returns a new LoggingConfig instance
func NewLoggingConfig(level, format string) LoggingConfig {
return &loggingConfig{
LogLevel: level,
LogFormat: format,
}
}
// DefaultLoggingConfig returns the default LoggingConfig instance
func DefaultLoggingConfig() LoggingConfig {
return NewLoggingConfig(zerolog.DebugLevel.String(), "text")
}
// GetLogLevel implements LoggingConfig
func (l *loggingConfig) GetLogLevel() string {
return l.LogLevel
}
// GetLogFormat implements LoggingConfig
func (l *loggingConfig) GetLogFormat() string {
return l.LogFormat
}
// ---------------------------------------------------------------------------------------------------------------------
// ParsingConfig represents the configuration of the parsing
type ParsingConfig interface {
GetWorkers() int64
ShouldParseNewBlocks() bool
ShouldParseOldBlocks() bool
ShouldParseGenesis() bool
GetGenesisFilePath() string
GetStartHeight() int64
UseFastSync() bool
}
var _ ParsingConfig = &parsingConfig{}
type parsingConfig struct {
Workers int64 `toml:"workers"`
ParseNewBlocks bool `toml:"listen_new_blocks"`
ParseOldBlocks bool `toml:"parse_old_blocks"`
GenesisFilePath string `toml:"genesis_file_path"`
ParseGenesis bool `toml:"parse_genesis"`
StartHeight int64 `toml:"start_height"`
FastSync bool `toml:"fast_sync"`
}
// NewParsingConfig allows to build a new ParsingConfig instance
func NewParsingConfig(
workers int64,
parseNewBlocks, parseOldBlocks bool,
parseGenesis bool, genesisFilePath string, startHeight int64, fastSync bool,
) ParsingConfig {
return &parsingConfig{
Workers: workers,
ParseOldBlocks: parseOldBlocks,
ParseNewBlocks: parseNewBlocks,
ParseGenesis: parseGenesis,
GenesisFilePath: genesisFilePath,
StartHeight: startHeight,
FastSync: fastSync,
}
}
// DefaultParsingConfig returns the default instance of ParsingConfig
func DefaultParsingConfig() ParsingConfig {
return NewParsingConfig(
1,
true,
true,
true,
"",
1,
false,
)
}
// GetWorkers implements ParsingConfig
func (p *parsingConfig) GetWorkers() int64 {
return p.Workers
}
// ShouldParseNewBlocks implements ParsingConfig
func (p *parsingConfig) ShouldParseNewBlocks() bool {
return p.ParseNewBlocks
}
// ShouldParseOldBlocks implements ParsingConfig
func (p *parsingConfig) ShouldParseOldBlocks() bool {
return p.ParseOldBlocks
}
// ShouldParseGenesis implements ParsingConfig
func (p *parsingConfig) ShouldParseGenesis() bool {
return p.ParseGenesis
}
func (p *parsingConfig) GetGenesisFilePath() string {
return p.GenesisFilePath
}
// GetStartHeight implements ParsingConfig
func (p *parsingConfig) GetStartHeight() int64 {
return p.StartHeight
}
// UseFastSync implements ParsingConfig
func (p *parsingConfig) UseFastSync() bool {
return p.FastSync
}
// ---------------------------------------------------------------------------------------------------------------------
// PruningConfig contains the configuration of the pruning strategy
type PruningConfig interface {
GetKeepRecent() int64
GetKeepEvery() int64
GetInterval() int64
}
var _ PruningConfig = &pruningConfig{}
type pruningConfig struct {
KeepRecent int64 `toml:"keep_recent"`
KeepEvery int64 `toml:"keep_every"`
Interval int64 `toml:"interval"`
}
// NewPruningConfig returns a new PruningConfig
func NewPruningConfig(keepRecent, keepEvery, interval int64) PruningConfig {
return &pruningConfig{
KeepRecent: keepRecent,
KeepEvery: keepEvery,
Interval: interval,
}
}
// DefaultPruningConfig returns the default PruningConfig instance
func DefaultPruningConfig() PruningConfig {
return NewPruningConfig(100, 500, 10)
}
// GetKeepRecent implements PruningConfig
func (p *pruningConfig) GetKeepRecent() int64 {
return p.KeepRecent
}
// GetKeepEvery implements PruningConfig
func (p *pruningConfig) GetKeepEvery() int64 {
return p.KeepEvery
}
// GetInterval implements PruningConfig
func (p *pruningConfig) GetInterval() int64 {
return p.Interval
}
// ---------------------------------------------------------------------------------------------------------------------
// TelemetryConfig contains the configuration of the pruning strategy
type TelemetryConfig interface {
IsEnabled() bool
GetPort() uint
}
var _ TelemetryConfig = &telemetryConfig{}
type telemetryConfig struct {
Enabled bool `toml:"enabled"`
Port uint `toml:"port"`
}
// NewTelemetryConfig allows to build a new TelemetryConfig instance
func NewTelemetryConfig(enabled bool, port uint) TelemetryConfig {
return &telemetryConfig{
Enabled: enabled,
Port: port,
}
}
// DefaultTelemetryConfig returns the default TelemetryConfig instance
func DefaultTelemetryConfig() TelemetryConfig {
return NewTelemetryConfig(false, 500)
}
// IsEnabled implements TelemetryConfig
func (p *telemetryConfig) IsEnabled() bool {
return p.Enabled
}
// GetPort implements TelemetryConfig
func (p *telemetryConfig) GetPort() uint {
return p.Port
}