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.

138 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. fast_sync = true
  26. db_backend = "leveldb"
  27. log_level = "info"
  28. rpc_laddr = "tcp://0.0.0.0:46657"
  29. [p2p]
  30. laddr = "tcp://0.0.0.0:46656"
  31. seeds = ""
  32. `
  33. func defaultConfig(moniker string) string {
  34. return strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
  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. fast_sync = false
  78. db_backend = "memdb"
  79. log_level = "info"
  80. rpc_laddr = "tcp://0.0.0.0:36657"
  81. [p2p]
  82. laddr = "tcp://0.0.0.0:36656"
  83. seeds = ""
  84. `
  85. func testConfig(moniker string) (testConfig string) {
  86. testConfig = strings.Replace(testConfigTmpl, "__MONIKER__", moniker, -1)
  87. return
  88. }
  89. var testGenesis = `{
  90. "genesis_time": "0001-01-01T00:00:00.000Z",
  91. "chain_id": "tendermint_test",
  92. "validators": [
  93. {
  94. "pub_key": {
  95. "type": "ed25519",
  96. "data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  97. },
  98. "amount": 10,
  99. "name": ""
  100. }
  101. ],
  102. "app_hash": ""
  103. }`
  104. var testPrivValidator = `{
  105. "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
  106. "pub_key": {
  107. "type": "ed25519",
  108. "data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  109. },
  110. "priv_key": {
  111. "type": "ed25519",
  112. "data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  113. },
  114. "last_height": 0,
  115. "last_round": 0,
  116. "last_step": 0
  117. }`