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.

424 lines
14 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. }
  17. // DefaultConfig returns a default configuration for a Tendermint node
  18. func DefaultConfig() *Config {
  19. return &Config{
  20. BaseConfig: DefaultBaseConfig(),
  21. RPC: DefaultRPCConfig(),
  22. P2P: DefaultP2PConfig(),
  23. Mempool: DefaultMempoolConfig(),
  24. Consensus: DefaultConsensusConfig(),
  25. }
  26. }
  27. // TestConfig returns a configuration that can be used for testing
  28. func TestConfig() *Config {
  29. return &Config{
  30. BaseConfig: TestBaseConfig(),
  31. RPC: TestRPCConfig(),
  32. P2P: TestP2PConfig(),
  33. Mempool: DefaultMempoolConfig(),
  34. Consensus: TestConsensusConfig(),
  35. }
  36. }
  37. // SetRoot sets the RootDir for all Config structs
  38. func (cfg *Config) SetRoot(root string) *Config {
  39. cfg.BaseConfig.RootDir = root
  40. cfg.RPC.RootDir = root
  41. cfg.P2P.RootDir = root
  42. cfg.Mempool.RootDir = root
  43. cfg.Consensus.RootDir = root
  44. return cfg
  45. }
  46. //-----------------------------------------------------------------------------
  47. // BaseConfig
  48. // BaseConfig defines the base configuration for a Tendermint node
  49. type BaseConfig struct {
  50. // The root directory for all data.
  51. // This should be set in viper so it can unmarshal into this struct
  52. RootDir string `mapstructure:"home"`
  53. // The ID of the chain to join (should be signed with every transaction and vote)
  54. ChainID string `mapstructure:"chain_id"`
  55. // A JSON file containing the initial validator set and other meta data
  56. Genesis string `mapstructure:"genesis_file"`
  57. // A JSON file containing the private key to use as a validator in the consensus protocol
  58. PrivValidator string `mapstructure:"priv_validator_file"`
  59. // A custom human readable name for this node
  60. Moniker string `mapstructure:"moniker"`
  61. // TCP or UNIX socket address of the ABCI application,
  62. // or the name of an ABCI application compiled in with the Tendermint binary
  63. ProxyApp string `mapstructure:"proxy_app"`
  64. // Mechanism to connect to the ABCI application: socket | grpc
  65. ABCI string `mapstructure:"abci"`
  66. // Output level for logging
  67. LogLevel string `mapstructure:"log_level"`
  68. // TCP or UNIX socket address for the profiling server to listen on
  69. ProfListenAddress string `mapstructure:"prof_laddr"`
  70. // If this node is many blocks behind the tip of the chain, FastSync
  71. // allows them to catchup quickly by downloading blocks in parallel
  72. // and verifying their commits
  73. FastSync bool `mapstructure:"fast_sync"`
  74. // If true, query the ABCI app on connecting to a new peer
  75. // so the app can decide if we should keep the connection or not
  76. FilterPeers bool `mapstructure:"filter_peers"` // false
  77. // What indexer to use for transactions
  78. TxIndex string `mapstructure:"tx_index"`
  79. // Database backend: leveldb | memdb
  80. DBBackend string `mapstructure:"db_backend"`
  81. // Database directory
  82. DBPath string `mapstructure:"db_dir"`
  83. }
  84. // DefaultBaseConfig returns a default base configuration for a Tendermint node
  85. func DefaultBaseConfig() BaseConfig {
  86. return BaseConfig{
  87. Genesis: "genesis.json",
  88. PrivValidator: "priv_validator.json",
  89. Moniker: "anonymous",
  90. ProxyApp: "tcp://127.0.0.1:46658",
  91. ABCI: "socket",
  92. LogLevel: DefaultPackageLogLevels(),
  93. ProfListenAddress: "",
  94. FastSync: true,
  95. FilterPeers: false,
  96. TxIndex: "kv",
  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 bool
  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. // Utils
  342. // helper function to make config creation independent of root dir
  343. func rootify(path, root string) string {
  344. if filepath.IsAbs(path) {
  345. return path
  346. }
  347. return filepath.Join(root, path)
  348. }