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.

249 lines
7.6 KiB

9 years ago
9 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 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
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
9 years ago
9 years ago
9 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "reflect"
  5. "github.com/tendermint/tendermint/libs/bytes"
  6. tmstrings "github.com/tendermint/tendermint/libs/strings"
  7. "github.com/tendermint/tendermint/version"
  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. //-------------------------------------------------------------
  18. // NodeInfo exposes basic info of a node
  19. // and determines if we're compatible.
  20. type NodeInfo interface {
  21. ID() ID
  22. nodeInfoAddress
  23. nodeInfoTransport
  24. }
  25. type nodeInfoAddress interface {
  26. NetAddress() (*NetAddress, error)
  27. }
  28. // nodeInfoTransport validates a nodeInfo and checks
  29. // our compatibility with it. It's for use in the handshake.
  30. type nodeInfoTransport interface {
  31. Validate() error
  32. CompatibleWith(other NodeInfo) error
  33. }
  34. //-------------------------------------------------------------
  35. // ProtocolVersion contains the protocol versions for the software.
  36. type ProtocolVersion struct {
  37. P2P uint64 `json:"p2p"`
  38. Block uint64 `json:"block"`
  39. App uint64 `json:"app"`
  40. }
  41. // defaultProtocolVersion populates the Block and P2P versions using
  42. // the global values, but not the App.
  43. var defaultProtocolVersion = NewProtocolVersion(
  44. version.P2PProtocol,
  45. version.BlockProtocol,
  46. 0,
  47. )
  48. // NewProtocolVersion returns a fully populated ProtocolVersion.
  49. func NewProtocolVersion(p2p, block, app uint64) ProtocolVersion {
  50. return ProtocolVersion{
  51. P2P: p2p,
  52. Block: block,
  53. App: app,
  54. }
  55. }
  56. //-------------------------------------------------------------
  57. // Assert DefaultNodeInfo satisfies NodeInfo
  58. var _ NodeInfo = DefaultNodeInfo{}
  59. // DefaultNodeInfo is the basic node information exchanged
  60. // between two peers during the Tendermint P2P handshake.
  61. type DefaultNodeInfo struct {
  62. ProtocolVersion ProtocolVersion `json:"protocol_version"`
  63. // Authenticate
  64. // TODO: replace with NetAddress
  65. DefaultNodeID ID `json:"id"` // authenticated identifier
  66. ListenAddr string `json:"listen_addr"` // accepting incoming
  67. // Check compatibility.
  68. // Channels are HexBytes so easier to read as JSON
  69. Network string `json:"network"` // network/chain ID
  70. Version string `json:"version"` // major.minor.revision
  71. Channels bytes.HexBytes `json:"channels"` // channels this node knows about
  72. // ASCIIText fields
  73. Moniker string `json:"moniker"` // arbitrary moniker
  74. Other DefaultNodeInfoOther `json:"other"` // other application specific data
  75. }
  76. // DefaultNodeInfoOther is the misc. applcation specific data
  77. type DefaultNodeInfoOther struct {
  78. TxIndex string `json:"tx_index"`
  79. RPCAddress string `json:"rpc_address"`
  80. }
  81. // ID returns the node's peer ID.
  82. func (info DefaultNodeInfo) ID() ID {
  83. return info.DefaultNodeID
  84. }
  85. // Validate checks the self-reported DefaultNodeInfo is safe.
  86. // It returns an error if there
  87. // are too many Channels, if there are any duplicate Channels,
  88. // if the ListenAddr is malformed, or if the ListenAddr is a host name
  89. // that can not be resolved to some IP.
  90. // TODO: constraints for Moniker/Other? Or is that for the UI ?
  91. // JAE: It needs to be done on the client, but to prevent ambiguous
  92. // unicode characters, maybe it's worth sanitizing it here.
  93. // In the future we might want to validate these, once we have a
  94. // name-resolution system up.
  95. // International clients could then use punycode (or we could use
  96. // url-encoding), and we just need to be careful with how we handle that in our
  97. // clients. (e.g. off by default).
  98. func (info DefaultNodeInfo) Validate() error {
  99. // ID is already validated.
  100. // Validate ListenAddr.
  101. _, err := NewNetAddressString(IDAddressString(info.ID(), info.ListenAddr))
  102. if err != nil {
  103. return err
  104. }
  105. // Network is validated in CompatibleWith.
  106. // Validate Version
  107. if len(info.Version) > 0 &&
  108. (!tmstrings.IsASCIIText(info.Version) || tmstrings.ASCIITrim(info.Version) == "") {
  109. return fmt.Errorf("info.Version must be valid ASCII text without tabs, but got %v", info.Version)
  110. }
  111. // Validate Channels - ensure max and check for duplicates.
  112. if len(info.Channels) > maxNumChannels {
  113. return fmt.Errorf("info.Channels is too long (%v). Max is %v", len(info.Channels), maxNumChannels)
  114. }
  115. channels := make(map[byte]struct{})
  116. for _, ch := range info.Channels {
  117. _, ok := channels[ch]
  118. if ok {
  119. return fmt.Errorf("info.Channels contains duplicate channel id %v", ch)
  120. }
  121. channels[ch] = struct{}{}
  122. }
  123. // Validate Moniker.
  124. if !tmstrings.IsASCIIText(info.Moniker) || tmstrings.ASCIITrim(info.Moniker) == "" {
  125. return fmt.Errorf("info.Moniker must be valid non-empty ASCII text without tabs, but got %v", info.Moniker)
  126. }
  127. // Validate Other.
  128. other := info.Other
  129. txIndex := other.TxIndex
  130. switch txIndex {
  131. case "", "on", "off":
  132. default:
  133. return fmt.Errorf("info.Other.TxIndex should be either 'on', 'off', or empty string, got '%v'", txIndex)
  134. }
  135. // XXX: Should we be more strict about address formats?
  136. rpcAddr := other.RPCAddress
  137. if len(rpcAddr) > 0 && (!tmstrings.IsASCIIText(rpcAddr) || tmstrings.ASCIITrim(rpcAddr) == "") {
  138. return fmt.Errorf("info.Other.RPCAddress=%v must be valid ASCII text without tabs", rpcAddr)
  139. }
  140. return nil
  141. }
  142. // CompatibleWith checks if two DefaultNodeInfo are compatible with eachother.
  143. // CONTRACT: two nodes are compatible if the Block version and network match
  144. // and they have at least one channel in common.
  145. func (info DefaultNodeInfo) CompatibleWith(otherInfo NodeInfo) error {
  146. other, ok := otherInfo.(DefaultNodeInfo)
  147. if !ok {
  148. return fmt.Errorf("wrong NodeInfo type. Expected DefaultNodeInfo, got %v", reflect.TypeOf(otherInfo))
  149. }
  150. if info.ProtocolVersion.Block != other.ProtocolVersion.Block {
  151. return fmt.Errorf("peer is on a different Block version. Got %v, expected %v",
  152. other.ProtocolVersion.Block, info.ProtocolVersion.Block)
  153. }
  154. // nodes must be on the same network
  155. if info.Network != other.Network {
  156. return fmt.Errorf("peer is on a different network. Got %v, expected %v", other.Network, info.Network)
  157. }
  158. // if we have no channels, we're just testing
  159. if len(info.Channels) == 0 {
  160. return nil
  161. }
  162. // for each of our channels, check if they have it
  163. found := false
  164. OUTER_LOOP:
  165. for _, ch1 := range info.Channels {
  166. for _, ch2 := range other.Channels {
  167. if ch1 == ch2 {
  168. found = true
  169. break OUTER_LOOP // only need one
  170. }
  171. }
  172. }
  173. if !found {
  174. return fmt.Errorf("peer has no common channels. Our channels: %v ; Peer channels: %v", info.Channels, other.Channels)
  175. }
  176. return nil
  177. }
  178. // NetAddress returns a NetAddress derived from the DefaultNodeInfo -
  179. // it includes the authenticated peer ID and the self-reported
  180. // ListenAddr. Note that the ListenAddr is not authenticated and
  181. // may not match that address actually dialed if its an outbound peer.
  182. func (info DefaultNodeInfo) NetAddress() (*NetAddress, error) {
  183. idAddr := IDAddressString(info.ID(), info.ListenAddr)
  184. return NewNetAddressString(idAddr)
  185. }
  186. //-----------------------------------------------------------
  187. // These methods are for Protobuf Compatibility
  188. // Size returns the size of the amino encoding, in bytes.
  189. func (info *DefaultNodeInfo) Size() int {
  190. bs, _ := info.Marshal()
  191. return len(bs)
  192. }
  193. // Marshal returns the amino encoding.
  194. func (info *DefaultNodeInfo) Marshal() ([]byte, error) {
  195. return cdc.MarshalBinaryBare(info)
  196. }
  197. // MarshalTo calls Marshal and copies to the given buffer.
  198. func (info *DefaultNodeInfo) MarshalTo(data []byte) (int, error) {
  199. bs, err := info.Marshal()
  200. if err != nil {
  201. return -1, err
  202. }
  203. return copy(data, bs), nil
  204. }
  205. // Unmarshal deserializes from amino encoded form.
  206. func (info *DefaultNodeInfo) Unmarshal(bs []byte) error {
  207. return cdc.UnmarshalBinaryBare(bs, info)
  208. }