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.

246 lines
7.3 KiB

9 years ago
9 years ago
9 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
9 years ago
9 years ago
9 years ago
9 years ago
  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/tendermint/tendermint/libs/bytes"
  6. tmstrings "github.com/tendermint/tendermint/libs/strings"
  7. tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
  8. )
  9. const (
  10. maxNodeInfoSize = 10240 // 10KB
  11. maxNumChannels = 16 // plenty of room for upgrades, for now
  12. )
  13. // Max size of the NodeInfo struct
  14. func MaxNodeInfoSize() int {
  15. return maxNodeInfoSize
  16. }
  17. // ProtocolVersion contains the protocol versions for the software.
  18. type ProtocolVersion struct {
  19. P2P uint64 `json:"p2p"`
  20. Block uint64 `json:"block"`
  21. App uint64 `json:"app"`
  22. }
  23. //-------------------------------------------------------------
  24. // NodeInfo is the basic node information exchanged
  25. // between two peers during the Tendermint P2P handshake.
  26. type NodeInfo struct {
  27. ProtocolVersion ProtocolVersion `json:"protocol_version"`
  28. // Authenticate
  29. NodeID NodeID `json:"id"` // authenticated identifier
  30. ListenAddr string `json:"listen_addr"` // accepting incoming
  31. // Check compatibility.
  32. // Channels are HexBytes so easier to read as JSON
  33. Network string `json:"network"` // network/chain ID
  34. Version string `json:"version"` // major.minor.revision
  35. // FIXME: This should be changed to uint16 to be consistent with the updated channel type
  36. Channels bytes.HexBytes `json:"channels"` // channels this node knows about
  37. // ASCIIText fields
  38. Moniker string `json:"moniker"` // arbitrary moniker
  39. Other NodeInfoOther `json:"other"` // other application specific data
  40. }
  41. // NodeInfoOther is the misc. applcation specific data
  42. type NodeInfoOther struct {
  43. TxIndex string `json:"tx_index"`
  44. RPCAddress string `json:"rpc_address"`
  45. }
  46. // ID returns the node's peer ID.
  47. func (info NodeInfo) ID() NodeID {
  48. return info.NodeID
  49. }
  50. // Validate checks the self-reported NodeInfo is safe.
  51. // It returns an error if there
  52. // are too many Channels, if there are any duplicate Channels,
  53. // if the ListenAddr is malformed, or if the ListenAddr is a host name
  54. // that can not be resolved to some IP.
  55. // TODO: constraints for Moniker/Other? Or is that for the UI ?
  56. // JAE: It needs to be done on the client, but to prevent ambiguous
  57. // unicode characters, maybe it's worth sanitizing it here.
  58. // In the future we might want to validate these, once we have a
  59. // name-resolution system up.
  60. // International clients could then use punycode (or we could use
  61. // url-encoding), and we just need to be careful with how we handle that in our
  62. // clients. (e.g. off by default).
  63. func (info NodeInfo) Validate() error {
  64. // ID is already validated.
  65. // Validate ListenAddr.
  66. _, err := NewNetAddressString(info.ID().AddressString(info.ListenAddr))
  67. if err != nil {
  68. return err
  69. }
  70. // Network is validated in CompatibleWith.
  71. // Validate Version
  72. if len(info.Version) > 0 &&
  73. (!tmstrings.IsASCIIText(info.Version) || tmstrings.ASCIITrim(info.Version) == "") {
  74. return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got %v", info.Version)
  75. }
  76. // Validate Channels - ensure max and check for duplicates.
  77. if len(info.Channels) > maxNumChannels {
  78. return fmt.Errorf("info.Channels is too long (%v). Max is %v", len(info.Channels), maxNumChannels)
  79. }
  80. channels := make(map[byte]struct{})
  81. for _, ch := range info.Channels {
  82. _, ok := channels[ch]
  83. if ok {
  84. return fmt.Errorf("info.Channels contains duplicate channel id %v", ch)
  85. }
  86. channels[ch] = struct{}{}
  87. }
  88. // Validate Moniker.
  89. if !tmstrings.IsASCIIText(info.Moniker) || tmstrings.ASCIITrim(info.Moniker) == "" {
  90. return fmt.Errorf("info.Moniker must be valid non-empty ASCII text without tabs, but got %v", info.Moniker)
  91. }
  92. // Validate Other.
  93. other := info.Other
  94. txIndex := other.TxIndex
  95. switch txIndex {
  96. case "", "on", "off":
  97. default:
  98. return fmt.Errorf("info.Other.TxIndex should be either 'on', 'off', or empty string, got '%v'", txIndex)
  99. }
  100. // XXX: Should we be more strict about address formats?
  101. rpcAddr := other.RPCAddress
  102. if len(rpcAddr) > 0 && (!tmstrings.IsASCIIText(rpcAddr) || tmstrings.ASCIITrim(rpcAddr) == "") {
  103. return fmt.Errorf("info.Other.RPCAddress=%v must be valid ASCII text without tabs", rpcAddr)
  104. }
  105. return nil
  106. }
  107. // CompatibleWith checks if two NodeInfo are compatible with each other.
  108. // CONTRACT: two nodes are compatible if the Block version and network match
  109. // and they have at least one channel in common.
  110. func (info NodeInfo) CompatibleWith(other NodeInfo) error {
  111. if info.ProtocolVersion.Block != other.ProtocolVersion.Block {
  112. return fmt.Errorf("peer is on a different Block version. Got %v, expected %v",
  113. other.ProtocolVersion.Block, info.ProtocolVersion.Block)
  114. }
  115. // nodes must be on the same network
  116. if info.Network != other.Network {
  117. return fmt.Errorf("peer is on a different network. Got %v, expected %v", other.Network, info.Network)
  118. }
  119. // if we have no channels, we're just testing
  120. if len(info.Channels) == 0 {
  121. return nil
  122. }
  123. // for each of our channels, check if they have it
  124. found := false
  125. OUTER_LOOP:
  126. for _, ch1 := range info.Channels {
  127. for _, ch2 := range other.Channels {
  128. if ch1 == ch2 {
  129. found = true
  130. break OUTER_LOOP // only need one
  131. }
  132. }
  133. }
  134. if !found {
  135. return fmt.Errorf("peer has no common channels. Our channels: %v ; Peer channels: %v", info.Channels, other.Channels)
  136. }
  137. return nil
  138. }
  139. // NetAddress returns a NetAddress derived from the NodeInfo -
  140. // it includes the authenticated peer ID and the self-reported
  141. // ListenAddr. Note that the ListenAddr is not authenticated and
  142. // may not match that address actually dialed if its an outbound peer.
  143. func (info NodeInfo) NetAddress() (*NetAddress, error) {
  144. idAddr := info.ID().AddressString(info.ListenAddr)
  145. return NewNetAddressString(idAddr)
  146. }
  147. // AddChannel is used by the router when a channel is opened to add it to the node info
  148. func (info *NodeInfo) AddChannel(channel uint16) {
  149. // check that the channel doesn't already exist
  150. for _, ch := range info.Channels {
  151. if ch == byte(channel) {
  152. return
  153. }
  154. }
  155. info.Channels = append(info.Channels, byte(channel))
  156. }
  157. func (info NodeInfo) Copy() NodeInfo {
  158. return NodeInfo{
  159. ProtocolVersion: info.ProtocolVersion,
  160. NodeID: info.NodeID,
  161. ListenAddr: info.ListenAddr,
  162. Network: info.Network,
  163. Version: info.Version,
  164. Channels: info.Channels,
  165. Moniker: info.Moniker,
  166. Other: info.Other,
  167. }
  168. }
  169. func (info NodeInfo) ToProto() *tmp2p.NodeInfo {
  170. dni := new(tmp2p.NodeInfo)
  171. dni.ProtocolVersion = tmp2p.ProtocolVersion{
  172. P2P: info.ProtocolVersion.P2P,
  173. Block: info.ProtocolVersion.Block,
  174. App: info.ProtocolVersion.App,
  175. }
  176. dni.NodeID = string(info.NodeID)
  177. dni.ListenAddr = info.ListenAddr
  178. dni.Network = info.Network
  179. dni.Version = info.Version
  180. dni.Channels = info.Channels
  181. dni.Moniker = info.Moniker
  182. dni.Other = tmp2p.NodeInfoOther{
  183. TxIndex: info.Other.TxIndex,
  184. RPCAddress: info.Other.RPCAddress,
  185. }
  186. return dni
  187. }
  188. func NodeInfoFromProto(pb *tmp2p.NodeInfo) (NodeInfo, error) {
  189. if pb == nil {
  190. return NodeInfo{}, errors.New("nil node info")
  191. }
  192. dni := NodeInfo{
  193. ProtocolVersion: ProtocolVersion{
  194. P2P: pb.ProtocolVersion.P2P,
  195. Block: pb.ProtocolVersion.Block,
  196. App: pb.ProtocolVersion.App,
  197. },
  198. NodeID: NodeID(pb.NodeID),
  199. ListenAddr: pb.ListenAddr,
  200. Network: pb.Network,
  201. Version: pb.Version,
  202. Channels: pb.Channels,
  203. Moniker: pb.Moniker,
  204. Other: NodeInfoOther{
  205. TxIndex: pb.Other.TxIndex,
  206. RPCAddress: pb.Other.RPCAddress,
  207. },
  208. }
  209. return dni, nil
  210. }