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.

285 lines
8.7 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. # Comma separated list of nodes to keep persistent connections to
  91. persistent_peers = ""
  92. # Path to address book
  93. addr_book_file = "{{ .P2P.AddrBook }}"
  94. # Set true for strict address routability rules
  95. addr_book_strict = {{ .P2P.AddrBookStrict }}
  96. # Time to wait before flushing messages out on the connection, in ms
  97. flush_throttle_timeout = {{ .P2P.FlushThrottleTimeout }}
  98. # Maximum number of peers to connect to
  99. max_num_peers = {{ .P2P.MaxNumPeers }}
  100. # Maximum size of a message packet payload, in bytes
  101. max_msg_packet_payload_size = {{ .P2P.MaxMsgPacketPayloadSize }}
  102. # Rate at which packets can be sent, in bytes/second
  103. send_rate = {{ .P2P.SendRate }}
  104. # Rate at which packets can be received, in bytes/second
  105. recv_rate = {{ .P2P.RecvRate }}
  106. ##### mempool configuration options #####
  107. [mempool]
  108. recheck = {{ .Mempool.Recheck }}
  109. recheck_empty = {{ .Mempool.RecheckEmpty }}
  110. broadcast = {{ .Mempool.Broadcast }}
  111. wal_dir = "{{ .Mempool.WalPath }}"
  112. ##### consensus configuration options #####
  113. [consensus]
  114. wal_file = "{{ .Consensus.WalPath }}"
  115. wal_light = {{ .Consensus.WalLight }}
  116. # All timeouts are in milliseconds
  117. timeout_propose = {{ .Consensus.TimeoutPropose }}
  118. timeout_propose_delta = {{ .Consensus.TimeoutProposeDelta }}
  119. timeout_prevote = {{ .Consensus.TimeoutPrevote }}
  120. timeout_prevote_delta = {{ .Consensus.TimeoutPrevoteDelta }}
  121. timeout_precommit = {{ .Consensus.TimeoutPrecommit }}
  122. timeout_precommit_delta = {{ .Consensus.TimeoutPrecommitDelta }}
  123. timeout_commit = {{ .Consensus.TimeoutCommit }}
  124. # Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  125. skip_timeout_commit = {{ .Consensus.SkipTimeoutCommit }}
  126. # BlockSize
  127. max_block_size_txs = {{ .Consensus.MaxBlockSizeTxs }}
  128. max_block_size_bytes = {{ .Consensus.MaxBlockSizeBytes }}
  129. # EmptyBlocks mode and possible interval between empty blocks in seconds
  130. create_empty_blocks = {{ .Consensus.CreateEmptyBlocks }}
  131. create_empty_blocks_interval = {{ .Consensus.CreateEmptyBlocksInterval }}
  132. # Reactor sleep duration parameters are in milliseconds
  133. peer_gossip_sleep_duration = {{ .Consensus.PeerGossipSleepDuration }}
  134. peer_query_maj23_sleep_duration = {{ .Consensus.PeerQueryMaj23SleepDuration }}
  135. ##### transactions indexer configuration options #####
  136. [tx_index]
  137. # What indexer to use for transactions
  138. #
  139. # Options:
  140. # 1) "null" (default)
  141. # 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  142. indexer = "{{ .TxIndex.Indexer }}"
  143. # Comma-separated list of tags to index (by default the only tag is tx hash)
  144. #
  145. # It's recommended to index only a subset of tags due to possible memory
  146. # bloat. This is, of course, depends on the indexer's DB and the volume of
  147. # transactions.
  148. index_tags = "{{ .TxIndex.IndexTags }}"
  149. # When set to true, tells indexer to index all tags. Note this may be not
  150. # desirable (see the comment above). IndexTags has a precedence over
  151. # IndexAllTags (i.e. when given both, IndexTags will be indexed).
  152. index_all_tags = {{ .TxIndex.IndexAllTags }}
  153. `
  154. /****** these are for test settings ***********/
  155. func ResetTestRoot(testName string) *Config {
  156. rootDir := os.ExpandEnv("$HOME/.tendermint_test")
  157. rootDir = filepath.Join(rootDir, testName)
  158. // Remove ~/.tendermint_test_bak
  159. if cmn.FileExists(rootDir + "_bak") {
  160. if err := os.RemoveAll(rootDir + "_bak"); err != nil {
  161. cmn.PanicSanity(err.Error())
  162. }
  163. }
  164. // Move ~/.tendermint_test to ~/.tendermint_test_bak
  165. if cmn.FileExists(rootDir) {
  166. if err := os.Rename(rootDir, rootDir+"_bak"); err != nil {
  167. cmn.PanicSanity(err.Error())
  168. }
  169. }
  170. // Create new dir
  171. if err := cmn.EnsureDir(rootDir, 0700); err != nil {
  172. cmn.PanicSanity(err.Error())
  173. }
  174. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultConfigDir), 0700); err != nil {
  175. cmn.PanicSanity(err.Error())
  176. }
  177. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultDataDir), 0700); err != nil {
  178. cmn.PanicSanity(err.Error())
  179. }
  180. baseConfig := DefaultBaseConfig()
  181. configFilePath := filepath.Join(rootDir, defaultConfigFilePath)
  182. genesisFilePath := filepath.Join(rootDir, baseConfig.Genesis)
  183. privFilePath := filepath.Join(rootDir, baseConfig.PrivValidator)
  184. // Write default config file if missing.
  185. if !cmn.FileExists(configFilePath) {
  186. writeConfigFile(configFilePath)
  187. }
  188. if !cmn.FileExists(genesisFilePath) {
  189. cmn.MustWriteFile(genesisFilePath, []byte(testGenesis), 0644)
  190. }
  191. // we always overwrite the priv val
  192. cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644)
  193. config := TestConfig().SetRoot(rootDir)
  194. return config
  195. }
  196. var testGenesis = `{
  197. "genesis_time": "0001-01-01T00:00:00.000Z",
  198. "chain_id": "tendermint_test",
  199. "validators": [
  200. {
  201. "pub_key": {
  202. "type": "ed25519",
  203. "data":"3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  204. },
  205. "power": 10,
  206. "name": ""
  207. }
  208. ],
  209. "app_hash": ""
  210. }`
  211. var testPrivValidator = `{
  212. "address": "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456",
  213. "pub_key": {
  214. "type": "ed25519",
  215. "data": "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  216. },
  217. "priv_key": {
  218. "type": "ed25519",
  219. "data": "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  220. },
  221. "last_height": 0,
  222. "last_round": 0,
  223. "last_step": 0
  224. }`