OSDN Git Service

merge node_p2p and chain.
[bytom/bytom-spv.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 proxy_app = "tcp://127.0.0.1:46658"
32 moniker = "__MONIKER__"
33 fast_sync = true
34 db_backend = "leveldb"
35 log_level = "state:info,*:info"
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/.tendermint_test")
53         rootDir = filepath.Join(rootDir, testName)
54         // Remove ~/.tendermint_test_bak
55         if cmn.FileExists(rootDir + "_bak") {
56                 err := os.RemoveAll(rootDir + "_bak")
57                 if err != nil {
58                         cmn.PanicSanity(err.Error())
59                 }
60         }
61         // Move ~/.tendermint_test to ~/.tendermint_test_bak
62         if cmn.FileExists(rootDir) {
63                 err := os.Rename(rootDir, rootDir+"_bak")
64                 if err != nil {
65                         cmn.PanicSanity(err.Error())
66                 }
67         }
68         // Create new dir
69         cmn.EnsureDir(rootDir, 0700)
70         cmn.EnsureDir(rootDir+"/data", 0700)
71
72         configFilePath := path.Join(rootDir, "config.toml")
73         genesisFilePath := path.Join(rootDir, "genesis.json")
74         privFilePath := path.Join(rootDir, "priv_validator.json")
75
76         // Write default config file if missing.
77         if !cmn.FileExists(configFilePath) {
78                 // Ask user for moniker
79                 cmn.MustWriteFile(configFilePath, []byte(testConfig("anonymous")), 0644)
80         }
81         if !cmn.FileExists(genesisFilePath) {
82                 cmn.MustWriteFile(genesisFilePath, []byte(testGenesis), 0644)
83         }
84         // we always overwrite the priv val
85         cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644)
86
87         config := TestConfig().SetRoot(rootDir)
88         return config
89 }
90
91 var testConfigTmpl = `# This is a TOML config file.
92 # For more information, see https://github.com/toml-lang/toml
93
94 proxy_app = "dummy"
95 moniker = "__MONIKER__"
96 fast_sync = false
97 db_backend = "memdb"
98 log_level = "info"
99
100 [rpc]
101 laddr = "tcp://0.0.0.0:36657"
102
103 [p2p]
104 laddr = "tcp://0.0.0.0:36656"
105 seeds = ""
106 `
107
108 func testConfig(moniker string) (testConfig string) {
109         testConfig = strings.Replace(testConfigTmpl, "__MONIKER__", moniker, -1)
110         return
111 }
112
113 var testGenesis = `{
114   "genesis_time": "0001-01-01T00:00:00.000Z",
115   "chain_id": "tendermint_test",
116   "validators": [
117     {
118       "pub_key": {
119         "type": "ed25519",
120         "data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
121       },
122       "amount": 10,
123       "name": ""
124     }
125   ],
126   "app_hash": ""
127 }`
128
129 var testPrivValidator = `{
130   "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
131   "pub_key": {
132     "type": "ed25519",
133     "data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
134   },
135   "priv_key": {
136     "type": "ed25519",
137     "data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
138   },
139   "last_height": 0,
140   "last_round": 0,
141   "last_step": 0
142 }`