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.

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