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.

552 lines
19 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 for Tendermint to listen on for
  89. // connections from an external PrivValidator process
  90. PrivValidatorListenAddr string `mapstructure:"priv_validator_laddr"`
  91. // TCP or UNIX socket address of the ABCI application,
  92. // or the name of an ABCI application compiled in with the Tendermint binary
  93. ProxyApp string `mapstructure:"proxy_app"`
  94. // Mechanism to connect to the ABCI application: socket | grpc
  95. ABCI string `mapstructure:"abci"`
  96. // Output level for logging
  97. LogLevel string `mapstructure:"log_level"`
  98. // TCP or UNIX socket address for the profiling server to listen on
  99. ProfListenAddress string `mapstructure:"prof_laddr"`
  100. // If this node is many blocks behind the tip of the chain, FastSync
  101. // allows them to catchup quickly by downloading blocks in parallel
  102. // and verifying their commits
  103. FastSync bool `mapstructure:"fast_sync"`
  104. // If true, query the ABCI app on connecting to a new peer
  105. // so the app can decide if we should keep the connection or not
  106. FilterPeers bool `mapstructure:"filter_peers"` // false
  107. // Database backend: leveldb | memdb
  108. DBBackend string `mapstructure:"db_backend"`
  109. // Database directory
  110. DBPath string `mapstructure:"db_dir"`
  111. }
  112. func (c BaseConfig) ChainID() string {
  113. return c.chainID
  114. }
  115. // DefaultBaseConfig returns a default base configuration for a Tendermint node
  116. func DefaultBaseConfig() BaseConfig {
  117. return BaseConfig{
  118. Genesis: defaultGenesisJSONPath,
  119. PrivValidator: defaultPrivValPath,
  120. NodeKey: defaultNodeKeyPath,
  121. Moniker: defaultMoniker,
  122. ProxyApp: "tcp://127.0.0.1:46658",
  123. ABCI: "socket",
  124. LogLevel: DefaultPackageLogLevels(),
  125. ProfListenAddress: "",
  126. FastSync: true,
  127. FilterPeers: false,
  128. DBBackend: "leveldb",
  129. DBPath: "data",
  130. }
  131. }
  132. // TestBaseConfig returns a base configuration for testing a Tendermint node
  133. func TestBaseConfig() BaseConfig {
  134. conf := DefaultBaseConfig()
  135. conf.chainID = "tendermint_test"
  136. conf.ProxyApp = "kvstore"
  137. conf.FastSync = false
  138. conf.DBBackend = "memdb"
  139. return conf
  140. }
  141. // GenesisFile returns the full path to the genesis.json file
  142. func (b BaseConfig) GenesisFile() string {
  143. return rootify(b.Genesis, b.RootDir)
  144. }
  145. // PrivValidatorFile returns the full path to the priv_validator.json file
  146. func (b BaseConfig) PrivValidatorFile() string {
  147. return rootify(b.PrivValidator, b.RootDir)
  148. }
  149. // NodeKeyFile returns the full path to the node_key.json file
  150. func (b BaseConfig) NodeKeyFile() string {
  151. return rootify(b.NodeKey, b.RootDir)
  152. }
  153. // DBDir returns the full path to the database directory
  154. func (b BaseConfig) DBDir() string {
  155. return rootify(b.DBPath, b.RootDir)
  156. }
  157. // DefaultLogLevel returns a default log level of "error"
  158. func DefaultLogLevel() string {
  159. return "error"
  160. }
  161. // DefaultPackageLogLevels returns a default log level setting so all packages
  162. // log at "error", while the `state` and `main` packages log at "info"
  163. func DefaultPackageLogLevels() string {
  164. return fmt.Sprintf("main:info,state:info,*:%s", DefaultLogLevel())
  165. }
  166. //-----------------------------------------------------------------------------
  167. // RPCConfig
  168. // RPCConfig defines the configuration options for the Tendermint RPC server
  169. type RPCConfig struct {
  170. RootDir string `mapstructure:"home"`
  171. // TCP or UNIX socket address for the RPC server to listen on
  172. ListenAddress string `mapstructure:"laddr"`
  173. // TCP or UNIX socket address for the gRPC server to listen on
  174. // NOTE: This server only supports /broadcast_tx_commit
  175. GRPCListenAddress string `mapstructure:"grpc_laddr"`
  176. // Activate unsafe RPC commands like /dial_persistent_peers and /unsafe_flush_mempool
  177. Unsafe bool `mapstructure:"unsafe"`
  178. }
  179. // DefaultRPCConfig returns a default configuration for the RPC server
  180. func DefaultRPCConfig() *RPCConfig {
  181. return &RPCConfig{
  182. ListenAddress: "tcp://0.0.0.0:46657",
  183. GRPCListenAddress: "",
  184. Unsafe: false,
  185. }
  186. }
  187. // TestRPCConfig returns a configuration for testing the RPC server
  188. func TestRPCConfig() *RPCConfig {
  189. conf := DefaultRPCConfig()
  190. conf.ListenAddress = "tcp://0.0.0.0:36657"
  191. conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
  192. conf.Unsafe = true
  193. return conf
  194. }
  195. //-----------------------------------------------------------------------------
  196. // P2PConfig
  197. // P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer
  198. type P2PConfig struct {
  199. RootDir string `mapstructure:"home"`
  200. // Address to listen for incoming connections
  201. ListenAddress string `mapstructure:"laddr"`
  202. // Comma separated list of seed nodes to connect to
  203. // We only use these if we can’t connect to peers in the addrbook
  204. Seeds string `mapstructure:"seeds"`
  205. // Comma separated list of persistent peers to connect to
  206. // We always connect to these
  207. PersistentPeers string `mapstructure:"persistent_peers"`
  208. // Skip UPNP port forwarding
  209. SkipUPNP bool `mapstructure:"skip_upnp"`
  210. // Path to address book
  211. AddrBook string `mapstructure:"addr_book_file"`
  212. // Set true for strict address routability rules
  213. AddrBookStrict bool `mapstructure:"addr_book_strict"`
  214. // Maximum number of peers to connect to
  215. MaxNumPeers int `mapstructure:"max_num_peers"`
  216. // Time to wait before flushing messages out on the connection, in ms
  217. FlushThrottleTimeout int `mapstructure:"flush_throttle_timeout"`
  218. // Maximum size of a message packet payload, in bytes
  219. MaxMsgPacketPayloadSize int `mapstructure:"max_msg_packet_payload_size"`
  220. // Rate at which packets can be sent, in bytes/second
  221. SendRate int64 `mapstructure:"send_rate"`
  222. // Rate at which packets can be received, in bytes/second
  223. RecvRate int64 `mapstructure:"recv_rate"`
  224. // Set true to enable the peer-exchange reactor
  225. PexReactor bool `mapstructure:"pex"`
  226. // Seed mode, in which node constantly crawls the network and looks for
  227. // peers. If another node asks it for addresses, it responds and disconnects.
  228. //
  229. // Does not work if the peer-exchange reactor is disabled.
  230. SeedMode bool `mapstructure:"seed_mode"`
  231. // Authenticated encryption
  232. AuthEnc bool `mapstructure:"auth_enc"`
  233. }
  234. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  235. func DefaultP2PConfig() *P2PConfig {
  236. return &P2PConfig{
  237. ListenAddress: "tcp://0.0.0.0:46656",
  238. AddrBook: defaultAddrBookPath,
  239. AddrBookStrict: true,
  240. MaxNumPeers: 50,
  241. FlushThrottleTimeout: 100,
  242. MaxMsgPacketPayloadSize: 1024, // 1 kB
  243. SendRate: 512000, // 500 kB/s
  244. RecvRate: 512000, // 500 kB/s
  245. PexReactor: true,
  246. SeedMode: false,
  247. AuthEnc: true,
  248. }
  249. }
  250. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  251. func TestP2PConfig() *P2PConfig {
  252. conf := DefaultP2PConfig()
  253. conf.ListenAddress = "tcp://0.0.0.0:36656"
  254. conf.SkipUPNP = true
  255. conf.FlushThrottleTimeout = 10
  256. return conf
  257. }
  258. // AddrBookFile returns the full path to the address book
  259. func (p *P2PConfig) AddrBookFile() string {
  260. return rootify(p.AddrBook, p.RootDir)
  261. }
  262. //-----------------------------------------------------------------------------
  263. // MempoolConfig
  264. // MempoolConfig defines the configuration options for the Tendermint mempool
  265. type MempoolConfig struct {
  266. RootDir string `mapstructure:"home"`
  267. Recheck bool `mapstructure:"recheck"`
  268. RecheckEmpty bool `mapstructure:"recheck_empty"`
  269. Broadcast bool `mapstructure:"broadcast"`
  270. WalPath string `mapstructure:"wal_dir"`
  271. CacheSize int `mapstructure:"cache_size"`
  272. }
  273. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  274. func DefaultMempoolConfig() *MempoolConfig {
  275. return &MempoolConfig{
  276. Recheck: true,
  277. RecheckEmpty: true,
  278. Broadcast: true,
  279. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  280. CacheSize: 100000,
  281. }
  282. }
  283. // TestMempoolConfig returns a configuration for testing the Tendermint mempool
  284. func TestMempoolConfig() *MempoolConfig {
  285. config := DefaultMempoolConfig()
  286. config.CacheSize = 1000
  287. return config
  288. }
  289. // WalDir returns the full path to the mempool's write-ahead log
  290. func (m *MempoolConfig) WalDir() string {
  291. return rootify(m.WalPath, m.RootDir)
  292. }
  293. //-----------------------------------------------------------------------------
  294. // ConsensusConfig
  295. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  296. // including timeouts and details about the WAL and the block structure.
  297. type ConsensusConfig struct {
  298. RootDir string `mapstructure:"home"`
  299. WalPath string `mapstructure:"wal_file"`
  300. WalLight bool `mapstructure:"wal_light"`
  301. walFile string // overrides WalPath if set
  302. // All timeouts are in milliseconds
  303. TimeoutPropose int `mapstructure:"timeout_propose"`
  304. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  305. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  306. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  307. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  308. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  309. TimeoutCommit int `mapstructure:"timeout_commit"`
  310. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  311. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  312. // BlockSize
  313. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  314. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  315. // EmptyBlocks mode and possible interval between empty blocks in seconds
  316. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  317. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  318. // Reactor sleep duration parameters are in milliseconds
  319. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  320. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  321. }
  322. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  323. func (cfg *ConsensusConfig) WaitForTxs() bool {
  324. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  325. }
  326. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  327. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  328. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  329. }
  330. // Propose returns the amount of time to wait for a proposal
  331. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  332. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  333. }
  334. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  335. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  336. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  337. }
  338. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  339. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  340. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  341. }
  342. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  343. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  344. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  345. }
  346. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  347. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  348. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  349. }
  350. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  351. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  352. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  353. }
  354. // DefaultConsensusConfig returns a default configuration for the consensus service
  355. func DefaultConsensusConfig() *ConsensusConfig {
  356. return &ConsensusConfig{
  357. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  358. WalLight: false,
  359. TimeoutPropose: 3000,
  360. TimeoutProposeDelta: 500,
  361. TimeoutPrevote: 1000,
  362. TimeoutPrevoteDelta: 500,
  363. TimeoutPrecommit: 1000,
  364. TimeoutPrecommitDelta: 500,
  365. TimeoutCommit: 1000,
  366. SkipTimeoutCommit: false,
  367. MaxBlockSizeTxs: 10000,
  368. MaxBlockSizeBytes: 1, // TODO
  369. CreateEmptyBlocks: true,
  370. CreateEmptyBlocksInterval: 0,
  371. PeerGossipSleepDuration: 100,
  372. PeerQueryMaj23SleepDuration: 2000,
  373. }
  374. }
  375. // TestConsensusConfig returns a configuration for testing the consensus service
  376. func TestConsensusConfig() *ConsensusConfig {
  377. config := DefaultConsensusConfig()
  378. config.TimeoutPropose = 100
  379. config.TimeoutProposeDelta = 1
  380. config.TimeoutPrevote = 10
  381. config.TimeoutPrevoteDelta = 1
  382. config.TimeoutPrecommit = 10
  383. config.TimeoutPrecommitDelta = 1
  384. config.TimeoutCommit = 10
  385. config.SkipTimeoutCommit = true
  386. config.PeerGossipSleepDuration = 5
  387. config.PeerQueryMaj23SleepDuration = 250
  388. return config
  389. }
  390. // WalFile returns the full path to the write-ahead log file
  391. func (c *ConsensusConfig) WalFile() string {
  392. if c.walFile != "" {
  393. return c.walFile
  394. }
  395. return rootify(c.WalPath, c.RootDir)
  396. }
  397. // SetWalFile sets the path to the write-ahead log file
  398. func (c *ConsensusConfig) SetWalFile(walFile string) {
  399. c.walFile = walFile
  400. }
  401. //-----------------------------------------------------------------------------
  402. // TxIndexConfig
  403. // TxIndexConfig defines the confuguration for the transaction
  404. // indexer, including tags to index.
  405. type TxIndexConfig struct {
  406. // What indexer to use for transactions
  407. //
  408. // Options:
  409. // 1) "null" (default)
  410. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  411. Indexer string `mapstructure:"indexer"`
  412. // Comma-separated list of tags to index (by default the only tag is tx hash)
  413. //
  414. // It's recommended to index only a subset of tags due to possible memory
  415. // bloat. This is, of course, depends on the indexer's DB and the volume of
  416. // transactions.
  417. IndexTags string `mapstructure:"index_tags"`
  418. // When set to true, tells indexer to index all tags. Note this may be not
  419. // desirable (see the comment above). IndexTags has a precedence over
  420. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  421. IndexAllTags bool `mapstructure:"index_all_tags"`
  422. }
  423. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  424. func DefaultTxIndexConfig() *TxIndexConfig {
  425. return &TxIndexConfig{
  426. Indexer: "kv",
  427. IndexTags: "",
  428. IndexAllTags: false,
  429. }
  430. }
  431. // TestTxIndexConfig returns a default configuration for the transaction indexer.
  432. func TestTxIndexConfig() *TxIndexConfig {
  433. return DefaultTxIndexConfig()
  434. }
  435. //-----------------------------------------------------------------------------
  436. // Utils
  437. // helper function to make config creation independent of root dir
  438. func rootify(path, root string) string {
  439. if filepath.IsAbs(path) {
  440. return path
  441. }
  442. return filepath.Join(root, path)
  443. }
  444. //-----------------------------------------------------------------------------
  445. // Moniker
  446. var defaultMoniker = getDefaultMoniker()
  447. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  448. // fails to get the host name, "anonymous" will be returned.
  449. func getDefaultMoniker() string {
  450. moniker, err := os.Hostname()
  451. if err != nil {
  452. moniker = "anonymous"
  453. }
  454. return moniker
  455. }