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.

310 lines
9.7 KiB

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
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
  1. package config
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "text/template"
  7. cmn "github.com/tendermint/tmlibs/common"
  8. )
  9. var configTemplate *template.Template
  10. func init() {
  11. var err error
  12. if configTemplate, err = template.New("configFileTemplate").Parse(defaultConfigTemplate); err != nil {
  13. panic(err)
  14. }
  15. }
  16. /****** these are for production settings ***********/
  17. // EnsureRoot creates the root, config, and data directories if they don't exist,
  18. // and panics if it fails.
  19. func EnsureRoot(rootDir string) {
  20. if err := cmn.EnsureDir(rootDir, 0700); err != nil {
  21. cmn.PanicSanity(err.Error())
  22. }
  23. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultConfigDir), 0700); err != nil {
  24. cmn.PanicSanity(err.Error())
  25. }
  26. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultDataDir), 0700); err != nil {
  27. cmn.PanicSanity(err.Error())
  28. }
  29. configFilePath := filepath.Join(rootDir, defaultConfigFilePath)
  30. // Write default config file if missing.
  31. if !cmn.FileExists(configFilePath) {
  32. writeDefaultConfigFile(configFilePath)
  33. }
  34. }
  35. // XXX: this func should probably be called by cmd/tendermint/commands/init.go
  36. // alongside the writing of the genesis.json and priv_validator.json
  37. func writeDefaultConfigFile(configFilePath string) {
  38. WriteConfigFile(configFilePath, DefaultConfig())
  39. }
  40. // WriteConfigFile renders config using the template and writes it to configFilePath.
  41. func WriteConfigFile(configFilePath string, config *Config) {
  42. var buffer bytes.Buffer
  43. if err := configTemplate.Execute(&buffer, config); err != nil {
  44. panic(err)
  45. }
  46. cmn.MustWriteFile(configFilePath, buffer.Bytes(), 0644)
  47. }
  48. // Note: any changes to the comments/variables/mapstructure
  49. // must be reflected in the appropriate struct in config/config.go
  50. const defaultConfigTemplate = `# This is a TOML config file.
  51. # For more information, see https://github.com/toml-lang/toml
  52. ##### main base config options #####
  53. # TCP or UNIX socket address of the ABCI application,
  54. # or the name of an ABCI application compiled in with the Tendermint binary
  55. proxy_app = "{{ .BaseConfig.ProxyApp }}"
  56. # A custom human readable name for this node
  57. moniker = "{{ .BaseConfig.Moniker }}"
  58. # If this node is many blocks behind the tip of the chain, FastSync
  59. # allows them to catchup quickly by downloading blocks in parallel
  60. # and verifying their commits
  61. fast_sync = {{ .BaseConfig.FastSync }}
  62. # Database backend: leveldb | memdb
  63. db_backend = "{{ .BaseConfig.DBBackend }}"
  64. # Database directory
  65. db_path = "{{ .BaseConfig.DBPath }}"
  66. # Output level for logging, including package level options
  67. log_level = "{{ .BaseConfig.LogLevel }}"
  68. ##### additional base config options #####
  69. # Path to the JSON file containing the initial validator set and other meta data
  70. genesis_file = "{{ .BaseConfig.Genesis }}"
  71. # Path to the JSON file containing the private key to use as a validator in the consensus protocol
  72. priv_validator_file = "{{ .BaseConfig.PrivValidator }}"
  73. # Path to the JSON file containing the private key to use for node authentication in the p2p protocol
  74. node_key_file = "{{ .BaseConfig.NodeKey}}"
  75. # Mechanism to connect to the ABCI application: socket | grpc
  76. abci = "{{ .BaseConfig.ABCI }}"
  77. # TCP or UNIX socket address for the profiling server to listen on
  78. prof_laddr = "{{ .BaseConfig.ProfListenAddress }}"
  79. # If true, query the ABCI app on connecting to a new peer
  80. # so the app can decide if we should keep the connection or not
  81. filter_peers = {{ .BaseConfig.FilterPeers }}
  82. ##### advanced configuration options #####
  83. ##### rpc server configuration options #####
  84. [rpc]
  85. # TCP or UNIX socket address for the RPC server to listen on
  86. laddr = "{{ .RPC.ListenAddress }}"
  87. # TCP or UNIX socket address for the gRPC server to listen on
  88. # NOTE: This server only supports /broadcast_tx_commit
  89. grpc_laddr = "{{ .RPC.GRPCListenAddress }}"
  90. # Activate unsafe RPC commands like /dial_seeds and /unsafe_flush_mempool
  91. unsafe = {{ .RPC.Unsafe }}
  92. ##### peer to peer configuration options #####
  93. [p2p]
  94. # Address to listen for incoming connections
  95. laddr = "{{ .P2P.ListenAddress }}"
  96. # Comma separated list of seed nodes to connect to
  97. seeds = "{{ .P2P.Seeds }}"
  98. # Comma separated list of nodes to keep persistent connections to
  99. # Do not add private peers to this list if you don't want them advertised
  100. persistent_peers = "{{ .P2P.PersistentPeers }}"
  101. # Path to address book
  102. addr_book_file = "{{ .P2P.AddrBook }}"
  103. # Set true for strict address routability rules
  104. addr_book_strict = {{ .P2P.AddrBookStrict }}
  105. # Time to wait before flushing messages out on the connection, in ms
  106. flush_throttle_timeout = {{ .P2P.FlushThrottleTimeout }}
  107. # Maximum number of peers to connect to
  108. max_num_peers = {{ .P2P.MaxNumPeers }}
  109. # Maximum size of a message packet payload, in bytes
  110. max_packet_msg_payload_size = {{ .P2P.MaxPacketMsgPayloadSize }}
  111. # Rate at which packets can be sent, in bytes/second
  112. send_rate = {{ .P2P.SendRate }}
  113. # Rate at which packets can be received, in bytes/second
  114. recv_rate = {{ .P2P.RecvRate }}
  115. # Set true to enable the peer-exchange reactor
  116. pex = {{ .P2P.PexReactor }}
  117. # Seed mode, in which node constantly crawls the network and looks for
  118. # peers. If another node asks it for addresses, it responds and disconnects.
  119. #
  120. # Does not work if the peer-exchange reactor is disabled.
  121. seed_mode = {{ .P2P.SeedMode }}
  122. # Authenticated encryption
  123. auth_enc = {{ .P2P.AuthEnc }}
  124. # Comma separated list of peer IDs to keep private (will not be gossiped to other peers)
  125. private_peer_ids = "{{ .P2P.PrivatePeerIDs }}"
  126. ##### mempool configuration options #####
  127. [mempool]
  128. recheck = {{ .Mempool.Recheck }}
  129. recheck_empty = {{ .Mempool.RecheckEmpty }}
  130. broadcast = {{ .Mempool.Broadcast }}
  131. wal_dir = "{{ .Mempool.WalPath }}"
  132. ##### consensus configuration options #####
  133. [consensus]
  134. wal_file = "{{ .Consensus.WalPath }}"
  135. # All timeouts are in milliseconds
  136. timeout_propose = {{ .Consensus.TimeoutPropose }}
  137. timeout_propose_delta = {{ .Consensus.TimeoutProposeDelta }}
  138. timeout_prevote = {{ .Consensus.TimeoutPrevote }}
  139. timeout_prevote_delta = {{ .Consensus.TimeoutPrevoteDelta }}
  140. timeout_precommit = {{ .Consensus.TimeoutPrecommit }}
  141. timeout_precommit_delta = {{ .Consensus.TimeoutPrecommitDelta }}
  142. timeout_commit = {{ .Consensus.TimeoutCommit }}
  143. # Make progress as soon as we have all the precommits (as if TimeoutCommit = 0)
  144. skip_timeout_commit = {{ .Consensus.SkipTimeoutCommit }}
  145. # BlockSize
  146. max_block_size_txs = {{ .Consensus.MaxBlockSizeTxs }}
  147. max_block_size_bytes = {{ .Consensus.MaxBlockSizeBytes }}
  148. # EmptyBlocks mode and possible interval between empty blocks in seconds
  149. create_empty_blocks = {{ .Consensus.CreateEmptyBlocks }}
  150. create_empty_blocks_interval = {{ .Consensus.CreateEmptyBlocksInterval }}
  151. # Reactor sleep duration parameters are in milliseconds
  152. peer_gossip_sleep_duration = {{ .Consensus.PeerGossipSleepDuration }}
  153. peer_query_maj23_sleep_duration = {{ .Consensus.PeerQueryMaj23SleepDuration }}
  154. ##### transactions indexer configuration options #####
  155. [tx_index]
  156. # What indexer to use for transactions
  157. #
  158. # Options:
  159. # 1) "null" (default)
  160. # 2) "kv" - the simplest possible indexer, backed by key-value storage (defaults to levelDB; see DBBackend).
  161. indexer = "{{ .TxIndex.Indexer }}"
  162. # Comma-separated list of tags to index (by default the only tag is tx hash)
  163. #
  164. # It's recommended to index only a subset of tags due to possible memory
  165. # bloat. This is, of course, depends on the indexer's DB and the volume of
  166. # transactions.
  167. index_tags = "{{ .TxIndex.IndexTags }}"
  168. # When set to true, tells indexer to index all tags. Note this may be not
  169. # desirable (see the comment above). IndexTags has a precedence over
  170. # IndexAllTags (i.e. when given both, IndexTags will be indexed).
  171. index_all_tags = {{ .TxIndex.IndexAllTags }}
  172. `
  173. /****** these are for test settings ***********/
  174. func ResetTestRoot(testName string) *Config {
  175. rootDir := os.ExpandEnv("$HOME/.tendermint_test")
  176. rootDir = filepath.Join(rootDir, testName)
  177. // Remove ~/.tendermint_test_bak
  178. if cmn.FileExists(rootDir + "_bak") {
  179. if err := os.RemoveAll(rootDir + "_bak"); err != nil {
  180. cmn.PanicSanity(err.Error())
  181. }
  182. }
  183. // Move ~/.tendermint_test to ~/.tendermint_test_bak
  184. if cmn.FileExists(rootDir) {
  185. if err := os.Rename(rootDir, rootDir+"_bak"); err != nil {
  186. cmn.PanicSanity(err.Error())
  187. }
  188. }
  189. // Create new dir
  190. if err := cmn.EnsureDir(rootDir, 0700); err != nil {
  191. cmn.PanicSanity(err.Error())
  192. }
  193. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultConfigDir), 0700); err != nil {
  194. cmn.PanicSanity(err.Error())
  195. }
  196. if err := cmn.EnsureDir(filepath.Join(rootDir, defaultDataDir), 0700); err != nil {
  197. cmn.PanicSanity(err.Error())
  198. }
  199. baseConfig := DefaultBaseConfig()
  200. configFilePath := filepath.Join(rootDir, defaultConfigFilePath)
  201. genesisFilePath := filepath.Join(rootDir, baseConfig.Genesis)
  202. privFilePath := filepath.Join(rootDir, baseConfig.PrivValidator)
  203. // Write default config file if missing.
  204. if !cmn.FileExists(configFilePath) {
  205. writeDefaultConfigFile(configFilePath)
  206. }
  207. if !cmn.FileExists(genesisFilePath) {
  208. cmn.MustWriteFile(genesisFilePath, []byte(testGenesis), 0644)
  209. }
  210. // we always overwrite the priv val
  211. cmn.MustWriteFile(privFilePath, []byte(testPrivValidator), 0644)
  212. config := TestConfig().SetRoot(rootDir)
  213. return config
  214. }
  215. var testGenesis = `{
  216. "genesis_time": "0001-01-01T00:00:00.000Z",
  217. "chain_id": "tendermint_test",
  218. "validators": [
  219. {
  220. "pub_key": {
  221. "type": "AC26791624DE60",
  222. "value":"AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
  223. },
  224. "power": 10,
  225. "name": ""
  226. }
  227. ],
  228. "app_hash": ""
  229. }`
  230. var testPrivValidator = `{
  231. "address": "849CB2C877F87A20925F35D00AE6688342D25B47",
  232. "pub_key": {
  233. "type": "AC26791624DE60",
  234. "value": "AT/+aaL1eB0477Mud9JMm8Sh8BIvOYlPGC9KkIUmFaE="
  235. },
  236. "priv_key": {
  237. "type": "954568A3288910",
  238. "value": "EVkqJO/jIXp3rkASXfh9YnyToYXRXhBr6g9cQVxPFnQBP/5povV4HTjvsy530kybxKHwEi85iU8YL0qQhSYVoQ=="
  239. },
  240. "last_height": 0,
  241. "last_round": 0,
  242. "last_step": 0
  243. }`