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.

150 lines
4.1 KiB

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