From 592419c83a1b804eb925c31c628c16d306f5df2d Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Mon, 9 Jul 2018 15:00:55 -0700 Subject: [PATCH] Reduce code complexity of testnet command --- server/testnet.go | 69 ++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/server/testnet.go b/server/testnet.go index d102b87cc..d7e4ec9ac 100644 --- a/server/testnet.go +++ b/server/testnet.go @@ -60,10 +60,11 @@ Example: } func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) error { - outDir := viper.GetString(outputDir) + numValidators := viper.GetInt(nValidators) + // 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) nodeDir := filepath.Join(outDir, nodeDirName, "gaiad") clientDir := filepath.Join(outDir, nodeDirName, "gaiacli") @@ -83,18 +84,9 @@ func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) err } config.Moniker = nodeDirName - - 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 - } + ip, err := getIP(i) + if err != nil { + return err } genTxConfig := gc.GenTx{ @@ -112,35 +104,22 @@ func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) err // Save private key seed words name := fmt.Sprintf("%v.json", "key_seed") - writePath := filepath.Join(clientDir) - file := filepath.Join(writePath, name) - err = cmn.EnsureDir(writePath, 0700) - if err != nil { - return err - } - err = cmn.WriteFile(file, cliPrint, 0600) + err = writeFile(name, clientDir, cliPrint) if err != nil { return err } // Gather gentxs folder name = fmt.Sprintf("%v.json", nodeDirName) - writePath = filepath.Join(gentxsDir) - file = filepath.Join(writePath, name) - err = cmn.EnsureDir(writePath, 0700) + err = writeFile(name, gentxsDir, genTxFile) if err != nil { return err } - err = cmn.WriteFile(file, genTxFile, 0644) - if err != nil { - return err - } - } // Generate genesis.json and config.toml 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) nodeDir := filepath.Join(outDir, nodeDirName, "gaiad") @@ -165,6 +144,36 @@ func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) err 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) { ipv4 := net.ParseIP(ip).To4() if ipv4 == nil {