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.

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