OSDN Git Service

Testnet (#86)
authorGuanghua Guo <1536310027@qq.com>
Mon, 6 Nov 2017 06:54:51 +0000 (14:54 +0800)
committerGitHub <noreply@github.com>
Mon, 6 Nov 2017 06:54:51 +0000 (14:54 +0800)
* Delete tendermint log initialize

* Delete init function

* Add network initialize function

cmd/bytomd/commands/root.go
config/toml.go
config/toml_test.go

index 009d34f..e8971a5 100644 (file)
@@ -20,7 +20,11 @@ var RootCmd = &cobra.Command{
                        return err
                }
                config.SetRoot(config.RootDir)
-               cfg.EnsureRoot(config.RootDir)
+               if len(args) == 0 {
+                       cfg.EnsureRoot(config.RootDir, "mainnet")
+               } else {
+                       cfg.EnsureRoot(config.RootDir, args[0])
+               }
                return nil
        },
 }
index 50262aa..1998d73 100644 (file)
@@ -1,17 +1,13 @@
 package config
 
 import (
-       "os"
        "path"
-       "path/filepath"
-       "strings"
 
        cmn "github.com/tendermint/tmlibs/common"
 )
 
 /****** these are for production settings ***********/
-
-func EnsureRoot(rootDir string) {
+func EnsureRoot(rootDir string, network string) {
        cmn.EnsureDir(rootDir, 0700)
        cmn.EnsureDir(rootDir+"/data", 0700)
 
@@ -19,122 +15,30 @@ func EnsureRoot(rootDir string) {
 
        // Write default config file if missing.
        if !cmn.FileExists(configFilePath) {
-               // Ask user for moniker
-               // moniker := cfg.Prompt("Type hostname: ", "anonymous")
-               cmn.MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")), 0644)
+               cmn.MustWriteFile(configFilePath, []byte(selectNetwork(network)), 0644)
        }
 }
 
 var defaultConfigTmpl = `# This is a TOML config file.
 # For more information, see https://github.com/toml-lang/toml
-
-moniker = "__MONIKER__"
 fast_sync = true
 db_backend = "leveldb"
-log_level = "state:info,*:info"
 api_addr = "0.0.0.0:1999"
 
-[rpc]
-laddr = "tcp://0.0.0.0:46657"
-
 [p2p]
 laddr = "tcp://0.0.0.0:46656"
-seeds = ""
 `
 
-func defaultConfig(moniker string) string {
-       return strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
-}
-
-/****** these are for test settings ***********/
-
-func ResetTestRoot(testName string) *Config {
-       rootDir := os.ExpandEnv("$HOME/.test")
-       rootDir = filepath.Join(rootDir, testName)
-       if cmn.FileExists(rootDir + "_bak") {
-               err := os.RemoveAll(rootDir + "_bak")
-               if err != nil {
-                       cmn.PanicSanity(err.Error())
-               }
-       }
-       if cmn.FileExists(rootDir) {
-               err := os.Rename(rootDir, rootDir+"_bak")
-               if err != nil {
-                       cmn.PanicSanity(err.Error())
-               }
-       }
-       // Create new dir
-       cmn.EnsureDir(rootDir, 0700)
-       cmn.EnsureDir(rootDir+"/data", 0700)
-
-       configFilePath := path.Join(rootDir, "config.toml")
-       genesisFilePath := path.Join(rootDir, "genesis.json")
-       privFilePath := path.Join(rootDir, "priv_validator.json")
-
-       // Write default config file if missing.
-       if !cmn.FileExists(configFilePath) {
-               // Ask user for moniker
-               cmn.MustWriteFile(configFilePath, []byte(testConfig("anonymous")), 0644)
-       }
-       if !cmn.FileExists(genesisFilePath) {
-               cmn.MustWriteFile(genesisFilePath, []byte(testGenesis), 0644)
-       }
-       // we always overwrite the priv val
-       cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644)
-
-       config := TestConfig().SetRoot(rootDir)
-       return config
-}
-
-var testConfigTmpl = `# This is a TOML config file.
-# For more information, see https://github.com/toml-lang/toml
-
-moniker = "__MONIKER__"
-fast_sync = false
-db_backend = "memdb"
-log_level = "info"
-api_addr = "0.0.0.0:1999"
-
-[rpc]
-laddr = "tcp://0.0.0.0:36657"
-
-[p2p]
-laddr = "tcp://0.0.0.0:36656"
-seeds = ""
+var testnetSeeds = `
+seeds = "139.162.105.40:46656,139.162.88.74:46656,139.162.96.53:46656,139.162.104.59:46656"
 `
-
-func testConfig(moniker string) (testConfig string) {
-       testConfig = strings.Replace(testConfigTmpl, "__MONIKER__", moniker, -1)
-       return
+var mainnetSeeds = `seeds = ""`
+
+// Select network seeds to merge a new string.
+func selectNetwork(network string) string {
+       if network == "testnet" {
+               return defaultConfigTmpl + testnetSeeds
+       } else {
+               return defaultConfigTmpl + mainnetSeeds
+       }
 }
-
-var testGenesis = `{
-  "genesis_time": "0001-01-01T00:00:00.000Z",
-  "chain_id": "tendermint_test",
-  "validators": [
-    {
-      "pub_key": {
-        "type": "ed25519",
-        "data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
-      },
-      "amount": 10,
-      "name": ""
-    }
-  ],
-  "app_hash": ""
-}`
-
-var testPrivValidator = `{
-  "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
-  "pub_key": {
-    "type": "ed25519",
-    "data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
-  },
-  "priv_key": {
-    "type": "ed25519",
-    "data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
-  },
-  "last_height": 0,
-  "last_round": 0,
-  "last_step": 0
-}`
index d8f372a..53bac8d 100644 (file)
@@ -27,31 +27,12 @@ func TestEnsureRoot(t *testing.T) {
        defer os.RemoveAll(tmpDir)
 
        // create root dir
-       EnsureRoot(tmpDir)
+       EnsureRoot(tmpDir, "mainnet")
 
        // make sure config is set properly
        data, err := ioutil.ReadFile(filepath.Join(tmpDir, "config.toml"))
        require.Nil(err)
-       assert.Equal([]byte(defaultConfig("anonymous")), data)
+       assert.Equal([]byte(selectNetwork("mainnet")), data)
 
        ensureFiles(t, tmpDir, "data")
 }
-
-func TestEnsureTestRoot(t *testing.T) {
-       assert, require := assert.New(t), require.New(t)
-
-       testName := "ensureTestRoot"
-
-       // create root dir
-       cfg := ResetTestRoot(testName)
-       rootDir := cfg.RootDir
-
-       // make sure config is set properly
-       data, err := ioutil.ReadFile(filepath.Join(rootDir, "config.toml"))
-       require.Nil(err)
-       assert.Equal([]byte(testConfig("anonymous")), data)
-
-       // TODO: make sure the cfg returned and testconfig are the same!
-
-       ensureFiles(t, rootDir, "data", "genesis.json", "priv_validator.json")
-}