/
config.go
181 lines (159 loc) · 4.84 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
package config
import (
"encoding/json"
"fmt"
"os"
"cosmossdk.io/math"
)
type Config struct {
GreenfieldConfig GreenfieldConfig `json:"greenfield_config"`
LogConfig LogConfig `json:"log_config"`
AlertConfig AlertConfig `json:"alert_config"`
DBConfig DBConfig `json:"db_config"`
MetricsConfig MetricsConfig `json:"metrics_config"`
SPConfig SPConfig `json:"sp_config"`
}
type GreenfieldConfig struct {
KeyType string `json:"key_type"`
AWSRegion string `json:"aws_region"`
AWSSecretName string `json:"aws_secret_name"`
AWSBlsSecretName string `json:"aws_bls_secret_name"`
PrivateKey string `json:"private_key"`
BlsPrivateKey string `json:"bls_private_key"`
RPCAddrs []string `json:"rpc_addrs"`
ChainIdString string `json:"chain_id_string"`
GasLimit uint64 `json:"gas_limit"`
FeeAmount string `json:"fee_amount"`
FeeDenom string `json:"fee_denom"`
}
func (cfg *GreenfieldConfig) Validate() {
if cfg.KeyType == "" {
panic("key_type should not be empty")
} else if cfg.KeyType == "aws_private_key" {
if cfg.AWSRegion == "" {
panic("aws_region should not be empty")
}
if cfg.AWSSecretName == "" {
panic("aws_secret_name should not be empty")
}
if cfg.AWSBlsSecretName == "" {
panic("aws_bls_secret_name should not be empty")
}
} else if cfg.KeyType == "local_private_key" {
if cfg.PrivateKey == "" {
panic("private_key should not be empty")
}
if cfg.BlsPrivateKey == "" {
panic("bls_private_key should not be empty")
}
} else {
panic(fmt.Sprintf("key_type %s is not supported", cfg.KeyType))
}
if cfg.RPCAddrs == nil || len(cfg.RPCAddrs) == 0 {
panic("rpc_addrs should not be empty")
}
if cfg.ChainIdString == "" {
panic("chain_id_string should not be empty")
}
if cfg.GasLimit == 0 {
panic("gas_limit should not be 0")
}
if cfg.FeeAmount == "" {
panic("fee_amount should not be empty")
}
if cfg.FeeDenom == "" {
panic("fee_denom should not be empty")
}
feeAmount, ok := math.NewIntFromString(cfg.FeeAmount)
if !ok {
panic("error converting fee_amount to math.Int")
}
if !feeAmount.IsPositive() {
panic("fee_amount should not be negative")
}
}
type LogConfig struct {
Level string `json:"level"`
Filename string `json:"filename"`
MaxFileSizeInMB int `json:"max_file_size_in_mb"`
MaxBackupsOfLogFiles int `json:"max_backups_of_log_files"`
MaxAgeToRetainLogFilesInDays int `json:"max_age_to_retain_log_files_in_days"`
UseConsoleLogger bool `json:"use_console_logger"`
UseFileLogger bool `json:"use_file_logger"`
Compress bool `json:"compress"`
}
func (cfg *LogConfig) Validate() {
if cfg.UseFileLogger {
if cfg.Filename == "" {
panic("filename should not be empty if use file logger")
}
if cfg.MaxFileSizeInMB <= 0 {
panic("max_file_size_in_mb should be larger than 0 if use file logger")
}
if cfg.MaxBackupsOfLogFiles <= 0 {
panic("max_backups_off_log_files should be larger than 0 if use file logger")
}
}
}
type DBConfig struct {
Dialect string `json:"dialect"`
DBPath string `json:"db_path"`
KeyType string `json:"key_type"`
AWSRegion string `json:"aws_region"`
AWSSecretName string `json:"aws_secret_name"`
Password string `json:"password"`
Username string `json:"username"`
MaxIdleConns int `json:"max_idle_conns"`
MaxOpenConns int `json:"max_open_conns"`
DebugMode bool `json:"debug_mode"`
}
func (cfg *DBConfig) Validate() {
if cfg.Dialect != DBDialectMysql && cfg.Dialect != DBDialectSqlite3 {
panic(fmt.Sprintf("only %s and %s supported", DBDialectMysql, DBDialectSqlite3))
}
if cfg.Username == "" || cfg.DBPath == "" {
panic("db config is not correct")
}
}
type SPConfig struct {
InternalSPEndpoints []string `json:"internal_sp_endpoints"`
}
type MetricsConfig struct {
Port uint16 `json:"port"`
}
func (cfg *MetricsConfig) Validate() {
if cfg.Port <= 0 || cfg.Port > 65535 {
panic("port should be within (0, 65535]")
}
}
func (cfg *Config) Validate() {
cfg.LogConfig.Validate()
cfg.DBConfig.Validate()
cfg.GreenfieldConfig.Validate()
cfg.MetricsConfig.Validate()
}
func ParseConfigFromJson(content string) *Config {
var config Config
if err := json.Unmarshal([]byte(content), &config); err != nil {
panic(err)
}
config.Validate()
return &config
}
func ParseConfigFromFile(filePath string) *Config {
bz, err := os.ReadFile(filePath)
if err != nil {
panic(err)
}
var config Config
if err := json.Unmarshal(bz, &config); err != nil {
panic(err)
}
config.Validate()
return &config
}
type AlertConfig struct {
Identity string `json:"identity"`
TelegramBotId string `json:"telegram_bot_id"`
TelegramChatId string `json:"telegram_chat_id"`
}