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.

137 lines
3.5 KiB

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