-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerator.go
More file actions
283 lines (232 loc) · 8.67 KB
/
generator.go
File metadata and controls
283 lines (232 loc) · 8.67 KB
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
package stresser
import (
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strings"
tmed25519 "github.com/cometbft/cometbft/crypto/ed25519"
"github.com/InjectiveLabs/chain-stresser/v2/chain"
)
type GeneratorEnvironment struct {
ChainID string
EthChainID int
EvmEnabled bool
NumOfValidators int
NumOfSentryNodes int
NumOfInstances int
NumOfAccountsPerInstance int
DockerImage string
DockerSubnet string
OutDirectory string
ProdLike bool
Debug bool
LocalNative bool
}
const (
bondDenom = "inj"
// initialBalanceStaker to be 100K INJ = 100000 * 10^18 inj
initialBalanceStaker = "100000000000000000000000" + bondDenom
// initialBalanceBonded to be 10K INJ = 10000 * 10^18 inj
initialBalanceBonded = "10000000000000000000000" + bondDenom
// initialBalanceAccount to be 1M INJ = 1000000 * 10^18 inj
initialBalanceAccount = "1000000000000000000000000" + bondDenom
// minimumGasPrices to be used for realistic bench (involving x/distribition)
minimumGasPrices = "1inj"
txfeesMinimumGasPrices = "1"
)
func GenerateConfigs(
env GeneratorEnvironment,
) {
if env.NumOfInstances <= 0 {
panic("number of instances must be greater than 0")
}
if env.NumOfValidators <= 0 {
panic("number of validators must be greater than 0")
}
rootOutDir := env.OutDirectory + "/chain-stresser-deploy"
if err := os.RemoveAll(rootOutDir); err != nil && !os.IsNotExist(err) {
panic(err)
}
genesis := chain.NewGenesis(&chain.GenesisConfig{
ChainID: env.ChainID,
EthChainID: env.EthChainID,
EvmEnabled: env.EvmEnabled,
ProdLike: env.ProdLike,
MinimumGasPrices: txfeesMinimumGasPrices,
})
persistentValidatorPeers := make([]string, 0, env.NumOfValidators)
validatorNodeIDs := make([]string, 0, env.NumOfValidators)
allNodeConfigs := make([]chain.NodeConfig, 0, env.NumOfValidators+env.NumOfSentryNodes)
for i := 0; i < env.NumOfValidators; i++ {
nodePrivateKey := tmed25519.GenPrivKey()
validatorPrivateKey := tmed25519.GenPrivKey()
stakerPublicKey, stakerPrivateKey := chain.GenerateSecp256k1Key()
relativeNodeDir := fmt.Sprintf("validators/%d", i)
valDir := fmt.Sprintf("%s/%s", rootOutDir, relativeNodeDir)
txIndexerKind := chain.TxIndexerKV
// if env.ProdLike {
// txIndexerKind = chain.TxIndexerDisabled
// }
nodeIPAddr := net.IPv4(127, 0, 0, 1)
if !env.LocalNative {
nodeIPAddr = ipInSubnet(env.DockerSubnet, i+1)
}
nodePorts := chain.DefaultPorts
if env.LocalNative {
nodePorts = shiftPorts(nodePorts, i)
}
nodeConfig := &chain.NodeConfig{
Home: fmt.Sprintf("./%s", relativeNodeDir),
Moniker: fmt.Sprintf("validator-%d", i),
PeerID: chain.NodeID(nodePrivateKey.PubKey()),
IPListen: net.IPv4zero,
IPAddr: nodeIPAddr,
Ports: nodePorts,
NodeKey: nodePrivateKey,
ValidatorKey: validatorPrivateKey,
ProdLike: env.ProdLike,
TxIndexer: txIndexerKind,
// DiscardABCIResponses: env.ProdLike, // discard in prod
PortsExposed: env.NumOfSentryNodes == 0 && i == 0, // expose ports only for the first validator (and no sentry nodes)
}
nodeConfig.Save(valDir)
allNodeConfigs = append(allNodeConfigs, *nodeConfig)
validatorNodeIDs = append(validatorNodeIDs, nodeConfig.PeerID)
persistentValidatorPeers = append(persistentValidatorPeers,
fmt.Sprintf("%s@%s:%d", validatorNodeIDs[i], nodeConfig.IPAddr.String(), nodeConfig.Ports.P2P),
)
appConfig := &chain.AppConfig{
MinimumGasPrices: minimumGasPrices,
EVMEnabled: env.EvmEnabled,
ProdLike: env.ProdLike,
IPListen: net.IPv4zero,
Ports: nodePorts,
PortsExposed: env.NumOfSentryNodes == 0 && i == 0, // expose ports only for the first validator (and no sentry nodes)
}
appConfig.Save(valDir)
genesis.AddAccount(stakerPublicKey.Address(), initialBalanceStaker)
genesis.AddValidator(validatorPrivateKey.PubKey(), stakerPrivateKey, initialBalanceBonded)
}
orPanic(os.WriteFile(rootOutDir+"/validators/ids.json", bytesOrPanic(json.Marshal(validatorNodeIDs)), 0o644))
for i := 0; i < env.NumOfInstances; i++ {
accounts := make([]chain.Secp256k1PrivateKey, 0, env.NumOfAccountsPerInstance)
for j := 0; j < env.NumOfAccountsPerInstance; j++ {
accountPublicKey, accountPrivateKey := chain.GenerateSecp256k1Key()
accounts = append(accounts, accountPrivateKey)
genesis.AddAccount(accountPublicKey.Address(), initialBalanceAccount)
}
instanceDir := fmt.Sprintf("%s/instances/%d", rootOutDir, i)
orPanic(os.MkdirAll(instanceDir, 0o755))
accountsJSON := bytesOrPanic(json.Marshal(accounts))
orPanic(os.WriteFile(instanceDir+"/accounts.json", accountsJSON, 0o644))
}
for i := 0; i < env.NumOfValidators; i++ {
genesis.Save(fmt.Sprintf("%s/validators/%d", rootOutDir, i))
}
if env.NumOfSentryNodes > 0 {
sentryNodeIDs := make([]string, 0, env.NumOfSentryNodes)
for i := 0; i < env.NumOfSentryNodes; i++ {
nodePrivateKey := tmed25519.GenPrivKey()
relativeNodeDir := fmt.Sprintf("sentry-nodes/%d", i)
nodeDir := fmt.Sprintf("%s/%s", rootOutDir, relativeNodeDir)
nodeConfig := &chain.NodeConfig{
Home: fmt.Sprintf("./%s", relativeNodeDir),
Moniker: fmt.Sprintf("sentry-node-%d", i),
PeerID: chain.NodeID(nodePrivateKey.PubKey()),
IPListen: net.IPv4zero,
IPAddr: ipInSubnet(env.DockerSubnet, env.NumOfValidators+i+1),
Ports: chain.DefaultPorts,
NodeKey: nodePrivateKey,
ProdLike: env.ProdLike,
TxIndexer: chain.TxIndexerKV,
DiscardABCIResponses: false,
PortsExposed: i == 0, // expose ports only for the first sentry node
DependsOn: makeIntRange(0, env.NumOfValidators),
PersistentPeers: strings.Join(persistentValidatorPeers, ","),
PrivatePeerIds: strings.Join(validatorNodeIDs[:env.NumOfValidators], ","),
}
allNodeConfigs = append(allNodeConfigs, *nodeConfig)
appConfig := &chain.AppConfig{
MinimumGasPrices: minimumGasPrices,
EVMEnabled: env.EvmEnabled,
ProdLike: env.ProdLike,
IPListen: net.IPv4zero,
Ports: chain.DefaultPorts,
PortsExposed: i == 0, // expose ports only for the first sentry node
}
nodeConfig.Save(nodeDir)
appConfig.Save(nodeDir)
genesis.Save(nodeDir)
sentryNodeIDs = append(sentryNodeIDs, nodeConfig.PeerID)
}
idsJSON := bytesOrPanic(json.Marshal(sentryNodeIDs))
orPanic(os.WriteFile(rootOutDir+"/sentry-nodes/ids.json", idsJSON, 0o644))
}
for i := 0; i < env.NumOfValidators; i++ {
nodeConfig := allNodeConfigs[i]
peerAddress := fmt.Sprintf("%s@%s:%d", validatorNodeIDs[i], nodeConfig.IPAddr.String(), nodeConfig.Ports.P2P)
nodeConfig.PersistentPeers = strings.Join(filterStringValue(persistentValidatorPeers, peerAddress), ",")
nodeConfig.PrivatePeerIds = strings.Join(validatorNodeIDs, ",")
allNodeConfigs[i] = nodeConfig
// save the node config again, for every validator
nodeConfig.Save(filepath.Join(rootOutDir, nodeConfig.Home))
}
if !env.LocalNative {
chain.GenerateDockerCompose(
env.ChainID,
env.DockerImage,
env.DockerSubnet,
allNodeConfigs,
rootOutDir,
env.Debug,
)
}
}
func filterStringValue(list []string, filter string) []string {
newList := make([]string, 0, len(list))
for _, v := range list {
if v != filter {
newList = append(newList, v)
}
}
return newList
}
func ipInSubnet(subnet string, offset int) net.IP {
_, ipNet, err := net.ParseCIDR(subnet)
if err != nil {
panic(err)
}
// Get the first IP in subnet
ip := ipNet.IP.To4()
if ip == nil {
panic("only IPv4 subnets supported")
}
// Add offset to last octet
ip[3] += byte(offset + 1) // +1 because we want to start from the second IP in the subnet (not gateway)
return ip
}
func shiftPorts(ports chain.Ports, offset int) chain.Ports {
// digitOffset is 100 then 26657 -> 26757 -> 26857 -> 26957
const digitOffset = 100
return chain.Ports{
RPC: ports.RPC + digitOffset*offset,
P2P: ports.P2P + digitOffset*offset,
API: ports.API + digitOffset*offset,
GRPC: ports.GRPC + digitOffset*offset,
GRPCWeb: ports.GRPCWeb + digitOffset*offset,
PProf: ports.PProf + digitOffset*offset,
Prometheus: ports.Prometheus + digitOffset*offset,
EVMRPC: ports.EVMRPC + digitOffset*offset,
EVMWSPort: ports.EVMWSPort + digitOffset*offset,
ProxyApp: ports.ProxyApp + digitOffset*offset,
}
}
func makeIntRange(start, end int) []int {
r := make([]int, end-start)
for i := range r {
r[i] = start + i
}
return r
}