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.

164 lines
5.0 KiB

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