/
params.go
287 lines (234 loc) · 6.01 KB
/
params.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
package loadbot
import (
"errors"
"fmt"
"math/big"
"strings"
"github.com/0xDaoChain/dao-chain/command/loadbot/generator"
"github.com/0xDaoChain/dao-chain/types"
"github.com/umbracle/ethgo/abi"
)
var (
params = &loadbotParams{}
)
var (
errInvalidMode = errors.New("invalid loadbot mode")
errInvalidValues = errors.New("invalid values")
errContractPath = errors.New("contract path not specified")
)
const (
// arbitrary value for total token supply
// token has 5 decimals
// transfers are done with 0.001 amount
erc20TokenSupply = "5000000000"
erc20TokenName = "ZexCoin"
erc20TokenSymbol = "ZEX"
erc721TokenName = "ZexNFT"
erc721TokenSymbol = "ZEXES"
)
const (
tpsFlag = "tps"
modeFlag = "mode"
detailedFlag = "detailed"
chainIDFlag = "chain-id"
senderFlag = "sender"
receiverFlag = "receiver"
valueFlag = "value"
countFlag = "count"
maxConnsFlag = "max-conns"
gasPriceFlag = "gas-price"
gasLimitFlag = "gas-limit"
contractFlag = "contract"
maxWaitFlag = "max-wait"
)
type loadbotParams struct {
tps uint64
chainID uint64
count uint64
maxConns uint64
maxWait uint64
contractPath string
detailed bool
modeRaw string
senderRaw string
receiverRaw string
valueRaw string
gasPriceRaw string
gasLimitRaw string
mode Mode
sender types.Address
receiver types.Address
value *big.Int
gasPrice *big.Int
gasLimit *big.Int
contractArtifact *generator.ContractArtifact
constructorArgs []byte
}
func (p *loadbotParams) validateFlags() error {
// check if valid mode is selected
if err := p.isValidMode(); err != nil {
return err
}
// validate the correct mode params
if err := p.hasValidDeployParams(); err != nil {
return err
}
return nil
}
func (p *loadbotParams) initRawParams() error {
if err := p.initGasValues(); err != nil {
return err
}
if err := p.initAddressValues(); err != nil {
return err
}
if err := p.initTxnValue(); err != nil {
return err
}
if err := p.initContract(); err != nil {
return err
}
if err := p.initContractArtifactAndArgs(); err != nil {
return err
}
return nil
}
func (p *loadbotParams) initGasValues() error {
var parseErr error
// Parse the gas price
if p.gasPriceRaw != "" {
if p.gasPrice, parseErr = types.ParseUint256orHex(&p.gasPriceRaw); parseErr != nil {
return fmt.Errorf("failed to decode gas price to value: %w", parseErr)
}
}
// Parse the gas limit
if p.gasLimitRaw != "" {
if p.gasLimit, parseErr = types.ParseUint256orHex(&p.gasLimitRaw); parseErr != nil {
return fmt.Errorf("failed to decode gas limit to value: %w", parseErr)
}
}
return nil
}
func (p *loadbotParams) initAddressValues() error {
if err := p.sender.UnmarshalText([]byte(p.senderRaw)); err != nil {
return fmt.Errorf("failed to decode sender address: %w", err)
}
if err := p.initReceiverAddress(); err != nil {
return fmt.Errorf("failed to decode receiver address: %w", err)
}
return nil
}
func (p *loadbotParams) initReceiverAddress() error {
if p.receiverRaw == "" {
// No receiving address specified,
// use the sender address as the receiving address
p.receiver = p.sender
return nil
}
return p.receiver.UnmarshalText([]byte(p.receiverRaw))
}
func (p *loadbotParams) initTxnValue() error {
value, err := types.ParseUint256orHex(&p.valueRaw)
if err != nil {
return fmt.Errorf("failed to decode to value: %w", err)
}
p.value = value
return nil
}
func (p *loadbotParams) initContract() error {
var readErr error
p.contractArtifact = &generator.ContractArtifact{
Bytecode: generator.DefaultContractBytecode,
}
if p.contractPath != "" {
if p.contractArtifact, readErr = generator.ReadContractArtifact(
p.contractPath,
); readErr != nil {
return fmt.Errorf(
"failed to read contract bytecode: %w",
readErr,
)
}
}
return nil
}
func (p *loadbotParams) getRequiredFlags() []string {
return []string{
senderFlag,
}
}
func (p *loadbotParams) generateConfig(
jsonRPCAddress string,
grpcAddress string,
) *Configuration {
return &Configuration{
TPS: p.tps,
Sender: p.sender,
Receiver: p.receiver,
Count: p.count,
Value: p.value,
JSONRPC: jsonRPCAddress,
GRPC: grpcAddress,
MaxConns: int(p.maxConns),
GeneratorMode: p.mode,
ChainID: p.chainID,
GasPrice: p.gasPrice,
GasLimit: p.gasLimit,
ContractArtifact: p.contractArtifact,
ConstructorArgs: p.constructorArgs,
MaxWait: p.maxWait,
}
}
func (p *loadbotParams) isValidMode() error {
// Set and validate the correct mode type
p.mode = Mode(strings.ToLower(p.modeRaw))
switch p.mode {
case transfer, deploy, erc20, erc721:
return nil
default:
return errInvalidMode
}
}
func (p *loadbotParams) hasValidDeployParams() error {
// fail if mode is deploy but we have no contract
if p.mode == deploy && p.contractPath == "" {
return errContractPath
}
return nil
}
func (p *loadbotParams) initContractArtifactAndArgs() error {
var (
ctrArtifact *generator.ContractArtifact
ctrArgs []byte
err error
)
switch p.mode {
case erc20:
ctrArtifact = &generator.ContractArtifact{
Bytecode: ERC20BIN,
ABI: abi.MustNewABI(ERC20ABI),
}
if ctrArgs, err = abi.Encode(
[]string{erc20TokenSupply, erc20TokenName, erc20TokenSymbol}, ctrArtifact.ABI.Constructor.Inputs); err != nil {
return fmt.Errorf("failed to encode erc20 constructor parameters: %w", err)
}
case erc721:
ctrArtifact = &generator.ContractArtifact{
Bytecode: ERC721BIN,
ABI: abi.MustNewABI(ERC721ABI),
}
if ctrArgs, err = abi.Encode(
[]string{erc721TokenName, erc721TokenSymbol},
ctrArtifact.ABI.Constructor.Inputs); err != nil {
return fmt.Errorf("failed to encode erc721 constructor parameters: %w", err)
}
default:
ctrArtifact = &generator.ContractArtifact{
Bytecode: generator.DefaultContractBytecode,
}
ctrArgs = nil
}
p.contractArtifact = ctrArtifact
p.constructorArgs = ctrArgs
return nil
}