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.

533 lines
17 KiB

  1. package pex
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "time"
  7. "github.com/tendermint/tendermint/internal/p2p"
  8. "github.com/tendermint/tendermint/internal/p2p/conn"
  9. "github.com/tendermint/tendermint/libs/log"
  10. tmmath "github.com/tendermint/tendermint/libs/math"
  11. "github.com/tendermint/tendermint/libs/service"
  12. protop2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. var (
  16. _ service.Service = (*ReactorV2)(nil)
  17. _ p2p.Wrapper = (*protop2p.PexMessage)(nil)
  18. )
  19. // TODO: Consolidate with params file.
  20. // See https://github.com/tendermint/tendermint/issues/6371
  21. const (
  22. // the minimum time one peer can send another request to the same peer
  23. minReceiveRequestInterval = 100 * time.Millisecond
  24. // the maximum amount of addresses that can be included in a response
  25. maxAddresses uint16 = 100
  26. // allocated time to resolve a node address into a set of endpoints
  27. resolveTimeout = 3 * time.Second
  28. // How long to wait when there are no peers available before trying again
  29. noAvailablePeersWaitPeriod = 1 * time.Second
  30. // indicates the ping rate of the pex reactor when the peer store is full.
  31. // The reactor should still look to add new peers in order to flush out low
  32. // scoring peers that are still in the peer store
  33. fullCapacityInterval = 10 * time.Minute
  34. )
  35. // TODO: We should decide whether we want channel descriptors to be housed
  36. // within each reactor (as they are now) or, considering that the reactor doesn't
  37. // really need to care about the channel descriptors, if they should be housed
  38. // in the node module.
  39. func ChannelDescriptor() conn.ChannelDescriptor {
  40. return conn.ChannelDescriptor{
  41. ID: PexChannel,
  42. Priority: 1,
  43. SendQueueCapacity: 10,
  44. RecvMessageCapacity: maxMsgSize,
  45. MaxSendBytes: 200,
  46. }
  47. }
  48. // ReactorV2 is a PEX reactor for the new P2P stack. The legacy reactor
  49. // is Reactor.
  50. //
  51. // FIXME: Rename this when Reactor is removed, and consider moving to p2p/.
  52. //
  53. // The peer exchange or PEX reactor supports the peer manager by sending
  54. // requests to other peers for addresses that can be given to the peer manager
  55. // and at the same time advertises addresses to peers that need more.
  56. //
  57. // The reactor is able to tweak the intensity of it's search by decreasing or
  58. // increasing the interval between each request. It tracks connected peers via
  59. // a linked list, sending a request to the node at the front of the list and
  60. // adding it to the back of the list once a response is received.
  61. type ReactorV2 struct {
  62. service.BaseService
  63. peerManager *p2p.PeerManager
  64. pexCh *p2p.Channel
  65. peerUpdates *p2p.PeerUpdates
  66. closeCh chan struct{}
  67. // list of available peers to loop through and send peer requests to
  68. availablePeers map[types.NodeID]struct{}
  69. mtx sync.RWMutex
  70. // requestsSent keeps track of which peers the PEX reactor has sent requests
  71. // to. This prevents the sending of spurious responses.
  72. // NOTE: If a node never responds, they will remain in this map until a
  73. // peer down status update is sent
  74. requestsSent map[types.NodeID]struct{}
  75. // lastReceivedRequests keeps track of when peers send a request to prevent
  76. // peers from sending requests too often (as defined by
  77. // minReceiveRequestInterval).
  78. lastReceivedRequests map[types.NodeID]time.Time
  79. // the time when another request will be sent
  80. nextRequestTime time.Time
  81. // keep track of how many new peers to existing peers we have received to
  82. // extrapolate the size of the network
  83. newPeers uint32
  84. totalPeers uint32
  85. // discoveryRatio is the inverse ratio of new peers to old peers squared.
  86. // This is multiplied by the minimum duration to calculate how long to wait
  87. // between each request.
  88. discoveryRatio float32
  89. }
  90. // NewReactor returns a reference to a new reactor.
  91. func NewReactorV2(
  92. logger log.Logger,
  93. peerManager *p2p.PeerManager,
  94. pexCh *p2p.Channel,
  95. peerUpdates *p2p.PeerUpdates,
  96. ) *ReactorV2 {
  97. r := &ReactorV2{
  98. peerManager: peerManager,
  99. pexCh: pexCh,
  100. peerUpdates: peerUpdates,
  101. closeCh: make(chan struct{}),
  102. availablePeers: make(map[types.NodeID]struct{}),
  103. requestsSent: make(map[types.NodeID]struct{}),
  104. lastReceivedRequests: make(map[types.NodeID]time.Time),
  105. }
  106. r.BaseService = *service.NewBaseService(logger, "PEX", r)
  107. return r
  108. }
  109. // OnStart starts separate go routines for each p2p Channel and listens for
  110. // envelopes on each. In addition, it also listens for peer updates and handles
  111. // messages on that p2p channel accordingly. The caller must be sure to execute
  112. // OnStop to ensure the outbound p2p Channels are closed.
  113. func (r *ReactorV2) OnStart() error {
  114. go r.processPexCh()
  115. go r.processPeerUpdates()
  116. return nil
  117. }
  118. // OnStop stops the reactor by signaling to all spawned goroutines to exit and
  119. // blocking until they all exit.
  120. func (r *ReactorV2) OnStop() {
  121. // Close closeCh to signal to all spawned goroutines to gracefully exit. All
  122. // p2p Channels should execute Close().
  123. close(r.closeCh)
  124. // Wait for all p2p Channels to be closed before returning. This ensures we
  125. // can easily reason about synchronization of all p2p Channels and ensure no
  126. // panics will occur.
  127. <-r.pexCh.Done()
  128. <-r.peerUpdates.Done()
  129. }
  130. // processPexCh implements a blocking event loop where we listen for p2p
  131. // Envelope messages from the pexCh.
  132. func (r *ReactorV2) processPexCh() {
  133. defer r.pexCh.Close()
  134. for {
  135. select {
  136. case <-r.closeCh:
  137. r.Logger.Debug("stopped listening on PEX channel; closing...")
  138. return
  139. // outbound requests for new peers
  140. case <-r.waitUntilNextRequest():
  141. r.sendRequestForPeers()
  142. // inbound requests for new peers or responses to requests sent by this
  143. // reactor
  144. case envelope := <-r.pexCh.In:
  145. if err := r.handleMessage(r.pexCh.ID, envelope); err != nil {
  146. r.Logger.Error("failed to process message", "ch_id", r.pexCh.ID, "envelope", envelope, "err", err)
  147. r.pexCh.Error <- p2p.PeerError{
  148. NodeID: envelope.From,
  149. Err: err,
  150. }
  151. }
  152. }
  153. }
  154. }
  155. // processPeerUpdates initiates a blocking process where we listen for and handle
  156. // PeerUpdate messages. When the reactor is stopped, we will catch the signal and
  157. // close the p2p PeerUpdatesCh gracefully.
  158. func (r *ReactorV2) processPeerUpdates() {
  159. defer r.peerUpdates.Close()
  160. for {
  161. select {
  162. case peerUpdate := <-r.peerUpdates.Updates():
  163. r.processPeerUpdate(peerUpdate)
  164. case <-r.closeCh:
  165. r.Logger.Debug("stopped listening on peer updates channel; closing...")
  166. return
  167. }
  168. }
  169. }
  170. // handlePexMessage handles envelopes sent from peers on the PexChannel.
  171. func (r *ReactorV2) handlePexMessage(envelope p2p.Envelope) error {
  172. logger := r.Logger.With("peer", envelope.From)
  173. switch msg := envelope.Message.(type) {
  174. case *protop2p.PexRequest:
  175. // Check if the peer hasn't sent a prior request too close to this one
  176. // in time.
  177. if err := r.markPeerRequest(envelope.From); err != nil {
  178. return err
  179. }
  180. // parse and send the legacy PEX addresses
  181. pexAddresses := r.resolve(r.peerManager.Advertise(envelope.From, maxAddresses))
  182. r.pexCh.Out <- p2p.Envelope{
  183. To: envelope.From,
  184. Message: &protop2p.PexResponse{Addresses: pexAddresses},
  185. }
  186. case *protop2p.PexResponse:
  187. // check if the response matches a request that was made to that peer
  188. if err := r.markPeerResponse(envelope.From); err != nil {
  189. return err
  190. }
  191. // check the size of the response
  192. if len(msg.Addresses) > int(maxAddresses) {
  193. return fmt.Errorf("peer sent too many addresses (max: %d, got: %d)",
  194. maxAddresses,
  195. len(msg.Addresses),
  196. )
  197. }
  198. for _, pexAddress := range msg.Addresses {
  199. // no protocol is prefixed so we assume the default (mconn)
  200. peerAddress, err := p2p.ParseNodeAddress(
  201. fmt.Sprintf("%s@%s:%d", pexAddress.ID, pexAddress.IP, pexAddress.Port))
  202. if err != nil {
  203. continue
  204. }
  205. added, err := r.peerManager.Add(peerAddress)
  206. if err != nil {
  207. logger.Error("failed to add PEX address", "address", peerAddress, "err", err)
  208. }
  209. if added {
  210. r.newPeers++
  211. logger.Debug("added PEX address", "address", peerAddress)
  212. }
  213. r.totalPeers++
  214. }
  215. // V2 PEX MESSAGES
  216. case *protop2p.PexRequestV2:
  217. // check if the peer hasn't sent a prior request too close to this one
  218. // in time
  219. if err := r.markPeerRequest(envelope.From); err != nil {
  220. return err
  221. }
  222. // request peers from the peer manager and parse the NodeAddresses into
  223. // URL strings
  224. nodeAddresses := r.peerManager.Advertise(envelope.From, maxAddresses)
  225. pexAddressesV2 := make([]protop2p.PexAddressV2, len(nodeAddresses))
  226. for idx, addr := range nodeAddresses {
  227. pexAddressesV2[idx] = protop2p.PexAddressV2{
  228. URL: addr.String(),
  229. }
  230. }
  231. r.pexCh.Out <- p2p.Envelope{
  232. To: envelope.From,
  233. Message: &protop2p.PexResponseV2{Addresses: pexAddressesV2},
  234. }
  235. case *protop2p.PexResponseV2:
  236. // check if the response matches a request that was made to that peer
  237. if err := r.markPeerResponse(envelope.From); err != nil {
  238. return err
  239. }
  240. // check the size of the response
  241. if len(msg.Addresses) > int(maxAddresses) {
  242. return fmt.Errorf("peer sent too many addresses (max: %d, got: %d)",
  243. maxAddresses,
  244. len(msg.Addresses),
  245. )
  246. }
  247. for _, pexAddress := range msg.Addresses {
  248. peerAddress, err := p2p.ParseNodeAddress(pexAddress.URL)
  249. if err != nil {
  250. continue
  251. }
  252. added, err := r.peerManager.Add(peerAddress)
  253. if err != nil {
  254. logger.Error("failed to add V2 PEX address", "address", peerAddress, "err", err)
  255. }
  256. if added {
  257. r.newPeers++
  258. logger.Debug("added V2 PEX address", "address", peerAddress)
  259. }
  260. r.totalPeers++
  261. }
  262. default:
  263. return fmt.Errorf("received unknown message: %T", msg)
  264. }
  265. return nil
  266. }
  267. // resolve resolves a set of peer addresses into PEX addresses.
  268. //
  269. // FIXME: This is necessary because the current PEX protocol only supports
  270. // IP/port pairs, while the P2P stack uses NodeAddress URLs. The PEX protocol
  271. // should really use URLs too, to exchange DNS names instead of IPs and allow
  272. // different transport protocols (e.g. QUIC and MemoryTransport).
  273. //
  274. // FIXME: We may want to cache and parallelize this, but for now we'll just rely
  275. // on the operating system to cache it for us.
  276. func (r *ReactorV2) resolve(addresses []p2p.NodeAddress) []protop2p.PexAddress {
  277. limit := len(addresses)
  278. pexAddresses := make([]protop2p.PexAddress, 0, limit)
  279. for _, address := range addresses {
  280. ctx, cancel := context.WithTimeout(context.Background(), resolveTimeout)
  281. endpoints, err := address.Resolve(ctx)
  282. r.Logger.Debug("resolved node address", "endpoints", endpoints)
  283. cancel()
  284. if err != nil {
  285. r.Logger.Debug("failed to resolve address", "address", address, "err", err)
  286. continue
  287. }
  288. for _, endpoint := range endpoints {
  289. r.Logger.Debug("checking endpint", "IP", endpoint.IP, "Port", endpoint.Port)
  290. if len(pexAddresses) >= limit {
  291. return pexAddresses
  292. } else if endpoint.IP != nil {
  293. r.Logger.Debug("appending pex address")
  294. // PEX currently only supports IP-networked transports (as
  295. // opposed to e.g. p2p.MemoryTransport).
  296. //
  297. // FIXME: as the PEX address contains no information about the
  298. // protocol, we jam this into the ID. We won't need to this once
  299. // we support URLs
  300. pexAddresses = append(pexAddresses, protop2p.PexAddress{
  301. ID: string(address.NodeID),
  302. IP: endpoint.IP.String(),
  303. Port: uint32(endpoint.Port),
  304. })
  305. }
  306. }
  307. }
  308. return pexAddresses
  309. }
  310. // handleMessage handles an Envelope sent from a peer on a specific p2p Channel.
  311. // It will handle errors and any possible panics gracefully. A caller can handle
  312. // any error returned by sending a PeerError on the respective channel.
  313. func (r *ReactorV2) handleMessage(chID p2p.ChannelID, envelope p2p.Envelope) (err error) {
  314. defer func() {
  315. if e := recover(); e != nil {
  316. err = fmt.Errorf("panic in processing message: %v", e)
  317. }
  318. }()
  319. r.Logger.Debug("received PEX message", "peer", envelope.From)
  320. switch chID {
  321. case p2p.ChannelID(PexChannel):
  322. err = r.handlePexMessage(envelope)
  323. default:
  324. err = fmt.Errorf("unknown channel ID (%d) for envelope (%v)", chID, envelope)
  325. }
  326. return err
  327. }
  328. // processPeerUpdate processes a PeerUpdate. For added peers, PeerStatusUp, we
  329. // send a request for addresses.
  330. func (r *ReactorV2) processPeerUpdate(peerUpdate p2p.PeerUpdate) {
  331. r.Logger.Debug("received PEX peer update", "peer", peerUpdate.NodeID, "status", peerUpdate.Status)
  332. r.mtx.Lock()
  333. defer r.mtx.Unlock()
  334. switch peerUpdate.Status {
  335. case p2p.PeerStatusUp:
  336. r.availablePeers[peerUpdate.NodeID] = struct{}{}
  337. case p2p.PeerStatusDown:
  338. delete(r.availablePeers, peerUpdate.NodeID)
  339. delete(r.requestsSent, peerUpdate.NodeID)
  340. delete(r.lastReceivedRequests, peerUpdate.NodeID)
  341. default:
  342. }
  343. }
  344. func (r *ReactorV2) waitUntilNextRequest() <-chan time.Time {
  345. return time.After(time.Until(r.nextRequestTime))
  346. }
  347. // sendRequestForPeers pops the first peerID off the list and sends the
  348. // peer a request for more peer addresses. The function then moves the
  349. // peer into the requestsSent bucket and calculates when the next request
  350. // time should be
  351. func (r *ReactorV2) sendRequestForPeers() {
  352. r.mtx.Lock()
  353. defer r.mtx.Unlock()
  354. if len(r.availablePeers) == 0 {
  355. // no peers are available
  356. r.Logger.Debug("no available peers to send request to, waiting...")
  357. r.nextRequestTime = time.Now().Add(noAvailablePeersWaitPeriod)
  358. return
  359. }
  360. var peerID types.NodeID
  361. // use range to get a random peer.
  362. for peerID = range r.availablePeers {
  363. break
  364. }
  365. // The node accommodates for both pex systems
  366. if r.isLegacyPeer(peerID) {
  367. r.pexCh.Out <- p2p.Envelope{
  368. To: peerID,
  369. Message: &protop2p.PexRequest{},
  370. }
  371. } else {
  372. r.pexCh.Out <- p2p.Envelope{
  373. To: peerID,
  374. Message: &protop2p.PexRequestV2{},
  375. }
  376. }
  377. // remove the peer from the abvailable peers list and mark it in the requestsSent map
  378. delete(r.availablePeers, peerID)
  379. r.requestsSent[peerID] = struct{}{}
  380. r.calculateNextRequestTime()
  381. r.Logger.Debug("peer request sent", "next_request_time", r.nextRequestTime)
  382. }
  383. // calculateNextRequestTime implements something of a proportional controller
  384. // to estimate how often the reactor should be requesting new peer addresses.
  385. // The dependent variable in this calculation is the ratio of new peers to
  386. // all peers that the reactor receives. The interval is thus calculated as the
  387. // inverse squared. In the beginning, all peers should be new peers.
  388. // We expect this ratio to be near 1 and thus the interval to be as short
  389. // as possible. As the node becomes more familiar with the network the ratio of
  390. // new nodes will plummet to a very small number, meaning the interval expands
  391. // to its upper bound.
  392. // CONTRACT: Must use a write lock as nextRequestTime is updated
  393. func (r *ReactorV2) calculateNextRequestTime() {
  394. // check if the peer store is full. If so then there is no need
  395. // to send peer requests too often
  396. if ratio := r.peerManager.PeerRatio(); ratio >= 0.95 {
  397. r.Logger.Debug("peer manager near full ratio, sleeping...",
  398. "sleep_period", fullCapacityInterval, "ratio", ratio)
  399. r.nextRequestTime = time.Now().Add(fullCapacityInterval)
  400. return
  401. }
  402. // baseTime represents the shortest interval that we can send peer requests
  403. // in. For example if we have 10 peers and we can't send a message to the
  404. // same peer every 500ms, then we can send a request every 50ms. In practice
  405. // we use a safety margin of 2, ergo 100ms
  406. peers := tmmath.MinInt(len(r.availablePeers), 50)
  407. baseTime := minReceiveRequestInterval
  408. if peers > 0 {
  409. baseTime = minReceiveRequestInterval * 2 / time.Duration(peers)
  410. }
  411. if r.totalPeers > 0 || r.discoveryRatio == 0 {
  412. // find the ratio of new peers. NOTE: We add 1 to both sides to avoid
  413. // divide by zero problems
  414. ratio := float32(r.totalPeers+1) / float32(r.newPeers+1)
  415. // square the ratio in order to get non linear time intervals
  416. // NOTE: The longest possible interval for a network with 100 or more peers
  417. // where a node is connected to 50 of them is 2 minutes.
  418. r.discoveryRatio = ratio * ratio
  419. r.newPeers = 0
  420. r.totalPeers = 0
  421. }
  422. // NOTE: As ratio is always >= 1, discovery ratio is >= 1. Therefore we don't need to worry
  423. // about the next request time being less than the minimum time
  424. r.nextRequestTime = time.Now().Add(baseTime * time.Duration(r.discoveryRatio))
  425. }
  426. func (r *ReactorV2) markPeerRequest(peer types.NodeID) error {
  427. r.mtx.Lock()
  428. defer r.mtx.Unlock()
  429. if lastRequestTime, ok := r.lastReceivedRequests[peer]; ok {
  430. if time.Now().Before(lastRequestTime.Add(minReceiveRequestInterval)) {
  431. return fmt.Errorf("peer sent a request too close after a prior one. Minimum interval: %v",
  432. minReceiveRequestInterval)
  433. }
  434. }
  435. r.lastReceivedRequests[peer] = time.Now()
  436. return nil
  437. }
  438. func (r *ReactorV2) markPeerResponse(peer types.NodeID) error {
  439. r.mtx.Lock()
  440. defer r.mtx.Unlock()
  441. // check if a request to this peer was sent
  442. if _, ok := r.requestsSent[peer]; !ok {
  443. return fmt.Errorf("peer sent a PEX response when none was requested (%v)", peer)
  444. }
  445. delete(r.requestsSent, peer)
  446. // attach to the back of the list so that the peer can be used again for
  447. // future requests
  448. r.availablePeers[peer] = struct{}{}
  449. return nil
  450. }
  451. // all addresses must use a MCONN protocol for the peer to be considered part of the
  452. // legacy p2p pex system
  453. func (r *ReactorV2) isLegacyPeer(peer types.NodeID) bool {
  454. for _, addr := range r.peerManager.Addresses(peer) {
  455. if addr.Protocol != p2p.MConnProtocol {
  456. return false
  457. }
  458. }
  459. return true
  460. }