You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package config
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "sync"
  11. flag "github.com/spf13/pflag"
  12. "github.com/tendermint/confer"
  13. )
  14. var app *confer.Config
  15. var appMtx sync.Mutex
  16. func App() *confer.Config {
  17. appMtx.Lock()
  18. defer appMtx.Unlock()
  19. if app == nil {
  20. Init("")
  21. }
  22. return app
  23. }
  24. func SetApp(a *confer.Config) {
  25. appMtx.Lock()
  26. defer appMtx.Unlock()
  27. app = a
  28. }
  29. // NOTE: If you change this, maybe also change initDefaults()
  30. var defaultConfigTmpl = `# This is a TOML config file.
  31. # For more information, see https://github.com/toml-lang/toml
  32. network = "tendermint_testnet_5"
  33. moniker = "__MONIKER__"
  34. node_laddr = "0.0.0.0:46656"
  35. seeds = "goldenalchemist.chaintest.net:46656"
  36. fast_sync = true
  37. db_backend = "leveldb"
  38. log_level = "debug"
  39. rpc_laddr = "0.0.0.0:46657"
  40. `
  41. var DefaultGenesis = `{
  42. "accounts": [
  43. {
  44. "address": "F81CB9ED0A868BD961C4F5BBC0E39B763B89FCB6",
  45. "amount": 690000000000
  46. },
  47. {
  48. "address": "0000000000000000000000000000000000000002",
  49. "amount": 565000000000
  50. },
  51. {
  52. "address": "9E54C9ECA9A3FD5D4496696818DA17A9E17F69DA",
  53. "amount": 525000000000
  54. },
  55. {
  56. "address": "0000000000000000000000000000000000000004",
  57. "amount": 110000000000
  58. }
  59. ],
  60. "validators": [
  61. {
  62. "pub_key": [1, "178EC6008A4364508979C70CBF100BD4BCBAA12DDE6251F5F486B4FD09014F06"],
  63. "amount": 5000000000,
  64. "unbond_to": [
  65. {
  66. "address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
  67. "amount": 5000000000
  68. }
  69. ]
  70. },
  71. {
  72. "pub_key": [1, "2A77777CC51467DE42350D4A8F34720D527734189BE64C7A930DD169E1FED3C6"],
  73. "amount": 5000000000,
  74. "unbond_to": [
  75. {
  76. "address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
  77. "amount": 5000000000
  78. }
  79. ]
  80. },
  81. {
  82. "pub_key": [1, "3718E69D09B11B3AD3FA31AEF07EC416D2AEED241CACE7B0F30AE9803FFB0F08"],
  83. "amount": 5000000000,
  84. "unbond_to": [
  85. {
  86. "address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
  87. "amount": 5000000000
  88. }
  89. ]
  90. },
  91. {
  92. "pub_key": [1, "C6B0440DEACD1E4CF1C736CEB8E38E788B700BA2B2045A55CB657A455CF5F889"],
  93. "amount": 5000000000,
  94. "unbond_to": [
  95. {
  96. "address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
  97. "amount": 5000000000
  98. }
  99. ]
  100. },
  101. {
  102. "pub_key": [1, "3BA1190D54F91EFBF8B0125F7EC116AD4BA2894B6EE38564A5D5FD3230D91F7B"],
  103. "amount": 5000000000,
  104. "unbond_to": [
  105. {
  106. "address": "93E243AC8A01F723DE353A4FA1ED911529CCB6E5",
  107. "amount": 5000000000
  108. }
  109. ]
  110. },
  111. {
  112. "pub_key": [1, "E56663353D01C58A1D4CDB4D14B70C2E3335BE1EBB6C3F697AF7882C03837962"],
  113. "amount": 5000000000,
  114. "unbond_to": [
  115. {
  116. "address": "9E54C9ECA9A3FD5D4496696818DA17A9E17F69DA",
  117. "amount": 5000000000
  118. }
  119. ]
  120. }
  121. ]
  122. }`
  123. // If not defined in the process args nor config file, then use these defaults.
  124. // NOTE: If you change this, maybe also change defaultConfig
  125. func initDefaults(rootDir string) {
  126. app.SetDefault("network", "tendermint_testnet0")
  127. app.SetDefault("version", "0.2.1")
  128. app.SetDefault("genesis_file", rootDir+"/genesis.json")
  129. app.SetDefault("moniker", "anonymous")
  130. app.SetDefault("node_laddr", "0.0.0.0:46656")
  131. app.SetDefault("seeds", "goldenalchemist.chaintest.net:46656")
  132. app.SetDefault("fast_sync", true)
  133. app.SetDefault("addrbook_file", rootDir+"/addrbook.json")
  134. app.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
  135. app.SetDefault("db_backend", "leveldb")
  136. app.SetDefault("db_dir", rootDir+"/data")
  137. app.SetDefault("log_level", "info")
  138. app.SetDefault("rpc_laddr", "0.0.0.0:46657")
  139. }
  140. func Init(rootDir string) {
  141. // Get rootdir
  142. if rootDir == "" {
  143. rootDir = os.Getenv("TMROOT")
  144. }
  145. if rootDir == "" {
  146. rootDir = os.Getenv("HOME") + "/.tendermint"
  147. }
  148. configFile := path.Join(rootDir, "config.toml")
  149. genesisFile := path.Join(rootDir, "genesis.json")
  150. // Write default config file if missing.
  151. if !fileExists(configFile) {
  152. // Ask user for moniker
  153. moniker := getInput("Type hostname: ", "anonymous")
  154. defaultConfig := strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
  155. writeFile(configFile, defaultConfig)
  156. }
  157. if !fileExists(genesisFile) {
  158. writeFile(genesisFile, DefaultGenesis)
  159. }
  160. // Initialize Config
  161. app = confer.NewConfig()
  162. initDefaults(rootDir)
  163. paths := []string{configFile}
  164. if err := app.ReadPaths(paths...); err != nil {
  165. log.Warn("Error reading configuration", "paths", paths, "error", err)
  166. }
  167. // Confused?
  168. //app.Debug()
  169. }
  170. func getInput(prompt string, defaultValue string) string {
  171. fmt.Print(prompt)
  172. reader := bufio.NewReader(os.Stdin)
  173. line, err := reader.ReadString('\n')
  174. if err != nil {
  175. log.Warn("Error reading stdin", "err", err)
  176. return defaultValue
  177. } else {
  178. line = strings.TrimSpace(line)
  179. if line == "" {
  180. return defaultValue
  181. }
  182. return line
  183. }
  184. }
  185. func fileExists(file string) bool {
  186. _, err := os.Stat(file)
  187. return !os.IsNotExist(err)
  188. }
  189. func writeFile(file, contents string) {
  190. if _, err := os.Stat(file); os.IsNotExist(err) {
  191. if strings.Index(file, "/") != -1 {
  192. err := os.MkdirAll(filepath.Dir(file), 0700)
  193. if err != nil {
  194. fmt.Printf("Could not create directory: %v", err)
  195. os.Exit(1)
  196. }
  197. }
  198. err := ioutil.WriteFile(file, []byte(contents), 0600)
  199. if err != nil {
  200. fmt.Printf("Could not write file: %v", err)
  201. os.Exit(1)
  202. }
  203. fmt.Printf("File written to %v.\n", file)
  204. }
  205. }
  206. func ParseFlags(args []string) {
  207. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  208. var printHelp = false
  209. // Declare flags
  210. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  211. flags.String("moniker", app.GetString("moniker"), "Node Name")
  212. flags.String("node_laddr", app.GetString("node_laddr"), "Node listen address. (0.0.0.0:0 means any interface, any port)")
  213. flags.String("seeds", app.GetString("seeds"), "Comma delimited seed nodes")
  214. flags.Bool("fast_sync", app.GetBool("fast_sync"), "Fast blockchain syncing")
  215. flags.String("rpc_laddr", app.GetString("rpc_laddr"), "RPC listen address. Port required")
  216. flags.String("log_level", app.GetString("log_level"), "Log level")
  217. flags.Parse(args)
  218. if printHelp {
  219. flags.PrintDefaults()
  220. os.Exit(0)
  221. }
  222. // Merge parsed flag values onto app.
  223. app.BindPFlag("moniker", flags.Lookup("moniker"))
  224. app.BindPFlag("node_laddr", flags.Lookup("node_laddr"))
  225. app.BindPFlag("seeds", flags.Lookup("seeds"))
  226. app.BindPFlag("fast_sync", flags.Lookup("fast_sync"))
  227. app.BindPFlag("rpc_laddr", flags.Lookup("rpc_laddr"))
  228. app.BindPFlag("log_level", flags.Lookup("log_level"))
  229. // Confused?
  230. //app.Debug()
  231. }