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.

276 lines
8.2 KiB

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