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.

254 lines
7.7 KiB

9 years ago
9 years ago
Replaced NodeInfo's pubkey to ID (#1443) * Replaced NodeInfo PubKey to NodeID * Fixed tests and replaced NodeID with ID * Removed unnecessary method ID() * Fixed codec_test.go * Fixed codec_test.go * Removed unnecessary bracket * Fixed all tests * Fixed peer_set_test.go * Fixed peer_test.go * Fixed common_test.go * Fixed common_test.go * Renamed node_id to id * Removed peer.ID() from RPC net.go * Replaced NodeInfo pubKey to ID * Fixed codec_test.go * Fixed peer_set_test.go * Fix pex_reactor_test.go * Refactored code for privateKey initiali * Fixed peer_set_test.go * Fixed test.proto and removed orphan string in codec_test.go * Fixed pointer to a string * generate node_key when running tendermint init * [docs] prefix IPs with node IDs Refs #1429 * gen_node_key cmd * [docs/specification/secure-p2p] add a note about config * fix data race Closes #1442 ``` WARNING: DATA RACE Write at 0x00c4209de7c8 by goroutine 23: github.com/tendermint/tendermint/types.(*Block).fillHeader() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:88 +0x157 github.com/tendermint/tendermint/types.(*Block).Hash() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:104 +0x121 github.com/tendermint/tendermint/types.(*Block).HashesTo() /home/vagrant/go/src/github.com/tendermint/tendermint/types/block.go:135 +0x4f github.com/tendermint/tendermint/consensus.(*ConsensusState).enterPrecommit() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1037 +0x182d github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1425 +0x1a6c github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:1318 +0x77 github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:581 +0x7a9 github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:539 +0x6c3 Previous read at 0x00c4209de7c8 by goroutine 47: github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*HexBytes).MarshalJSON() <autogenerated>:1 +0x52 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.invokeMarshalJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:433 +0x88 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:82 +0x8d2 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSONStruct() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:348 +0x539 github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec)._encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:119 +0x83f github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).encodeReflectJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/json-encode.go:50 +0x10e github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino.(*Codec).MarshalJSON() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/go-amino/amino.go:296 +0x182 github.com/tendermint/tendermint/rpc/lib/types.NewRPCSuccessResponse() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/types/types.go:100 +0x12c github.com/tendermint/tendermint/rpc/lib/server.makeJSONRPCHandler.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:152 +0xab7 net/http.HandlerFunc.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:1918 +0x51 net/http.(*ServeMux).ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:2254 +0xa2 github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:138 +0x4fa net/http.HandlerFunc.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:1918 +0x51 net/http.serverHandler.ServeHTTP() /usr/lib/go-1.9/src/net/http/server.go:2619 +0xbc net/http.(*conn).serve() /usr/lib/go-1.9/src/net/http/server.go:1801 +0x83b Goroutine 23 (running) created at: github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/state.go:250 +0x35b github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/consensus.(*ConsensusReactor).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/consensus/reactor.go:69 +0x1b4 github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/consensus.(*ConsensusReactor).Start() <autogenerated>:1 +0x43 github.com/tendermint/tendermint/p2p.(*Switch).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch.go:177 +0x124 github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/node.(*Node).OnStart() /home/vagrant/go/src/github.com/tendermint/tendermint/node/node.go:416 +0xa1b github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common.(*BaseService).Start() /home/vagrant/go/src/github.com/tendermint/tendermint/vendor/github.com/tendermint/tmlibs/common/service.go:130 +0x5fc github.com/tendermint/tendermint/rpc/test.StartTendermint() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/test/helpers.go:100 +0x5b github.com/tendermint/tendermint/rpc/client_test.TestMain() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/client/main_test.go:17 +0x4c main.main() github.com/tendermint/tendermint/rpc/client/_test/_testmain.go:76 +0x1cd Goroutine 47 (running) created at: net/http.(*Server).Serve() /usr/lib/go-1.9/src/net/http/server.go:2720 +0x37c net/http.Serve() /usr/lib/go-1.9/src/net/http/server.go:2323 +0xe2 github.com/tendermint/tendermint/rpc/lib/server.StartHTTPServer.func1() /home/vagrant/go/src/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:35 +0xb3 ``` * removed excessive comment Refs https://github.com/tendermint/tendermint/pull/1446#discussion_r180353446 * use the tag interface for pubsub. (#1438) * use the tag interface for pubsub. * update tmlibs. * Fix unresolved conflict. * improve `show_node_id` (#1433) * fix show_node_id * make LoadNodeKey public * make LoadNodeKey public * remove if * remove if
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
9 years ago
9 years ago
9 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "reflect"
  5. cmn "github.com/tendermint/tendermint/libs/common"
  6. "github.com/tendermint/tendermint/version"
  7. )
  8. const (
  9. maxNodeInfoSize = 10240 // 10KB
  10. maxNumChannels = 16 // plenty of room for upgrades, for now
  11. )
  12. // Max size of the NodeInfo struct
  13. func MaxNodeInfoSize() int {
  14. return maxNodeInfoSize
  15. }
  16. //-------------------------------------------------------------
  17. // NodeInfo exposes basic info of a node
  18. // and determines if we're compatible.
  19. type NodeInfo interface {
  20. ID() ID
  21. nodeInfoTransport
  22. }
  23. // nodeInfoTransport validates a nodeInfo and checks
  24. // our compatibility with it. It's for use in the handshake.
  25. type nodeInfoTransport interface {
  26. Validate() error
  27. CompatibleWith(other NodeInfo) error
  28. }
  29. //-------------------------------------------------------------
  30. // ProtocolVersion contains the protocol versions for the software.
  31. type ProtocolVersion struct {
  32. P2P version.Protocol `json:"p2p"`
  33. Block version.Protocol `json:"block"`
  34. App version.Protocol `json:"app"`
  35. }
  36. // defaultProtocolVersion populates the Block and P2P versions using
  37. // the global values, but not the App.
  38. var defaultProtocolVersion = NewProtocolVersion(
  39. version.P2PProtocol,
  40. version.BlockProtocol,
  41. 0,
  42. )
  43. // NewProtocolVersion returns a fully populated ProtocolVersion.
  44. func NewProtocolVersion(p2p, block, app version.Protocol) ProtocolVersion {
  45. return ProtocolVersion{
  46. P2P: p2p,
  47. Block: block,
  48. App: app,
  49. }
  50. }
  51. //-------------------------------------------------------------
  52. // Assert DefaultNodeInfo satisfies NodeInfo
  53. var _ NodeInfo = DefaultNodeInfo{}
  54. // DefaultNodeInfo is the basic node information exchanged
  55. // between two peers during the Tendermint P2P handshake.
  56. type DefaultNodeInfo struct {
  57. ProtocolVersion ProtocolVersion `json:"protocol_version"`
  58. // Authenticate
  59. // TODO: replace with NetAddress
  60. ID_ ID `json:"id"` // authenticated identifier
  61. ListenAddr string `json:"listen_addr"` // accepting incoming
  62. // Check compatibility.
  63. // Channels are HexBytes so easier to read as JSON
  64. Network string `json:"network"` // network/chain ID
  65. Version string `json:"version"` // major.minor.revision
  66. Channels cmn.HexBytes `json:"channels"` // channels this node knows about
  67. // ASCIIText fields
  68. Moniker string `json:"moniker"` // arbitrary moniker
  69. Other DefaultNodeInfoOther `json:"other"` // other application specific data
  70. }
  71. // DefaultNodeInfoOther is the misc. applcation specific data
  72. type DefaultNodeInfoOther struct {
  73. TxIndex string `json:"tx_index"`
  74. RPCAddress string `json:"rpc_address"`
  75. }
  76. // ID returns the node's peer ID.
  77. func (info DefaultNodeInfo) ID() ID {
  78. return info.ID_
  79. }
  80. // Validate checks the self-reported DefaultNodeInfo is safe.
  81. // It returns an error if there
  82. // are too many Channels, if there are any duplicate Channels,
  83. // if the ListenAddr is malformed, or if the ListenAddr is a host name
  84. // that can not be resolved to some IP.
  85. // TODO: constraints for Moniker/Other? Or is that for the UI ?
  86. // JAE: It needs to be done on the client, but to prevent ambiguous
  87. // unicode characters, maybe it's worth sanitizing it here.
  88. // In the future we might want to validate these, once we have a
  89. // name-resolution system up.
  90. // International clients could then use punycode (or we could use
  91. // url-encoding), and we just need to be careful with how we handle that in our
  92. // clients. (e.g. off by default).
  93. func (info DefaultNodeInfo) Validate() error {
  94. // ID is already validated.
  95. // Validate ListenAddr.
  96. _, err := NewNetAddressString(IDAddressString(info.ID(), info.ListenAddr))
  97. if err != nil {
  98. return err
  99. }
  100. // Network is validated in CompatibleWith.
  101. // Validate Version
  102. if len(info.Version) > 0 &&
  103. (!cmn.IsASCIIText(info.Version) || cmn.ASCIITrim(info.Version) == "") {
  104. return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got %v", info.Version)
  105. }
  106. // Validate Channels - ensure max and check for duplicates.
  107. if len(info.Channels) > maxNumChannels {
  108. return fmt.Errorf("info.Channels is too long (%v). Max is %v", len(info.Channels), maxNumChannels)
  109. }
  110. channels := make(map[byte]struct{})
  111. for _, ch := range info.Channels {
  112. _, ok := channels[ch]
  113. if ok {
  114. return fmt.Errorf("info.Channels contains duplicate channel id %v", ch)
  115. }
  116. channels[ch] = struct{}{}
  117. }
  118. // Validate Moniker.
  119. if !cmn.IsASCIIText(info.Moniker) || cmn.ASCIITrim(info.Moniker) == "" {
  120. return fmt.Errorf("info.Moniker must be valid non-empty ASCII text without tabs, but got %v", info.Moniker)
  121. }
  122. // Validate Other.
  123. other := info.Other
  124. txIndex := other.TxIndex
  125. switch txIndex {
  126. case "", "on", "off":
  127. default:
  128. return fmt.Errorf("info.Other.TxIndex should be either 'on', 'off', or empty string, got '%v'", txIndex)
  129. }
  130. // XXX: Should we be more strict about address formats?
  131. rpcAddr := other.RPCAddress
  132. if len(rpcAddr) > 0 && (!cmn.IsASCIIText(rpcAddr) || cmn.ASCIITrim(rpcAddr) == "") {
  133. return fmt.Errorf("info.Other.RPCAddress=%v must be valid ASCII text without tabs", rpcAddr)
  134. }
  135. return nil
  136. }
  137. // CompatibleWith checks if two DefaultNodeInfo are compatible with eachother.
  138. // CONTRACT: two nodes are compatible if the Block version and network match
  139. // and they have at least one channel in common.
  140. func (info DefaultNodeInfo) CompatibleWith(other_ NodeInfo) error {
  141. other, ok := other_.(DefaultNodeInfo)
  142. if !ok {
  143. return fmt.Errorf("wrong NodeInfo type. Expected DefaultNodeInfo, got %v", reflect.TypeOf(other_))
  144. }
  145. if info.ProtocolVersion.Block != other.ProtocolVersion.Block {
  146. return fmt.Errorf("Peer is on a different Block version. Got %v, expected %v",
  147. other.ProtocolVersion.Block, info.ProtocolVersion.Block)
  148. }
  149. // nodes must be on the same network
  150. if info.Network != other.Network {
  151. return fmt.Errorf("Peer is on a different network. Got %v, expected %v", other.Network, info.Network)
  152. }
  153. // if we have no channels, we're just testing
  154. if len(info.Channels) == 0 {
  155. return nil
  156. }
  157. // for each of our channels, check if they have it
  158. found := false
  159. OUTER_LOOP:
  160. for _, ch1 := range info.Channels {
  161. for _, ch2 := range other.Channels {
  162. if ch1 == ch2 {
  163. found = true
  164. break OUTER_LOOP // only need one
  165. }
  166. }
  167. }
  168. if !found {
  169. return fmt.Errorf("Peer has no common channels. Our channels: %v ; Peer channels: %v", info.Channels, other.Channels)
  170. }
  171. return nil
  172. }
  173. // NetAddress returns a NetAddress derived from the DefaultNodeInfo -
  174. // it includes the authenticated peer ID and the self-reported
  175. // ListenAddr. Note that the ListenAddr is not authenticated and
  176. // may not match that address actually dialed if its an outbound peer.
  177. func (info DefaultNodeInfo) NetAddress() *NetAddress {
  178. idAddr := IDAddressString(info.ID(), info.ListenAddr)
  179. netAddr, err := NewNetAddressString(idAddr)
  180. if err != nil {
  181. switch err.(type) {
  182. case ErrNetAddressLookup:
  183. // XXX If the peer provided a host name and the lookup fails here
  184. // we're out of luck.
  185. // TODO: use a NetAddress in DefaultNodeInfo
  186. default:
  187. panic(err) // everything should be well formed by now
  188. }
  189. }
  190. return netAddr
  191. }
  192. //-----------------------------------------------------------
  193. // These methods are for Protobuf Compatibility
  194. // Size returns the size of the amino encoding, in bytes.
  195. func (info *DefaultNodeInfo) Size() int {
  196. bs, _ := info.Marshal()
  197. return len(bs)
  198. }
  199. // Marshal returns the amino encoding.
  200. func (info *DefaultNodeInfo) Marshal() ([]byte, error) {
  201. return cdc.MarshalBinaryBare(info)
  202. }
  203. // MarshalTo calls Marshal and copies to the given buffer.
  204. func (info *DefaultNodeInfo) MarshalTo(data []byte) (int, error) {
  205. bs, err := info.Marshal()
  206. if err != nil {
  207. return -1, err
  208. }
  209. return copy(data, bs), nil
  210. }
  211. // Unmarshal deserializes from amino encoded form.
  212. func (info *DefaultNodeInfo) Unmarshal(bs []byte) error {
  213. return cdc.UnmarshalBinaryBare(bs, info)
  214. }