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.

142 lines
3.4 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. 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. fast_sync = true
  26. db_backend = "leveldb"
  27. log_level = "state:info,*:error"
  28. [rpc]
  29. laddr = "tcp://0.0.0.0:46657"
  30. [p2p]
  31. laddr = "tcp://0.0.0.0:46656"
  32. seeds = ""
  33. `
  34. func defaultConfig(moniker string) string {
  35. return strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
  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. return config
  73. }
  74. var testConfigTmpl = `# This is a TOML config file.
  75. # For more information, see https://github.com/toml-lang/toml
  76. proxy_app = "dummy"
  77. moniker = "__MONIKER__"
  78. fast_sync = false
  79. db_backend = "memdb"
  80. log_level = "info"
  81. [rpc]
  82. laddr = "tcp://0.0.0.0:36657"
  83. [p2p]
  84. laddr = "tcp://0.0.0.0:36656"
  85. seeds = ""
  86. `
  87. func testConfig(moniker string) (testConfig string) {
  88. testConfig = strings.Replace(testConfigTmpl, "__MONIKER__", moniker, -1)
  89. return
  90. }
  91. var testGenesis = `{
  92. "genesis_time": "0001-01-01T00:00:00.000Z",
  93. "chain_id": "tendermint_test",
  94. "validators": [
  95. {
  96. "pub_key": {
  97. "type": "ed25519",
  98. "data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  99. },
  100. "amount": 10,
  101. "name": ""
  102. }
  103. ],
  104. "app_hash": ""
  105. }`
  106. var testPrivValidator = `{
  107. "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
  108. "pub_key": {
  109. "type": "ed25519",
  110. "data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  111. },
  112. "priv_key": {
  113. "type": "ed25519",
  114. "data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  115. },
  116. "last_height": 0,
  117. "last_round": 0,
  118. "last_step": 0
  119. }`