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.

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