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.

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