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.

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