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.

145 lines
3.6 KiB

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