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.

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