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.

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