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.

151 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. // 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("anonymous")))
  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("skip_upnp", true)
  61. mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json")
  62. mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
  63. mapConfig.SetDefault("db_backend", "memdb")
  64. mapConfig.SetDefault("db_dir", rootDir+"/data")
  65. mapConfig.SetDefault("log_level", "debug")
  66. mapConfig.SetDefault("rpc_laddr", "0.0.0.0:36657")
  67. mapConfig.SetDefault("revisions_file", rootDir+"/revisions")
  68. return mapConfig
  69. }
  70. func ensureDefault(mapConfig cfg.MapConfig, key string, value interface{}) {
  71. if !mapConfig.IsSet(key) {
  72. mapConfig[key] = value
  73. }
  74. }
  75. var defaultConfigTmpl = `# This is a TOML config file.
  76. # For more information, see https://github.com/toml-lang/toml
  77. moniker = "__MONIKER__"
  78. node_laddr = "0.0.0.0:36656"
  79. seeds = ""
  80. fast_sync = false
  81. db_backend = "memdb"
  82. log_level = "debug"
  83. rpc_laddr = "0.0.0.0:36657"
  84. `
  85. func defaultConfig(moniker string) (defaultConfig string) {
  86. defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
  87. return
  88. }
  89. // priv keys generated deterministically eg rpc/tests/helpers.go
  90. var defaultGenesis = `{
  91. "chain_id" : "tendermint_test",
  92. "accounts": [
  93. {
  94. "address": "E9B5D87313356465FAE33C406CE2C2979DE60BCB",
  95. "amount": 200000000
  96. },
  97. {
  98. "address": "DFE4AFFA4CEE17CD01CB9E061D77C3ECED29BD88",
  99. "amount": 200000000
  100. },
  101. {
  102. "address": "F60D30722E7B497FA532FB3207C3FB29C31B1992",
  103. "amount": 200000000
  104. },
  105. {
  106. "address": "336CB40A5EB92E496E19B74FDFF2BA017C877FD6",
  107. "amount": 200000000
  108. },
  109. {
  110. "address": "D218F0F439BF0384F6F5EF8D0F8B398D941BD1DC",
  111. "amount": 200000000
  112. }
  113. ],
  114. "validators": [
  115. {
  116. "pub_key": [1, "583779C3BFA3F6C7E23C7D830A9C3D023A216B55079AD38BFED1207B94A19548"],
  117. "amount": 1000000,
  118. "unbond_to": [
  119. {
  120. "address": "E9B5D87313356465FAE33C406CE2C2979DE60BCB",
  121. "amount": 100000
  122. }
  123. ]
  124. }
  125. ]
  126. }`
  127. var defaultPrivValidator = `{
  128. "address": "1D7A91CB32F758A02EBB9BE1FB6F8DEE56F90D42",
  129. "pub_key": [1,"06FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"],
  130. "priv_key": [1,"C453604BD6480D5538B4C6FD2E3E314B5BCE518D75ADE4DA3DA85AB8ADFD819606FBAC4E285285D1D91FCBC7E91C780ADA11516F67462340B3980CE2B94940E8"],
  131. "last_height":0,
  132. "last_round":0,
  133. "last_step":0
  134. }`