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.

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