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.

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