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.

349 lines
11 KiB

  1. package config
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "text/template"
  7. cmn "github.com/tendermint/tendermint/libs/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. writeDefaultConfigFile(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 writeDefaultConfigFile(configFilePath string) {
  38. WriteConfigFile(configFilePath, DefaultConfig())
  39. }
  40. // WriteConfigFile renders config using the template and writes it to configFilePath.
  41. func WriteConfigFile(configFilePath string, config *Config) {
  42. var buffer bytes.Buffer
  43. if err := configTemplate.Execute(&buffer, config); err != nil {
  44. panic(err)
  45. }
  46. cmn.MustWriteFile(configFilePath, buffer.Bytes(), 0644)
  47. }
  48. // Note: any changes to the comments/variables/mapstructure
  49. // must be reflected in the appropriate struct in config/config.go
  50. const defaultConfigTemplate = `# This is a TOML config file.
  51. # For more information, see https://github.com/toml-lang/toml
  52. ##### main base config options #####
  53. # TCP or UNIX socket address of the ABCI application,
  54. # or the name of an ABCI application compiled in with the Tendermint binary
  55. proxy_app = "{{ .BaseConfig.ProxyApp }}"
  56. # A custom human readable name for this node
  57. moniker = "{{ .BaseConfig.Moniker }}"
  58. # If this node is many blocks behind the tip of the chain, FastSync
  59. # allows them to catchup quickly by downloading blocks in parallel
  60. # and verifying their commits
  61. fast_sync = {{ .BaseConfig.FastSync }}
  62. # Database backend: leveldb | memdb
  63. db_backend = "{{ .BaseConfig.DBBackend }}"
  64. # Database directory
  65. db_path = "{{ js .BaseConfig.DBPath }}"
  66. # Output level for logging, including package level options
  67. log_level = "{{ .BaseConfig.LogLevel }}"
  68. ##### additional base config options #####
  69. # Path to the JSON file containing the initial validator set and other meta data
  70. genesis_file = "{{ js .BaseConfig.Genesis }}"
  71. # Path to the JSON file containing the private key to use as a validator in the consensus protocol
  72. priv_validator_file = "{{ js .BaseConfig.PrivValidator }}"
  73. # Path to the JSON file containing the private key to use for node authentication in the p2p protocol
  74. node_key_file = "{{ js .BaseConfig.NodeKey}}"
  75. # Mechanism to connect to the ABCI application: socket | grpc
  76. abci = "{{ .BaseConfig.ABCI }}"
  77. # TCP or UNIX socket address for the profiling server to listen on
  78. prof_laddr = "{{ .BaseConfig.ProfListenAddress }}"
  79. # If true, query the ABCI app on connecting to a new peer
  80. # so the app can decide if we should keep the connection or not
  81. filter_peers = {{ .BaseConfig.FilterPeers }}
  82. ##### advanced configuration options #####
  83. ##### rpc server configuration options #####
  84. [rpc]
  85. # TCP or UNIX socket address for the RPC server to listen on
  86. laddr = "{{ .RPC.ListenAddress }}"
  87. # TCP or UNIX socket address for the gRPC server to listen on
  88. # NOTE: This server only supports /broadcast_tx_commit
  89. grpc_laddr = "{{ .RPC.GRPCListenAddress }}"
  90. # Maximum number of simultaneous connections.
  91. # Does not include RPC (HTTP&WebSocket) connections. See max_open_connections
  92. # If you want to accept more significant number than the default, make sure
  93. # you increase your OS limits.
  94. # 0 - unlimited.
  95. grpc_max_open_connections = {{ .RPC.GRPCMaxOpenConnections }}
  96. # Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool
  97. unsafe = {{ .RPC.Unsafe }}
  98. # Maximum number of simultaneous connections (including WebSocket).
  99. # Does not include gRPC connections. See grpc_max_open_connections
  100. # If you want to accept more significant number than the default, make sure
  101. # you increase your OS limits.
  102. # 0 - unlimited.
  103. max_open_connections = {{ .RPC.MaxOpenConnections }}
  104. ##### peer to peer configuration options #####
  105. [p2p]
  106. # Address to listen for incoming connections
  107. laddr = "{{ .P2P.ListenAddress }}"
  108. # Address to advertise to peers for them to dial
  109. # If empty, will use the same port as the laddr,
  110. # and will introspect on the listener or use UPnP
  111. # to figure out the address.
  112. external_address = "{{ .P2P.ExternalAddress }}"
  113. # Comma separated list of seed nodes to connect to
  114. seeds = "{{ .P2P.Seeds }}"
  115. # Comma separated list of nodes to keep persistent connections to
  116. # Do not add private peers to this list if you don't want them advertised
  117. persistent_peers = "{{ .P2P.PersistentPeers }}"
  118. # UPNP port forwarding
  119. upnp = {{ .P2P.UPNP }}
  120. # Path to address book
  121. addr_book_file = "{{ js .P2P.AddrBook }}"
  122. # Set true for strict address routability rules
  123. addr_book_strict = {{ .P2P.AddrBookStrict }}
  124. # Time to wait before flushing messages out on the connection, in ms
  125. flush_throttle_timeout = {{ .P2P.FlushThrottleTimeout }}
  126. # Maximum number of peers to connect to
  127. max_num_peers = {{ .P2P.MaxNumPeers }}
  128. # Maximum size of a message packet payload, in bytes
  129. max_packet_msg_payload_size = {{ .P2P.MaxPacketMsgPayloadSize }}
  130. # Rate at which packets can be sent, in bytes/second
  131. send_rate = {{ .P2P.SendRate }}
  132. # Rate at which packets can be received, in bytes/second
  133. recv_rate = {{ .P2P.RecvRate }}
  134. # Set true to enable the peer-exchange reactor
  135. pex = {{ .P2P.PexReactor }}
  136. # Seed mode, in which node constantly crawls the network and looks for
  137. # peers. If another node asks it for addresses, it responds and disconnects.
  138. #
  139. # Does not work if the peer-exchange reactor is disabled.
  140. seed_mode = {{ .P2P.SeedMode }}
  141. # Comma separated list of peer IDs to keep private (will not be gossiped to other peers)
  142. private_peer_ids = "{{ .P2P.PrivatePeerIDs }}"
  143. ##### mempool configuration options #####
  144. [mempool]
  145. recheck = {{ .Mempool.Recheck }}
  146. recheck_empty = {{ .Mempool.RecheckEmpty }}
  147. broadcast = {{ .Mempool.Broadcast }}
  148. wal_dir = "{{ js .Mempool.WalPath }}"
  149. # size of the mempool
  150. size = {{ .Mempool.Size }}
  151. # size of the cache (used to filter transactions we saw earlier)
  152. cache_size = {{ .Mempool.CacheSize }}
  153. ##### consensus configuration options #####
  154. [consensus]
  155. wal_file = "{{ js .Consensus.WalPath }}"
  156. # All timeouts are in milliseconds
  157. timeout_propose = {{ .Consensus.TimeoutPropose }}
  158. timeout_propose_delta = {{ .Consensus.TimeoutProposeDelta }}
  159. timeout_prevote = {{ .Consensus.TimeoutPrevote }}
  160. timeout_prevote_delta = {{ .Consensus.TimeoutPrevoteDelta }}
  161. timeout_precommit = {{ .Consensus.TimeoutPrecommit }}
  162. timeout_precommit_delta = {{ .Consensus.TimeoutPrecommitDelta }}
  163. timeout_commit = {{ .Consensus.TimeoutCommit }}
  164. # Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  165. skip_timeout_commit = {{ .Consensus.SkipTimeoutCommit }}
  166. # EmptyBlocks mode and possible interval between empty blocks in seconds
  167. create_empty_blocks = {{ .Consensus.CreateEmptyBlocks }}
  168. create_empty_blocks_interval = {{ .Consensus.CreateEmptyBlocksInterval }}
  169. # Reactor sleep duration parameters are in milliseconds
  170. peer_gossip_sleep_duration = {{ .Consensus.PeerGossipSleepDuration }}
  171. peer_query_maj23_sleep_duration = {{ .Consensus.PeerQueryMaj23SleepDuration }}
  172. ##### transactions indexer configuration options #####
  173. [tx_index]
  174. # What indexer to use for transactions
  175. #
  176. # Options:
  177. # 1) "null" (default)
  178. # 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  179. indexer = "{{ .TxIndex.Indexer }}"
  180. # Comma-separated list of tags to index (by default the only tag is tx hash)
  181. #
  182. # It's recommended to index only a subset of tags due to possible memory
  183. # bloat. This is, of course, depends on the indexer's DB and the volume of
  184. # transactions.
  185. index_tags = "{{ .TxIndex.IndexTags }}"
  186. # When set to true, tells indexer to index all tags. Note this may be not
  187. # desirable (see the comment above). IndexTags has a precedence over
  188. # IndexAllTags (i.e. when given both, IndexTags will be indexed).
  189. index_all_tags = {{ .TxIndex.IndexAllTags }}
  190. ##### instrumentation configuration options #####
  191. [instrumentation]
  192. # When true, Prometheus metrics are served under /metrics on
  193. # PrometheusListenAddr.
  194. # Check out the documentation for the list of available metrics.
  195. prometheus = {{ .Instrumentation.Prometheus }}
  196. # Address to listen for Prometheus collector(s) connections
  197. prometheus_listen_addr = "{{ .Instrumentation.PrometheusListenAddr }}"
  198. # Maximum number of simultaneous connections.
  199. # If you want to accept more significant number than the default, make sure
  200. # you increase your OS limits.
  201. # 0 - unlimited.
  202. max_open_connections = {{ .Instrumentation.MaxOpenConnections }}
  203. `
  204. /****** these are for test settings ***********/
  205. func ResetTestRoot(testName string) *Config {
  206. rootDir := os.ExpandEnv("$HOME/.tendermint_test")
  207. rootDir = filepath.Join(rootDir, testName)
  208. // Remove ~/.tendermint_test_bak
  209. if cmn.FileExists(rootDir + "_bak") {
  210. if err := os.RemoveAll(rootDir + "_bak"); err != nil {
  211. cmn.PanicSanity(err.Error())
  212. }
  213. }
  214. // Move ~/.tendermint_test to ~/.tendermint_test_bak
  215. if cmn.FileExists(rootDir) {
  216. if err := os.Rename(rootDir, rootDir+"_bak"); err != nil {
  217. cmn.PanicSanity(err.Error())
  218. }
  219. }
  220. // Create new dir
  221. if err := cmn.EnsureDir(rootDir, 0700); err != nil {
  222. cmn.PanicSanity(err.Error())
  223. }
  224. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultConfigDir), 0700); err != nil {
  225. cmn.PanicSanity(err.Error())
  226. }
  227. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultDataDir), 0700); err != nil {
  228. cmn.PanicSanity(err.Error())
  229. }
  230. baseConfig := DefaultBaseConfig()
  231. configFilePath := filepath.Join(rootDir, defaultConfigFilePath)
  232. genesisFilePath := filepath.Join(rootDir, baseConfig.Genesis)
  233. privFilePath := filepath.Join(rootDir, baseConfig.PrivValidator)
  234. // Write default config file if missing.
  235. if !cmn.FileExists(configFilePath) {
  236. writeDefaultConfigFile(configFilePath)
  237. }
  238. if !cmn.FileExists(genesisFilePath) {
  239. cmn.MustWriteFile(genesisFilePath, []byte(testGenesis), 0644)
  240. }
  241. // we always overwrite the priv val
  242. cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644)
  243. config := TestConfig().SetRoot(rootDir)
  244. return config
  245. }
  246. var testGenesis = `{
  247. "genesis_time": "0001-01-01T00:00:00.000Z",
  248. "chain_id": "tendermint_test",
  249. "validators": [
  250. {
  251. "pub_key": {
  252. "type": "tendermint/PubKeyEd25519",
  253. "value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
  254. },
  255. "power": "10",
  256. "name": ""
  257. }
  258. ],
  259. "app_hash": ""
  260. }`
  261. var testPrivValidator = `{
  262. "address": "A3258DCBF45DCA0DF052981870F2D1441A36D145",
  263. "pub_key": {
  264. "type": "tendermint/PubKeyEd25519",
  265. "value": "AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
  266. },
  267. "priv_key": {
  268. "type": "tendermint/PrivKeyEd25519",
  269. "value": "EVkqJO/jIXp3rkASXfh9YnyToYXRXhBr6g9cQVxPFnQBP/5povV4HTjvsy530kybxKHwEi85iU8YL0qQhSYVoQ=="
  270. },
  271. "last_height": "0",
  272. "last_round": "0",
  273. "last_step": 0
  274. }`