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.

153 lines
4.2 KiB

10 years ago
  1. // Import this in all *_test.go files to initialize ~/.tendermint_test.
  2. package tendermint_test
  3. import (
  4. "github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/naoina/toml"
  5. "os"
  6. "path"
  7. "strings"
  8. . "github.com/tendermint/tendermint/common"
  9. cfg "github.com/tendermint/tendermint/config"
  10. )
  11. func init() {
  12. // Creates ~/.tendermint_test/*
  13. config := GetConfig("")
  14. cfg.ApplyConfig(config)
  15. }
  16. func getTMRoot(rootDir string) string {
  17. if rootDir == "" {
  18. rootDir = os.Getenv("HOME") + "/.tendermint_test"
  19. }
  20. return rootDir
  21. }
  22. func initTMRoot(rootDir string) {
  23. rootDir = getTMRoot(rootDir)
  24. EnsureDir(rootDir)
  25. configFilePath := path.Join(rootDir, "config.toml")
  26. genesisFilePath := path.Join(rootDir, "genesis.json")
  27. privValFilePath := path.Join(rootDir, "priv_validator.json")
  28. // Write default config file if missing.
  29. if !FileExists(configFilePath) {
  30. // Ask user for moniker
  31. moniker := cfg.Prompt("Type hostname: ", "anonymous")
  32. MustWriteFile(configFilePath, []byte(defaultConfig(moniker)))
  33. }
  34. if !FileExists(genesisFilePath) {
  35. MustWriteFile(genesisFilePath, []byte(defaultGenesis))
  36. }
  37. if !FileExists(privValFilePath) {
  38. MustWriteFile(privValFilePath, []byte(privValFilePath))
  39. }
  40. }
  41. func GetConfig(rootDir string) cfg.Config {
  42. rootDir = getTMRoot(rootDir)
  43. initTMRoot(rootDir)
  44. var mapConfig = cfg.MapConfig(make(map[string]interface{}))
  45. configFilePath := path.Join(rootDir, "config.toml")
  46. configFileBytes := MustReadFile(configFilePath)
  47. err := toml.Unmarshal(configFileBytes, mapConfig)
  48. if err != nil {
  49. Exit(Fmt("Could not read config: %v", err))
  50. }
  51. // Set defaults or panic
  52. if mapConfig.IsSet("chain_id") {
  53. Exit("Cannot set 'chain_id' via config.toml")
  54. }
  55. if mapConfig.IsSet("version") {
  56. Exit("Cannot set 'version' via config.toml")
  57. }
  58. mapConfig.SetDefault("chain_id", "tendermint_test")
  59. mapConfig.SetDefault("version", "0.4.0")
  60. mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json")
  61. mapConfig.SetDefault("moniker", "anonymous")
  62. mapConfig.SetDefault("node_laddr", "0.0.0.0:36656")
  63. mapConfig.SetDefault("fast_sync", false)
  64. mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json")
  65. mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
  66. mapConfig.SetDefault("db_backend", "memdb")
  67. mapConfig.SetDefault("db_dir", rootDir+"/data")
  68. mapConfig.SetDefault("log_level", "debug")
  69. mapConfig.SetDefault("rpc_laddr", "0.0.0.0:36657")
  70. return mapConfig
  71. }
  72. func ensureDefault(mapConfig cfg.MapConfig, key string, value interface{}) {
  73. if !mapConfig.IsSet(key) {
  74. mapConfig[key] = value
  75. }
  76. }
  77. var defaultConfigTmpl = `# This is a TOML config file.
  78. # For more information, see https://github.com/toml-lang/toml
  79. moniker = "__MONIKER__"
  80. node_laddr = "0.0.0.0:36656"
  81. seeds = ""
  82. fast_sync = false
  83. db_backend = "memdb"
  84. log_level = "debug"
  85. rpc_laddr = "0.0.0.0:36657"
  86. `
  87. func defaultConfig(moniker string) (defaultConfig string) {
  88. defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
  89. return
  90. }
  91. // priv keys generated deterministically eg rpc/tests/helpers.go
  92. var defaultGenesis = `{
  93. "chain_id" : "tendermint_test",
  94. "accounts": [
  95. {
  96. "address": "E9B5D87313356465FAE33C406CE2C2979DE60BCB",
  97. "amount": 200000000
  98. },
  99. {
  100. "address": "DFE4AFFA4CEE17CD01CB9E061D77C3ECED29BD88",
  101. "amount": 200000000
  102. },
  103. {
  104. "address": "F60D30722E7B497FA532FB3207C3FB29C31B1992",
  105. "amount": 200000000
  106. },
  107. {
  108. "address": "336CB40A5EB92E496E19B74FDFF2BA017C877FD6",
  109. "amount": 200000000
  110. },
  111. {
  112. "address": "D218F0F439BF0384F6F5EF8D0F8B398D941BD1DC",
  113. "amount": 200000000
  114. }
  115. ],
  116. "validators": [
  117. {
  118. "pub_key": [1, "583779C3BFA3F6C7E23C7D830A9C3D023A216B55079AD38BFED1207B94A19548"],
  119. "amount": 1000000,
  120. "unbond_to": [
  121. {
  122. "address": "E9B5D87313356465FAE33C406CE2C2979DE60BCB",
  123. "amount": 100000
  124. }
  125. ]
  126. }
  127. ]
  128. }`
  129. var defaultPrivValidator = `{
  130. "address": "1D7A91CB32F758A02EBB9BE1FB6F8DEE56F90D42",
  131. "pub_key": [1,"06FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"],
  132. "priv_key": [1,"C453604BD6480D5538B4C6FD2E3E314B5BCE518D75ADE4DA3DA85AB8ADFD819606FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"],
  133. "last_height":0,
  134. "last_round":0,
  135. "last_step":0
  136. }`