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.

555 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
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
  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. // DefaultBaseConfig returns a default base configuration for a Tendermint node
  113. func DefaultBaseConfig() BaseConfig {
  114. return BaseConfig{
  115. Genesis: defaultGenesisJSONPath,
  116. PrivValidator: defaultPrivValPath,
  117. NodeKey: defaultNodeKeyPath,
  118. Moniker: defaultMoniker,
  119. ProxyApp: "tcp://127.0.0.1:46658",
  120. ABCI: "socket",
  121. LogLevel: DefaultPackageLogLevels(),
  122. ProfListenAddress: "",
  123. FastSync: true,
  124. FilterPeers: false,
  125. DBBackend: "leveldb",
  126. DBPath: "data",
  127. }
  128. }
  129. // TestBaseConfig returns a base configuration for testing a Tendermint node
  130. func TestBaseConfig() BaseConfig {
  131. cfg := DefaultBaseConfig()
  132. cfg.chainID = "tendermint_test"
  133. cfg.ProxyApp = "kvstore"
  134. cfg.FastSync = false
  135. cfg.DBBackend = "memdb"
  136. return cfg
  137. }
  138. func (cfg BaseConfig) ChainID() string {
  139. return cfg.chainID
  140. }
  141. // GenesisFile returns the full path to the genesis.json file
  142. func (cfg BaseConfig) GenesisFile() string {
  143. return rootify(cfg.Genesis, cfg.RootDir)
  144. }
  145. // PrivValidatorFile returns the full path to the priv_validator.json file
  146. func (cfg BaseConfig) PrivValidatorFile() string {
  147. return rootify(cfg.PrivValidator, cfg.RootDir)
  148. }
  149. // NodeKeyFile returns the full path to the node_key.json file
  150. func (cfg BaseConfig) NodeKeyFile() string {
  151. return rootify(cfg.NodeKey, cfg.RootDir)
  152. }
  153. // DBDir returns the full path to the database directory
  154. func (cfg BaseConfig) DBDir() string {
  155. return rootify(cfg.DBPath, cfg.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. cfg := DefaultRPCConfig()
  190. cfg.ListenAddress = "tcp://0.0.0.0:36657"
  191. cfg.GRPCListenAddress = "tcp://0.0.0.0:36658"
  192. cfg.Unsafe = true
  193. return cfg
  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 nodes to keep persistent connections to
  206. // Do not add private peers to this list if you don't want them advertised
  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. MaxPacketMsgPayloadSize int `mapstructure:"max_packet_msg_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. // Comma separated list of peer IDs to keep private (will not be gossiped to other peers)
  234. PrivatePeerIDs string `mapstructure:"private_peer_ids"`
  235. }
  236. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  237. func DefaultP2PConfig() *P2PConfig {
  238. return &P2PConfig{
  239. ListenAddress: "tcp://0.0.0.0:46656",
  240. AddrBook: defaultAddrBookPath,
  241. AddrBookStrict: true,
  242. MaxNumPeers: 50,
  243. FlushThrottleTimeout: 100,
  244. MaxPacketMsgPayloadSize: 1024, // 1 kB
  245. SendRate: 512000, // 500 kB/s
  246. RecvRate: 512000, // 500 kB/s
  247. PexReactor: true,
  248. SeedMode: false,
  249. AuthEnc: true,
  250. }
  251. }
  252. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  253. func TestP2PConfig() *P2PConfig {
  254. cfg := DefaultP2PConfig()
  255. cfg.ListenAddress = "tcp://0.0.0.0:36656"
  256. cfg.SkipUPNP = true
  257. cfg.FlushThrottleTimeout = 10
  258. return cfg
  259. }
  260. // AddrBookFile returns the full path to the address book
  261. func (cfg *P2PConfig) AddrBookFile() string {
  262. return rootify(cfg.AddrBook, cfg.RootDir)
  263. }
  264. //-----------------------------------------------------------------------------
  265. // MempoolConfig
  266. // MempoolConfig defines the configuration options for the Tendermint mempool
  267. type MempoolConfig struct {
  268. RootDir string `mapstructure:"home"`
  269. Recheck bool `mapstructure:"recheck"`
  270. RecheckEmpty bool `mapstructure:"recheck_empty"`
  271. Broadcast bool `mapstructure:"broadcast"`
  272. WalPath string `mapstructure:"wal_dir"`
  273. Size int `mapstructure:"size"`
  274. CacheSize int `mapstructure:"cache_size"`
  275. }
  276. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  277. func DefaultMempoolConfig() *MempoolConfig {
  278. return &MempoolConfig{
  279. Recheck: true,
  280. RecheckEmpty: true,
  281. Broadcast: true,
  282. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  283. Size: 100000,
  284. CacheSize: 100000,
  285. }
  286. }
  287. // TestMempoolConfig returns a configuration for testing the Tendermint mempool
  288. func TestMempoolConfig() *MempoolConfig {
  289. cfg := DefaultMempoolConfig()
  290. cfg.CacheSize = 1000
  291. return cfg
  292. }
  293. // WalDir returns the full path to the mempool's write-ahead log
  294. func (cfg *MempoolConfig) WalDir() string {
  295. return rootify(cfg.WalPath, cfg.RootDir)
  296. }
  297. //-----------------------------------------------------------------------------
  298. // ConsensusConfig
  299. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  300. // including timeouts and details about the WAL and the block structure.
  301. type ConsensusConfig struct {
  302. RootDir string `mapstructure:"home"`
  303. WalPath string `mapstructure:"wal_file"`
  304. walFile string // overrides WalPath if set
  305. // All timeouts are in milliseconds
  306. TimeoutPropose int `mapstructure:"timeout_propose"`
  307. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  308. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  309. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  310. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  311. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  312. TimeoutCommit int `mapstructure:"timeout_commit"`
  313. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  314. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  315. // BlockSize
  316. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  317. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  318. // EmptyBlocks mode and possible interval between empty blocks in seconds
  319. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  320. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  321. // Reactor sleep duration parameters are in milliseconds
  322. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  323. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  324. }
  325. // DefaultConsensusConfig returns a default configuration for the consensus service
  326. func DefaultConsensusConfig() *ConsensusConfig {
  327. return &ConsensusConfig{
  328. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  329. TimeoutPropose: 3000,
  330. TimeoutProposeDelta: 500,
  331. TimeoutPrevote: 1000,
  332. TimeoutPrevoteDelta: 500,
  333. TimeoutPrecommit: 1000,
  334. TimeoutPrecommitDelta: 500,
  335. TimeoutCommit: 1000,
  336. SkipTimeoutCommit: false,
  337. MaxBlockSizeTxs: 10000,
  338. MaxBlockSizeBytes: 1, // TODO
  339. CreateEmptyBlocks: true,
  340. CreateEmptyBlocksInterval: 0,
  341. PeerGossipSleepDuration: 100,
  342. PeerQueryMaj23SleepDuration: 2000,
  343. }
  344. }
  345. // TestConsensusConfig returns a configuration for testing the consensus service
  346. func TestConsensusConfig() *ConsensusConfig {
  347. cfg := DefaultConsensusConfig()
  348. cfg.TimeoutPropose = 100
  349. cfg.TimeoutProposeDelta = 1
  350. cfg.TimeoutPrevote = 10
  351. cfg.TimeoutPrevoteDelta = 1
  352. cfg.TimeoutPrecommit = 10
  353. cfg.TimeoutPrecommitDelta = 1
  354. cfg.TimeoutCommit = 10
  355. cfg.SkipTimeoutCommit = true
  356. cfg.PeerGossipSleepDuration = 5
  357. cfg.PeerQueryMaj23SleepDuration = 250
  358. return cfg
  359. }
  360. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  361. func (cfg *ConsensusConfig) WaitForTxs() bool {
  362. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  363. }
  364. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  365. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  366. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  367. }
  368. // Propose returns the amount of time to wait for a proposal
  369. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  370. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  371. }
  372. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  373. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  374. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  375. }
  376. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  377. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  378. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  379. }
  380. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  381. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  382. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  383. }
  384. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  385. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  386. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  387. }
  388. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  389. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  390. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  391. }
  392. // WalFile returns the full path to the write-ahead log file
  393. func (cfg *ConsensusConfig) WalFile() string {
  394. if cfg.walFile != "" {
  395. return cfg.walFile
  396. }
  397. return rootify(cfg.WalPath, cfg.RootDir)
  398. }
  399. // SetWalFile sets the path to the write-ahead log file
  400. func (cfg *ConsensusConfig) SetWalFile(walFile string) {
  401. cfg.walFile = walFile
  402. }
  403. //-----------------------------------------------------------------------------
  404. // TxIndexConfig
  405. // TxIndexConfig defines the confuguration for the transaction
  406. // indexer, including tags to index.
  407. type TxIndexConfig struct {
  408. // What indexer to use for transactions
  409. //
  410. // Options:
  411. // 1) "null" (default)
  412. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  413. Indexer string `mapstructure:"indexer"`
  414. // Comma-separated list of tags to index (by default the only tag is tx hash)
  415. //
  416. // It's recommended to index only a subset of tags due to possible memory
  417. // bloat. This is, of course, depends on the indexer's DB and the volume of
  418. // transactions.
  419. IndexTags string `mapstructure:"index_tags"`
  420. // When set to true, tells indexer to index all tags. Note this may be not
  421. // desirable (see the comment above). IndexTags has a precedence over
  422. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  423. IndexAllTags bool `mapstructure:"index_all_tags"`
  424. }
  425. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  426. func DefaultTxIndexConfig() *TxIndexConfig {
  427. return &TxIndexConfig{
  428. Indexer: "kv",
  429. IndexTags: "",
  430. IndexAllTags: false,
  431. }
  432. }
  433. // TestTxIndexConfig returns a default configuration for the transaction indexer.
  434. func TestTxIndexConfig() *TxIndexConfig {
  435. return DefaultTxIndexConfig()
  436. }
  437. //-----------------------------------------------------------------------------
  438. // Utils
  439. // helper function to make config creation independent of root dir
  440. func rootify(path, root string) string {
  441. if filepath.IsAbs(path) {
  442. return path
  443. }
  444. return filepath.Join(root, path)
  445. }
  446. //-----------------------------------------------------------------------------
  447. // Moniker
  448. var defaultMoniker = getDefaultMoniker()
  449. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  450. // fails to get the host name, "anonymous" will be returned.
  451. func getDefaultMoniker() string {
  452. moniker, err := os.Hostname()
  453. if err != nil {
  454. moniker = "anonymous"
  455. }
  456. return moniker
  457. }