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.

152 lines
4.3 KiB

9 years ago
9 years ago
  1. // Import this in all *_test.go files to initialize ~/.tendermint_test.
  2. package tendermint_test
  3. import (
  4. "os"
  5. "path"
  6. "strings"
  7. . "github.com/tendermint/go-common"
  8. cfg "github.com/tendermint/go-config"
  9. )
  10. func init() {
  11. // Creates ~/.tendermint_test
  12. EnsureDir(os.Getenv("HOME")+"/.tendermint_test", 0700)
  13. }
  14. func ResetConfig(path string) {
  15. rootDir := os.Getenv("HOME") + "/.tendermint_test/" + path
  16. cfg.ApplyConfig(GetConfig(rootDir))
  17. }
  18. func initTMRoot(rootDir string) {
  19. // Remove ~/.tendermint_test_bak
  20. if FileExists(rootDir + "_bak") {
  21. err := os.RemoveAll(rootDir + "_bak")
  22. if err != nil {
  23. PanicSanity(err.Error())
  24. }
  25. }
  26. // Move ~/.tendermint_test to ~/.tendermint_test_bak
  27. if FileExists(rootDir) {
  28. err := os.Rename(rootDir, rootDir+"_bak")
  29. if err != nil {
  30. PanicSanity(err.Error())
  31. }
  32. }
  33. // Create new dir
  34. EnsureDir(rootDir, 0700)
  35. configFilePath := path.Join(rootDir, "config.toml")
  36. genesisFilePath := path.Join(rootDir, "genesis.json")
  37. privFilePath := path.Join(rootDir, "priv_validator.json")
  38. // Write default config file if missing.
  39. if !FileExists(configFilePath) {
  40. // Ask user for moniker
  41. // moniker := cfg.Prompt("Type hostname: ", "anonymous")
  42. MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")), 0644)
  43. }
  44. if !FileExists(genesisFilePath) {
  45. MustWriteFile(genesisFilePath, []byte(defaultGenesis), 0644)
  46. }
  47. // we always overwrite the priv val
  48. MustWriteFile(privFilePath, []byte(defaultPrivValidator), 0644)
  49. }
  50. func GetConfig(rootDir string) cfg.Config {
  51. initTMRoot(rootDir)
  52. configFilePath := path.Join(rootDir, "config.toml")
  53. mapConfig, err := cfg.ReadMapConfigFromFile(configFilePath)
  54. if err != nil {
  55. Exit(Fmt("Could not read config: %v", err))
  56. }
  57. // Set defaults or panic
  58. if mapConfig.IsSet("chain_id") {
  59. Exit("Cannot set 'chain_id' via config.toml")
  60. }
  61. mapConfig.SetDefault("chain_id", "tendermint_test")
  62. mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json")
  63. mapConfig.SetDefault("proxy_app", "dummy")
  64. mapConfig.SetDefault("moniker", "anonymous")
  65. mapConfig.SetDefault("node_laddr", "0.0.0.0:36656")
  66. mapConfig.SetDefault("fast_sync", false)
  67. mapConfig.SetDefault("skip_upnp", true)
  68. mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json")
  69. mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
  70. mapConfig.SetDefault("db_backend", "memdb")
  71. mapConfig.SetDefault("db_dir", rootDir+"/data")
  72. mapConfig.SetDefault("log_level", "debug")
  73. mapConfig.SetDefault("rpc_laddr", "0.0.0.0:36657")
  74. mapConfig.SetDefault("prof_laddr", "")
  75. mapConfig.SetDefault("revision_file", rootDir+"/revision")
  76. mapConfig.SetDefault("cswal", rootDir+"/data/cswal")
  77. mapConfig.SetDefault("cswal_light", false)
  78. mapConfig.SetDefault("block_size", 10000)
  79. mapConfig.SetDefault("disable_data_hash", false)
  80. mapConfig.SetDefault("timeout_propose", 100)
  81. mapConfig.SetDefault("timeout_propose_delta", 1)
  82. mapConfig.SetDefault("timeout_prevote", 1)
  83. mapConfig.SetDefault("timeout_prevote_delta", 1)
  84. mapConfig.SetDefault("timeout_precommit", 1)
  85. mapConfig.SetDefault("timeout_precommit_delta", 1)
  86. mapConfig.SetDefault("timeout_commit", 1)
  87. mapConfig.SetDefault("mempool_recheck", true)
  88. mapConfig.SetDefault("mempool_broadcast", true)
  89. return mapConfig
  90. }
  91. var defaultConfigTmpl = `# This is a TOML config file.
  92. # For more information, see https://github.com/toml-lang/toml
  93. proxy_app = "dummy"
  94. moniker = "__MONIKER__"
  95. node_laddr = "0.0.0.0:36656"
  96. seeds = ""
  97. fast_sync = false
  98. db_backend = "memdb"
  99. log_level = "debug"
  100. rpc_laddr = "0.0.0.0:36657"
  101. `
  102. func defaultConfig(moniker string) (defaultConfig string) {
  103. defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
  104. return
  105. }
  106. var defaultGenesis = `{
  107. "genesis_time": "0001-01-01T00:00:00.000Z",
  108. "chain_id": "tendermint_test",
  109. "validators": [
  110. {
  111. "pub_key": [
  112. 1,
  113. "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  114. ],
  115. "amount": 10,
  116. "name": ""
  117. }
  118. ],
  119. "app_hash": ""
  120. }`
  121. var defaultPrivValidator = `{
  122. "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
  123. "pub_key": [
  124. 1,
  125. "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  126. ],
  127. "priv_key": [
  128. 1,
  129. "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  130. ],
  131. "last_height": 0,
  132. "last_round": 0,
  133. "last_step": 0
  134. }`