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.

289 lines
7.9 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,string"`
  23. Block uint64 `json:"block,string"`
  24. App uint64 `json:"app,string"`
  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. if ver, err := tmstrings.ASCIITrim(info.Version); err != nil || ver == "" {
  73. return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got, %q [%s]", info.Version, ver)
  74. }
  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. if m, err := tmstrings.ASCIITrim(info.Moniker); err != nil || m == "" {
  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 {
  102. if a, err := tmstrings.ASCIITrim(rpcAddr); err != nil || a == "" {
  103. return fmt.Errorf("info.Other.RPCAddress=%v must be valid ASCII text without tabs", rpcAddr)
  104. }
  105. }
  106. return nil
  107. }
  108. // CompatibleWith checks if two NodeInfo are compatible with each other.
  109. // CONTRACT: two nodes are compatible if the Block version and network match
  110. // and they have at least one channel in common.
  111. func (info NodeInfo) CompatibleWith(other NodeInfo) error {
  112. if info.ProtocolVersion.Block != other.ProtocolVersion.Block {
  113. return fmt.Errorf("peer is on a different Block version. Got %v, expected %v",
  114. other.ProtocolVersion.Block, info.ProtocolVersion.Block)
  115. }
  116. // nodes must be on the same network
  117. if info.Network != other.Network {
  118. return fmt.Errorf("peer is on a different network. Got %v, expected %v", other.Network, info.Network)
  119. }
  120. // if we have no channels, we're just testing
  121. if len(info.Channels) == 0 {
  122. return nil
  123. }
  124. // for each of our channels, check if they have it
  125. found := false
  126. OUTER_LOOP:
  127. for _, ch1 := range info.Channels {
  128. for _, ch2 := range other.Channels {
  129. if ch1 == ch2 {
  130. found = true
  131. break OUTER_LOOP // only need one
  132. }
  133. }
  134. }
  135. if !found {
  136. return fmt.Errorf("peer has no common channels. Our channels: %v ; Peer channels: %v", info.Channels, other.Channels)
  137. }
  138. return nil
  139. }
  140. // AddChannel is used by the router when a channel is opened to add it to the node info
  141. func (info *NodeInfo) AddChannel(channel uint16) {
  142. // check that the channel doesn't already exist
  143. for _, ch := range info.Channels {
  144. if ch == byte(channel) {
  145. return
  146. }
  147. }
  148. info.Channels = append(info.Channels, byte(channel))
  149. }
  150. func (info NodeInfo) Copy() NodeInfo {
  151. return NodeInfo{
  152. ProtocolVersion: info.ProtocolVersion,
  153. NodeID: info.NodeID,
  154. ListenAddr: info.ListenAddr,
  155. Network: info.Network,
  156. Version: info.Version,
  157. Channels: info.Channels,
  158. Moniker: info.Moniker,
  159. Other: info.Other,
  160. }
  161. }
  162. func (info NodeInfo) ToProto() *tmp2p.NodeInfo {
  163. dni := new(tmp2p.NodeInfo)
  164. dni.ProtocolVersion = tmp2p.ProtocolVersion{
  165. P2P: info.ProtocolVersion.P2P,
  166. Block: info.ProtocolVersion.Block,
  167. App: info.ProtocolVersion.App,
  168. }
  169. dni.NodeID = string(info.NodeID)
  170. dni.ListenAddr = info.ListenAddr
  171. dni.Network = info.Network
  172. dni.Version = info.Version
  173. dni.Channels = info.Channels
  174. dni.Moniker = info.Moniker
  175. dni.Other = tmp2p.NodeInfoOther{
  176. TxIndex: info.Other.TxIndex,
  177. RPCAddress: info.Other.RPCAddress,
  178. }
  179. return dni
  180. }
  181. func NodeInfoFromProto(pb *tmp2p.NodeInfo) (NodeInfo, error) {
  182. if pb == nil {
  183. return NodeInfo{}, errors.New("nil node info")
  184. }
  185. dni := NodeInfo{
  186. ProtocolVersion: ProtocolVersion{
  187. P2P: pb.ProtocolVersion.P2P,
  188. Block: pb.ProtocolVersion.Block,
  189. App: pb.ProtocolVersion.App,
  190. },
  191. NodeID: NodeID(pb.NodeID),
  192. ListenAddr: pb.ListenAddr,
  193. Network: pb.Network,
  194. Version: pb.Version,
  195. Channels: pb.Channels,
  196. Moniker: pb.Moniker,
  197. Other: NodeInfoOther{
  198. TxIndex: pb.Other.TxIndex,
  199. RPCAddress: pb.Other.RPCAddress,
  200. },
  201. }
  202. return dni, nil
  203. }
  204. // ParseAddressString reads an address string, and returns the IP
  205. // address and port information, returning an error for any validation
  206. // errors.
  207. func ParseAddressString(addr string) (net.IP, uint16, error) {
  208. addrWithoutProtocol := removeProtocolIfDefined(addr)
  209. spl := strings.Split(addrWithoutProtocol, "@")
  210. if len(spl) != 2 {
  211. return nil, 0, errors.New("invalid address")
  212. }
  213. id, err := NewNodeID(spl[0])
  214. if err != nil {
  215. return nil, 0, err
  216. }
  217. if err := id.Validate(); err != nil {
  218. return nil, 0, err
  219. }
  220. addrWithoutProtocol = spl[1]
  221. // get host and port
  222. host, portStr, err := net.SplitHostPort(addrWithoutProtocol)
  223. if err != nil {
  224. return nil, 0, err
  225. }
  226. if len(host) == 0 {
  227. return nil, 0, err
  228. }
  229. ip := net.ParseIP(host)
  230. if ip == nil {
  231. ips, err := net.LookupIP(host)
  232. if err != nil {
  233. return nil, 0, err
  234. }
  235. ip = ips[0]
  236. }
  237. port, err := strconv.ParseUint(portStr, 10, 16)
  238. if err != nil {
  239. return nil, 0, err
  240. }
  241. return ip, uint16(port), nil
  242. }
  243. func removeProtocolIfDefined(addr string) string {
  244. if strings.Contains(addr, "://") {
  245. return strings.Split(addr, "://")[1]
  246. }
  247. return addr
  248. }