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.

210 lines
5.6 KiB

11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 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. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. "sync"
  10. flag "github.com/spf13/pflag"
  11. "github.com/tendermint/confer"
  12. )
  13. var app *confer.Config
  14. var appMtx sync.Mutex
  15. func App() *confer.Config {
  16. appMtx.Lock()
  17. defer appMtx.Unlock()
  18. if app == nil {
  19. Init("")
  20. }
  21. return app
  22. }
  23. func SetApp(a *confer.Config) {
  24. appMtx.Lock()
  25. defer appMtx.Unlock()
  26. app = a
  27. }
  28. // NOTE: If you change this, maybe also change initDefaults()
  29. var defaultConfig = `# This is a TOML config file.
  30. # For more information, see https://github.com/toml-lang/toml
  31. Moniker = "anonymous"
  32. Network = "tendermint_testnet0"
  33. ListenAddr = "0.0.0.0:8080"
  34. # First node to connect to. Command-line overridable.
  35. SeedNode = "188.166.55.222:8080"
  36. [DB]
  37. # The only other available backend is "memdb"
  38. Backend = "leveldb"
  39. # Dir = "~/.tendermint/data"
  40. [Log.Stdout]
  41. Level = "info"
  42. [Log.File]
  43. Level = "debug"
  44. # Dir = "~/.tendermint/log"
  45. [RPC.HTTP]
  46. # For the RPC API HTTP server. Port required.
  47. ListenAddr = "127.0.0.1:8081"
  48. [Alert]
  49. # TODO: Document options
  50. [SMTP]
  51. # TODO: Document options
  52. `
  53. var DefaultGenesis = `{
  54. "Accounts": [
  55. {
  56. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  57. "Amount": 2099600000000000
  58. }
  59. ],
  60. "Validators": [
  61. {
  62. "PubKey": [1, "323A31EB01877858592AB7D593E9447110AFCD3ACF280D60C4F8E7C04FACC955"],
  63. "Amount": 100000000000,
  64. "UnbondTo": [
  65. {
  66. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  67. "Amount": 100000000000
  68. }
  69. ]
  70. },
  71. {
  72. "PubKey": [1, "DD2206E8F889EED3ABAAECEB2D18962D062A887346241820493FFE3B1DEF255D"],
  73. "Amount": 100000000000,
  74. "UnbondTo": [
  75. {
  76. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  77. "Amount": 100000000000
  78. }
  79. ]
  80. },
  81. {
  82. "PubKey": [1, "1B3256A3754FC6AB01110C166199A2F619E2D76DB3EE751E376FE404AC9FDCFF"],
  83. "Amount": 100000000000,
  84. "UnbondTo": [
  85. {
  86. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  87. "Amount": 100000000000
  88. }
  89. ]
  90. },
  91. {
  92. "PubKey": [1, "62CF1048BAEBB4FFFF360D5E896E3F4EC72D03D55183596931ED14995D512926"],
  93. "Amount": 100000000000,
  94. "UnbondTo": [
  95. {
  96. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  97. "Amount": 100000000000
  98. }
  99. ]
  100. }
  101. ]
  102. }`
  103. // NOTE: If you change this, maybe also change defaultConfig
  104. func initDefaults(rootDir string) {
  105. app.SetDefault("Moniker", "anonymous")
  106. app.SetDefault("Network", "tendermint_testnet0")
  107. app.SetDefault("ListenAddr", "0.0.0.0:8080")
  108. app.SetDefault("DB.Backend", "leveldb")
  109. app.SetDefault("DB.Dir", rootDir+"/data")
  110. app.SetDefault("Log.Stdout.Level", "info")
  111. app.SetDefault("Log.File.Dir", rootDir+"/log")
  112. app.SetDefault("Log.File.Level", "debug")
  113. app.SetDefault("RPC.HTTP.ListenAddr", "0.0.0.0:8081")
  114. app.SetDefault("GenesisFile", rootDir+"/genesis.json")
  115. app.SetDefault("AddrBookFile", rootDir+"/addrbook.json")
  116. app.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json")
  117. app.SetDefault("FastSync", false)
  118. }
  119. func Init(rootDir string) {
  120. // Get rootdir
  121. if rootDir == "" {
  122. rootDir = os.Getenv("TMROOT")
  123. }
  124. if rootDir == "" {
  125. rootDir = os.Getenv("HOME") + "/.tendermint"
  126. }
  127. configFile := path.Join(rootDir, "config.toml")
  128. genesisFile := path.Join(rootDir, "genesis.json")
  129. // Write default config file if missing.
  130. checkWriteFile(configFile, defaultConfig)
  131. checkWriteFile(genesisFile, DefaultGenesis)
  132. // Initialize Config
  133. app = confer.NewConfig()
  134. initDefaults(rootDir)
  135. paths := []string{configFile}
  136. if err := app.ReadPaths(paths...); err != nil {
  137. log.Warn("Error reading configuration", "paths", paths, "error", err)
  138. }
  139. // Confused?
  140. // app.Debug()
  141. }
  142. // Check if a file exists; if not, ensure the directory is made and write the file
  143. func checkWriteFile(configFile, contents string) {
  144. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  145. if strings.Index(configFile, "/") != -1 {
  146. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  147. if err != nil {
  148. fmt.Printf("Could not create directory: %v", err)
  149. os.Exit(1)
  150. }
  151. }
  152. err := ioutil.WriteFile(configFile, []byte(contents), 0600)
  153. if err != nil {
  154. fmt.Printf("Could not write config file: %v", err)
  155. os.Exit(1)
  156. }
  157. fmt.Printf("Config file written to %v.\n", configFile)
  158. }
  159. }
  160. func ParseFlags(args []string) {
  161. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  162. var printHelp = false
  163. // Declare flags
  164. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  165. flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
  166. flags.String("seed_node", app.GetString("SeedNode"), "Address of seed node")
  167. flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
  168. flags.Bool("fast_sync", app.GetBool("FastSync"), "Fast blockchain syncing")
  169. flags.String("log_stdout_level", app.GetString("Log.Stdout.Level"), "Stdout log level")
  170. flags.Parse(args)
  171. if printHelp {
  172. flags.PrintDefaults()
  173. os.Exit(0)
  174. }
  175. // Merge parsed flag values onto app.
  176. app.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
  177. app.BindPFlag("SeedNode", flags.Lookup("seed_node"))
  178. app.BindPFlag("FastSync", flags.Lookup("fast_sync"))
  179. app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
  180. app.BindPFlag("Log.Stdout.Level", flags.Lookup("log_stdout_level"))
  181. // Confused?
  182. //app.Debug()
  183. }