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.

142 lines
3.9 KiB

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