Reduce code complexity of testnet command

This commit is contained in:
ValarDragon 2018-07-09 15:00:55 -07:00
parent e906272ca0
commit 592419c83a
1 changed files with 39 additions and 30 deletions

View File

@ -60,10 +60,11 @@ Example:
} }
func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) error { func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) error {
outDir := viper.GetString(outputDir) outDir := viper.GetString(outputDir)
numValidators := viper.GetInt(nValidators)
// Generate private key, node ID, initial transaction // Generate private key, node ID, initial transaction
for i := 0; i < viper.GetInt(nValidators); i++ { for i := 0; i < numValidators; i++ {
nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i)
nodeDir := filepath.Join(outDir, nodeDirName, "gaiad") nodeDir := filepath.Join(outDir, nodeDirName, "gaiad")
clientDir := filepath.Join(outDir, nodeDirName, "gaiacli") clientDir := filepath.Join(outDir, nodeDirName, "gaiacli")
@ -83,18 +84,9 @@ func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) err
} }
config.Moniker = nodeDirName config.Moniker = nodeDirName
ip, err := getIP(i)
ip := viper.GetString(startingIPAddress) if err != nil {
if len(ip) == 0 { return err
ip, err = externalIP()
if err != nil {
return err
}
} else {
ip, err = calculateIP(ip, i)
if err != nil {
return err
}
} }
genTxConfig := gc.GenTx{ genTxConfig := gc.GenTx{
@ -112,35 +104,22 @@ func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) err
// Save private key seed words // Save private key seed words
name := fmt.Sprintf("%v.json", "key_seed") name := fmt.Sprintf("%v.json", "key_seed")
writePath := filepath.Join(clientDir) err = writeFile(name, clientDir, cliPrint)
file := filepath.Join(writePath, name)
err = cmn.EnsureDir(writePath, 0700)
if err != nil {
return err
}
err = cmn.WriteFile(file, cliPrint, 0600)
if err != nil { if err != nil {
return err return err
} }
// Gather gentxs folder // Gather gentxs folder
name = fmt.Sprintf("%v.json", nodeDirName) name = fmt.Sprintf("%v.json", nodeDirName)
writePath = filepath.Join(gentxsDir) err = writeFile(name, gentxsDir, genTxFile)
file = filepath.Join(writePath, name)
err = cmn.EnsureDir(writePath, 0700)
if err != nil { if err != nil {
return err return err
} }
err = cmn.WriteFile(file, genTxFile, 0644)
if err != nil {
return err
}
} }
// Generate genesis.json and config.toml // Generate genesis.json and config.toml
chainID := "chain-" + cmn.RandStr(6) chainID := "chain-" + cmn.RandStr(6)
for i := 0; i < viper.GetInt(nValidators); i++ { for i := 0; i < numValidators; i++ {
nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i) nodeDirName := fmt.Sprintf("%s%d", viper.GetString(nodeDirPrefix), i)
nodeDir := filepath.Join(outDir, nodeDirName, "gaiad") nodeDir := filepath.Join(outDir, nodeDirName, "gaiad")
@ -165,6 +144,36 @@ func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) err
return nil return nil
} }
func getIP(i int) (ip string, err error) {
ip = viper.GetString(startingIPAddress)
if len(ip) == 0 {
ip, err = externalIP()
if err != nil {
return "", err
}
} else {
ip, err = calculateIP(ip, i)
if err != nil {
return "", err
}
}
return ip, nil
}
func writeFile(name string, dir string, contents []byte) error {
writePath := filepath.Join(dir)
file := filepath.Join(writePath, name)
err := cmn.EnsureDir(writePath, 0700)
if err != nil {
return err
}
err = cmn.WriteFile(file, contents, 0600)
if err != nil {
return err
}
return nil
}
func calculateIP(ip string, i int) (string, error) { func calculateIP(ip string, i int) (string, error) {
ipv4 := net.ParseIP(ip).To4() ipv4 := net.ParseIP(ip).To4()
if ipv4 == nil { if ipv4 == nil {