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.

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