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.

295 lines
8.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package config
  2. import (
  3. "path/filepath"
  4. "time"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. type Config struct {
  8. // Top level options use an anonymous struct
  9. BaseConfig `mapstructure:",squash"`
  10. // Options for services
  11. P2P *P2PConfig `mapstructure:"p2p"`
  12. Mempool *MempoolConfig `mapstructure:"mempool"`
  13. Consensus *ConsensusConfig `mapstructure:"consensus"`
  14. }
  15. func DefaultConfig() *Config {
  16. return &Config{
  17. BaseConfig: DefaultBaseConfig(),
  18. P2P: DefaultP2PConfig(),
  19. Mempool: DefaultMempoolConfig(),
  20. Consensus: DefaultConsensusConfig(),
  21. }
  22. }
  23. func TestConfig() *Config {
  24. return &Config{
  25. BaseConfig: TestBaseConfig(),
  26. P2P: TestP2PConfig(),
  27. Mempool: DefaultMempoolConfig(),
  28. Consensus: TestConsensusConfig(),
  29. }
  30. }
  31. // Set the RootDir for all Config structs
  32. func (cfg *Config) SetRoot(root string) *Config {
  33. cfg.BaseConfig.RootDir = root
  34. cfg.P2P.RootDir = root
  35. cfg.Mempool.RootDir = root
  36. cfg.Consensus.RootDir = root
  37. return cfg
  38. }
  39. // BaseConfig struct for a Tendermint node
  40. type BaseConfig struct {
  41. // The root directory for all data.
  42. // This should be set in viper so it can unmarshal into this struct
  43. RootDir string `mapstructure:"home"`
  44. // The ID of the chain to join (should be signed with every transaction and vote)
  45. ChainID string `mapstructure:"chain_id"`
  46. // A JSON file containing the initial validator set and other meta data
  47. Genesis string `mapstructure:"genesis_file"`
  48. // A JSON file containing the private key to use as a validator in the consensus protocol
  49. PrivValidator string `mapstructure:"priv_validator_file"`
  50. // A custom human readable name for this node
  51. Moniker string `mapstructure:"moniker"`
  52. // TCP or UNIX socket address of the ABCI application,
  53. // or the name of an ABCI application compiled in with the Tendermint binary
  54. ProxyApp string `mapstructure:"proxy_app"`
  55. // Mechanism to connect to the ABCI application: socket | grpc
  56. ABCI string `mapstructure:"abci"`
  57. // Output level for logging
  58. LogLevel string `mapstructure:"log_level"`
  59. // TCP or UNIX socket address for the profiling server to listen on
  60. ProfListenAddress string `mapstructure:"prof_laddr"`
  61. // If this node is many blocks behind the tip of the chain, FastSync
  62. // allows them to catchup quickly by downloading blocks in parallel
  63. // and verifying their commits
  64. FastSync bool `mapstructure:"fast_sync"`
  65. // If true, query the ABCI app on connecting to a new peer
  66. // so the app can decide if we should keep the connection or not
  67. FilterPeers bool `mapstructure:"filter_peers"` // false
  68. // What indexer to use for transactions
  69. TxIndex string `mapstructure:"tx_index"`
  70. // Database backend: leveldb | memdb
  71. DBBackend string `mapstructure:"db_backend"`
  72. // Database directory
  73. DBPath string `mapstructure:"db_dir"`
  74. // TCP or UNIX socket address for the RPC server to listen on
  75. RPCListenAddress string `mapstructure:"rpc_laddr"`
  76. // TCP or UNIX socket address for the gRPC server to listen on
  77. // NOTE: This server only supports /broadcast_tx_commit
  78. GRPCListenAddress string `mapstructure:"grpc_laddr"`
  79. }
  80. func DefaultBaseConfig() BaseConfig {
  81. return BaseConfig{
  82. Genesis: "genesis.json",
  83. PrivValidator: "priv_validator.json",
  84. Moniker: "anonymous",
  85. ProxyApp: "tcp://127.0.0.1:46658",
  86. ABCI: "socket",
  87. LogLevel: "info",
  88. ProfListenAddress: "",
  89. FastSync: true,
  90. FilterPeers: false,
  91. TxIndex: "kv",
  92. DBBackend: "leveldb",
  93. DBPath: "data",
  94. RPCListenAddress: "tcp://0.0.0.0:46657",
  95. GRPCListenAddress: "",
  96. }
  97. }
  98. func TestBaseConfig() BaseConfig {
  99. conf := DefaultBaseConfig()
  100. conf.ChainID = "tendermint_test"
  101. conf.ProxyApp = "dummy"
  102. conf.FastSync = false
  103. conf.DBBackend = "memdb"
  104. conf.RPCListenAddress = "tcp://0.0.0.0:36657"
  105. conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
  106. return conf
  107. }
  108. func (b BaseConfig) GenesisFile() string {
  109. return rootify(b.Genesis, b.RootDir)
  110. }
  111. func (b BaseConfig) PrivValidatorFile() string {
  112. return rootify(b.PrivValidator, b.RootDir)
  113. }
  114. func (b BaseConfig) DBDir() string {
  115. return rootify(b.DBPath, b.RootDir)
  116. }
  117. type P2PConfig struct {
  118. RootDir string `mapstructure:"home"`
  119. ListenAddress string `mapstructure:"laddr"`
  120. Seeds string `mapstructure:"seeds"`
  121. SkipUPNP bool `mapstructure:"skip_upnp"`
  122. AddrBook string `mapstructure:"addr_book_file"`
  123. AddrBookStrict bool `mapstructure:"addr_book_strict"`
  124. PexReactor bool `mapstructure:"pex"`
  125. MaxNumPeers int `mapstructure:"max_num_peers"`
  126. }
  127. func DefaultP2PConfig() *P2PConfig {
  128. return &P2PConfig{
  129. ListenAddress: "tcp://0.0.0.0:46656",
  130. AddrBook: "addrbook.json",
  131. AddrBookStrict: true,
  132. MaxNumPeers: 50,
  133. }
  134. }
  135. func TestP2PConfig() *P2PConfig {
  136. conf := DefaultP2PConfig()
  137. conf.ListenAddress = "tcp://0.0.0.0:36656"
  138. conf.SkipUPNP = true
  139. return conf
  140. }
  141. func (p *P2PConfig) AddrBookFile() string {
  142. return rootify(p.AddrBook, p.RootDir)
  143. }
  144. type MempoolConfig struct {
  145. RootDir string `mapstructure:"home"`
  146. Recheck bool `mapstructure:"recheck"`
  147. RecheckEmpty bool `mapstructure:"recheck_empty"`
  148. Broadcast bool `mapstructure:"broadcast"`
  149. WalPath string `mapstructure:"wal_dir"`
  150. }
  151. func DefaultMempoolConfig() *MempoolConfig {
  152. return &MempoolConfig{
  153. Recheck: true,
  154. RecheckEmpty: true,
  155. Broadcast: true,
  156. WalPath: "data/mempool.wal",
  157. }
  158. }
  159. func (m *MempoolConfig) WalDir() string {
  160. return rootify(m.WalPath, m.RootDir)
  161. }
  162. // ConsensusConfig holds timeouts and details about the WAL, the block structure,
  163. // and timeouts in the consensus protocol.
  164. type ConsensusConfig struct {
  165. RootDir string `mapstructure:"home"`
  166. WalPath string `mapstructure:"wal_file"`
  167. WalLight bool `mapstructure:"wal_light"`
  168. walFile string // overrides WalPath if set
  169. // All timeouts are in ms
  170. TimeoutPropose int `mapstructure:"timeout_propose"`
  171. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  172. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  173. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  174. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  175. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  176. TimeoutCommit int `mapstructure:"timeout_commit"`
  177. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  178. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  179. // BlockSize
  180. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  181. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  182. // TODO: This probably shouldn't be exposed but it makes it
  183. // easy to write tests for the wal/replay
  184. BlockPartSize int `mapstructure:"block_part_size"`
  185. }
  186. // Wait this long for a proposal
  187. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  188. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  189. }
  190. // After receiving any +2/3 prevote, wait this long for stragglers
  191. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  192. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  193. }
  194. // After receiving any +2/3 precommits, wait this long for stragglers
  195. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  196. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  197. }
  198. // After receiving +2/3 precommits for a single block (a commit), wait this long for stragglers in the next height's RoundStepNewHeight
  199. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  200. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  201. }
  202. func DefaultConsensusConfig() *ConsensusConfig {
  203. return &ConsensusConfig{
  204. WalPath: "data/cs.wal/wal",
  205. WalLight: false,
  206. TimeoutPropose: 3000,
  207. TimeoutProposeDelta: 500,
  208. TimeoutPrevote: 1000,
  209. TimeoutPrevoteDelta: 500,
  210. TimeoutPrecommit: 1000,
  211. TimeoutPrecommitDelta: 500,
  212. TimeoutCommit: 1000,
  213. SkipTimeoutCommit: false,
  214. MaxBlockSizeTxs: 10000,
  215. MaxBlockSizeBytes: 1, // TODO
  216. BlockPartSize: types.DefaultBlockPartSize, // TODO: we shouldnt be importing types
  217. }
  218. }
  219. func TestConsensusConfig() *ConsensusConfig {
  220. config := DefaultConsensusConfig()
  221. config.TimeoutPropose = 2000
  222. config.TimeoutProposeDelta = 1
  223. config.TimeoutPrevote = 10
  224. config.TimeoutPrevoteDelta = 1
  225. config.TimeoutPrecommit = 10
  226. config.TimeoutPrecommitDelta = 1
  227. config.TimeoutCommit = 10
  228. config.SkipTimeoutCommit = true
  229. return config
  230. }
  231. func (c *ConsensusConfig) WalFile() string {
  232. if c.walFile != "" {
  233. return c.walFile
  234. }
  235. return rootify(c.WalPath, c.RootDir)
  236. }
  237. func (c *ConsensusConfig) SetWalFile(walFile string) {
  238. c.walFile = walFile
  239. }
  240. // helper function to make config creation independent of root dir
  241. func rootify(path, root string) string {
  242. if filepath.IsAbs(path) {
  243. return path
  244. }
  245. return filepath.Join(root, path)
  246. }