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.

288 lines
8.9 KiB

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