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.

474 lines
16 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
  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. }
  200. }
  201. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  202. func TestP2PConfig() *P2PConfig {
  203. conf := DefaultP2PConfig()
  204. conf.ListenAddress = "tcp://0.0.0.0:36656"
  205. conf.SkipUPNP = true
  206. return conf
  207. }
  208. // AddrBookFile returns the full path to the address book
  209. func (p *P2PConfig) AddrBookFile() string {
  210. return rootify(p.AddrBook, p.RootDir)
  211. }
  212. //-----------------------------------------------------------------------------
  213. // MempoolConfig
  214. // MempoolConfig defines the configuration options for the Tendermint mempool
  215. type MempoolConfig struct {
  216. RootDir string `mapstructure:"home"`
  217. Recheck bool `mapstructure:"recheck"`
  218. RecheckEmpty bool `mapstructure:"recheck_empty"`
  219. Broadcast bool `mapstructure:"broadcast"`
  220. WalPath string `mapstructure:"wal_dir"`
  221. }
  222. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  223. func DefaultMempoolConfig() *MempoolConfig {
  224. return &MempoolConfig{
  225. Recheck: true,
  226. RecheckEmpty: true,
  227. Broadcast: true,
  228. WalPath: "data/mempool.wal",
  229. }
  230. }
  231. // WalDir returns the full path to the mempool's write-ahead log
  232. func (m *MempoolConfig) WalDir() string {
  233. return rootify(m.WalPath, m.RootDir)
  234. }
  235. //-----------------------------------------------------------------------------
  236. // ConsensusConfig
  237. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  238. // including timeouts and details about the WAL and the block structure.
  239. type ConsensusConfig struct {
  240. RootDir string `mapstructure:"home"`
  241. WalPath string `mapstructure:"wal_file"`
  242. WalLight bool `mapstructure:"wal_light"`
  243. walFile string // overrides WalPath if set
  244. // All timeouts are in ms
  245. TimeoutPropose int `mapstructure:"timeout_propose"`
  246. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  247. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  248. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  249. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  250. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  251. TimeoutCommit int `mapstructure:"timeout_commit"`
  252. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  253. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  254. // BlockSize
  255. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  256. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  257. // EmptyBlocks mode and possible interval between empty blocks in seconds
  258. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  259. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  260. // Reactor sleep duration parameters are in ms
  261. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  262. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  263. }
  264. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  265. func (cfg *ConsensusConfig) WaitForTxs() bool {
  266. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  267. }
  268. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  269. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  270. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  271. }
  272. // Propose returns the amount of time to wait for a proposal
  273. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  274. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  275. }
  276. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  277. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  278. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  279. }
  280. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  281. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  282. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  283. }
  284. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  285. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  286. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  287. }
  288. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  289. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  290. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  291. }
  292. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  293. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  294. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  295. }
  296. // DefaultConsensusConfig returns a default configuration for the consensus service
  297. func DefaultConsensusConfig() *ConsensusConfig {
  298. return &ConsensusConfig{
  299. WalPath: "data/cs.wal/wal",
  300. WalLight: false,
  301. TimeoutPropose: 3000,
  302. TimeoutProposeDelta: 500,
  303. TimeoutPrevote: 1000,
  304. TimeoutPrevoteDelta: 500,
  305. TimeoutPrecommit: 1000,
  306. TimeoutPrecommitDelta: 500,
  307. TimeoutCommit: 1000,
  308. SkipTimeoutCommit: false,
  309. MaxBlockSizeTxs: 10000,
  310. MaxBlockSizeBytes: 1, // TODO
  311. CreateEmptyBlocks: true,
  312. CreateEmptyBlocksInterval: 0,
  313. PeerGossipSleepDuration: 100,
  314. PeerQueryMaj23SleepDuration: 2000,
  315. }
  316. }
  317. // TestConsensusConfig returns a configuration for testing the consensus service
  318. func TestConsensusConfig() *ConsensusConfig {
  319. config := DefaultConsensusConfig()
  320. config.TimeoutPropose = 2000
  321. config.TimeoutProposeDelta = 1
  322. config.TimeoutPrevote = 10
  323. config.TimeoutPrevoteDelta = 1
  324. config.TimeoutPrecommit = 10
  325. config.TimeoutPrecommitDelta = 1
  326. config.TimeoutCommit = 10
  327. config.SkipTimeoutCommit = true
  328. return config
  329. }
  330. // WalFile returns the full path to the write-ahead log file
  331. func (c *ConsensusConfig) WalFile() string {
  332. if c.walFile != "" {
  333. return c.walFile
  334. }
  335. return rootify(c.WalPath, c.RootDir)
  336. }
  337. // SetWalFile sets the path to the write-ahead log file
  338. func (c *ConsensusConfig) SetWalFile(walFile string) {
  339. c.walFile = walFile
  340. }
  341. //-----------------------------------------------------------------------------
  342. // TxIndexConfig
  343. // TxIndexConfig defines the confuguration for the transaction
  344. // indexer, including tags to index.
  345. type TxIndexConfig struct {
  346. // What indexer to use for transactions
  347. //
  348. // Options:
  349. // 1) "null" (default)
  350. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  351. Indexer string `mapstructure:"indexer"`
  352. // Comma-separated list of tags to index (by default the only tag is tx hash)
  353. //
  354. // It's recommended to index only a subset of tags due to possible memory
  355. // bloat. This is, of course, depends on the indexer's DB and the volume of
  356. // transactions.
  357. IndexTags string `mapstructure:"index_tags"`
  358. // When set to true, tells indexer to index all tags. Note this may be not
  359. // desirable (see the comment above). IndexTags has a precedence over
  360. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  361. IndexAllTags bool `mapstructure:"index_all_tags"`
  362. }
  363. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  364. func DefaultTxIndexConfig() *TxIndexConfig {
  365. return &TxIndexConfig{
  366. Indexer: "kv",
  367. IndexTags: "",
  368. IndexAllTags: false,
  369. }
  370. }
  371. //-----------------------------------------------------------------------------
  372. // Utils
  373. // helper function to make config creation independent of root dir
  374. func rootify(path, root string) string {
  375. if filepath.IsAbs(path) {
  376. return path
  377. }
  378. return filepath.Join(root, path)
  379. }
  380. //-----------------------------------------------------------------------------
  381. // Moniker
  382. var defaultMoniker = getDefaultMoniker()
  383. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  384. // fails to get the host name, "anonymous" will be returned.
  385. func getDefaultMoniker() string {
  386. moniker, err := os.Hostname()
  387. if err != nil {
  388. moniker = "anonymous"
  389. }
  390. return moniker
  391. }