-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
245 lines (232 loc) · 7.53 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
// Copyright 2019 The berith Authors
// This file is part of berith.
//
// berith is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// berith is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with berith. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"time"
"unicode"
"github.com/BerithFoundation/berith-chain/cmd/utils"
"github.com/naoina/toml"
cli "gopkg.in/urfave/cli.v1"
)
type berithenchConfig struct {
ChainID int64 `json:"chainId"` // chain id
Nodes []string `json:"nodes"` // endpoints of nodes
Keystore string `json:"keystore"` // keystore path
Addresses []string `json:"addresses"` // from addresses in tx
Password string `json:"password"` // password
PasswordFile string `json:"passwordFile"` // password file path
Duration string `json:"duration"` // time duration for test execution
TxCount uint64 `json:"txCount"` // tx count of test execution
InitDelay uint64 `json:"initDelay"` // initial sleep before testing
TxInterval uint64 `json:"txInterval"` // interval between send transactions
OutputPath string `json:"outputPath"` // path of output
StakeCount uint64 `json:"stakeCount"` // stake count of test execution
ContractCount uint64 `json:"contractCount"` //contract count of test execution
ContractData map[string][]string `json:"contractData"` // transaction data to call smart contract
TxValue uint64 `json:"txAmount"` //value of transaction
StakeValue uint64 `json:"stakeAmount"` //value of Stake
ContractValue uint64 `json:"contractAmount"` //value of Contract Call
GasLimit uint64 `json:"gasLimit"` //gas limit
GasPrice uint64 `json:"gasPrice"` //gas price
EnableCpuProfile bool `json:"enableCpuProfile"` // enable cpu profile
EnableGoTrace bool `json:"enableGoTrace"` // enable go trace
}
var (
defaultConfig = berithenchConfig{
Keystore: "",
Password: "",
Duration: "",
TxCount: 0,
TxInterval: 10,
InitDelay: 0,
OutputPath: getDefaultWorkingDir(),
EnableCpuProfile: false,
EnableGoTrace: false,
}
tomlSettings = toml.Config{
NormFieldName: func(rt reflect.Type, key string) string {
return key
},
FieldToKey: func(rt reflect.Type, field string) string {
return field
},
MissingField: func(rt reflect.Type, field string) error {
link := ""
if unicode.IsUpper(rune(rt.Name()[0])) && rt.PkgPath() != "main" {
link = fmt.Sprintf(", see https://godoc.org/%s#%s for available fields", rt.PkgPath(), rt.Name())
}
return fmt.Errorf("field '%s' is not defined in %s%s", field, rt.String(), link)
},
}
)
// parseConfig parse bbench config from cli context.
func parseConfig(ctx *cli.Context) (*berithenchConfig, error) {
cfg := defaultConfig
// load config file
if file := ctx.String(ConfigFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}
// apply flags
var (
chainID = ctx.Int64(ChainIDFlag.Name)
nodes = ctx.String(NodesFlag.Name)
keystore = ctx.String(KeystoreFlag.Name)
addrs = ctx.String(AddressesFlag.Name)
password = ctx.String(PasswordFlag.Name)
passwordFile = ctx.String(PasswordFileFlag.Name)
duration = ctx.String(DurationFlag.Name)
txCount = ctx.Uint64(TxCountFlag.Name)
txInterval = ctx.Uint64(TxIntervalFlag.Name)
initDelay = ctx.Uint64(InitDelay.Name)
output = ctx.String(OutputPath.Name)
stakeCount = ctx.Uint64(StakeCountFlag.Name)
contractCount = ctx.Uint64(ContractCountFlag.Name)
txValue = ctx.Uint64(TxValueFlag.Name)
stakeValue = ctx.Uint64(StakeValueFlag.Name)
contractValue = ctx.Uint64(ContractValueFlag.Name)
gasLimit = ctx.Uint64(GasLimitFlag.Name)
gasPrice = ctx.Uint64(GasPriceFlag.Name)
enableCpuProfile = ctx.IsSet(EnableCpuProfile.Name)
enableGoTrace = ctx.IsSet(EnableGoTrace.Name)
)
// FIXME : more efficient compare
if chainID != 0 {
cfg.ChainID = chainID
}
if nodes != "" {
cfg.Nodes = strings.Split(nodes, ",")
}
if keystore != "" {
cfg.Keystore = keystore
}
if addrs != "" {
cfg.Addresses = strings.Split(addrs, ",")
}
if password != "" {
cfg.Password = password
}
if passwordFile != "" {
cfg.PasswordFile = passwordFile
}
if duration != "" {
cfg.Duration = duration
}
if txCount > 0 {
cfg.TxCount = txCount
}
if txInterval > 0 {
cfg.TxInterval = txInterval
}
if initDelay > 0 {
cfg.InitDelay = initDelay
}
if output != "" {
cfg.OutputPath = output
}
if stakeCount > 0 {
cfg.StakeCount = stakeCount
}
if contractCount > 0 {
cfg.ContractCount = contractCount
}
if txValue > 0 {
cfg.TxValue = txValue
}
if stakeValue > 0 {
cfg.StakeValue = stakeValue
}
if contractValue > 0 {
cfg.ContractValue = contractValue
}
if gasLimit > 0 {
cfg.GasLimit = gasLimit
}
if gasPrice > 0 {
cfg.GasPrice = gasPrice
}
if enableCpuProfile {
cfg.EnableCpuProfile = enableCpuProfile
}
if enableGoTrace {
cfg.EnableGoTrace = enableGoTrace
}
return &cfg, nil
}
// loadConfig load bbench config from toml file.
func loadConfig(file string, cfg *berithenchConfig) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
// Add file name to errors that have a line number.
if _, ok := err.(*toml.LineError); ok {
err = errors.New(file + ", " + err.Error())
}
return err
}
// makePasswordList reads password lines from the file
func makePasswordList(config *berithenchConfig) []string {
if config.PasswordFile == "" {
return nil
}
content, err := ioutil.ReadFile(config.PasswordFile)
if err != nil {
utils.Fatalf("failed to read password file: %v", err)
}
var ret []string
lines := strings.Split(string(content), "\n")
for i := range lines {
lines[i] = strings.TrimRight(lines[i], "\r")
if lines[i] != "" {
ret = append(ret, lines[i])
}
}
return ret
}
// parseDuration parse duration string to time with HH:mm:DD format
func parseDuration(config *berithenchConfig) time.Duration {
if config.Duration == "" {
return time.Duration(0)
}
t, err := time.Parse("15:04:05", config.Duration)
if err != nil {
utils.Fatalf("invalid duration. needs HH:mm:DD but %s.\n%v", config.Duration, err)
}
d := 0
d += t.Hour() * int(time.Hour)
d += t.Minute() * int(time.Minute)
d += t.Second() * int(time.Second)
return time.Duration(d)
}
// getWorkingDir returns user's home dir or temp dir if occur error
func getDefaultWorkingDir() string {
home, err := os.UserHomeDir()
if err == nil {
return home
}
return os.TempDir()
}