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.

282 lines
8.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. package config
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "text/template"
  7. cmn "github.com/tendermint/tmlibs/common"
  8. )
  9. var configTemplate *template.Template
  10. func init() {
  11. var err error
  12. if configTemplate, err = template.New("configFileTemplate").Parse(defaultConfigTemplate); err != nil {
  13. panic(err)
  14. }
  15. }
  16. /****** these are for production settings ***********/
  17. func EnsureRoot(rootDir string) {
  18. if err := cmn.EnsureDir(rootDir, 0700); err != nil {
  19. cmn.PanicSanity(err.Error())
  20. }
  21. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultConfigDir), 0700); err != nil {
  22. cmn.PanicSanity(err.Error())
  23. }
  24. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultDataDir), 0700); err != nil {
  25. cmn.PanicSanity(err.Error())
  26. }
  27. configFilePath := filepath.Join(rootDir, defaultConfigFilePath)
  28. // Write default config file if missing.
  29. if !cmn.FileExists(configFilePath) {
  30. writeConfigFile(configFilePath)
  31. }
  32. }
  33. // XXX: this func should probably be called by cmd/tendermint/commands/init.go
  34. // alongside the writing of the genesis.json and priv_validator.json
  35. func writeConfigFile(configFilePath string) {
  36. var buffer bytes.Buffer
  37. if err := configTemplate.Execute(&buffer, DefaultConfig()); err != nil {
  38. panic(err)
  39. }
  40. cmn.MustWriteFile(configFilePath, buffer.Bytes(), 0644)
  41. }
  42. // Note: any changes to the comments/variables/mapstructure
  43. // must be reflected in the appropriate struct in config/config.go
  44. const defaultConfigTemplate = `# This is a TOML config file.
  45. # For more information, see https://github.com/toml-lang/toml
  46. ##### main base config options #####
  47. # TCP or UNIX socket address of the ABCI application,
  48. # or the name of an ABCI application compiled in with the Tendermint binary
  49. proxy_app = "{{ .BaseConfig.ProxyApp }}"
  50. # A custom human readable name for this node
  51. moniker = "{{ .BaseConfig.Moniker }}"
  52. # If this node is many blocks behind the tip of the chain, FastSync
  53. # allows them to catchup quickly by downloading blocks in parallel
  54. # and verifying their commits
  55. fast_sync = {{ .BaseConfig.FastSync }}
  56. # Database backend: leveldb | memdb
  57. db_backend = "{{ .BaseConfig.DBBackend }}"
  58. # Database directory
  59. db_path = "{{ .BaseConfig.DBPath }}"
  60. # Output level for logging, including package level options
  61. log_level = "{{ .BaseConfig.LogLevel }}"
  62. ##### additional base config options #####
  63. # Path to the JSON file containing the initial validator set and other meta data
  64. genesis_file = "{{ .BaseConfig.Genesis }}"
  65. # Path to the JSON file containing the private key to use as a validator in the consensus protocol
  66. priv_validator_file = "{{ .BaseConfig.PrivValidator }}"
  67. # Mechanism to connect to the ABCI application: socket | grpc
  68. abci = "{{ .BaseConfig.ABCI }}"
  69. # TCP or UNIX socket address for the profiling server to listen on
  70. prof_laddr = "{{ .BaseConfig.ProfListenAddress }}"
  71. # If true, query the ABCI app on connecting to a new peer
  72. # so the app can decide if we should keep the connection or not
  73. filter_peers = {{ .BaseConfig.FilterPeers }}
  74. ##### advanced configuration options #####
  75. ##### rpc server configuration options #####
  76. [rpc]
  77. # TCP or UNIX socket address for the RPC server to listen on
  78. laddr = "{{ .RPC.ListenAddress }}"
  79. # TCP or UNIX socket address for the gRPC server to listen on
  80. # NOTE: This server only supports /broadcast_tx_commit
  81. grpc_laddr = "{{ .RPC.GRPCListenAddress }}"
  82. # Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool
  83. unsafe = {{ .RPC.Unsafe }}
  84. ##### peer to peer configuration options #####
  85. [p2p]
  86. # Address to listen for incoming connections
  87. laddr = "{{ .P2P.ListenAddress }}"
  88. # Comma separated list of seed nodes to connect to
  89. seeds = ""
  90. # Path to address book
  91. addr_book_file = "{{ .P2P.AddrBook }}"
  92. # Set true for strict address routability rules
  93. addr_book_strict = {{ .P2P.AddrBookStrict }}
  94. # Time to wait before flushing messages out on the connection, in ms
  95. flush_throttle_timeout = {{ .P2P.FlushThrottleTimeout }}
  96. # Maximum number of peers to connect to
  97. max_num_peers = {{ .P2P.MaxNumPeers }}
  98. # Maximum size of a message packet payload, in bytes
  99. max_msg_packet_payload_size = {{ .P2P.MaxMsgPacketPayloadSize }}
  100. # Rate at which packets can be sent, in bytes/second
  101. send_rate = {{ .P2P.SendRate }}
  102. # Rate at which packets can be received, in bytes/second
  103. recv_rate = {{ .P2P.RecvRate }}
  104. ##### mempool configuration options #####
  105. [mempool]
  106. recheck = {{ .Mempool.Recheck }}
  107. recheck_empty = {{ .Mempool.RecheckEmpty }}
  108. broadcast = {{ .Mempool.Broadcast }}
  109. wal_dir = "{{ .Mempool.WalPath }}"
  110. ##### consensus configuration options #####
  111. [consensus]
  112. wal_file = "{{ .Consensus.WalPath }}"
  113. wal_light = {{ .Consensus.WalLight }}
  114. # All timeouts are in milliseconds
  115. timeout_propose = {{ .Consensus.TimeoutPropose }}
  116. timeout_propose_delta = {{ .Consensus.TimeoutProposeDelta }}
  117. timeout_prevote = {{ .Consensus.TimeoutPrevote }}
  118. timeout_prevote_delta = {{ .Consensus.TimeoutPrevoteDelta }}
  119. timeout_precommit = {{ .Consensus.TimeoutPrecommit }}
  120. timeout_precommit_delta = {{ .Consensus.TimeoutPrecommitDelta }}
  121. timeout_commit = {{ .Consensus.TimeoutCommit }}
  122. # Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  123. skip_timeout_commit = {{ .Consensus.SkipTimeoutCommit }}
  124. # BlockSize
  125. max_block_size_txs = {{ .Consensus.MaxBlockSizeTxs }}
  126. max_block_size_bytes = {{ .Consensus.MaxBlockSizeBytes }}
  127. # EmptyBlocks mode and possible interval between empty blocks in seconds
  128. create_empty_blocks = {{ .Consensus.CreateEmptyBlocks }}
  129. create_empty_blocks_interval = {{ .Consensus.CreateEmptyBlocksInterval }}
  130. # Reactor sleep duration parameters are in milliseconds
  131. peer_gossip_sleep_duration = {{ .Consensus.PeerGossipSleepDuration }}
  132. peer_query_maj23_sleep_duration = {{ .Consensus.PeerQueryMaj23SleepDuration }}
  133. ##### transactions indexer configuration options #####
  134. [tx_index]
  135. # What indexer to use for transactions
  136. #
  137. # Options:
  138. # 1) "null" (default)
  139. # 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  140. indexer = "{{ .TxIndex.Indexer }}"
  141. # Comma-separated list of tags to index (by default the only tag is tx hash)
  142. #
  143. # It's recommended to index only a subset of tags due to possible memory
  144. # bloat. This is, of course, depends on the indexer's DB and the volume of
  145. # transactions.
  146. index_tags = "{{ .TxIndex.IndexTags }}"
  147. # When set to true, tells indexer to index all tags. Note this may be not
  148. # desirable (see the comment above). IndexTags has a precedence over
  149. # IndexAllTags (i.e. when given both, IndexTags will be indexed).
  150. index_all_tags = {{ .TxIndex.IndexAllTags }}
  151. `
  152. /****** these are for test settings ***********/
  153. func ResetTestRoot(testName string) *Config {
  154. rootDir := os.ExpandEnv("$HOME/.tendermint_test")
  155. rootDir = filepath.Join(rootDir, testName)
  156. // Remove ~/.tendermint_test_bak
  157. if cmn.FileExists(rootDir + "_bak") {
  158. if err := os.RemoveAll(rootDir + "_bak"); err != nil {
  159. cmn.PanicSanity(err.Error())
  160. }
  161. }
  162. // Move ~/.tendermint_test to ~/.tendermint_test_bak
  163. if cmn.FileExists(rootDir) {
  164. if err := os.Rename(rootDir, rootDir+"_bak"); err != nil {
  165. cmn.PanicSanity(err.Error())
  166. }
  167. }
  168. // Create new dir
  169. if err := cmn.EnsureDir(rootDir, 0700); err != nil {
  170. cmn.PanicSanity(err.Error())
  171. }
  172. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultConfigDir), 0700); err != nil {
  173. cmn.PanicSanity(err.Error())
  174. }
  175. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultDataDir), 0700); err != nil {
  176. cmn.PanicSanity(err.Error())
  177. }
  178. baseConfig := DefaultBaseConfig()
  179. configFilePath := filepath.Join(rootDir, defaultConfigFilePath)
  180. genesisFilePath := filepath.Join(rootDir, baseConfig.Genesis)
  181. privFilePath := filepath.Join(rootDir, baseConfig.PrivValidator)
  182. // Write default config file if missing.
  183. if !cmn.FileExists(configFilePath) {
  184. writeConfigFile(configFilePath)
  185. }
  186. if !cmn.FileExists(genesisFilePath) {
  187. cmn.MustWriteFile(genesisFilePath, []byte(testGenesis), 0644)
  188. }
  189. // we always overwrite the priv val
  190. cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644)
  191. config := TestConfig().SetRoot(rootDir)
  192. return config
  193. }
  194. var testGenesis = `{
  195. "genesis_time": "0001-01-01T00:00:00.000Z",
  196. "chain_id": "tendermint_test",
  197. "validators": [
  198. {
  199. "pub_key": {
  200. "type": "ed25519",
  201. "data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  202. },
  203. "power": 10,
  204. "name": ""
  205. }
  206. ],
  207. "app_hash": ""
  208. }`
  209. var testPrivValidator = `{
  210. "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
  211. "pub_key": {
  212. "type": "ed25519",
  213. "data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  214. },
  215. "priv_key": {
  216. "type": "ed25519",
  217. "data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  218. },
  219. "last_height": 0,
  220. "last_round": 0,
  221. "last_step": 0
  222. }`