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.

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