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.

499 lines
17 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 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. // chainID is unexposed and immutable but here for convenience
  71. chainID string
  72. // The root directory for all data.
  73. // This should be set in viper so it can unmarshal into this struct
  74. RootDir string `mapstructure:"home"`
  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. func (c BaseConfig) ChainID() string {
  103. return c.chainID
  104. }
  105. // DefaultBaseConfig returns a default base configuration for a Tendermint node
  106. func DefaultBaseConfig() BaseConfig {
  107. return BaseConfig{
  108. Genesis: defaultGenesisJSONPath,
  109. PrivValidator: defaultPrivValPath,
  110. Moniker: defaultMoniker,
  111. ProxyApp: "tcp://127.0.0.1:46658",
  112. ABCI: "socket",
  113. LogLevel: DefaultPackageLogLevels(),
  114. ProfListenAddress: "",
  115. FastSync: true,
  116. FilterPeers: false,
  117. DBBackend: "leveldb",
  118. DBPath: "data",
  119. }
  120. }
  121. // TestBaseConfig returns a base configuration for testing a Tendermint node
  122. func TestBaseConfig() BaseConfig {
  123. conf := DefaultBaseConfig()
  124. conf.chainID = "tendermint_test"
  125. conf.ProxyApp = "dummy"
  126. conf.FastSync = false
  127. conf.DBBackend = "memdb"
  128. return conf
  129. }
  130. // GenesisFile returns the full path to the genesis.json file
  131. func (b BaseConfig) GenesisFile() string {
  132. return rootify(b.Genesis, b.RootDir)
  133. }
  134. // PrivValidatorFile returns the full path to the priv_validator.json file
  135. func (b BaseConfig) PrivValidatorFile() string {
  136. return rootify(b.PrivValidator, b.RootDir)
  137. }
  138. // DBDir returns the full path to the database directory
  139. func (b BaseConfig) DBDir() string {
  140. return rootify(b.DBPath, b.RootDir)
  141. }
  142. // DefaultLogLevel returns a default log level of "error"
  143. func DefaultLogLevel() string {
  144. return "error"
  145. }
  146. // DefaultPackageLogLevels returns a default log level setting so all packages log at "error", while the `state` package logs at "info"
  147. func DefaultPackageLogLevels() string {
  148. return fmt.Sprintf("state:info,*:%s", DefaultLogLevel())
  149. }
  150. //-----------------------------------------------------------------------------
  151. // RPCConfig
  152. // RPCConfig defines the configuration options for the Tendermint RPC server
  153. type RPCConfig struct {
  154. RootDir string `mapstructure:"home"`
  155. // TCP or UNIX socket address for the RPC server to listen on
  156. ListenAddress string `mapstructure:"laddr"`
  157. // TCP or UNIX socket address for the gRPC server to listen on
  158. // NOTE: This server only supports /broadcast_tx_commit
  159. GRPCListenAddress string `mapstructure:"grpc_laddr"`
  160. // Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool
  161. Unsafe bool `mapstructure:"unsafe"`
  162. }
  163. // DefaultRPCConfig returns a default configuration for the RPC server
  164. func DefaultRPCConfig() *RPCConfig {
  165. return &RPCConfig{
  166. ListenAddress: "tcp://0.0.0.0:46657",
  167. GRPCListenAddress: "",
  168. Unsafe: false,
  169. }
  170. }
  171. // TestRPCConfig returns a configuration for testing the RPC server
  172. func TestRPCConfig() *RPCConfig {
  173. conf := DefaultRPCConfig()
  174. conf.ListenAddress = "tcp://0.0.0.0:36657"
  175. conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
  176. conf.Unsafe = true
  177. return conf
  178. }
  179. //-----------------------------------------------------------------------------
  180. // P2PConfig
  181. // P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer
  182. type P2PConfig struct {
  183. RootDir string `mapstructure:"home"`
  184. // Address to listen for incoming connections
  185. ListenAddress string `mapstructure:"laddr"`
  186. // Comma separated list of seed nodes to connect to
  187. Seeds string `mapstructure:"seeds"`
  188. // Skip UPNP port forwarding
  189. SkipUPNP bool `mapstructure:"skip_upnp"`
  190. // Path to address book
  191. AddrBook string `mapstructure:"addr_book_file"`
  192. // Set true for strict address routability rules
  193. AddrBookStrict bool `mapstructure:"addr_book_strict"`
  194. // Set true to enable the peer-exchange reactor
  195. PexReactor bool `mapstructure:"pex"`
  196. // Maximum number of peers to connect to
  197. MaxNumPeers int `mapstructure:"max_num_peers"`
  198. // Time to wait before flushing messages out on the connection, in ms
  199. FlushThrottleTimeout int `mapstructure:"flush_throttle_timeout"`
  200. // Maximum size of a message packet payload, in bytes
  201. MaxMsgPacketPayloadSize int `mapstructure:"max_msg_packet_payload_size"`
  202. // Rate at which packets can be sent, in bytes/second
  203. SendRate int64 `mapstructure:"send_rate"`
  204. // Rate at which packets can be received, in bytes/second
  205. RecvRate int64 `mapstructure:"recv_rate"`
  206. }
  207. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  208. func DefaultP2PConfig() *P2PConfig {
  209. return &P2PConfig{
  210. ListenAddress: "tcp://0.0.0.0:46656",
  211. AddrBook: "addrbook.json",
  212. AddrBookStrict: true,
  213. MaxNumPeers: 50,
  214. FlushThrottleTimeout: 100,
  215. MaxMsgPacketPayloadSize: 1024, // 1 kB
  216. SendRate: 512000, // 500 kB/s
  217. RecvRate: 512000, // 500 kB/s
  218. PexReactor: true,
  219. }
  220. }
  221. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  222. func TestP2PConfig() *P2PConfig {
  223. conf := DefaultP2PConfig()
  224. conf.ListenAddress = "tcp://0.0.0.0:36656"
  225. conf.SkipUPNP = true
  226. return conf
  227. }
  228. // AddrBookFile returns the full path to the address book
  229. func (p *P2PConfig) AddrBookFile() string {
  230. return rootify(p.AddrBook, p.RootDir)
  231. }
  232. //-----------------------------------------------------------------------------
  233. // MempoolConfig
  234. // MempoolConfig defines the configuration options for the Tendermint mempool
  235. type MempoolConfig struct {
  236. RootDir string `mapstructure:"home"`
  237. Recheck bool `mapstructure:"recheck"`
  238. RecheckEmpty bool `mapstructure:"recheck_empty"`
  239. Broadcast bool `mapstructure:"broadcast"`
  240. WalPath string `mapstructure:"wal_dir"`
  241. }
  242. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  243. func DefaultMempoolConfig() *MempoolConfig {
  244. return &MempoolConfig{
  245. Recheck: true,
  246. RecheckEmpty: true,
  247. Broadcast: true,
  248. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  249. }
  250. }
  251. // WalDir returns the full path to the mempool's write-ahead log
  252. func (m *MempoolConfig) WalDir() string {
  253. return rootify(m.WalPath, m.RootDir)
  254. }
  255. //-----------------------------------------------------------------------------
  256. // ConsensusConfig
  257. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  258. // including timeouts and details about the WAL and the block structure.
  259. type ConsensusConfig struct {
  260. RootDir string `mapstructure:"home"`
  261. WalPath string `mapstructure:"wal_file"`
  262. WalLight bool `mapstructure:"wal_light"`
  263. walFile string // overrides WalPath if set
  264. // All timeouts are in milliseconds
  265. TimeoutPropose int `mapstructure:"timeout_propose"`
  266. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  267. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  268. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  269. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  270. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  271. TimeoutCommit int `mapstructure:"timeout_commit"`
  272. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  273. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  274. // BlockSize
  275. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  276. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  277. // EmptyBlocks mode and possible interval between empty blocks in seconds
  278. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  279. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  280. // Reactor sleep duration parameters are in milliseconds
  281. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  282. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  283. }
  284. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  285. func (cfg *ConsensusConfig) WaitForTxs() bool {
  286. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  287. }
  288. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  289. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  290. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  291. }
  292. // Propose returns the amount of time to wait for a proposal
  293. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  294. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  295. }
  296. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  297. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  298. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  299. }
  300. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  301. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  302. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  303. }
  304. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  305. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  306. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  307. }
  308. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  309. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  310. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  311. }
  312. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  313. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  314. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  315. }
  316. // DefaultConsensusConfig returns a default configuration for the consensus service
  317. func DefaultConsensusConfig() *ConsensusConfig {
  318. return &ConsensusConfig{
  319. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  320. WalLight: false,
  321. TimeoutPropose: 3000,
  322. TimeoutProposeDelta: 500,
  323. TimeoutPrevote: 1000,
  324. TimeoutPrevoteDelta: 500,
  325. TimeoutPrecommit: 1000,
  326. TimeoutPrecommitDelta: 500,
  327. TimeoutCommit: 1000,
  328. SkipTimeoutCommit: false,
  329. MaxBlockSizeTxs: 10000,
  330. MaxBlockSizeBytes: 1, // TODO
  331. CreateEmptyBlocks: true,
  332. CreateEmptyBlocksInterval: 0,
  333. PeerGossipSleepDuration: 100,
  334. PeerQueryMaj23SleepDuration: 2000,
  335. }
  336. }
  337. // TestConsensusConfig returns a configuration for testing the consensus service
  338. func TestConsensusConfig() *ConsensusConfig {
  339. config := DefaultConsensusConfig()
  340. config.TimeoutPropose = 100
  341. config.TimeoutProposeDelta = 1
  342. config.TimeoutPrevote = 10
  343. config.TimeoutPrevoteDelta = 1
  344. config.TimeoutPrecommit = 10
  345. config.TimeoutPrecommitDelta = 1
  346. config.TimeoutCommit = 10
  347. config.SkipTimeoutCommit = true
  348. return config
  349. }
  350. // WalFile returns the full path to the write-ahead log file
  351. func (c *ConsensusConfig) WalFile() string {
  352. if c.walFile != "" {
  353. return c.walFile
  354. }
  355. return rootify(c.WalPath, c.RootDir)
  356. }
  357. // SetWalFile sets the path to the write-ahead log file
  358. func (c *ConsensusConfig) SetWalFile(walFile string) {
  359. c.walFile = walFile
  360. }
  361. //-----------------------------------------------------------------------------
  362. // TxIndexConfig
  363. // TxIndexConfig defines the confuguration for the transaction
  364. // indexer, including tags to index.
  365. type TxIndexConfig struct {
  366. // What indexer to use for transactions
  367. //
  368. // Options:
  369. // 1) "null" (default)
  370. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  371. Indexer string `mapstructure:"indexer"`
  372. // Comma-separated list of tags to index (by default the only tag is tx hash)
  373. //
  374. // It's recommended to index only a subset of tags due to possible memory
  375. // bloat. This is, of course, depends on the indexer's DB and the volume of
  376. // transactions.
  377. IndexTags string `mapstructure:"index_tags"`
  378. // When set to true, tells indexer to index all tags. Note this may be not
  379. // desirable (see the comment above). IndexTags has a precedence over
  380. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  381. IndexAllTags bool `mapstructure:"index_all_tags"`
  382. }
  383. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  384. func DefaultTxIndexConfig() *TxIndexConfig {
  385. return &TxIndexConfig{
  386. Indexer: "kv",
  387. IndexTags: "",
  388. IndexAllTags: false,
  389. }
  390. }
  391. //-----------------------------------------------------------------------------
  392. // Utils
  393. // helper function to make config creation independent of root dir
  394. func rootify(path, root string) string {
  395. if filepath.IsAbs(path) {
  396. return path
  397. }
  398. return filepath.Join(root, path)
  399. }
  400. //-----------------------------------------------------------------------------
  401. // Moniker
  402. var defaultMoniker = getDefaultMoniker()
  403. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  404. // fails to get the host name, "anonymous" will be returned.
  405. func getDefaultMoniker() string {
  406. moniker, err := os.Hostname()
  407. if err != nil {
  408. moniker = "anonymous"
  409. }
  410. return moniker
  411. }