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.

553 lines
19 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
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 ```
6 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 ```
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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. CacheSize int `mapstructure:"cache_size"`
  274. }
  275. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  276. func DefaultMempoolConfig() *MempoolConfig {
  277. return &MempoolConfig{
  278. Recheck: true,
  279. RecheckEmpty: true,
  280. Broadcast: true,
  281. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  282. CacheSize: 100000,
  283. }
  284. }
  285. // TestMempoolConfig returns a configuration for testing the Tendermint mempool
  286. func TestMempoolConfig() *MempoolConfig {
  287. cfg := DefaultMempoolConfig()
  288. cfg.CacheSize = 1000
  289. return cfg
  290. }
  291. // WalDir returns the full path to the mempool's write-ahead log
  292. func (cfg *MempoolConfig) WalDir() string {
  293. return rootify(cfg.WalPath, cfg.RootDir)
  294. }
  295. //-----------------------------------------------------------------------------
  296. // ConsensusConfig
  297. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  298. // including timeouts and details about the WAL and the block structure.
  299. type ConsensusConfig struct {
  300. RootDir string `mapstructure:"home"`
  301. WalPath string `mapstructure:"wal_file"`
  302. walFile string // overrides WalPath if set
  303. // All timeouts are in milliseconds
  304. TimeoutPropose int `mapstructure:"timeout_propose"`
  305. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  306. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  307. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  308. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  309. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  310. TimeoutCommit int `mapstructure:"timeout_commit"`
  311. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  312. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  313. // BlockSize
  314. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  315. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  316. // EmptyBlocks mode and possible interval between empty blocks in seconds
  317. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  318. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  319. // Reactor sleep duration parameters are in milliseconds
  320. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  321. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  322. }
  323. // DefaultConsensusConfig returns a default configuration for the consensus service
  324. func DefaultConsensusConfig() *ConsensusConfig {
  325. return &ConsensusConfig{
  326. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  327. TimeoutPropose: 3000,
  328. TimeoutProposeDelta: 500,
  329. TimeoutPrevote: 1000,
  330. TimeoutPrevoteDelta: 500,
  331. TimeoutPrecommit: 1000,
  332. TimeoutPrecommitDelta: 500,
  333. TimeoutCommit: 1000,
  334. SkipTimeoutCommit: false,
  335. MaxBlockSizeTxs: 10000,
  336. MaxBlockSizeBytes: 1, // TODO
  337. CreateEmptyBlocks: true,
  338. CreateEmptyBlocksInterval: 0,
  339. PeerGossipSleepDuration: 100,
  340. PeerQueryMaj23SleepDuration: 2000,
  341. }
  342. }
  343. // TestConsensusConfig returns a configuration for testing the consensus service
  344. func TestConsensusConfig() *ConsensusConfig {
  345. cfg := DefaultConsensusConfig()
  346. cfg.TimeoutPropose = 100
  347. cfg.TimeoutProposeDelta = 1
  348. cfg.TimeoutPrevote = 10
  349. cfg.TimeoutPrevoteDelta = 1
  350. cfg.TimeoutPrecommit = 10
  351. cfg.TimeoutPrecommitDelta = 1
  352. cfg.TimeoutCommit = 10
  353. cfg.SkipTimeoutCommit = true
  354. cfg.PeerGossipSleepDuration = 5
  355. cfg.PeerQueryMaj23SleepDuration = 250
  356. return cfg
  357. }
  358. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  359. func (cfg *ConsensusConfig) WaitForTxs() bool {
  360. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  361. }
  362. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  363. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  364. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  365. }
  366. // Propose returns the amount of time to wait for a proposal
  367. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  368. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  369. }
  370. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  371. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  372. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  373. }
  374. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  375. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  376. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  377. }
  378. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  379. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  380. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  381. }
  382. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  383. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  384. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  385. }
  386. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  387. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  388. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  389. }
  390. // WalFile returns the full path to the write-ahead log file
  391. func (cfg *ConsensusConfig) WalFile() string {
  392. if cfg.walFile != "" {
  393. return cfg.walFile
  394. }
  395. return rootify(cfg.WalPath, cfg.RootDir)
  396. }
  397. // SetWalFile sets the path to the write-ahead log file
  398. func (cfg *ConsensusConfig) SetWalFile(walFile string) {
  399. cfg.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. }