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.

419 lines
14 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. "path/filepath"
  5. "time"
  6. "github.com/tendermint/tendermint/types" // TODO: remove
  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. }
  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. }
  27. }
  28. // TestConfig returns a configuration that can be used for testing
  29. func TestConfig() *Config {
  30. return &Config{
  31. BaseConfig: TestBaseConfig(),
  32. RPC: TestRPCConfig(),
  33. P2P: TestP2PConfig(),
  34. Mempool: DefaultMempoolConfig(),
  35. Consensus: TestConsensusConfig(),
  36. }
  37. }
  38. // SetRoot sets the RootDir for all Config structs
  39. func (cfg *Config) SetRoot(root string) *Config {
  40. cfg.BaseConfig.RootDir = root
  41. cfg.RPC.RootDir = root
  42. cfg.P2P.RootDir = root
  43. cfg.Mempool.RootDir = root
  44. cfg.Consensus.RootDir = root
  45. return cfg
  46. }
  47. //-----------------------------------------------------------------------------
  48. // BaseConfig
  49. // BaseConfig defines the base configuration for a Tendermint node
  50. type BaseConfig struct {
  51. // The root directory for all data.
  52. // This should be set in viper so it can unmarshal into this struct
  53. RootDir string `mapstructure:"home"`
  54. // The ID of the chain to join (should be signed with every transaction and vote)
  55. ChainID string `mapstructure:"chain_id"`
  56. // A JSON file containing the initial validator set and other meta data
  57. Genesis string `mapstructure:"genesis_file"`
  58. // A JSON file containing the private key to use as a validator in the consensus protocol
  59. PrivValidator string `mapstructure:"priv_validator_file"`
  60. // A custom human readable name for this node
  61. Moniker string `mapstructure:"moniker"`
  62. // TCP or UNIX socket address of the ABCI application,
  63. // or the name of an ABCI application compiled in with the Tendermint binary
  64. ProxyApp string `mapstructure:"proxy_app"`
  65. // Mechanism to connect to the ABCI application: socket | grpc
  66. ABCI string `mapstructure:"abci"`
  67. // Output level for logging
  68. LogLevel string `mapstructure:"log_level"`
  69. // TCP or UNIX socket address for the profiling server to listen on
  70. ProfListenAddress string `mapstructure:"prof_laddr"`
  71. // If this node is many blocks behind the tip of the chain, FastSync
  72. // allows them to catchup quickly by downloading blocks in parallel
  73. // and verifying their commits
  74. FastSync bool `mapstructure:"fast_sync"`
  75. // If true, query the ABCI app on connecting to a new peer
  76. // so the app can decide if we should keep the connection or not
  77. FilterPeers bool `mapstructure:"filter_peers"` // false
  78. // What indexer to use for transactions
  79. TxIndex string `mapstructure:"tx_index"`
  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. TxIndex: "kv",
  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. }
  182. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  183. func DefaultP2PConfig() *P2PConfig {
  184. return &P2PConfig{
  185. ListenAddress: "tcp://0.0.0.0:46656",
  186. AddrBook: "addrbook.json",
  187. AddrBookStrict: true,
  188. MaxNumPeers: 50,
  189. FlushThrottleTimeout: 100,
  190. }
  191. }
  192. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  193. func TestP2PConfig() *P2PConfig {
  194. conf := DefaultP2PConfig()
  195. conf.ListenAddress = "tcp://0.0.0.0:36656"
  196. conf.SkipUPNP = true
  197. return conf
  198. }
  199. // AddrBookFile returns the full path to the address bool
  200. func (p *P2PConfig) AddrBookFile() string {
  201. return rootify(p.AddrBook, p.RootDir)
  202. }
  203. //-----------------------------------------------------------------------------
  204. // MempoolConfig
  205. // MempoolConfig defines the configuration options for the Tendermint mempool
  206. type MempoolConfig struct {
  207. RootDir string `mapstructure:"home"`
  208. Recheck bool `mapstructure:"recheck"`
  209. RecheckEmpty bool `mapstructure:"recheck_empty"`
  210. Broadcast bool `mapstructure:"broadcast"`
  211. WalPath string `mapstructure:"wal_dir"`
  212. }
  213. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  214. func DefaultMempoolConfig() *MempoolConfig {
  215. return &MempoolConfig{
  216. Recheck: true,
  217. RecheckEmpty: true,
  218. Broadcast: true,
  219. WalPath: "data/mempool.wal",
  220. }
  221. }
  222. // WalDir returns the full path to the mempool's write-ahead log
  223. func (m *MempoolConfig) WalDir() string {
  224. return rootify(m.WalPath, m.RootDir)
  225. }
  226. //-----------------------------------------------------------------------------
  227. // ConsensusConfig
  228. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  229. // including timeouts and details about the WAL and the block structure.
  230. type ConsensusConfig struct {
  231. RootDir string `mapstructure:"home"`
  232. WalPath string `mapstructure:"wal_file"`
  233. WalLight bool `mapstructure:"wal_light"`
  234. walFile string // overrides WalPath if set
  235. // All timeouts are in ms
  236. TimeoutPropose int `mapstructure:"timeout_propose"`
  237. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  238. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  239. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  240. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  241. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  242. TimeoutCommit int `mapstructure:"timeout_commit"`
  243. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  244. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  245. // BlockSize
  246. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  247. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  248. // EmptyBlocks mode and possible interval between empty blocks in seconds
  249. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  250. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  251. // TODO: This probably shouldn't be exposed but it makes it
  252. // easy to write tests for the wal/replay
  253. BlockPartSize int `mapstructure:"block_part_size"`
  254. // Reactor sleep duration parameters are in ms
  255. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  256. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  257. }
  258. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  259. func (cfg *ConsensusConfig) WaitForTxs() bool {
  260. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  261. }
  262. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  263. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  264. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  265. }
  266. // Propose returns the amount of time to wait for a proposal
  267. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  268. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  269. }
  270. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  271. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  272. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  273. }
  274. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  275. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  276. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  277. }
  278. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  279. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  280. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  281. }
  282. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  283. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  284. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  285. }
  286. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  287. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  288. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  289. }
  290. // DefaultConsensusConfig returns a default configuration for the consensus service
  291. func DefaultConsensusConfig() *ConsensusConfig {
  292. return &ConsensusConfig{
  293. WalPath: "data/cs.wal/wal",
  294. WalLight: false,
  295. TimeoutPropose: 3000,
  296. TimeoutProposeDelta: 500,
  297. TimeoutPrevote: 1000,
  298. TimeoutPrevoteDelta: 500,
  299. TimeoutPrecommit: 1000,
  300. TimeoutPrecommitDelta: 500,
  301. TimeoutCommit: 1000,
  302. SkipTimeoutCommit: false,
  303. MaxBlockSizeTxs: 10000,
  304. MaxBlockSizeBytes: 1, // TODO
  305. CreateEmptyBlocks: true,
  306. CreateEmptyBlocksInterval: 0,
  307. BlockPartSize: types.DefaultBlockPartSize, // TODO: we shouldnt be importing types
  308. PeerGossipSleepDuration: 100,
  309. PeerQueryMaj23SleepDuration: 2000,
  310. }
  311. }
  312. // TestConsensusConfig returns a configuration for testing the consensus service
  313. func TestConsensusConfig() *ConsensusConfig {
  314. config := DefaultConsensusConfig()
  315. config.TimeoutPropose = 2000
  316. config.TimeoutProposeDelta = 1
  317. config.TimeoutPrevote = 10
  318. config.TimeoutPrevoteDelta = 1
  319. config.TimeoutPrecommit = 10
  320. config.TimeoutPrecommitDelta = 1
  321. config.TimeoutCommit = 10
  322. config.SkipTimeoutCommit = true
  323. return config
  324. }
  325. // WalFile returns the full path to the write-ahead log file
  326. func (c *ConsensusConfig) WalFile() string {
  327. if c.walFile != "" {
  328. return c.walFile
  329. }
  330. return rootify(c.WalPath, c.RootDir)
  331. }
  332. // SetWalFile sets the path to the write-ahead log file
  333. func (c *ConsensusConfig) SetWalFile(walFile string) {
  334. c.walFile = walFile
  335. }
  336. //-----------------------------------------------------------------------------
  337. // Utils
  338. // helper function to make config creation independent of root dir
  339. func rootify(path, root string) string {
  340. if filepath.IsAbs(path) {
  341. return path
  342. }
  343. return filepath.Join(root, path)
  344. }