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.

600 lines
20 KiB

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