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.

238 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. NodeID NodeID `json:"id"` // authenticated identifier
  46. ListenAddr string `json:"listen_addr"` // accepting incoming
  47. // Check compatibility.
  48. // Channels are HexBytes so easier to read as JSON
  49. Network string `json:"network"` // network/chain ID
  50. Version string `json:"version"` // major.minor.revision
  51. Channels bytes.HexBytes `json:"channels"` // channels this node knows about
  52. // ASCIIText fields
  53. Moniker string `json:"moniker"` // arbitrary moniker
  54. Other NodeInfoOther `json:"other"` // other application specific data
  55. }
  56. // NodeInfoOther is the misc. applcation specific data
  57. type NodeInfoOther struct {
  58. TxIndex string `json:"tx_index"`
  59. RPCAddress string `json:"rpc_address"`
  60. }
  61. // ID returns the node's peer ID.
  62. func (info NodeInfo) ID() NodeID {
  63. return info.NodeID
  64. }
  65. // Validate checks the self-reported NodeInfo is safe.
  66. // It returns an error if there
  67. // are too many Channels, if there are any duplicate Channels,
  68. // if the ListenAddr is malformed, or if the ListenAddr is a host name
  69. // that can not be resolved to some IP.
  70. // TODO: constraints for Moniker/Other? Or is that for the UI ?
  71. // JAE: It needs to be done on the client, but to prevent ambiguous
  72. // unicode characters, maybe it's worth sanitizing it here.
  73. // In the future we might want to validate these, once we have a
  74. // name-resolution system up.
  75. // International clients could then use punycode (or we could use
  76. // url-encoding), and we just need to be careful with how we handle that in our
  77. // clients. (e.g. off by default).
  78. func (info NodeInfo) Validate() error {
  79. // ID is already validated.
  80. // Validate ListenAddr.
  81. _, err := NewNetAddressString(IDAddressString(info.ID(), info.ListenAddr))
  82. if err != nil {
  83. return err
  84. }
  85. // Network is validated in CompatibleWith.
  86. // Validate Version
  87. if len(info.Version) > 0 &&
  88. (!tmstrings.IsASCIIText(info.Version) || tmstrings.ASCIITrim(info.Version) == "") {
  89. return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got %v", info.Version)
  90. }
  91. // Validate Channels - ensure max and check for duplicates.
  92. if len(info.Channels) > maxNumChannels {
  93. return fmt.Errorf("info.Channels is too long (%v). Max is %v", len(info.Channels), maxNumChannels)
  94. }
  95. channels := make(map[byte]struct{})
  96. for _, ch := range info.Channels {
  97. _, ok := channels[ch]
  98. if ok {
  99. return fmt.Errorf("info.Channels contains duplicate channel id %v", ch)
  100. }
  101. channels[ch] = struct{}{}
  102. }
  103. // Validate Moniker.
  104. if !tmstrings.IsASCIIText(info.Moniker) || tmstrings.ASCIITrim(info.Moniker) == "" {
  105. return fmt.Errorf("info.Moniker must be valid non-empty ASCII text without tabs, but got %v", info.Moniker)
  106. }
  107. // Validate Other.
  108. other := info.Other
  109. txIndex := other.TxIndex
  110. switch txIndex {
  111. case "", "on", "off":
  112. default:
  113. return fmt.Errorf("info.Other.TxIndex should be either 'on', 'off', or empty string, got '%v'", txIndex)
  114. }
  115. // XXX: Should we be more strict about address formats?
  116. rpcAddr := other.RPCAddress
  117. if len(rpcAddr) > 0 && (!tmstrings.IsASCIIText(rpcAddr) || tmstrings.ASCIITrim(rpcAddr) == "") {
  118. return fmt.Errorf("info.Other.RPCAddress=%v must be valid ASCII text without tabs", rpcAddr)
  119. }
  120. return nil
  121. }
  122. // CompatibleWith checks if two NodeInfo are compatible with each other.
  123. // CONTRACT: two nodes are compatible if the Block version and network match
  124. // and they have at least one channel in common.
  125. func (info NodeInfo) CompatibleWith(other NodeInfo) error {
  126. if info.ProtocolVersion.Block != other.ProtocolVersion.Block {
  127. return fmt.Errorf("peer is on a different Block version. Got %v, expected %v",
  128. other.ProtocolVersion.Block, info.ProtocolVersion.Block)
  129. }
  130. // nodes must be on the same network
  131. if info.Network != other.Network {
  132. return fmt.Errorf("peer is on a different network. Got %v, expected %v", other.Network, info.Network)
  133. }
  134. // if we have no channels, we're just testing
  135. if len(info.Channels) == 0 {
  136. return nil
  137. }
  138. // for each of our channels, check if they have it
  139. found := false
  140. OUTER_LOOP:
  141. for _, ch1 := range info.Channels {
  142. for _, ch2 := range other.Channels {
  143. if ch1 == ch2 {
  144. found = true
  145. break OUTER_LOOP // only need one
  146. }
  147. }
  148. }
  149. if !found {
  150. return fmt.Errorf("peer has no common channels. Our channels: %v ; Peer channels: %v", info.Channels, other.Channels)
  151. }
  152. return nil
  153. }
  154. // NetAddress returns a NetAddress derived from the NodeInfo -
  155. // it includes the authenticated peer ID and the self-reported
  156. // ListenAddr. Note that the ListenAddr is not authenticated and
  157. // may not match that address actually dialed if its an outbound peer.
  158. func (info NodeInfo) NetAddress() (*NetAddress, error) {
  159. idAddr := IDAddressString(info.ID(), info.ListenAddr)
  160. return NewNetAddressString(idAddr)
  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. }