OSDN Git Service

Merge branch 'master' of https://github.com/Bytom/bytom-btmhash
[bytom/bytom.git] / config / toml.go
1 package config
2
3 import (
4         "os"
5         "path"
6         "path/filepath"
7         "strings"
8
9         cmn "github.com/tendermint/tmlibs/common"
10 )
11
12 /****** these are for production settings ***********/
13
14 func EnsureRoot(rootDir string) {
15         cmn.EnsureDir(rootDir, 0700)
16         cmn.EnsureDir(rootDir+"/data", 0700)
17
18         configFilePath := path.Join(rootDir, "config.toml")
19
20         // Write default config file if missing.
21         if !cmn.FileExists(configFilePath) {
22                 // Ask user for moniker
23                 // moniker := cfg.Prompt("Type hostname: ", "anonymous")
24                 cmn.MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")), 0644)
25         }
26 }
27
28 var defaultConfigTmpl = `# This is a TOML config file.
29 # For more information, see https://github.com/toml-lang/toml
30
31 moniker = "__MONIKER__"
32 fast_sync = true
33 db_backend = "leveldb"
34 log_level = "state:info,*:info"
35 api_addr = "0.0.0.0:1999"
36
37 [rpc]
38 laddr = "tcp://0.0.0.0:46657"
39
40 [p2p]
41 laddr = "tcp://0.0.0.0:46656"
42 seeds = ""
43 `
44
45 func defaultConfig(moniker string) string {
46         return strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
47 }
48
49 /****** these are for test settings ***********/
50
51 func ResetTestRoot(testName string) *Config {
52         rootDir := os.ExpandEnv("$HOME/.test")
53         rootDir = filepath.Join(rootDir, testName)
54         if cmn.FileExists(rootDir + "_bak") {
55                 err := os.RemoveAll(rootDir + "_bak")
56                 if err != nil {
57                         cmn.PanicSanity(err.Error())
58                 }
59         }
60         if cmn.FileExists(rootDir) {
61                 err := os.Rename(rootDir, rootDir+"_bak")
62                 if err != nil {
63                         cmn.PanicSanity(err.Error())
64                 }
65         }
66         // Create new dir
67         cmn.EnsureDir(rootDir, 0700)
68         cmn.EnsureDir(rootDir+"/data", 0700)
69
70         configFilePath := path.Join(rootDir, "config.toml")
71         genesisFilePath := path.Join(rootDir, "genesis.json")
72         privFilePath := path.Join(rootDir, "priv_validator.json")
73
74         // Write default config file if missing.
75         if !cmn.FileExists(configFilePath) {
76                 // Ask user for moniker
77                 cmn.MustWriteFile(configFilePath, []byte(testConfig("anonymous")), 0644)
78         }
79         if !cmn.FileExists(genesisFilePath) {
80                 cmn.MustWriteFile(genesisFilePath, []byte(testGenesis), 0644)
81         }
82         // we always overwrite the priv val
83         cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644)
84
85         config := TestConfig().SetRoot(rootDir)
86         return config
87 }
88
89 var testConfigTmpl = `# This is a TOML config file.
90 # For more information, see https://github.com/toml-lang/toml
91
92 moniker = "__MONIKER__"
93 fast_sync = false
94 db_backend = "memdb"
95 log_level = "info"
96 api_addr = "0.0.0.0:1999"
97
98 [rpc]
99 laddr = "tcp://0.0.0.0:36657"
100
101 [p2p]
102 laddr = "tcp://0.0.0.0:36656"
103 seeds = ""
104 `
105
106 func testConfig(moniker string) (testConfig string) {
107         testConfig = strings.Replace(testConfigTmpl, "__MONIKER__", moniker, -1)
108         return
109 }
110
111 var testGenesis = `{
112   "genesis_time": "0001-01-01T00:00:00.000Z",
113   "chain_id": "tendermint_test",
114   "validators": [
115     {
116       "pub_key": {
117         "type": "ed25519",
118         "data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
119       },
120       "amount": 10,
121       "name": ""
122     }
123   ],
124   "app_hash": ""
125 }`
126
127 var testPrivValidator = `{
128   "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
129   "pub_key": {
130     "type": "ed25519",
131     "data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
132   },
133   "priv_key": {
134     "type": "ed25519",
135     "data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
136   },
137   "last_height": 0,
138   "last_round": 0,
139   "last_step": 0
140 }`