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.

537 lines
17 KiB

  1. package pex
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "time"
  7. "github.com/tendermint/tendermint/libs/clist"
  8. "github.com/tendermint/tendermint/libs/log"
  9. tmmath "github.com/tendermint/tendermint/libs/math"
  10. "github.com/tendermint/tendermint/libs/service"
  11. "github.com/tendermint/tendermint/p2p"
  12. "github.com/tendermint/tendermint/p2p/conn"
  13. protop2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
  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 = 300 * 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 *clist.CList
  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[p2p.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[p2p.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: clist.New(),
  103. requestsSent: make(map[p2p.NodeID]struct{}),
  104. lastReceivedRequests: make(map[p2p.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. switch peerUpdate.Status {
  333. case p2p.PeerStatusUp:
  334. r.availablePeers.PushBack(peerUpdate.NodeID)
  335. case p2p.PeerStatusDown:
  336. r.removePeer(peerUpdate.NodeID)
  337. default:
  338. }
  339. }
  340. func (r *ReactorV2) waitUntilNextRequest() <-chan time.Time {
  341. return time.After(time.Until(r.nextRequestTime))
  342. }
  343. // sendRequestForPeers pops the first peerID off the list and sends the
  344. // peer a request for more peer addresses. The function then moves the
  345. // peer into the requestsSent bucket and calculates when the next request
  346. // time should be
  347. func (r *ReactorV2) sendRequestForPeers() {
  348. r.mtx.Lock()
  349. defer r.mtx.Unlock()
  350. peer := r.availablePeers.Front()
  351. if peer == nil {
  352. // no peers are available
  353. r.Logger.Debug("no available peers to send request to, waiting...")
  354. r.nextRequestTime = time.Now().Add(noAvailablePeersWaitPeriod)
  355. return
  356. }
  357. peerID := peer.Value.(p2p.NodeID)
  358. // The node accommodates for both pex systems
  359. if r.isLegacyPeer(peerID) {
  360. r.pexCh.Out <- p2p.Envelope{
  361. To: peerID,
  362. Message: &protop2p.PexRequest{},
  363. }
  364. } else {
  365. r.pexCh.Out <- p2p.Envelope{
  366. To: peerID,
  367. Message: &protop2p.PexRequestV2{},
  368. }
  369. }
  370. // remove the peer from the available peers list and mark it in the requestsSent map
  371. r.availablePeers.Remove(peer)
  372. peer.DetachPrev()
  373. r.requestsSent[peerID] = struct{}{}
  374. r.calculateNextRequestTime()
  375. r.Logger.Debug("peer request sent", "next_request_time", r.nextRequestTime)
  376. }
  377. // calculateNextRequestTime implements something of a proportional controller
  378. // to estimate how often the reactor should be requesting new peer addresses.
  379. // The dependent variable in this calculation is the ratio of new peers to
  380. // all peers that the reactor receives. The interval is thus calculated as the
  381. // inverse squared. In the beginning, all peers should be new peers.
  382. // We expect this ratio to be near 1 and thus the interval to be as short
  383. // as possible. As the node becomes more familiar with the network the ratio of
  384. // new nodes will plummet to a very small number, meaning the interval expands
  385. // to its upper bound.
  386. // CONTRACT: Must use a write lock as nextRequestTime is updated
  387. func (r *ReactorV2) calculateNextRequestTime() {
  388. // check if the peer store is full. If so then there is no need
  389. // to send peer requests too often
  390. if ratio := r.peerManager.PeerRatio(); ratio >= 0.95 {
  391. r.Logger.Debug("peer manager near full ratio, sleeping...",
  392. "sleep_period", fullCapacityInterval, "ratio", ratio)
  393. r.nextRequestTime = time.Now().Add(fullCapacityInterval)
  394. return
  395. }
  396. // baseTime represents the shortest interval that we can send peer requests
  397. // in. For example if we have 10 peers and we can't send a message to the
  398. // same peer every 500ms, then we can send a request every 50ms. In practice
  399. // we use a safety margin of 2, ergo 100ms
  400. peers := tmmath.MinInt(r.availablePeers.Len(), 50)
  401. baseTime := minReceiveRequestInterval
  402. if peers > 0 {
  403. baseTime = minReceiveRequestInterval * 2 / time.Duration(peers)
  404. }
  405. if r.totalPeers > 0 || r.discoveryRatio == 0 {
  406. // find the ratio of new peers. NOTE: We add 1 to both sides to avoid
  407. // divide by zero problems
  408. ratio := float32(r.totalPeers+1) / float32(r.newPeers+1)
  409. // square the ratio in order to get non linear time intervals
  410. // NOTE: The longest possible interval for a network with 100 or more peers
  411. // where a node is connected to 50 of them is 2 minutes.
  412. r.discoveryRatio = ratio * ratio
  413. r.newPeers = 0
  414. r.totalPeers = 0
  415. }
  416. // NOTE: As ratio is always >= 1, discovery ratio is >= 1. Therefore we don't need to worry
  417. // about the next request time being less than the minimum time
  418. r.nextRequestTime = time.Now().Add(baseTime * time.Duration(r.discoveryRatio))
  419. }
  420. func (r *ReactorV2) removePeer(id p2p.NodeID) {
  421. for e := r.availablePeers.Front(); e != nil; e = e.Next() {
  422. if e.Value == id {
  423. r.availablePeers.Remove(e)
  424. e.DetachPrev()
  425. break
  426. }
  427. }
  428. r.mtx.Lock()
  429. defer r.mtx.Unlock()
  430. delete(r.requestsSent, id)
  431. delete(r.lastReceivedRequests, id)
  432. }
  433. func (r *ReactorV2) markPeerRequest(peer p2p.NodeID) error {
  434. r.mtx.Lock()
  435. defer r.mtx.Unlock()
  436. if lastRequestTime, ok := r.lastReceivedRequests[peer]; ok {
  437. if time.Now().Before(lastRequestTime.Add(minReceiveRequestInterval)) {
  438. return fmt.Errorf("peer sent a request too close after a prior one. Minimum interval: %v",
  439. minReceiveRequestInterval)
  440. }
  441. }
  442. r.lastReceivedRequests[peer] = time.Now()
  443. return nil
  444. }
  445. func (r *ReactorV2) markPeerResponse(peer p2p.NodeID) error {
  446. r.mtx.Lock()
  447. defer r.mtx.Unlock()
  448. // check if a request to this peer was sent
  449. if _, ok := r.requestsSent[peer]; !ok {
  450. return fmt.Errorf("peer sent a PEX response when none was requested (%v)", peer)
  451. }
  452. delete(r.requestsSent, peer)
  453. // attach to the back of the list so that the peer can be used again for
  454. // future requests
  455. r.availablePeers.PushBack(peer)
  456. return nil
  457. }
  458. // all addresses must use a MCONN protocol for the peer to be considered part of the
  459. // legacy p2p pex system
  460. func (r *ReactorV2) isLegacyPeer(peer p2p.NodeID) bool {
  461. for _, addr := range r.peerManager.Addresses(peer) {
  462. if addr.Protocol != p2p.MConnProtocol {
  463. return false
  464. }
  465. }
  466. return true
  467. }