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.

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