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.

170 lines
5.0 KiB

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