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.

494 lines
17 KiB

7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. )
  8. // Note: Most of the structs & relevant comments + the
  9. // default configuration options were used to manually
  10. // generate the config.toml. Please reflect any changes
  11. // made here in the defaultConfigTemplate constant in
  12. // config/toml.go
  13. var (
  14. DefaultTendermintDir = ".tendermint"
  15. defaultConfigDir = "config"
  16. defaultDataDir = "data"
  17. defaultConfigFileName = "config.toml"
  18. defaultGenesisJSONName = "genesis.json"
  19. defaultPrivValName = "priv_validator.json"
  20. defaultConfigFilePath = filepath.Join(defaultConfigDir, defaultConfigFileName)
  21. defaultGenesisJSONPath = filepath.Join(defaultConfigDir, defaultGenesisJSONName)
  22. defaultPrivValPath = filepath.Join(defaultConfigDir, defaultPrivValName)
  23. )
  24. // Config defines the top level configuration for a Tendermint node
  25. type Config struct {
  26. // Top level options use an anonymous struct
  27. BaseConfig `mapstructure:",squash"`
  28. // Options for services
  29. RPC *RPCConfig `mapstructure:"rpc"`
  30. P2P *P2PConfig `mapstructure:"p2p"`
  31. Mempool *MempoolConfig `mapstructure:"mempool"`
  32. Consensus *ConsensusConfig `mapstructure:"consensus"`
  33. TxIndex *TxIndexConfig `mapstructure:"tx_index"`
  34. }
  35. // DefaultConfig returns a default configuration for a Tendermint node
  36. func DefaultConfig() *Config {
  37. return &Config{
  38. BaseConfig: DefaultBaseConfig(),
  39. RPC: DefaultRPCConfig(),
  40. P2P: DefaultP2PConfig(),
  41. Mempool: DefaultMempoolConfig(),
  42. Consensus: DefaultConsensusConfig(),
  43. TxIndex: DefaultTxIndexConfig(),
  44. }
  45. }
  46. // TestConfig returns a configuration that can be used for testing
  47. func TestConfig() *Config {
  48. return &Config{
  49. BaseConfig: TestBaseConfig(),
  50. RPC: TestRPCConfig(),
  51. P2P: TestP2PConfig(),
  52. Mempool: DefaultMempoolConfig(),
  53. Consensus: TestConsensusConfig(),
  54. TxIndex: DefaultTxIndexConfig(),
  55. }
  56. }
  57. // SetRoot sets the RootDir for all Config structs
  58. func (cfg *Config) SetRoot(root string) *Config {
  59. cfg.BaseConfig.RootDir = root
  60. cfg.RPC.RootDir = root
  61. cfg.P2P.RootDir = root
  62. cfg.Mempool.RootDir = root
  63. cfg.Consensus.RootDir = root
  64. return cfg
  65. }
  66. //-----------------------------------------------------------------------------
  67. // BaseConfig
  68. // BaseConfig defines the base configuration for a Tendermint node
  69. type BaseConfig struct {
  70. // The root directory for all data.
  71. // This should be set in viper so it can unmarshal into this struct
  72. RootDir string `mapstructure:"home"`
  73. // The ID of the chain to join (should be signed with every transaction and vote)
  74. ChainID string `mapstructure:"chain_id"`
  75. // Path to the JSON file containing the initial validator set and other meta data
  76. Genesis string `mapstructure:"genesis_file"`
  77. // Path to the JSON file containing the private key to use as a validator in the consensus protocol
  78. PrivValidator string `mapstructure:"priv_validator_file"`
  79. // A custom human readable name for this node
  80. Moniker string `mapstructure:"moniker"`
  81. // TCP or UNIX socket address of the ABCI application,
  82. // or the name of an ABCI application compiled in with the Tendermint binary
  83. ProxyApp string `mapstructure:"proxy_app"`
  84. // Mechanism to connect to the ABCI application: socket | grpc
  85. ABCI string `mapstructure:"abci"`
  86. // Output level for logging
  87. LogLevel string `mapstructure:"log_level"`
  88. // TCP or UNIX socket address for the profiling server to listen on
  89. ProfListenAddress string `mapstructure:"prof_laddr"`
  90. // If this node is many blocks behind the tip of the chain, FastSync
  91. // allows them to catchup quickly by downloading blocks in parallel
  92. // and verifying their commits
  93. FastSync bool `mapstructure:"fast_sync"`
  94. // If true, query the ABCI app on connecting to a new peer
  95. // so the app can decide if we should keep the connection or not
  96. FilterPeers bool `mapstructure:"filter_peers"` // false
  97. // Database backend: leveldb | memdb
  98. DBBackend string `mapstructure:"db_backend"`
  99. // Database directory
  100. DBPath string `mapstructure:"db_dir"`
  101. }
  102. // DefaultBaseConfig returns a default base configuration for a Tendermint node
  103. func DefaultBaseConfig() BaseConfig {
  104. return BaseConfig{
  105. Genesis: defaultGenesisJSONPath,
  106. PrivValidator: defaultPrivValPath,
  107. Moniker: defaultMoniker,
  108. ProxyApp: "tcp://127.0.0.1:46658",
  109. ABCI: "socket",
  110. LogLevel: DefaultPackageLogLevels(),
  111. ProfListenAddress: "",
  112. FastSync: true,
  113. FilterPeers: false,
  114. DBBackend: "leveldb",
  115. DBPath: "data",
  116. }
  117. }
  118. // TestBaseConfig returns a base configuration for testing a Tendermint node
  119. func TestBaseConfig() BaseConfig {
  120. conf := DefaultBaseConfig()
  121. conf.ChainID = "tendermint_test"
  122. conf.ProxyApp = "dummy"
  123. conf.FastSync = false
  124. conf.DBBackend = "memdb"
  125. return conf
  126. }
  127. // GenesisFile returns the full path to the genesis.json file
  128. func (b BaseConfig) GenesisFile() string {
  129. return rootify(b.Genesis, b.RootDir)
  130. }
  131. // PrivValidatorFile returns the full path to the priv_validator.json file
  132. func (b BaseConfig) PrivValidatorFile() string {
  133. return rootify(b.PrivValidator, b.RootDir)
  134. }
  135. // DBDir returns the full path to the database directory
  136. func (b BaseConfig) DBDir() string {
  137. return rootify(b.DBPath, b.RootDir)
  138. }
  139. // DefaultLogLevel returns a default log level of "error"
  140. func DefaultLogLevel() string {
  141. return "error"
  142. }
  143. // DefaultPackageLogLevels returns a default log level setting so all packages log at "error", while the `state` package logs at "info"
  144. func DefaultPackageLogLevels() string {
  145. return fmt.Sprintf("state:info,*:%s", DefaultLogLevel())
  146. }
  147. //-----------------------------------------------------------------------------
  148. // RPCConfig
  149. // RPCConfig defines the configuration options for the Tendermint RPC server
  150. type RPCConfig struct {
  151. RootDir string `mapstructure:"home"`
  152. // TCP or UNIX socket address for the RPC server to listen on
  153. ListenAddress string `mapstructure:"laddr"`
  154. // TCP or UNIX socket address for the gRPC server to listen on
  155. // NOTE: This server only supports /broadcast_tx_commit
  156. GRPCListenAddress string `mapstructure:"grpc_laddr"`
  157. // Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool
  158. Unsafe bool `mapstructure:"unsafe"`
  159. }
  160. // DefaultRPCConfig returns a default configuration for the RPC server
  161. func DefaultRPCConfig() *RPCConfig {
  162. return &RPCConfig{
  163. ListenAddress: "tcp://0.0.0.0:46657",
  164. GRPCListenAddress: "",
  165. Unsafe: false,
  166. }
  167. }
  168. // TestRPCConfig returns a configuration for testing the RPC server
  169. func TestRPCConfig() *RPCConfig {
  170. conf := DefaultRPCConfig()
  171. conf.ListenAddress = "tcp://0.0.0.0:36657"
  172. conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
  173. conf.Unsafe = true
  174. return conf
  175. }
  176. //-----------------------------------------------------------------------------
  177. // P2PConfig
  178. // P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer
  179. type P2PConfig struct {
  180. RootDir string `mapstructure:"home"`
  181. // Address to listen for incoming connections
  182. ListenAddress string `mapstructure:"laddr"`
  183. // Comma separated list of seed nodes to connect to
  184. Seeds string `mapstructure:"seeds"`
  185. // Skip UPNP port forwarding
  186. SkipUPNP bool `mapstructure:"skip_upnp"`
  187. // Path to address book
  188. AddrBook string `mapstructure:"addr_book_file"`
  189. // Set true for strict address routability rules
  190. AddrBookStrict bool `mapstructure:"addr_book_strict"`
  191. // Set true to enable the peer-exchange reactor
  192. PexReactor bool `mapstructure:"pex"`
  193. // Maximum number of peers to connect to
  194. MaxNumPeers int `mapstructure:"max_num_peers"`
  195. // Time to wait before flushing messages out on the connection, in ms
  196. FlushThrottleTimeout int `mapstructure:"flush_throttle_timeout"`
  197. // Maximum size of a message packet payload, in bytes
  198. MaxMsgPacketPayloadSize int `mapstructure:"max_msg_packet_payload_size"`
  199. // Rate at which packets can be sent, in bytes/second
  200. SendRate int64 `mapstructure:"send_rate"`
  201. // Rate at which packets can be received, in bytes/second
  202. RecvRate int64 `mapstructure:"recv_rate"`
  203. }
  204. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  205. func DefaultP2PConfig() *P2PConfig {
  206. return &P2PConfig{
  207. ListenAddress: "tcp://0.0.0.0:46656",
  208. AddrBook: "addrbook.json",
  209. AddrBookStrict: true,
  210. MaxNumPeers: 50,
  211. FlushThrottleTimeout: 100,
  212. MaxMsgPacketPayloadSize: 1024, // 1 kB
  213. SendRate: 512000, // 500 kB/s
  214. RecvRate: 512000, // 500 kB/s
  215. PexReactor: true,
  216. }
  217. }
  218. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  219. func TestP2PConfig() *P2PConfig {
  220. conf := DefaultP2PConfig()
  221. conf.ListenAddress = "tcp://0.0.0.0:36656"
  222. conf.SkipUPNP = true
  223. return conf
  224. }
  225. // AddrBookFile returns the full path to the address book
  226. func (p *P2PConfig) AddrBookFile() string {
  227. return rootify(p.AddrBook, p.RootDir)
  228. }
  229. //-----------------------------------------------------------------------------
  230. // MempoolConfig
  231. // MempoolConfig defines the configuration options for the Tendermint mempool
  232. type MempoolConfig struct {
  233. RootDir string `mapstructure:"home"`
  234. Recheck bool `mapstructure:"recheck"`
  235. RecheckEmpty bool `mapstructure:"recheck_empty"`
  236. Broadcast bool `mapstructure:"broadcast"`
  237. WalPath string `mapstructure:"wal_dir"`
  238. }
  239. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  240. func DefaultMempoolConfig() *MempoolConfig {
  241. return &MempoolConfig{
  242. Recheck: true,
  243. RecheckEmpty: true,
  244. Broadcast: true,
  245. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  246. }
  247. }
  248. // WalDir returns the full path to the mempool's write-ahead log
  249. func (m *MempoolConfig) WalDir() string {
  250. return rootify(m.WalPath, m.RootDir)
  251. }
  252. //-----------------------------------------------------------------------------
  253. // ConsensusConfig
  254. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  255. // including timeouts and details about the WAL and the block structure.
  256. type ConsensusConfig struct {
  257. RootDir string `mapstructure:"home"`
  258. WalPath string `mapstructure:"wal_file"`
  259. WalLight bool `mapstructure:"wal_light"`
  260. walFile string // overrides WalPath if set
  261. // All timeouts are in milliseconds
  262. TimeoutPropose int `mapstructure:"timeout_propose"`
  263. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  264. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  265. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  266. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  267. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  268. TimeoutCommit int `mapstructure:"timeout_commit"`
  269. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  270. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  271. // BlockSize
  272. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  273. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  274. // EmptyBlocks mode and possible interval between empty blocks in seconds
  275. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  276. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  277. // Reactor sleep duration parameters are in milliseconds
  278. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  279. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  280. }
  281. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  282. func (cfg *ConsensusConfig) WaitForTxs() bool {
  283. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  284. }
  285. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  286. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  287. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  288. }
  289. // Propose returns the amount of time to wait for a proposal
  290. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  291. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  292. }
  293. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  294. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  295. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  296. }
  297. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  298. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  299. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  300. }
  301. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  302. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  303. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  304. }
  305. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  306. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  307. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  308. }
  309. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  310. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  311. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  312. }
  313. // DefaultConsensusConfig returns a default configuration for the consensus service
  314. func DefaultConsensusConfig() *ConsensusConfig {
  315. return &ConsensusConfig{
  316. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  317. WalLight: false,
  318. TimeoutPropose: 3000,
  319. TimeoutProposeDelta: 500,
  320. TimeoutPrevote: 1000,
  321. TimeoutPrevoteDelta: 500,
  322. TimeoutPrecommit: 1000,
  323. TimeoutPrecommitDelta: 500,
  324. TimeoutCommit: 1000,
  325. SkipTimeoutCommit: false,
  326. MaxBlockSizeTxs: 10000,
  327. MaxBlockSizeBytes: 1, // TODO
  328. CreateEmptyBlocks: true,
  329. CreateEmptyBlocksInterval: 0,
  330. PeerGossipSleepDuration: 100,
  331. PeerQueryMaj23SleepDuration: 2000,
  332. }
  333. }
  334. // TestConsensusConfig returns a configuration for testing the consensus service
  335. func TestConsensusConfig() *ConsensusConfig {
  336. config := DefaultConsensusConfig()
  337. config.TimeoutPropose = 100
  338. config.TimeoutProposeDelta = 1
  339. config.TimeoutPrevote = 10
  340. config.TimeoutPrevoteDelta = 1
  341. config.TimeoutPrecommit = 10
  342. config.TimeoutPrecommitDelta = 1
  343. config.TimeoutCommit = 10
  344. config.SkipTimeoutCommit = true
  345. return config
  346. }
  347. // WalFile returns the full path to the write-ahead log file
  348. func (c *ConsensusConfig) WalFile() string {
  349. if c.walFile != "" {
  350. return c.walFile
  351. }
  352. return rootify(c.WalPath, c.RootDir)
  353. }
  354. // SetWalFile sets the path to the write-ahead log file
  355. func (c *ConsensusConfig) SetWalFile(walFile string) {
  356. c.walFile = walFile
  357. }
  358. //-----------------------------------------------------------------------------
  359. // TxIndexConfig
  360. // TxIndexConfig defines the confuguration for the transaction
  361. // indexer, including tags to index.
  362. type TxIndexConfig struct {
  363. // What indexer to use for transactions
  364. //
  365. // Options:
  366. // 1) "null" (default)
  367. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  368. Indexer string `mapstructure:"indexer"`
  369. // Comma-separated list of tags to index (by default the only tag is tx hash)
  370. //
  371. // It's recommended to index only a subset of tags due to possible memory
  372. // bloat. This is, of course, depends on the indexer's DB and the volume of
  373. // transactions.
  374. IndexTags string `mapstructure:"index_tags"`
  375. // When set to true, tells indexer to index all tags. Note this may be not
  376. // desirable (see the comment above). IndexTags has a precedence over
  377. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  378. IndexAllTags bool `mapstructure:"index_all_tags"`
  379. }
  380. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  381. func DefaultTxIndexConfig() *TxIndexConfig {
  382. return &TxIndexConfig{
  383. Indexer: "kv",
  384. IndexTags: "",
  385. IndexAllTags: false,
  386. }
  387. }
  388. //-----------------------------------------------------------------------------
  389. // Utils
  390. // helper function to make config creation independent of root dir
  391. func rootify(path, root string) string {
  392. if filepath.IsAbs(path) {
  393. return path
  394. }
  395. return filepath.Join(root, path)
  396. }
  397. //-----------------------------------------------------------------------------
  398. // Moniker
  399. var defaultMoniker = getDefaultMoniker()
  400. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  401. // fails to get the host name, "anonymous" will be returned.
  402. func getDefaultMoniker() string {
  403. moniker, err := os.Hostname()
  404. if err != nil {
  405. moniker = "anonymous"
  406. }
  407. return moniker
  408. }