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.

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