/
config.go
executable file
·401 lines (351 loc) · 10.6 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
package repo
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/ethereum/go-ethereum/event"
"github.com/fsnotify/fsnotify"
"github.com/meshplus/bitxhub-core/tss"
"github.com/mitchellh/go-homedir"
ma "github.com/multiformats/go-multiaddr"
"github.com/spf13/viper"
)
const (
// defaultPathName is the default config dir name
defaultPathName = ".bitxhub"
// defaultPathRoot is the path to the default config dir location.
defaultPathRoot = "~/" + defaultPathName
// envDir is the environment variable used to change the path root.
envDir = "BITXHUB_PATH"
// Config name
configName = "bitxhub.toml"
// key name
KeyName = "key.json"
// API name
APIName = "api"
// admin weight
SuperAdminWeight = 2
NormalAdminWeight = 1
// governance strategy default participate threshold
DefaultSimpleMajorityExpression = "a > 0.5 * t"
DefaultZeroStrategyExpression = "a >= 0"
//Passwd
DefaultPasswd = "bitxhub"
SuperMajorityApprove = "SuperMajorityApprove"
SuperMajorityAgainst = "SuperMajorityAgainst"
SimpleMajority = "SimpleMajority"
ZeroPermission = "ZeroPermission"
AppchainMgr = "appchain_mgr"
RuleMgr = "rule_mgr"
NodeMgr = "node_mgr"
ServiceMgr = "service_mgr"
RoleMgr = "role_mgr"
ProposalStrategyMgr = "proposal_strategy_mgr"
DappMgr = "dapp_mgr"
AllMgr = "all_mgr"
)
type Config struct {
RepoRoot string `json:"repo_root"`
Title string `json:"title"`
Solo bool `json:"solo"`
Port `json:"port"`
PProf `json:"pprof"`
Monitor `json:"monitor"`
Limiter `json:"limiter"`
Appchain `json:"appchain"`
Gateway `json:"gateway"`
Ping `json:"ping"`
Log `json:"log"`
Cert `json:"cert"`
Txpool `json:"txpool"`
Order `json:"order"`
Executor `json:"executor"`
Ledger `json:"ledger"`
Genesis `json:"genesis"`
Security Security `toml:"security" json:"security"`
License License `toml:"license" json:"license"`
Crypto Crypto `toml:"crypto" json:"crypto"`
Tss tss.TssConfig `toml:"tss" json:"tss"`
}
// Security are files used to setup connection with tls
type Security struct {
EnableTLS bool `mapstructure:"enable_tls"`
PemFilePath string `mapstructure:"pem_file_path" json:"pem_file_path"`
ServerKeyPath string `mapstructure:"server_key_path" json:"server_key_path"`
GatewayCertPath string `mapstructure:"gateway_cert_path" json:"gateway_cert_path"`
GatewayKeyPath string `mapstructure:"gateway_key_path" json:"gateway_key_path"`
}
type Port struct {
JsonRpc int64 `toml:"jsonrpc" json:"jsonrpc"`
Grpc int64 `toml:"grpc" json:"grpc"`
Gateway int64 `toml:"gateway" json:"gateway"`
PProf int64 `toml:"pprof" json:"pprof"`
Monitor int64 `toml:"monitor" json:"monitor"`
}
type Monitor struct {
Enable bool
}
type PProf struct {
Enable bool `toml:"enbale" json:"enable"`
PType string `toml:"ptype" json:"ptype"`
Mode string `toml:"mode" json:"mode"`
Duration time.Duration `toml:"duration" json:"duration"`
}
type Limiter struct {
Interval time.Duration `toml:"interval" json:"interval"`
Quantum int64 `toml:"quantum" json:"quantum"`
Capacity int64 `toml:"capacity" json:"capacity"`
}
type Appchain struct {
Enable bool `toml:"enable" json:"enable"`
EthHeaderPath string `mapstructure:"eth_header_path"`
}
type Gateway struct {
AllowedOrigins []string `mapstructure:"allowed_origins"`
}
type Ping struct {
Enable bool `toml:"enable" json:"enable"`
Duration time.Duration `toml:"duration" json:"duration"`
}
type Log struct {
Level string `toml:"level" json:"level"`
Dir string `toml:"dir" json:"dir"`
Filename string `toml:"filename" json:"filename"`
ReportCaller bool `mapstructure:"report_caller" json:"report_caller"`
Module LogModule `toml:"module" json:"module"`
}
type LogModule struct {
P2P string `toml:"p2p" json:"p2p"`
Consensus string `toml:"consensus" json:"consensus"`
Executor string `toml:"executor" json:"executor"`
Router string `toml:"router" json:"router"`
API string `toml:"api" json:"api"`
CoreAPI string `mapstructure:"coreapi" toml:"coreapi" json:"coreapi"`
Storage string `toml:"storage" json:"storage"`
Profile string `toml:"profile" json:"profile"`
TSS string `toml:"tss" json:"tss"`
}
type Strategy struct {
Module string `json:"module" toml:"module"`
Typ string `json:"typ" toml:"typ"`
Extra string `json:"extra" toml:"extra"`
}
type Genesis struct {
ChainID uint64 `json:"chainid" toml:"chainid"`
GasLimit uint64 `mapstructure:"gas_limit" json:"gas_limit" toml:"gas_limit"`
BvmGasPrice uint64 `mapstructure:"bvm_gas_price" json:"bvm_gas_price" toml:"bvm_gas_price"`
Balance string `json:"balance" toml:"balance"`
Admins []*Admin `json:"admins" toml:"admins"`
Strategy []*Strategy `json:"strategy" toml:"strategy"`
}
type Admin struct {
Address string `json:"address" toml:"address"`
Weight uint64 `json:"weight" toml:"weight"`
}
type Cert struct {
Verify bool `toml:"verify" json:"verify"`
NodeCertPath string `mapstructure:"node_cert_path" json:"node_cert_path"`
AgencyCertPath string `mapstructure:"agency_cert_path" json:"agency_cert_path"`
CACertPath string `mapstructure:"ca_cert_path" json:"ca_cert_path"`
}
type Txpool struct {
BatchSize int `mapstructure:"batch_size" json:"batch_size"`
BatchTimeout time.Duration `mapstructure:"batch_timeout" json:"batch_timeout"`
}
type Order struct {
Type string `toml:"type" json:"type"`
}
type Executor struct {
Type string `toml:"type" json:"type"`
}
type Ledger struct {
Type string `toml:"type" json:"type"`
}
type License struct {
Key string `toml:"key" json:"key"`
Verifier string `toml:"verifier" json:"verifier"`
}
type Crypto struct {
Algorithms []string `json:"algorithms" toml:"algorithms"`
}
func (c *Config) Bytes() ([]byte, error) {
ret, err := json.Marshal(c)
if err != nil {
return nil, err
}
return ret, nil
}
func DefaultConfig() (*Config, error) {
return &Config{
Title: "BitXHub configuration file",
Solo: false,
Port: Port{
Grpc: 60011,
Gateway: 9091,
PProf: 53121,
Monitor: 40011,
},
PProf: PProf{Enable: false},
Ping: Ping{Enable: false},
Gateway: Gateway{AllowedOrigins: []string{"*"}},
Log: Log{
Level: "info",
Dir: "logs",
Filename: "bitxhub.log",
Module: LogModule{
P2P: "info",
Consensus: "debug",
Executor: "info",
Router: "info",
API: "info",
CoreAPI: "info",
TSS: "info",
},
},
Cert: Cert{
Verify: true,
NodeCertPath: "certs/node.cert",
AgencyCertPath: "certs/agency.cert",
CACertPath: "certs/ca.cert",
},
Txpool: Txpool{
BatchSize: 500,
BatchTimeout: 500 * time.Millisecond,
},
Order: Order{
Type: "raft",
},
Executor: Executor{
Type: "serial",
},
Genesis: Genesis{
ChainID: 1,
GasLimit: 0x5f5e100,
Balance: "100000000000000000000000000000000000",
},
Ledger: Ledger{Type: "complex"},
Crypto: Crypto{Algorithms: []string{"Secp256k1"}},
}, nil
}
func UnmarshalConfig(viper *viper.Viper, repoRoot string, configPath string) (*Config, error) {
if len(configPath) == 0 {
viper.SetConfigFile(filepath.Join(repoRoot, configName))
} else {
viper.SetConfigFile(configPath)
fileData, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("read bitxhub config error: %w", err)
}
err = ioutil.WriteFile(filepath.Join(repoRoot, configName), fileData, 0644)
if err != nil {
return nil, fmt.Errorf("write bitxhub config failed: %w", err)
}
}
viper.SetConfigType("toml")
viper.AutomaticEnv()
viper.SetEnvPrefix("BITXHUB")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("readInConfig error: %w", err)
}
config, err := DefaultConfig()
if err != nil {
return nil, err
}
if err := viper.Unmarshal(config); err != nil {
return nil, fmt.Errorf("unmarshal config error: %w", err)
}
config.RepoRoot = repoRoot
return config, nil
}
func WatchBitxhubConfig(viper *viper.Viper, feed *event.Feed) {
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
fmt.Println("bitxhub config file changed: ", in.String())
config, err := DefaultConfig()
if err != nil {
fmt.Println("get default config: ", err)
return
}
if err := viper.Unmarshal(config); err != nil {
fmt.Println("unmarshal config: ", err)
return
}
feed.Send(&Repo{Config: config})
})
}
func WatchNetworkConfig(viper *viper.Viper, feed *event.Feed) {
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
fmt.Println("network config file changed: ", in.String())
var config *NetworkConfig
if err := viper.Unmarshal(config); err != nil {
fmt.Println("unmarshal config: ", err)
return
}
checkReaptAddr := make(map[string]uint64)
for _, node := range config.Nodes {
if node.ID == config.ID {
if len(node.Hosts) == 0 {
fmt.Printf("no hosts found by node:%d \n", node.ID)
return
}
config.LocalAddr = node.Hosts[0]
addr, err := ma.NewMultiaddr(fmt.Sprintf("%s%s", node.Hosts[0], node.Pid))
if err != nil {
fmt.Printf("new multiaddr: %v \n", err)
return
}
config.LocalAddr = strings.Replace(config.LocalAddr, ma.Split(addr)[0].String(), "/ip4/0.0.0.0", -1)
}
if _, ok := checkReaptAddr[node.Hosts[0]]; !ok {
checkReaptAddr[node.Hosts[0]] = node.ID
} else {
err := fmt.Errorf("reapt address with Node: nodeID = %d,Host = %s \n",
checkReaptAddr[node.Hosts[0]], node.Hosts[0])
panic(err)
}
}
if config.LocalAddr == "" {
fmt.Printf("lack of local address \n")
return
}
idx := strings.LastIndex(config.LocalAddr, "/p2p/")
if idx == -1 {
fmt.Printf("pid is not existed in bootstrap \n")
return
}
config.LocalAddr = config.LocalAddr[:idx]
feed.Send(&Repo{NetworkConfig: config})
})
}
func ReadConfig(v *viper.Viper, path, configType string, config interface{}) error {
v.SetConfigFile(path)
v.SetConfigType(configType)
if err := v.ReadInConfig(); err != nil {
return fmt.Errorf("readInConfig error: %w", err)
}
if err := v.Unmarshal(config); err != nil {
return fmt.Errorf("unmarshal config error: %w", err)
}
return nil
}
func PathRoot() (string, error) {
dir := os.Getenv(envDir)
var err error
if len(dir) == 0 {
dir, err = homedir.Expand(defaultPathRoot)
}
return dir, err
}
func PathRootWithDefault(path string) (string, error) {
if len(path) == 0 {
return PathRoot()
}
return path, nil
}