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.

546 lines
18 KiB

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