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.

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