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.

551 lines
18 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
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 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. "os"
  5. "path/filepath"
  6. "time"
  7. )
  8. // NOTE: Most of the structs & relevant comments + the
  9. // default configuration options were used to manually
  10. // generate the config.toml. Please reflect any changes
  11. // made here in the defaultConfigTemplate constant in
  12. // config/toml.go
  13. // NOTE: tmlibs/cli must know to look in the config dir!
  14. var (
  15. DefaultTendermintDir = ".tendermint"
  16. defaultConfigDir = "config"
  17. defaultDataDir = "data"
  18. defaultConfigFileName = "config.toml"
  19. defaultGenesisJSONName = "genesis.json"
  20. defaultPrivValName = "priv_validator.json"
  21. defaultNodeKeyName = "node_key.json"
  22. defaultAddrBookName = "addrbook.json"
  23. defaultConfigFilePath = filepath.Join(defaultConfigDir, defaultConfigFileName)
  24. defaultGenesisJSONPath = filepath.Join(defaultConfigDir, defaultGenesisJSONName)
  25. defaultPrivValPath = filepath.Join(defaultConfigDir, defaultPrivValName)
  26. defaultNodeKeyPath = filepath.Join(defaultConfigDir, defaultNodeKeyName)
  27. defaultAddrBookPath = filepath.Join(defaultConfigDir, defaultAddrBookName)
  28. )
  29. // Config defines the top level configuration for a Tendermint node
  30. type Config struct {
  31. // Top level options use an anonymous struct
  32. BaseConfig `mapstructure:",squash"`
  33. // Options for services
  34. RPC *RPCConfig `mapstructure:"rpc"`
  35. P2P *P2PConfig `mapstructure:"p2p"`
  36. Mempool *MempoolConfig `mapstructure:"mempool"`
  37. Consensus *ConsensusConfig `mapstructure:"consensus"`
  38. TxIndex *TxIndexConfig `mapstructure:"tx_index"`
  39. }
  40. // DefaultConfig returns a default configuration for a Tendermint node
  41. func DefaultConfig() *Config {
  42. return &Config{
  43. BaseConfig: DefaultBaseConfig(),
  44. RPC: DefaultRPCConfig(),
  45. P2P: DefaultP2PConfig(),
  46. Mempool: DefaultMempoolConfig(),
  47. Consensus: DefaultConsensusConfig(),
  48. TxIndex: DefaultTxIndexConfig(),
  49. }
  50. }
  51. // TestConfig returns a configuration that can be used for testing
  52. func TestConfig() *Config {
  53. return &Config{
  54. BaseConfig: TestBaseConfig(),
  55. RPC: TestRPCConfig(),
  56. P2P: TestP2PConfig(),
  57. Mempool: TestMempoolConfig(),
  58. Consensus: TestConsensusConfig(),
  59. TxIndex: TestTxIndexConfig(),
  60. }
  61. }
  62. // SetRoot sets the RootDir for all Config structs
  63. func (cfg *Config) SetRoot(root string) *Config {
  64. cfg.BaseConfig.RootDir = root
  65. cfg.RPC.RootDir = root
  66. cfg.P2P.RootDir = root
  67. cfg.Mempool.RootDir = root
  68. cfg.Consensus.RootDir = root
  69. return cfg
  70. }
  71. //-----------------------------------------------------------------------------
  72. // BaseConfig
  73. // BaseConfig defines the base configuration for a Tendermint node
  74. type BaseConfig struct {
  75. // chainID is unexposed and immutable but here for convenience
  76. chainID string
  77. // The root directory for all data.
  78. // This should be set in viper so it can unmarshal into this struct
  79. RootDir string `mapstructure:"home"`
  80. // Path to the JSON file containing the initial validator set and other meta data
  81. Genesis string `mapstructure:"genesis_file"`
  82. // Path to the JSON file containing the private key to use as a validator in the consensus protocol
  83. PrivValidator string `mapstructure:"priv_validator_file"`
  84. // A JSON file containing the private key to use for p2p authenticated encryption
  85. NodeKey string `mapstructure:"node_key_file"`
  86. // A custom human readable name for this node
  87. Moniker string `mapstructure:"moniker"`
  88. // TCP or UNIX socket address of the PrivValidator server
  89. PrivValidatorAddr string `mapstructure:"priv_validator_addr"`
  90. // TCP or UNIX socket address of the ABCI application,
  91. // or the name of an ABCI application compiled in with the Tendermint binary
  92. ProxyApp string `mapstructure:"proxy_app"`
  93. // Mechanism to connect to the ABCI application: socket | grpc
  94. ABCI string `mapstructure:"abci"`
  95. // Output level for logging
  96. LogLevel string `mapstructure:"log_level"`
  97. // TCP or UNIX socket address for the profiling server to listen on
  98. ProfListenAddress string `mapstructure:"prof_laddr"`
  99. // If this node is many blocks behind the tip of the chain, FastSync
  100. // allows them to catchup quickly by downloading blocks in parallel
  101. // and verifying their commits
  102. FastSync bool `mapstructure:"fast_sync"`
  103. // If true, query the ABCI app on connecting to a new peer
  104. // so the app can decide if we should keep the connection or not
  105. FilterPeers bool `mapstructure:"filter_peers"` // false
  106. // Database backend: leveldb | memdb
  107. DBBackend string `mapstructure:"db_backend"`
  108. // Database directory
  109. DBPath string `mapstructure:"db_dir"`
  110. }
  111. func (c BaseConfig) ChainID() string {
  112. return c.chainID
  113. }
  114. // DefaultBaseConfig returns a default base configuration for a Tendermint node
  115. func DefaultBaseConfig() BaseConfig {
  116. return BaseConfig{
  117. Genesis: defaultGenesisJSONPath,
  118. PrivValidator: defaultPrivValPath,
  119. NodeKey: defaultNodeKeyPath,
  120. Moniker: defaultMoniker,
  121. ProxyApp: "tcp://127.0.0.1:46658",
  122. ABCI: "socket",
  123. LogLevel: DefaultPackageLogLevels(),
  124. ProfListenAddress: "",
  125. FastSync: true,
  126. FilterPeers: false,
  127. DBBackend: "leveldb",
  128. DBPath: "data",
  129. }
  130. }
  131. // TestBaseConfig returns a base configuration for testing a Tendermint node
  132. func TestBaseConfig() BaseConfig {
  133. conf := DefaultBaseConfig()
  134. conf.chainID = "tendermint_test"
  135. conf.ProxyApp = "kvstore"
  136. conf.FastSync = false
  137. conf.DBBackend = "memdb"
  138. return conf
  139. }
  140. // GenesisFile returns the full path to the genesis.json file
  141. func (b BaseConfig) GenesisFile() string {
  142. return rootify(b.Genesis, b.RootDir)
  143. }
  144. // PrivValidatorFile returns the full path to the priv_validator.json file
  145. func (b BaseConfig) PrivValidatorFile() string {
  146. return rootify(b.PrivValidator, b.RootDir)
  147. }
  148. // NodeKeyFile returns the full path to the node_key.json file
  149. func (b BaseConfig) NodeKeyFile() string {
  150. return rootify(b.NodeKey, b.RootDir)
  151. }
  152. // DBDir returns the full path to the database directory
  153. func (b BaseConfig) DBDir() string {
  154. return rootify(b.DBPath, b.RootDir)
  155. }
  156. // DefaultLogLevel returns a default log level of "error"
  157. func DefaultLogLevel() string {
  158. return "error"
  159. }
  160. // DefaultPackageLogLevels returns a default log level setting so all packages
  161. // log at "error", while the `state` and `main` packages log at "info"
  162. func DefaultPackageLogLevels() string {
  163. return fmt.Sprintf("main:info,state:info,*:%s", DefaultLogLevel())
  164. }
  165. //-----------------------------------------------------------------------------
  166. // RPCConfig
  167. // RPCConfig defines the configuration options for the Tendermint RPC server
  168. type RPCConfig struct {
  169. RootDir string `mapstructure:"home"`
  170. // TCP or UNIX socket address for the RPC server to listen on
  171. ListenAddress string `mapstructure:"laddr"`
  172. // TCP or UNIX socket address for the gRPC server to listen on
  173. // NOTE: This server only supports /broadcast_tx_commit
  174. GRPCListenAddress string `mapstructure:"grpc_laddr"`
  175. // Activate unsafe RPC commands like /dial_persistent_peers and /unsafe_flush_mempool
  176. Unsafe bool `mapstructure:"unsafe"`
  177. }
  178. // DefaultRPCConfig returns a default configuration for the RPC server
  179. func DefaultRPCConfig() *RPCConfig {
  180. return &RPCConfig{
  181. ListenAddress: "tcp://0.0.0.0:46657",
  182. GRPCListenAddress: "",
  183. Unsafe: false,
  184. }
  185. }
  186. // TestRPCConfig returns a configuration for testing the RPC server
  187. func TestRPCConfig() *RPCConfig {
  188. conf := DefaultRPCConfig()
  189. conf.ListenAddress = "tcp://0.0.0.0:36657"
  190. conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
  191. conf.Unsafe = true
  192. return conf
  193. }
  194. //-----------------------------------------------------------------------------
  195. // P2PConfig
  196. // P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer
  197. type P2PConfig struct {
  198. RootDir string `mapstructure:"home"`
  199. // Address to listen for incoming connections
  200. ListenAddress string `mapstructure:"laddr"`
  201. // Comma separated list of seed nodes to connect to
  202. // We only use these if we can’t connect to peers in the addrbook
  203. Seeds string `mapstructure:"seeds"`
  204. // Comma separated list of persistent peers to connect to
  205. // We always connect to these
  206. PersistentPeers string `mapstructure:"persistent_peers"`
  207. // Skip UPNP port forwarding
  208. SkipUPNP bool `mapstructure:"skip_upnp"`
  209. // Path to address book
  210. AddrBook string `mapstructure:"addr_book_file"`
  211. // Set true for strict address routability rules
  212. AddrBookStrict bool `mapstructure:"addr_book_strict"`
  213. // Maximum number of peers to connect to
  214. MaxNumPeers int `mapstructure:"max_num_peers"`
  215. // Time to wait before flushing messages out on the connection, in ms
  216. FlushThrottleTimeout int `mapstructure:"flush_throttle_timeout"`
  217. // Maximum size of a message packet payload, in bytes
  218. MaxMsgPacketPayloadSize int `mapstructure:"max_msg_packet_payload_size"`
  219. // Rate at which packets can be sent, in bytes/second
  220. SendRate int64 `mapstructure:"send_rate"`
  221. // Rate at which packets can be received, in bytes/second
  222. RecvRate int64 `mapstructure:"recv_rate"`
  223. // Set true to enable the peer-exchange reactor
  224. PexReactor bool `mapstructure:"pex"`
  225. // Seed mode, in which node constantly crawls the network and looks for
  226. // peers. If another node asks it for addresses, it responds and disconnects.
  227. //
  228. // Does not work if the peer-exchange reactor is disabled.
  229. SeedMode bool `mapstructure:"seed_mode"`
  230. // Authenticated encryption
  231. AuthEnc bool `mapstructure:"auth_enc"`
  232. }
  233. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  234. func DefaultP2PConfig() *P2PConfig {
  235. return &P2PConfig{
  236. ListenAddress: "tcp://0.0.0.0:46656",
  237. AddrBook: defaultAddrBookPath,
  238. AddrBookStrict: true,
  239. MaxNumPeers: 50,
  240. FlushThrottleTimeout: 100,
  241. MaxMsgPacketPayloadSize: 1024, // 1 kB
  242. SendRate: 512000, // 500 kB/s
  243. RecvRate: 512000, // 500 kB/s
  244. PexReactor: true,
  245. SeedMode: false,
  246. AuthEnc: true,
  247. }
  248. }
  249. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  250. func TestP2PConfig() *P2PConfig {
  251. conf := DefaultP2PConfig()
  252. conf.ListenAddress = "tcp://0.0.0.0:36656"
  253. conf.SkipUPNP = true
  254. conf.FlushThrottleTimeout = 10
  255. return conf
  256. }
  257. // AddrBookFile returns the full path to the address book
  258. func (p *P2PConfig) AddrBookFile() string {
  259. return rootify(p.AddrBook, p.RootDir)
  260. }
  261. //-----------------------------------------------------------------------------
  262. // MempoolConfig
  263. // MempoolConfig defines the configuration options for the Tendermint mempool
  264. type MempoolConfig struct {
  265. RootDir string `mapstructure:"home"`
  266. Recheck bool `mapstructure:"recheck"`
  267. RecheckEmpty bool `mapstructure:"recheck_empty"`
  268. Broadcast bool `mapstructure:"broadcast"`
  269. WalPath string `mapstructure:"wal_dir"`
  270. CacheSize int `mapstructure:"cache_size"`
  271. }
  272. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  273. func DefaultMempoolConfig() *MempoolConfig {
  274. return &MempoolConfig{
  275. Recheck: true,
  276. RecheckEmpty: true,
  277. Broadcast: true,
  278. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  279. CacheSize: 100000,
  280. }
  281. }
  282. // TestMempoolConfig returns a configuration for testing the Tendermint mempool
  283. func TestMempoolConfig() *MempoolConfig {
  284. config := DefaultMempoolConfig()
  285. config.CacheSize = 1000
  286. return config
  287. }
  288. // WalDir returns the full path to the mempool's write-ahead log
  289. func (m *MempoolConfig) WalDir() string {
  290. return rootify(m.WalPath, m.RootDir)
  291. }
  292. //-----------------------------------------------------------------------------
  293. // ConsensusConfig
  294. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  295. // including timeouts and details about the WAL and the block structure.
  296. type ConsensusConfig struct {
  297. RootDir string `mapstructure:"home"`
  298. WalPath string `mapstructure:"wal_file"`
  299. WalLight bool `mapstructure:"wal_light"`
  300. walFile string // overrides WalPath if set
  301. // All timeouts are in milliseconds
  302. TimeoutPropose int `mapstructure:"timeout_propose"`
  303. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  304. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  305. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  306. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  307. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  308. TimeoutCommit int `mapstructure:"timeout_commit"`
  309. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  310. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  311. // BlockSize
  312. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  313. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  314. // EmptyBlocks mode and possible interval between empty blocks in seconds
  315. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  316. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  317. // Reactor sleep duration parameters are in milliseconds
  318. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  319. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  320. }
  321. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  322. func (cfg *ConsensusConfig) WaitForTxs() bool {
  323. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  324. }
  325. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  326. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  327. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  328. }
  329. // Propose returns the amount of time to wait for a proposal
  330. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  331. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  332. }
  333. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  334. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  335. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  336. }
  337. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  338. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  339. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  340. }
  341. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  342. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  343. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  344. }
  345. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  346. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  347. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  348. }
  349. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  350. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  351. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  352. }
  353. // DefaultConsensusConfig returns a default configuration for the consensus service
  354. func DefaultConsensusConfig() *ConsensusConfig {
  355. return &ConsensusConfig{
  356. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  357. WalLight: false,
  358. TimeoutPropose: 3000,
  359. TimeoutProposeDelta: 500,
  360. TimeoutPrevote: 1000,
  361. TimeoutPrevoteDelta: 500,
  362. TimeoutPrecommit: 1000,
  363. TimeoutPrecommitDelta: 500,
  364. TimeoutCommit: 1000,
  365. SkipTimeoutCommit: false,
  366. MaxBlockSizeTxs: 10000,
  367. MaxBlockSizeBytes: 1, // TODO
  368. CreateEmptyBlocks: true,
  369. CreateEmptyBlocksInterval: 0,
  370. PeerGossipSleepDuration: 100,
  371. PeerQueryMaj23SleepDuration: 2000,
  372. }
  373. }
  374. // TestConsensusConfig returns a configuration for testing the consensus service
  375. func TestConsensusConfig() *ConsensusConfig {
  376. config := DefaultConsensusConfig()
  377. config.TimeoutPropose = 100
  378. config.TimeoutProposeDelta = 1
  379. config.TimeoutPrevote = 10
  380. config.TimeoutPrevoteDelta = 1
  381. config.TimeoutPrecommit = 10
  382. config.TimeoutPrecommitDelta = 1
  383. config.TimeoutCommit = 10
  384. config.SkipTimeoutCommit = true
  385. config.PeerGossipSleepDuration = 5
  386. config.PeerQueryMaj23SleepDuration = 250
  387. return config
  388. }
  389. // WalFile returns the full path to the write-ahead log file
  390. func (c *ConsensusConfig) WalFile() string {
  391. if c.walFile != "" {
  392. return c.walFile
  393. }
  394. return rootify(c.WalPath, c.RootDir)
  395. }
  396. // SetWalFile sets the path to the write-ahead log file
  397. func (c *ConsensusConfig) SetWalFile(walFile string) {
  398. c.walFile = walFile
  399. }
  400. //-----------------------------------------------------------------------------
  401. // TxIndexConfig
  402. // TxIndexConfig defines the confuguration for the transaction
  403. // indexer, including tags to index.
  404. type TxIndexConfig struct {
  405. // What indexer to use for transactions
  406. //
  407. // Options:
  408. // 1) "null" (default)
  409. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  410. Indexer string `mapstructure:"indexer"`
  411. // Comma-separated list of tags to index (by default the only tag is tx hash)
  412. //
  413. // It's recommended to index only a subset of tags due to possible memory
  414. // bloat. This is, of course, depends on the indexer's DB and the volume of
  415. // transactions.
  416. IndexTags string `mapstructure:"index_tags"`
  417. // When set to true, tells indexer to index all tags. Note this may be not
  418. // desirable (see the comment above). IndexTags has a precedence over
  419. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  420. IndexAllTags bool `mapstructure:"index_all_tags"`
  421. }
  422. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  423. func DefaultTxIndexConfig() *TxIndexConfig {
  424. return &TxIndexConfig{
  425. Indexer: "kv",
  426. IndexTags: "",
  427. IndexAllTags: false,
  428. }
  429. }
  430. // TestTxIndexConfig returns a default configuration for the transaction indexer.
  431. func TestTxIndexConfig() *TxIndexConfig {
  432. return DefaultTxIndexConfig()
  433. }
  434. //-----------------------------------------------------------------------------
  435. // Utils
  436. // helper function to make config creation independent of root dir
  437. func rootify(path, root string) string {
  438. if filepath.IsAbs(path) {
  439. return path
  440. }
  441. return filepath.Join(root, path)
  442. }
  443. //-----------------------------------------------------------------------------
  444. // Moniker
  445. var defaultMoniker = getDefaultMoniker()
  446. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  447. // fails to get the host name, "anonymous" will be returned.
  448. func getDefaultMoniker() string {
  449. moniker, err := os.Hostname()
  450. if err != nil {
  451. moniker = "anonymous"
  452. }
  453. return moniker
  454. }