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.

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