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.

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