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.

547 lines
18 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
p2p: introduce peerConn to simplify peer creation (#1226) * expose AuthEnc in the P2P config if AuthEnc is true, dialed peers must have a node ID in the address and it must match the persistent pubkey from the secret handshake. Refs #1157 * fixes after my own review * fix docs * fix build failure ``` p2p/pex/pex_reactor_test.go:288:88: cannot use seed.NodeInfo().NetAddress() (type *p2p.NetAddress) as type string in array or slice literal ``` * p2p: introduce peerConn to simplify peer creation * Introduce `peerConn` containing the known fields of `peer` * `peer` only created in `sw.addPeer` once handshake is complete and NodeInfo is checked * Eliminates some mutable variables and makes the code flow better * Simplifies the `newXxxPeer` funcs * Use ID instead of PubKey where possible. * SetPubKeyFilter -> SetIDFilter * nodeInfo.Validate takes ID * remove peer.PubKey() * persistent node ids * fixes from review * test: use ip_plus_id.sh more * fix invalid memory panic during fast_sync test ``` 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: panic: runtime error: invalid memory address or nil pointer dereference 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x98dd3e] 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: goroutine 3432 [running]: 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.newOutboundPeerConn(0xc423fd1380, 0xc420933e00, 0x1, 0x1239a60, 0 xc420128c40, 0x2, 0x42caf6, 0xc42001f300, 0xc422831d98, 0xc4227951c0, ...) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/peer.go:123 +0x31e 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).addOutboundPeerWithConfig(0xc4200ad040, 0xc423fd1380, 0 xc420933e00, 0xc423f48801, 0x28, 0x2) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:455 +0x12b 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).DialPeerWithAddress(0xc4200ad040, 0xc423fd1380, 0x1, 0x 0, 0x0) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:371 +0xdc 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: github.com/tendermint/tendermint/p2p.(*Switch).reconnectToPeer(0xc4200ad040, 0x123e000, 0xc42007bb00) 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:290 +0x25f 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: created by github.com/tendermint/tendermint/p2p.(*Switch).StopPeerForError 2018-02-21T06:30:05Z box887.localdomain docker/local_testnet_4[14907]: #011/go/src/github.com/tendermint/tendermint/p2p/switch.go:256 +0x1b7 ```
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. )
  8. // NOTE: Most of the structs & relevant comments + the
  9. // default configuration options were used to manually
  10. // generate the config.toml. Please reflect any changes
  11. // made here in the defaultConfigTemplate constant in
  12. // config/toml.go
  13. // NOTE: tmlibs/cli must know to look in the config dir!
  14. var (
  15. DefaultTendermintDir = ".tendermint"
  16. defaultConfigDir = "config"
  17. defaultDataDir = "data"
  18. defaultConfigFileName = "config.toml"
  19. defaultGenesisJSONName = "genesis.json"
  20. defaultPrivValName = "priv_validator.json"
  21. defaultNodeKeyName = "node_key.json"
  22. defaultAddrBookName = "addrbook.json"
  23. defaultConfigFilePath = filepath.Join(defaultConfigDir, defaultConfigFileName)
  24. defaultGenesisJSONPath = filepath.Join(defaultConfigDir, defaultGenesisJSONName)
  25. defaultPrivValPath = filepath.Join(defaultConfigDir, defaultPrivValName)
  26. defaultNodeKeyPath = filepath.Join(defaultConfigDir, defaultNodeKeyName)
  27. defaultAddrBookPath = filepath.Join(defaultConfigDir, defaultAddrBookName)
  28. )
  29. // Config defines the top level configuration for a Tendermint node
  30. type Config struct {
  31. // Top level options use an anonymous struct
  32. BaseConfig `mapstructure:",squash"`
  33. // Options for services
  34. RPC *RPCConfig `mapstructure:"rpc"`
  35. P2P *P2PConfig `mapstructure:"p2p"`
  36. Mempool *MempoolConfig `mapstructure:"mempool"`
  37. Consensus *ConsensusConfig `mapstructure:"consensus"`
  38. TxIndex *TxIndexConfig `mapstructure:"tx_index"`
  39. }
  40. // DefaultConfig returns a default configuration for a Tendermint node
  41. func DefaultConfig() *Config {
  42. return &Config{
  43. BaseConfig: DefaultBaseConfig(),
  44. RPC: DefaultRPCConfig(),
  45. P2P: DefaultP2PConfig(),
  46. Mempool: DefaultMempoolConfig(),
  47. Consensus: DefaultConsensusConfig(),
  48. TxIndex: DefaultTxIndexConfig(),
  49. }
  50. }
  51. // TestConfig returns a configuration that can be used for testing
  52. func TestConfig() *Config {
  53. return &Config{
  54. BaseConfig: TestBaseConfig(),
  55. RPC: TestRPCConfig(),
  56. P2P: TestP2PConfig(),
  57. Mempool: TestMempoolConfig(),
  58. Consensus: TestConsensusConfig(),
  59. TxIndex: TestTxIndexConfig(),
  60. }
  61. }
  62. // SetRoot sets the RootDir for all Config structs
  63. func (cfg *Config) SetRoot(root string) *Config {
  64. cfg.BaseConfig.RootDir = root
  65. cfg.RPC.RootDir = root
  66. cfg.P2P.RootDir = root
  67. cfg.Mempool.RootDir = root
  68. cfg.Consensus.RootDir = root
  69. return cfg
  70. }
  71. //-----------------------------------------------------------------------------
  72. // BaseConfig
  73. // BaseConfig defines the base configuration for a Tendermint node
  74. type BaseConfig struct {
  75. // chainID is unexposed and immutable but here for convenience
  76. chainID string
  77. // The root directory for all data.
  78. // This should be set in viper so it can unmarshal into this struct
  79. RootDir string `mapstructure:"home"`
  80. // Path to the JSON file containing the initial validator set and other meta data
  81. Genesis string `mapstructure:"genesis_file"`
  82. // Path to the JSON file containing the private key to use as a validator in the consensus protocol
  83. PrivValidator string `mapstructure:"priv_validator_file"`
  84. // A JSON file containing the private key to use for p2p authenticated encryption
  85. NodeKey string `mapstructure:"node_key_file"`
  86. // A custom human readable name for this node
  87. Moniker string `mapstructure:"moniker"`
  88. // TCP or UNIX socket address of the ABCI application,
  89. // or the name of an ABCI application compiled in with the Tendermint binary
  90. ProxyApp string `mapstructure:"proxy_app"`
  91. // Mechanism to connect to the ABCI application: socket | grpc
  92. ABCI string `mapstructure:"abci"`
  93. // Output level for logging
  94. LogLevel string `mapstructure:"log_level"`
  95. // TCP or UNIX socket address for the profiling server to listen on
  96. ProfListenAddress string `mapstructure:"prof_laddr"`
  97. // If this node is many blocks behind the tip of the chain, FastSync
  98. // allows them to catchup quickly by downloading blocks in parallel
  99. // and verifying their commits
  100. FastSync bool `mapstructure:"fast_sync"`
  101. // If true, query the ABCI app on connecting to a new peer
  102. // so the app can decide if we should keep the connection or not
  103. FilterPeers bool `mapstructure:"filter_peers"` // false
  104. // Database backend: leveldb | memdb
  105. DBBackend string `mapstructure:"db_backend"`
  106. // Database directory
  107. DBPath string `mapstructure:"db_dir"`
  108. }
  109. func (c BaseConfig) ChainID() string {
  110. return c.chainID
  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. conf := DefaultBaseConfig()
  132. conf.chainID = "tendermint_test"
  133. conf.ProxyApp = "dummy"
  134. conf.FastSync = false
  135. conf.DBBackend = "memdb"
  136. return conf
  137. }
  138. // GenesisFile returns the full path to the genesis.json file
  139. func (b BaseConfig) GenesisFile() string {
  140. return rootify(b.Genesis, b.RootDir)
  141. }
  142. // PrivValidatorFile returns the full path to the priv_validator.json file
  143. func (b BaseConfig) PrivValidatorFile() string {
  144. return rootify(b.PrivValidator, b.RootDir)
  145. }
  146. // NodeKeyFile returns the full path to the node_key.json file
  147. func (b BaseConfig) NodeKeyFile() string {
  148. return rootify(b.NodeKey, b.RootDir)
  149. }
  150. // DBDir returns the full path to the database directory
  151. func (b BaseConfig) DBDir() string {
  152. return rootify(b.DBPath, b.RootDir)
  153. }
  154. // DefaultLogLevel returns a default log level of "error"
  155. func DefaultLogLevel() string {
  156. return "error"
  157. }
  158. // DefaultPackageLogLevels returns a default log level setting so all packages
  159. // log at "error", while the `state` and `main` packages log at "info"
  160. func DefaultPackageLogLevels() string {
  161. return fmt.Sprintf("main:info,state:info,*:%s", DefaultLogLevel())
  162. }
  163. //-----------------------------------------------------------------------------
  164. // RPCConfig
  165. // RPCConfig defines the configuration options for the Tendermint RPC server
  166. type RPCConfig struct {
  167. RootDir string `mapstructure:"home"`
  168. // TCP or UNIX socket address for the RPC server to listen on
  169. ListenAddress string `mapstructure:"laddr"`
  170. // TCP or UNIX socket address for the gRPC server to listen on
  171. // NOTE: This server only supports /broadcast_tx_commit
  172. GRPCListenAddress string `mapstructure:"grpc_laddr"`
  173. // Activate unsafe RPC commands like /dial_persistent_peers and /unsafe_flush_mempool
  174. Unsafe bool `mapstructure:"unsafe"`
  175. }
  176. // DefaultRPCConfig returns a default configuration for the RPC server
  177. func DefaultRPCConfig() *RPCConfig {
  178. return &RPCConfig{
  179. ListenAddress: "tcp://0.0.0.0:46657",
  180. GRPCListenAddress: "",
  181. Unsafe: false,
  182. }
  183. }
  184. // TestRPCConfig returns a configuration for testing the RPC server
  185. func TestRPCConfig() *RPCConfig {
  186. conf := DefaultRPCConfig()
  187. conf.ListenAddress = "tcp://0.0.0.0:36657"
  188. conf.GRPCListenAddress = "tcp://0.0.0.0:36658"
  189. conf.Unsafe = true
  190. return conf
  191. }
  192. //-----------------------------------------------------------------------------
  193. // P2PConfig
  194. // P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer
  195. type P2PConfig struct {
  196. RootDir string `mapstructure:"home"`
  197. // Address to listen for incoming connections
  198. ListenAddress string `mapstructure:"laddr"`
  199. // Comma separated list of seed nodes to connect to
  200. // We only use these if we can’t connect to peers in the addrbook
  201. Seeds string `mapstructure:"seeds"`
  202. // Comma separated list of persistent peers to connect to
  203. // We always connect to these
  204. PersistentPeers string `mapstructure:"persistent_peers"`
  205. // Skip UPNP port forwarding
  206. SkipUPNP bool `mapstructure:"skip_upnp"`
  207. // Path to address book
  208. AddrBook string `mapstructure:"addr_book_file"`
  209. // Set true for strict address routability rules
  210. AddrBookStrict bool `mapstructure:"addr_book_strict"`
  211. // Maximum number of peers to connect to
  212. MaxNumPeers int `mapstructure:"max_num_peers"`
  213. // Time to wait before flushing messages out on the connection, in ms
  214. FlushThrottleTimeout int `mapstructure:"flush_throttle_timeout"`
  215. // Maximum size of a message packet payload, in bytes
  216. MaxMsgPacketPayloadSize int `mapstructure:"max_msg_packet_payload_size"`
  217. // Rate at which packets can be sent, in bytes/second
  218. SendRate int64 `mapstructure:"send_rate"`
  219. // Rate at which packets can be received, in bytes/second
  220. RecvRate int64 `mapstructure:"recv_rate"`
  221. // Set true to enable the peer-exchange reactor
  222. PexReactor bool `mapstructure:"pex"`
  223. // Seed mode, in which node constantly crawls the network and looks for
  224. // peers. If another node asks it for addresses, it responds and disconnects.
  225. //
  226. // Does not work if the peer-exchange reactor is disabled.
  227. SeedMode bool `mapstructure:"seed_mode"`
  228. // Authenticated encryption
  229. AuthEnc bool `mapstructure:"auth_enc"`
  230. }
  231. // DefaultP2PConfig returns a default configuration for the peer-to-peer layer
  232. func DefaultP2PConfig() *P2PConfig {
  233. return &P2PConfig{
  234. ListenAddress: "tcp://0.0.0.0:46656",
  235. AddrBook: defaultAddrBookPath,
  236. AddrBookStrict: true,
  237. MaxNumPeers: 50,
  238. FlushThrottleTimeout: 100,
  239. MaxMsgPacketPayloadSize: 1024, // 1 kB
  240. SendRate: 512000, // 500 kB/s
  241. RecvRate: 512000, // 500 kB/s
  242. PexReactor: true,
  243. SeedMode: false,
  244. AuthEnc: true,
  245. }
  246. }
  247. // TestP2PConfig returns a configuration for testing the peer-to-peer layer
  248. func TestP2PConfig() *P2PConfig {
  249. conf := DefaultP2PConfig()
  250. conf.ListenAddress = "tcp://0.0.0.0:36656"
  251. conf.SkipUPNP = true
  252. conf.FlushThrottleTimeout = 10
  253. return conf
  254. }
  255. // AddrBookFile returns the full path to the address book
  256. func (p *P2PConfig) AddrBookFile() string {
  257. return rootify(p.AddrBook, p.RootDir)
  258. }
  259. //-----------------------------------------------------------------------------
  260. // MempoolConfig
  261. // MempoolConfig defines the configuration options for the Tendermint mempool
  262. type MempoolConfig struct {
  263. RootDir string `mapstructure:"home"`
  264. Recheck bool `mapstructure:"recheck"`
  265. RecheckEmpty bool `mapstructure:"recheck_empty"`
  266. Broadcast bool `mapstructure:"broadcast"`
  267. WalPath string `mapstructure:"wal_dir"`
  268. CacheSize int `mapstructure:"cache_size"`
  269. }
  270. // DefaultMempoolConfig returns a default configuration for the Tendermint mempool
  271. func DefaultMempoolConfig() *MempoolConfig {
  272. return &MempoolConfig{
  273. Recheck: true,
  274. RecheckEmpty: true,
  275. Broadcast: true,
  276. WalPath: filepath.Join(defaultDataDir, "mempool.wal"),
  277. CacheSize: 100000,
  278. }
  279. }
  280. // TestMempoolConfig returns a configuration for testing the Tendermint mempool
  281. func TestMempoolConfig() *MempoolConfig {
  282. config := DefaultMempoolConfig()
  283. config.CacheSize = 1000
  284. return config
  285. }
  286. // WalDir returns the full path to the mempool's write-ahead log
  287. func (m *MempoolConfig) WalDir() string {
  288. return rootify(m.WalPath, m.RootDir)
  289. }
  290. //-----------------------------------------------------------------------------
  291. // ConsensusConfig
  292. // ConsensusConfig defines the confuguration for the Tendermint consensus service,
  293. // including timeouts and details about the WAL and the block structure.
  294. type ConsensusConfig struct {
  295. RootDir string `mapstructure:"home"`
  296. WalPath string `mapstructure:"wal_file"`
  297. WalLight bool `mapstructure:"wal_light"`
  298. walFile string // overrides WalPath if set
  299. // All timeouts are in milliseconds
  300. TimeoutPropose int `mapstructure:"timeout_propose"`
  301. TimeoutProposeDelta int `mapstructure:"timeout_propose_delta"`
  302. TimeoutPrevote int `mapstructure:"timeout_prevote"`
  303. TimeoutPrevoteDelta int `mapstructure:"timeout_prevote_delta"`
  304. TimeoutPrecommit int `mapstructure:"timeout_precommit"`
  305. TimeoutPrecommitDelta int `mapstructure:"timeout_precommit_delta"`
  306. TimeoutCommit int `mapstructure:"timeout_commit"`
  307. // Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  308. SkipTimeoutCommit bool `mapstructure:"skip_timeout_commit"`
  309. // BlockSize
  310. MaxBlockSizeTxs int `mapstructure:"max_block_size_txs"`
  311. MaxBlockSizeBytes int `mapstructure:"max_block_size_bytes"`
  312. // EmptyBlocks mode and possible interval between empty blocks in seconds
  313. CreateEmptyBlocks bool `mapstructure:"create_empty_blocks"`
  314. CreateEmptyBlocksInterval int `mapstructure:"create_empty_blocks_interval"`
  315. // Reactor sleep duration parameters are in milliseconds
  316. PeerGossipSleepDuration int `mapstructure:"peer_gossip_sleep_duration"`
  317. PeerQueryMaj23SleepDuration int `mapstructure:"peer_query_maj23_sleep_duration"`
  318. }
  319. // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
  320. func (cfg *ConsensusConfig) WaitForTxs() bool {
  321. return !cfg.CreateEmptyBlocks || cfg.CreateEmptyBlocksInterval > 0
  322. }
  323. // EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
  324. func (cfg *ConsensusConfig) EmptyBlocksInterval() time.Duration {
  325. return time.Duration(cfg.CreateEmptyBlocksInterval) * time.Second
  326. }
  327. // Propose returns the amount of time to wait for a proposal
  328. func (cfg *ConsensusConfig) Propose(round int) time.Duration {
  329. return time.Duration(cfg.TimeoutPropose+cfg.TimeoutProposeDelta*round) * time.Millisecond
  330. }
  331. // Prevote returns the amount of time to wait for straggler votes after receiving any +2/3 prevotes
  332. func (cfg *ConsensusConfig) Prevote(round int) time.Duration {
  333. return time.Duration(cfg.TimeoutPrevote+cfg.TimeoutPrevoteDelta*round) * time.Millisecond
  334. }
  335. // Precommit returns the amount of time to wait for straggler votes after receiving any +2/3 precommits
  336. func (cfg *ConsensusConfig) Precommit(round int) time.Duration {
  337. return time.Duration(cfg.TimeoutPrecommit+cfg.TimeoutPrecommitDelta*round) * time.Millisecond
  338. }
  339. // Commit returns the amount of time to wait for straggler votes after receiving +2/3 precommits for a single block (ie. a commit).
  340. func (cfg *ConsensusConfig) Commit(t time.Time) time.Time {
  341. return t.Add(time.Duration(cfg.TimeoutCommit) * time.Millisecond)
  342. }
  343. // PeerGossipSleep returns the amount of time to sleep if there is nothing to send from the ConsensusReactor
  344. func (cfg *ConsensusConfig) PeerGossipSleep() time.Duration {
  345. return time.Duration(cfg.PeerGossipSleepDuration) * time.Millisecond
  346. }
  347. // PeerQueryMaj23Sleep returns the amount of time to sleep after each VoteSetMaj23Message is sent in the ConsensusReactor
  348. func (cfg *ConsensusConfig) PeerQueryMaj23Sleep() time.Duration {
  349. return time.Duration(cfg.PeerQueryMaj23SleepDuration) * time.Millisecond
  350. }
  351. // DefaultConsensusConfig returns a default configuration for the consensus service
  352. func DefaultConsensusConfig() *ConsensusConfig {
  353. return &ConsensusConfig{
  354. WalPath: filepath.Join(defaultDataDir, "cs.wal", "wal"),
  355. WalLight: false,
  356. TimeoutPropose: 3000,
  357. TimeoutProposeDelta: 500,
  358. TimeoutPrevote: 1000,
  359. TimeoutPrevoteDelta: 500,
  360. TimeoutPrecommit: 1000,
  361. TimeoutPrecommitDelta: 500,
  362. TimeoutCommit: 1000,
  363. SkipTimeoutCommit: false,
  364. MaxBlockSizeTxs: 10000,
  365. MaxBlockSizeBytes: 1, // TODO
  366. CreateEmptyBlocks: true,
  367. CreateEmptyBlocksInterval: 0,
  368. PeerGossipSleepDuration: 100,
  369. PeerQueryMaj23SleepDuration: 2000,
  370. }
  371. }
  372. // TestConsensusConfig returns a configuration for testing the consensus service
  373. func TestConsensusConfig() *ConsensusConfig {
  374. config := DefaultConsensusConfig()
  375. config.TimeoutPropose = 100
  376. config.TimeoutProposeDelta = 1
  377. config.TimeoutPrevote = 10
  378. config.TimeoutPrevoteDelta = 1
  379. config.TimeoutPrecommit = 10
  380. config.TimeoutPrecommitDelta = 1
  381. config.TimeoutCommit = 10
  382. config.SkipTimeoutCommit = true
  383. config.PeerGossipSleepDuration = 5
  384. config.PeerQueryMaj23SleepDuration = 250
  385. return config
  386. }
  387. // WalFile returns the full path to the write-ahead log file
  388. func (c *ConsensusConfig) WalFile() string {
  389. if c.walFile != "" {
  390. return c.walFile
  391. }
  392. return rootify(c.WalPath, c.RootDir)
  393. }
  394. // SetWalFile sets the path to the write-ahead log file
  395. func (c *ConsensusConfig) SetWalFile(walFile string) {
  396. c.walFile = walFile
  397. }
  398. //-----------------------------------------------------------------------------
  399. // TxIndexConfig
  400. // TxIndexConfig defines the confuguration for the transaction
  401. // indexer, including tags to index.
  402. type TxIndexConfig struct {
  403. // What indexer to use for transactions
  404. //
  405. // Options:
  406. // 1) "null" (default)
  407. // 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  408. Indexer string `mapstructure:"indexer"`
  409. // Comma-separated list of tags to index (by default the only tag is tx hash)
  410. //
  411. // It's recommended to index only a subset of tags due to possible memory
  412. // bloat. This is, of course, depends on the indexer's DB and the volume of
  413. // transactions.
  414. IndexTags string `mapstructure:"index_tags"`
  415. // When set to true, tells indexer to index all tags. Note this may be not
  416. // desirable (see the comment above). IndexTags has a precedence over
  417. // IndexAllTags (i.e. when given both, IndexTags will be indexed).
  418. IndexAllTags bool `mapstructure:"index_all_tags"`
  419. }
  420. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
  421. func DefaultTxIndexConfig() *TxIndexConfig {
  422. return &TxIndexConfig{
  423. Indexer: "kv",
  424. IndexTags: "",
  425. IndexAllTags: false,
  426. }
  427. }
  428. // TestTxIndexConfig returns a default configuration for the transaction indexer.
  429. func TestTxIndexConfig() *TxIndexConfig {
  430. return DefaultTxIndexConfig()
  431. }
  432. //-----------------------------------------------------------------------------
  433. // Utils
  434. // helper function to make config creation independent of root dir
  435. func rootify(path, root string) string {
  436. if filepath.IsAbs(path) {
  437. return path
  438. }
  439. return filepath.Join(root, path)
  440. }
  441. //-----------------------------------------------------------------------------
  442. // Moniker
  443. var defaultMoniker = getDefaultMoniker()
  444. // getDefaultMoniker returns a default moniker, which is the host name. If runtime
  445. // fails to get the host name, "anonymous" will be returned.
  446. func getDefaultMoniker() string {
  447. moniker, err := os.Hostname()
  448. if err != nil {
  449. moniker = "anonymous"
  450. }
  451. return moniker
  452. }