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.

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