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.

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