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.

222 lines
6.3 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
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_testnet3"
  33. ListenAddr = "0.0.0.0:46656"
  34. # First node to connect to. Command-line overridable.
  35. SeedNode = ""
  36. # Pool of seeds. Best to use these, and specify one on command line
  37. # if needed to override
  38. SeedNodes = ["navytoad.chaintest.net:46656", "whiteferret.chaintest.net:46656", "magentagriffin.chaintest.net:46656", "greensalamander.chaintest.net:46656", "blackshadow.chaintest.net:46656", "purpleanteater.chaintest.net:46656", "pinkpenguin.chaintest.net:46656", "polkapig.chaintest.net:46656", "128.199.230.153:8080"]
  39. [DB]
  40. # The only other available backend is "memdb"
  41. Backend = "leveldb"
  42. # Dir = "~/.tendermint/data"
  43. [Log.Stdout]
  44. Level = "info"
  45. [RPC.HTTP]
  46. # For the RPC API HTTP server. Port required.
  47. ListenAddr = "0.0.0.0:46657"
  48. [Alert]
  49. # TODO: Document options
  50. [SMTP]
  51. # TODO: Document options
  52. `
  53. var DefaultGenesis = `{
  54. "Accounts": [
  55. {
  56. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  57. "Amount": 1049800000000000
  58. },
  59. {
  60. "Address": "D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB",
  61. "Amount": 1049800000000000
  62. }
  63. ],
  64. "Validators": [
  65. {
  66. "PubKey": [1, "323A31EB01877858592AB7D593E9447110AFCD3ACF280D60C4F8E7C04FACC955"],
  67. "Amount": 100000000000,
  68. "UnbondTo": [
  69. {
  70. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  71. "Amount": 100000000000
  72. }
  73. ]
  74. },
  75. {
  76. "PubKey": [1, "2239C21C81EA7173A6C489145490C015E05D4B97448933B708A7EC5B7B4921E3"],
  77. "Amount": 100000000000,
  78. "UnbondTo": [
  79. {
  80. "Address": "D7DFF9806078899C8DA3FE3633CC0BF3C6C2B1BB",
  81. "Amount": 100000000000
  82. }
  83. ]
  84. },
  85. {
  86. "PubKey": [1, "DD2206E8F889EED3ABAAECEB2D18962D062A887346241820493FFE3B1DEF255D"],
  87. "Amount": 100000000000,
  88. "UnbondTo": [
  89. {
  90. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  91. "Amount": 100000000000
  92. }
  93. ]
  94. },
  95. {
  96. "PubKey": [1, "1B3256A3754FC6AB01110C166199A2F619E2D76DB3EE751E376FE404AC9FDCFF"],
  97. "Amount": 100000000000,
  98. "UnbondTo": [
  99. {
  100. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  101. "Amount": 50000000000
  102. }
  103. ]
  104. },
  105. {
  106. "PubKey": [1, "62CF1048BAEBB4FFFF360D5E896E3F4EC72D03D55183596931ED14995D512926"],
  107. "Amount": 100000000000,
  108. "UnbondTo": [
  109. {
  110. "Address": "69988763FCF806AC35D1A2F9C4885B7DD7B0599C",
  111. "Amount": 50000000000
  112. }
  113. ]
  114. }
  115. ]
  116. }`
  117. // NOTE: If you change this, maybe also change defaultConfig
  118. func initDefaults(rootDir string) {
  119. app.SetDefault("Moniker", "anonymous")
  120. app.SetDefault("Network", "tendermint_testnet0")
  121. app.SetDefault("ListenAddr", "0.0.0.0:46656")
  122. app.SetDefault("DB.Backend", "leveldb")
  123. app.SetDefault("DB.Dir", rootDir+"/data")
  124. app.SetDefault("Log.Stdout.Level", "info")
  125. app.SetDefault("RPC.HTTP.ListenAddr", "0.0.0.0:46657")
  126. app.SetDefault("GenesisFile", rootDir+"/genesis.json")
  127. app.SetDefault("AddrBookFile", rootDir+"/addrbook.json")
  128. app.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json")
  129. app.SetDefault("FastSync", false)
  130. }
  131. func Init(rootDir string) {
  132. // Get rootdir
  133. if rootDir == "" {
  134. rootDir = os.Getenv("TMROOT")
  135. }
  136. if rootDir == "" {
  137. rootDir = os.Getenv("HOME") + "/.tendermint"
  138. }
  139. configFile := path.Join(rootDir, "config.toml")
  140. genesisFile := path.Join(rootDir, "genesis.json")
  141. // Write default config file if missing.
  142. checkWriteFile(configFile, defaultConfig)
  143. checkWriteFile(genesisFile, DefaultGenesis)
  144. // Initialize Config
  145. app = confer.NewConfig()
  146. initDefaults(rootDir)
  147. paths := []string{configFile}
  148. if err := app.ReadPaths(paths...); err != nil {
  149. log.Warn("Error reading configuration", "paths", paths, "error", err)
  150. }
  151. // Confused?
  152. //app.Debug()
  153. }
  154. // Check if a file exists; if not, ensure the directory is made and write the file
  155. func checkWriteFile(configFile, contents string) {
  156. if _, err := os.Stat(configFile); os.IsNotExist(err) {
  157. if strings.Index(configFile, "/") != -1 {
  158. err := os.MkdirAll(filepath.Dir(configFile), 0700)
  159. if err != nil {
  160. fmt.Printf("Could not create directory: %v", err)
  161. os.Exit(1)
  162. }
  163. }
  164. err := ioutil.WriteFile(configFile, []byte(contents), 0600)
  165. if err != nil {
  166. fmt.Printf("Could not write config file: %v", err)
  167. os.Exit(1)
  168. }
  169. fmt.Printf("Config file written to %v.\n", configFile)
  170. }
  171. }
  172. func ParseFlags(args []string) {
  173. var flags = flag.NewFlagSet("main", flag.ExitOnError)
  174. var printHelp = false
  175. // Declare flags
  176. flags.BoolVar(&printHelp, "help", false, "Print this help message.")
  177. flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
  178. flags.String("seed_node", app.GetString("SeedNode"), "Address of seed nodes")
  179. flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
  180. flags.Bool("fast_sync", app.GetBool("FastSync"), "Fast blockchain syncing")
  181. flags.String("log_stdout_level", app.GetString("Log.Stdout.Level"), "Stdout log level")
  182. flags.Parse(args)
  183. if printHelp {
  184. flags.PrintDefaults()
  185. os.Exit(0)
  186. }
  187. // Merge parsed flag values onto app.
  188. app.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
  189. app.BindPFlag("SeedNode", flags.Lookup("seed_node"))
  190. app.BindPFlag("FastSync", flags.Lookup("fast_sync"))
  191. app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
  192. app.BindPFlag("Log.Stdout.Level", flags.Lookup("log_stdout_level"))
  193. // Confused?
  194. //app.Debug()
  195. }