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.

504 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_persistent_peers 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. // We only use these if we can’t connect to peers in the addrbook
  188. Seeds string `mapstructure:"seeds"`
  189. // Comma separated list of persistent peers to connect to
  190. // We always connect to these
  191. PersistentPeers string `mapstructure:"persistent_peers"`
  192. // Skip UPNP port forwarding
  193. SkipUPNP bool `mapstructure:"skip_upnp"`
  194. // Path to address book
  195. AddrBook string `mapstructure:"addr_book_file"`
  196. // Set true for strict address routability rules
  197. AddrBookStrict bool `mapstructure:"addr_book_strict"`
  198. // Set true to enable the peer-exchange reactor
  199. PexReactor bool `mapstructure:"pex"`
  200. // Maximum number of peers to connect to
  201. MaxNumPeers int `mapstructure:"max_num_peers"`
  202. // Time to wait before flushing messages out on the connection, in ms
  203. FlushThrottleTimeout int `mapstructure:"flush_throttle_timeout"`
  204. // Maximum size of a message packet payload, in bytes
  205. MaxMsgPacketPayloadSize int `mapstructure:"max_msg_packet_payload_size"`
  206. // Rate at which packets can be sent, in bytes/second
  207. SendRate int64 `mapstructure:"send_rate"`
  208. // Rate at which packets can be received, in bytes/second
  209. RecvRate int64 `mapstructure:"recv_rate"`
  210. }
  211. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  212. func DefaultP2PConfig() *P2PConfig {
  213. return &P2PConfig{
  214. ListenAddress: "tcp://0.0.0.0:46656",
  215. AddrBook: "addrbook.json",
  216. AddrBookStrict: true,
  217. MaxNumPeers: 50,
  218. FlushThrottleTimeout: 100,
  219. MaxMsgPacketPayloadSize: 1024, // 1 kB
  220. SendRate: 512000, // 500 kB/s
  221. RecvRate: 512000, // 500 kB/s
  222. PexReactor: true,
  223. }
  224. }
  225. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  226. func TestP2PConfig() *P2PConfig {
  227. conf := DefaultP2PConfig()
  228. conf.ListenAddress = "tcp://0.0.0.0:36656"
  229. conf.SkipUPNP = true
  230. return conf
  231. }
  232. // AddrBookFile returns the full path to the address book
  233. func (p *P2PConfig) AddrBookFile() string {
  234. return rootify(p.AddrBook, p.RootDir)
  235. }
  236. //-----------------------------------------------------------------------------
  237. // MempoolConfig
  238. // MempoolConfig defines the configuration options for the Tendermint mempool
  239. type MempoolConfig struct {
  240. RootDir string `mapstructure:"home"`
  241. Recheck bool `mapstructure:"recheck"`
  242. RecheckEmpty bool `mapstructure:"recheck_empty"`
  243. Broadcast bool `mapstructure:"broadcast"`
  244. WalPath string `mapstructure:"wal_dir"`
  245. }
  246. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  247. func DefaultMempoolConfig() *MempoolConfig {
  248. return &MempoolConfig{
  249. Recheck: true,
  250. RecheckEmpty: true,
  251. Broadcast: true,
  252. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  253. }
  254. }
  255. // WalDir returns the full path to the mempool's write-ahead log
  256. func (m *MempoolConfig) WalDir() string {
  257. return rootify(m.WalPath, m.RootDir)
  258. }
  259. //-----------------------------------------------------------------------------
  260. // ConsensusConfig
  261. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  262. // including timeouts and details about the WAL and the block structure.
  263. type ConsensusConfig struct {
  264. RootDir string `mapstructure:"home"`
  265. WalPath string `mapstructure:"wal_file"`
  266. WalLight bool `mapstructure:"wal_light"`
  267. walFile string // overrides WalPath if set
  268. // All timeouts are in milliseconds
  269. TimeoutPropose int `mapstructure:"timeout_propose"`
  270. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  271. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  272. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  273. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  274. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  275. TimeoutCommit int `mapstructure:"timeout_commit"`
  276. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  277. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  278. // BlockSize
  279. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  280. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  281. // EmptyBlocks mode and possible interval between empty blocks in seconds
  282. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  283. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  284. // Reactor sleep duration parameters are in milliseconds
  285. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  286. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  287. }
  288. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  289. func (cfg *ConsensusConfig) WaitForTxs() bool {
  290. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  291. }
  292. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  293. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  294. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  295. }
  296. // Propose returns the amount of time to wait for a proposal
  297. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  298. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  299. }
  300. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  301. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  302. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  303. }
  304. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  305. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  306. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  307. }
  308. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  309. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  310. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  311. }
  312. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  313. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  314. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  315. }
  316. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  317. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  318. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  319. }
  320. // DefaultConsensusConfig returns a default configuration for the consensus service
  321. func DefaultConsensusConfig() *ConsensusConfig {
  322. return &ConsensusConfig{
  323. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  324. WalLight: false,
  325. TimeoutPropose: 3000,
  326. TimeoutProposeDelta: 500,
  327. TimeoutPrevote: 1000,
  328. TimeoutPrevoteDelta: 500,
  329. TimeoutPrecommit: 1000,
  330. TimeoutPrecommitDelta: 500,
  331. TimeoutCommit: 1000,
  332. SkipTimeoutCommit: false,
  333. MaxBlockSizeTxs: 10000,
  334. MaxBlockSizeBytes: 1, // TODO
  335. CreateEmptyBlocks: true,
  336. CreateEmptyBlocksInterval: 0,
  337. PeerGossipSleepDuration: 100,
  338. PeerQueryMaj23SleepDuration: 2000,
  339. }
  340. }
  341. // TestConsensusConfig returns a configuration for testing the consensus service
  342. func TestConsensusConfig() *ConsensusConfig {
  343. config := DefaultConsensusConfig()
  344. config.TimeoutPropose = 100
  345. config.TimeoutProposeDelta = 1
  346. config.TimeoutPrevote = 10
  347. config.TimeoutPrevoteDelta = 1
  348. config.TimeoutPrecommit = 10
  349. config.TimeoutPrecommitDelta = 1
  350. config.TimeoutCommit = 10
  351. config.SkipTimeoutCommit = true
  352. return config
  353. }
  354. // WalFile returns the full path to the write-ahead log file
  355. func (c *ConsensusConfig) WalFile() string {
  356. if c.walFile != "" {
  357. return c.walFile
  358. }
  359. return rootify(c.WalPath, c.RootDir)
  360. }
  361. // SetWalFile sets the path to the write-ahead log file
  362. func (c *ConsensusConfig) SetWalFile(walFile string) {
  363. c.walFile = walFile
  364. }
  365. //-----------------------------------------------------------------------------
  366. // TxIndexConfig
  367. // TxIndexConfig defines the confuguration for the transaction
  368. // indexer, including tags to index.
  369. type TxIndexConfig struct {
  370. // What indexer to use for transactions
  371. //
  372. // Options:
  373. // 1) "null" (default)
  374. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  375. Indexer string `mapstructure:"indexer"`
  376. // Comma-separated list of tags to index (by default the only tag is tx hash)
  377. //
  378. // It's recommended to index only a subset of tags due to possible memory
  379. // bloat. This is, of course, depends on the indexer's DB and the volume of
  380. // transactions.
  381. IndexTags string `mapstructure:"index_tags"`
  382. // When set to true, tells indexer to index all tags. Note this may be not
  383. // desirable (see the comment above). IndexTags has a precedence over
  384. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  385. IndexAllTags bool `mapstructure:"index_all_tags"`
  386. }
  387. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  388. func DefaultTxIndexConfig() *TxIndexConfig {
  389. return &TxIndexConfig{
  390. Indexer: "kv",
  391. IndexTags: "",
  392. IndexAllTags: false,
  393. }
  394. }
  395. //-----------------------------------------------------------------------------
  396. // Utils
  397. // helper function to make config creation independent of root dir
  398. func rootify(path, root string) string {
  399. if filepath.IsAbs(path) {
  400. return path
  401. }
  402. return filepath.Join(root, path)
  403. }
  404. //-----------------------------------------------------------------------------
  405. // Moniker
  406. var defaultMoniker = getDefaultMoniker()
  407. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  408. // fails to get the host name, "anonymous" will be returned.
  409. func getDefaultMoniker() string {
  410. moniker, err := os.Hostname()
  411. if err != nil {
  412. moniker = "anonymous"
  413. }
  414. return moniker
  415. }