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.

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