Repository navigation
Expand file tree
/
Copy pathtestnet.go
More file actions
528 lines (444 loc) · 17.2 KB
/
Copy pathtestnet.go
File metadata and controls
528 lines (444 loc) · 17.2 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package client
// DONTCOVER
import (
"bufio"
"encoding/json"
"fmt"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/okex/exchain/app/crypto/hd"
ethermint "github.com/okex/exchain/app/types"
"github.com/okex/exchain/libs/cosmos-sdk/client/flags"
clientkeys "github.com/okex/exchain/libs/cosmos-sdk/client/keys"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
"github.com/okex/exchain/libs/cosmos-sdk/crypto/keys"
"github.com/okex/exchain/libs/cosmos-sdk/server"
srvconfig "github.com/okex/exchain/libs/cosmos-sdk/server/config"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/cosmos-sdk/types/module"
authexported "github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported"
authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types"
"github.com/okex/exchain/libs/cosmos-sdk/x/crisis"
genutiltypes "github.com/okex/exchain/libs/cosmos-sdk/x/genutil/types"
"github.com/okex/exchain/libs/cosmos-sdk/x/mint"
tmconfig "github.com/okex/exchain/libs/tendermint/config"
tmcrypto "github.com/okex/exchain/libs/tendermint/crypto"
tmos "github.com/okex/exchain/libs/tendermint/libs/os"
tmrand "github.com/okex/exchain/libs/tendermint/libs/rand"
tmtypes "github.com/okex/exchain/libs/tendermint/types"
tmtime "github.com/okex/exchain/libs/tendermint/types/time"
"github.com/okex/exchain/x/common"
"github.com/okex/exchain/x/genutil"
"github.com/okex/exchain/x/gov"
stakingtypes "github.com/okex/exchain/x/staking/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net"
"os"
"path/filepath"
)
var (
flagNodeDirPrefix = "node-dir-prefix"
flagNumValidators = "v"
flagOutputDir = "output-dir"
flagNodeDaemonHome = "node-daemon-home"
flagNodeCLIHome = "node-cli-home"
flagStartingIPAddress = "starting-ip-address"
flagCoinDenom = "coin-denom"
flagKeyAlgo = "algo"
flagIPAddrs = "ip-addrs"
flagBaseport = "base-port"
flagLocal = "local"
flagEqualVotingPower = "equal-voting-power"
flagNumRPCs = "r"
)
const nodeDirPerm = 0755
// mnemonicList contains some hard-coded mnemonic (generated by entropy bytes that bit size is 128).
var mnemonicList = []string{
"puzzle glide follow cruel say burst deliver wild tragic galaxy lumber offer",
"palace cube bitter light woman side pave cereal donor bronze twice work",
"antique onion adult slot sad dizzy sure among cement demise submit scare",
"lazy cause kite fence gravity regret visa fuel tone clerk motor rent",
}
// TestnetCmd initializes all files for tendermint testnet and application
func TestnetCmd(ctx *server.Context, cdc *codec.Codec,
mbm module.BasicManager, genAccIterator authtypes.GenesisAccountIterator,
) *cobra.Command {
cmd := &cobra.Command{
Use: "testnet",
Short: "Initialize files for an OKExChain testnet",
Long: `testnet will create "v" number of directories and populate each with
necessary files (private validator, genesis, config, etc.).
Note, strict routability for addresses is turned off in the config file.`,
Example: "exchaind testnet --v 4 --keyring-backend test --output-dir ./output --starting-ip-address 192.168.10.2",
RunE: func(cmd *cobra.Command, _ []string) error {
config := ctx.Config
outputDir, _ := cmd.Flags().GetString(flagOutputDir)
keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend)
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
minGasPrices, _ := cmd.Flags().GetString(server.FlagMinGasPrices)
nodeDirPrefix, _ := cmd.Flags().GetString(flagNodeDirPrefix)
nodeDaemonHome, _ := cmd.Flags().GetString(flagNodeDaemonHome)
nodeCLIHome, _ := cmd.Flags().GetString(flagNodeCLIHome)
startingIPAddress, _ := cmd.Flags().GetString(flagStartingIPAddress)
ipAddresses, _ := cmd.Flags().GetStringSlice(flagIPAddrs)
numValidators, _ := cmd.Flags().GetInt(flagNumValidators)
numRPCs, _ := cmd.Flags().GetInt(flagNumRPCs)
coinDenom, _ := cmd.Flags().GetString(flagCoinDenom)
algo, _ := cmd.Flags().GetString(flagKeyAlgo)
isLocal := viper.GetBool(flagLocal)
isEqualVotingPower, _ := cmd.Flags().GetBool(flagEqualVotingPower)
return InitTestnet(
cmd, config, cdc, mbm, genAccIterator, outputDir, chainID, coinDenom, minGasPrices,
nodeDirPrefix, nodeDaemonHome, nodeCLIHome, startingIPAddress, ipAddresses, keyringBackend,
algo, numValidators, isLocal, numRPCs, isEqualVotingPower)
},
}
cmd.Flags().Int(flagNumValidators, 4, "Number of validators to initialize the testnet with")
cmd.Flags().StringP(flagOutputDir, "o", "./build", "Directory to store initialization data for the testnet")
cmd.Flags().String(flagNodeDirPrefix, "node", "Prefix the directory name for each node with (node results in node0, node1, ...)")
cmd.Flags().String(flagNodeDaemonHome, "exchaind", "Home directory of the node's daemon configuration")
cmd.Flags().String(flagNodeCLIHome, "exchaincli", "Home directory of the node's cli configuration")
cmd.Flags().String(flagStartingIPAddress, "192.168.0.1", "Starting IP address (192.168.0.1 results in persistent peers list ID0@192.168.0.1:46656, ID1@192.168.0.2:46656, ...)")
cmd.Flags().StringSlice(flagIPAddrs, []string{}, "List of IP addresses to use (i.e. `192.168.0.1,172.168.0.1` results in persistent peers list ID0@192.168.0.1:46656, ID1@172.168.0.1)")
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().String(flagCoinDenom, ethermint.NativeToken, "Coin denomination used for staking, governance, mint, crisis and evm parameters")
cmd.Flags().String(server.FlagMinGasPrices, fmt.Sprintf("0.000006%s", ethermint.NativeToken), "Minimum gas prices to accept for transactions; All fees in a tx must meet this minimum (e.g. 0.01aphoton,0.001stake)")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|test)")
cmd.Flags().String(flagKeyAlgo, string(hd.EthSecp256k1), "Key signing algorithm to generate keys for")
cmd.Flags().Int(flagBaseport, 26656, "testnet base port")
cmd.Flags().BoolP(flagLocal, "l", false, "run all nodes on local host")
cmd.Flags().Int(flagNumRPCs, 0, "Number of RPC nodes to initialize the testnet with")
cmd.Flags().BoolP(flagEqualVotingPower, "", false, "Create validators with equal voting power")
return cmd
}
// InitTestnet initializes the testnet configuration
// 1.initializes the "validator" node configuration;
// 2.based the "validator" node, initializes the "rpc" node.
func InitTestnet(
cmd *cobra.Command,
config *tmconfig.Config,
cdc *codec.Codec,
mbm module.BasicManager,
genAccIterator authtypes.GenesisAccountIterator,
outputDir,
chainID,
coinDenom,
minGasPrices,
nodeDirPrefix,
nodeDaemonHome,
nodeCLIHome,
startingIPAddress string,
ipAddresses []string,
keyringBackend,
algo string,
numValidators int,
isLocal bool,
numRPCs int,
isEqualVotingPower bool,
) error {
if chainID == "" {
chainID = fmt.Sprintf("exchain-%d", tmrand.Int63n(9999999999999)+1)
}
if !ethermint.IsValidChainID(chainID) {
return fmt.Errorf("invalid chain-id: %s", chainID)
}
if err := sdk.ValidateDenom(coinDenom); err != nil {
return err
}
numNodes := numValidators + numRPCs
if len(ipAddresses) != 0 {
numNodes = len(ipAddresses)
}
nodeIDs := make([]string, numNodes)
valPubKeys := make([]tmcrypto.PubKey, numNodes)
simappConfig := srvconfig.DefaultConfig()
simappConfig.MinGasPrices = minGasPrices
var (
genAccounts []authexported.GenesisAccount
genFiles []string
)
inBuf := bufio.NewReader(cmd.InOrStdin())
// generate private keys, node IDs, and initial transactions
for i := 0; i < numNodes; i++ {
nodeDirName := fmt.Sprintf("%s%d", nodeDirPrefix, i)
nodeDir := filepath.Join(outputDir, nodeDirName, nodeDaemonHome)
clientDir := filepath.Join(outputDir, nodeDirName, nodeCLIHome)
gentxsDir := filepath.Join(outputDir, "gentxs")
// generate private keys, node IDs for all nodes
config.SetRoot(nodeDir)
config.RPC.ListenAddress = "tcp://0.0.0.0:26657"
if err := os.MkdirAll(filepath.Join(nodeDir, "config"), nodeDirPerm); err != nil {
_ = os.RemoveAll(outputDir)
return err
}
config.Moniker = nodeDirName
var err error
nodeIDs[i], valPubKeys[i], err = genutil.InitializeNodeValidatorFilesByIndex(config, i)
if err != nil {
_ = os.RemoveAll(outputDir)
return err
}
genFiles = append(genFiles, config.GenesisFile())
if i >= numValidators {
// rpc nodes do not need to add initial transactions, key_seeds etc.
continue
}
// validator nodes add initial transactions
if err := os.MkdirAll(clientDir, nodeDirPerm); err != nil {
_ = os.RemoveAll(outputDir)
return err
}
var ip string
port := viper.GetInt(flagBaseport)
if isLocal {
ip, err = getIP(0, startingIPAddress)
port += i * 100
} else {
if len(ipAddresses) == 0 {
ip, err = getIP(i, startingIPAddress)
if err != nil {
_ = os.RemoveAll(outputDir)
return err
}
} else {
ip = ipAddresses[i]
}
}
memo := fmt.Sprintf("%s@%s:%d", nodeIDs[i], ip, port)
kb, err := keys.NewKeyring(
sdk.KeyringServiceName(),
keyringBackend,
clientDir,
inBuf,
hd.EthSecp256k1Options()...,
)
if err != nil {
return err
}
cmd.Printf(
"Password for account '%s' :\n", nodeDirName,
)
keyPass := clientkeys.DefaultKeyPass
mnemonic := ""
if i < len(mnemonicList) {
mnemonic = mnemonicList[i]
}
addr, secret, err := GenerateSaveCoinKey(kb, nodeDirName, keyPass, true, keys.SigningAlgo(algo), mnemonic)
if err != nil {
_ = os.RemoveAll(outputDir)
return err
}
fmt.Printf("nodeDir: %s\nnodeDirName: %s\naddr: %s\nmnenonics: %s\n--------------------------------------\n",
clientDir, nodeDirName, addr, secret)
info := map[string]string{"secret": secret}
cliPrint, err := json.Marshal(info)
if err != nil {
return err
}
// save private key seed words
if err := writeFile(fmt.Sprintf("%v.json", "key_seed"), clientDir, cliPrint); err != nil {
return err
}
coins := sdk.NewCoins(
sdk.NewCoin(coinDenom, sdk.NewDec(9000000)),
)
genAccounts = append(genAccounts, ethermint.EthAccount{
BaseAccount: authtypes.NewBaseAccount(addr, coins, nil, 0, 0),
CodeHash: ethcrypto.Keccak256(nil),
})
//make and save create validator tx
sequence := uint64(0)
msgCreateVal := stakingtypes.NewMsgCreateValidator(
sdk.ValAddress(addr), valPubKeys[i],
stakingtypes.NewDescription(nodeDirName, "", "", ""),
sdk.NewDecCoinFromDec(common.NativeToken, stakingtypes.DefaultMinSelfDelegation),
)
if err := makeTxAndWriteFile(msgCreateVal, inBuf, chainID, kb, nodeDirName, sequence, memo, cdc, gentxsDir, outputDir); err != nil {
return err
}
if !isEqualVotingPower {
//make and save deposit tx
sequence++
msgDeposit := stakingtypes.NewMsgDeposit(addr, sdk.NewDecCoinFromDec(common.NativeToken, sdk.NewDec(10000*int64(i+1))))
if err := makeTxAndWriteFile(msgDeposit, inBuf, chainID, kb, nodeDirName, sequence, "", cdc, gentxsDir, outputDir); err != nil {
return err
}
//make and save add shares tx
sequence++
msgAddShares := stakingtypes.NewMsgAddShares(addr, []sdk.ValAddress{sdk.ValAddress(addr)})
if err := makeTxAndWriteFile(msgAddShares, inBuf, chainID, kb, nodeDirName, sequence, "", cdc, gentxsDir, outputDir); err != nil {
return err
}
}
srvconfig.WriteConfigFile(filepath.Join(nodeDir, "config/app.toml"), simappConfig)
}
if err := initGenFiles(cdc, mbm, chainID, coinDenom, genAccounts, genFiles, numNodes); err != nil {
return err
}
err := collectGenFiles(
cdc, config, chainID, nodeIDs, valPubKeys, numNodes,
outputDir, nodeDirPrefix, nodeDaemonHome, genAccIterator,
)
if err != nil {
return err
}
cmd.Printf("Successfully initialized %d validator nodes directories, %d rpc nodes directories\n", numValidators, numRPCs)
return nil
}
func makeTxAndWriteFile(msg sdk.Msg, inBuf *bufio.Reader, chainID string, kb keys.Keybase,
nodeDirName string, sequence uint64, memo string, cdc *codec.Codec, gentxsDir string,
outputDir string) error {
tx := authtypes.NewStdTx([]sdk.Msg{msg}, authtypes.StdFee{}, []authtypes.StdSignature{}, memo)
txBldr := authtypes.NewTxBuilderFromCLI(inBuf).WithChainID(chainID).WithMemo(memo).WithKeybase(kb).WithSequence(sequence)
signedTx, err := txBldr.SignStdTx(nodeDirName, clientkeys.DefaultKeyPass, tx, false)
if err != nil {
_ = os.RemoveAll(outputDir)
return err
}
txBytes, err := cdc.MarshalJSON(signedTx)
if err != nil {
_ = os.RemoveAll(outputDir)
return err
}
// gather gentxs folder
if err := writeFile(fmt.Sprintf("%v-%d.json", nodeDirName, sequence), gentxsDir, txBytes); err != nil {
_ = os.RemoveAll(outputDir)
return err
}
return nil
}
func initGenFiles(
cdc *codec.Codec, mbm module.BasicManager,
chainID, coinDenom string,
genAccounts []authexported.GenesisAccount,
genFiles []string, numValidators int,
) error {
appGenState := mbm.DefaultGenesis()
// set the accounts in the genesis state
var authGenState authtypes.GenesisState
cdc.MustUnmarshalJSON(appGenState[authtypes.ModuleName], &authGenState)
authGenState.Accounts = genAccounts
appGenState[authtypes.ModuleName] = cdc.MustMarshalJSON(authGenState)
var govGenState gov.GenesisState
cdc.MustUnmarshalJSON(appGenState[gov.ModuleName], &govGenState)
govGenState.DepositParams.MinDeposit[0].Denom = coinDenom
appGenState[gov.ModuleName] = cdc.MustMarshalJSON(govGenState)
var mintGenState mint.GenesisState
cdc.MustUnmarshalJSON(appGenState[mint.ModuleName], &mintGenState)
mintGenState.Params.MintDenom = coinDenom
appGenState[mint.ModuleName] = cdc.MustMarshalJSON(mintGenState)
var crisisGenState crisis.GenesisState
cdc.MustUnmarshalJSON(appGenState[crisis.ModuleName], &crisisGenState)
crisisGenState.ConstantFee.Denom = coinDenom
appGenState[crisis.ModuleName] = cdc.MustMarshalJSON(crisisGenState)
appGenStateJSON, err := codec.MarshalJSONIndent(cdc, appGenState)
if err != nil {
return err
}
genDoc := tmtypes.GenesisDoc{
ChainID: chainID,
AppState: appGenStateJSON,
Validators: nil,
}
// generate empty genesis files for each validator and save
for i := 0; i < numValidators; i++ {
if err := genDoc.SaveAs(genFiles[i]); err != nil {
return err
}
}
return nil
}
// GenerateSaveCoinKey returns the address of a public key, along with the secret
// phrase to recover the private key.
func GenerateSaveCoinKey(keybase keys.Keybase, keyName, keyPass string, overwrite bool, algo keys.SigningAlgo, mnemonic string) (sdk.AccAddress, string, error) {
// ensure no overwrite
if !overwrite {
_, err := keybase.Get(keyName)
if err == nil {
return sdk.AccAddress([]byte{}), "", fmt.Errorf(
"key already exists, overwrite is disabled")
}
}
// generate a private key, with recovery phrase
// If mnemonic is not "", secret is this mnemonic, or secret is random mnemonic.
info, secret, err := keybase.CreateMnemonic(keyName, keys.English, keyPass, algo, mnemonic)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
}
func collectGenFiles(
cdc *codec.Codec, config *tmconfig.Config, chainID string,
nodeIDs []string, valPubKeys []tmcrypto.PubKey,
numValidators int, outputDir, nodeDirPrefix, nodeDaemonHome string,
genAccIterator authtypes.GenesisAccountIterator,
) error {
var appState json.RawMessage
genTime := tmtime.Now()
for i := 0; i < numValidators; i++ {
nodeDirName := fmt.Sprintf("%s%d", nodeDirPrefix, i)
nodeDir := filepath.Join(outputDir, nodeDirName, nodeDaemonHome)
gentxsDir := filepath.Join(outputDir, "gentxs")
config.Moniker = nodeDirName
config.SetRoot(nodeDir)
// set node's port
port := viper.GetInt(flagBaseport)
p2pPort := port + i*100
rpcPort := port + i*100 + 1
config.P2P.ListenAddress = fmt.Sprintf("tcp://0.0.0.0:%d", p2pPort)
config.RPC.ListenAddress = fmt.Sprintf("tcp://0.0.0.0:%d", rpcPort)
nodeID, valPubKey := nodeIDs[i], valPubKeys[i]
initCfg := genutiltypes.NewInitConfig(chainID, gentxsDir, nodeID, nodeID, valPubKey)
genDoc, err := tmtypes.GenesisDocFromFile(config.GenesisFile())
if err != nil {
return err
}
nodeAppState, err := genutil.GenAppStateFromConfig(cdc, config, initCfg, *genDoc, genAccIterator)
if err != nil {
return err
}
if appState == nil {
// set the canonical application state (they should not differ)
appState = nodeAppState
}
genFile := config.GenesisFile()
// overwrite each validator's genesis file to have a canonical genesis time
if err := genutil.ExportGenesisFileWithTime(genFile, chainID, nil, appState, genTime); err != nil {
return err
}
}
return nil
}
func getIP(i int, startingIPAddr string) (ip string, err error) {
if len(startingIPAddr) == 0 {
ip, err = server.ExternalIP()
if err != nil {
return "", err
}
return ip, nil
}
return calculateIP(startingIPAddr, i)
}
func calculateIP(ip string, i int) (string, error) {
ipv4 := net.ParseIP(ip).To4()
if ipv4 == nil {
return "", fmt.Errorf("%v: non ipv4 address", ip)
}
for j := 0; j < i; j++ {
ipv4[3]++
}
return ipv4.String(), nil
}
func writeFile(name string, dir string, contents []byte) error {
writePath := filepath.Join(dir)
file := filepath.Join(writePath, name)
err := tmos.EnsureDir(writePath, 0755)
if err != nil {
return err
}
err = tmos.WriteFile(file, contents, 0644)
if err != nil {
return err
}
return nil
}