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.

148 lines
3.7 KiB

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