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.

431 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. "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. // 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 bool
  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. // TODO: This probably shouldn't be exposed but it makes it
  261. // easy to write tests for the wal/replay
  262. BlockPartSize int `mapstructure:"block_part_size"`
  263. // Reactor sleep duration parameters are in ms
  264. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  265. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  266. }
  267. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  268. func (cfg *ConsensusConfig) WaitForTxs() bool {
  269. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  270. }
  271. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  272. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  273. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  274. }
  275. // Propose returns the amount of time to wait for a proposal
  276. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  277. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  278. }
  279. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  280. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  281. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  282. }
  283. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  284. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  285. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  286. }
  287. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  288. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  289. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  290. }
  291. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  292. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  293. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  294. }
  295. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  296. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  297. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  298. }
  299. // DefaultConsensusConfig returns a default configuration for the consensus service
  300. func DefaultConsensusConfig() *ConsensusConfig {
  301. return &ConsensusConfig{
  302. WalPath: "data/cs.wal/wal",
  303. WalLight: false,
  304. TimeoutPropose: 3000,
  305. TimeoutProposeDelta: 500,
  306. TimeoutPrevote: 1000,
  307. TimeoutPrevoteDelta: 500,
  308. TimeoutPrecommit: 1000,
  309. TimeoutPrecommitDelta: 500,
  310. TimeoutCommit: 1000,
  311. SkipTimeoutCommit: false,
  312. MaxBlockSizeTxs: 10000,
  313. MaxBlockSizeBytes: 1, // TODO
  314. CreateEmptyBlocks: true,
  315. CreateEmptyBlocksInterval: 0,
  316. BlockPartSize: types.DefaultBlockPartSize, // TODO: we shouldnt be importing types
  317. PeerGossipSleepDuration: 100,
  318. PeerQueryMaj23SleepDuration: 2000,
  319. }
  320. }
  321. // TestConsensusConfig returns a configuration for testing the consensus service
  322. func TestConsensusConfig() *ConsensusConfig {
  323. config := DefaultConsensusConfig()
  324. config.TimeoutPropose = 2000
  325. config.TimeoutProposeDelta = 1
  326. config.TimeoutPrevote = 10
  327. config.TimeoutPrevoteDelta = 1
  328. config.TimeoutPrecommit = 10
  329. config.TimeoutPrecommitDelta = 1
  330. config.TimeoutCommit = 10
  331. config.SkipTimeoutCommit = true
  332. return config
  333. }
  334. // WalFile returns the full path to the write-ahead log file
  335. func (c *ConsensusConfig) WalFile() string {
  336. if c.walFile != "" {
  337. return c.walFile
  338. }
  339. return rootify(c.WalPath, c.RootDir)
  340. }
  341. // SetWalFile sets the path to the write-ahead log file
  342. func (c *ConsensusConfig) SetWalFile(walFile string) {
  343. c.walFile = walFile
  344. }
  345. //-----------------------------------------------------------------------------
  346. // Utils
  347. // helper function to make config creation independent of root dir
  348. func rootify(path, root string) string {
  349. if filepath.IsAbs(path) {
  350. return path
  351. }
  352. return filepath.Join(root, path)
  353. }