/
config.go
184 lines (159 loc) · 3.63 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
package common
import (
"os"
"time"
"github.com/empow-blockchain/go-empow/ilog"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
// Constant of limit
var (
MaxBlockGasLimit = int64(800000000)
MaxTxTimeLimit = 200 * time.Millisecond
MaxBlockTimeLimit = 400 * time.Millisecond
)
// ACCConfig account of the system
type ACCConfig struct {
Address string
SecKey string
Algorithm string
}
// Witness config of the genesis block
type Witness struct {
Address string
Owner string
Active string
SignatureBlock string
Balance int64
}
// TokenInfo config of the genesis block
type TokenInfo struct {
FoundationAccount string
EMTotalSupply int64
EMDecimal int64
}
// GenesisConfig config of the genesis bloc
type GenesisConfig struct {
CreateGenesis bool
InitialTimestamp string
TokenInfo *TokenInfo
WitnessInfo []*Witness
ContractPath string
AdminInfo *Witness
FoundationInfo *Witness
}
// DBConfig config of the database
type DBConfig struct {
LdbPath string
}
// VMConfig config of the v8vm
type VMConfig struct {
JsPath string
LogLevel string
}
// P2PConfig is the config for p2p network.
type P2PConfig struct {
ListenAddr string
SeedNodes []string
ChainID uint32
Version uint16
DataPath string
InboundConn int
OutboundConn int
BlackPID []string
BlackIP []string
AdminPort string
}
//RPCConfig is the config for RPC Server.
type RPCConfig struct {
Enable bool
GatewayAddr string
GRPCAddr string
AllowOrigins []string
TryTx bool
ExecTx bool
}
// FileLogConfig is the config for filewriter of ilog.
type FileLogConfig struct {
Path string
Level string
Enable bool
}
// ConsoleLogConfig is the config for consolewriter of ilog.
type ConsoleLogConfig struct {
Level string
Enable bool
}
// LogConfig is the config of ilog.
type LogConfig struct {
FileLog *FileLogConfig
ConsoleLog *ConsoleLogConfig
AsyncWrite bool
EnableContractLog bool
}
// MetricsConfig is the config of metrics.
type MetricsConfig struct {
PushAddr string
Username string
Password string
Enable bool
ID string
}
// SnapshotConfig is the config of snapshot
type SnapshotConfig struct {
Enable bool
FilePath string
}
// DebugConfig is the config of debug.
type DebugConfig struct {
ListenAddr string
}
// VersionConfig contrains netname(mainnet / testnet etc) and protocol info
type VersionConfig struct {
NetName string
ProtocolVersion string
}
// Config provide all configuration for the application
type Config struct {
ACC *ACCConfig
Genesis string
VM *VMConfig
DB *DBConfig
Snapshot *SnapshotConfig
P2P *P2PConfig
RPC *RPCConfig
Log *LogConfig
Metrics *MetricsConfig
Debug *DebugConfig
Version *VersionConfig
}
// LoadYamlAsViper load yaml file as viper object
func LoadYamlAsViper(configfile string) *viper.Viper {
v := viper.GetViper()
v.SetConfigType("yaml")
f, err := os.Open(configfile)
if err != nil {
ilog.Fatalf("Failed to open config file '%v', %v", configfile, err)
}
if err := v.ReadConfig(f); err != nil {
ilog.Fatalf("Failed to read config file: %v", err)
}
return v
}
// NewConfig returns a new instance of Config
func NewConfig(configfile string) *Config {
v := LoadYamlAsViper(configfile)
c := &Config{}
if err := v.Unmarshal(c); err != nil {
ilog.Fatalf("Unable to decode into struct, %v", err)
}
return c
}
// YamlString config to string
func (c *Config) YamlString() string {
bs, err := yaml.Marshal(c)
if err != nil {
ilog.Fatalf("Unable to marshal config to YAML: %v", err)
}
return string(bs)
}