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.

132 lines
5.5 KiB

  1. # Peer Strategy and Exchange
  2. Here we outline the design of the AddressBook
  3. and how it used by the Peer Exchange Reactor (PEX) to ensure we are connected
  4. to good peers and to gossip peers to others.
  5. ## Peer Types
  6. Certain peers are special in that they are specified by the user as `persistent`,
  7. which means we auto-redial them if the connection fails, or if we fail to dial
  8. them.
  9. Some peers can be marked as `private`, which means
  10. we will not put them in the address book or gossip them to others.
  11. All peers except private peers and peers coming from them are tracked using the
  12. address book.
  13. The rest of our peers are only distinguished by being either
  14. inbound (they dialed our public address) or outbound (we dialed them).
  15. ## Discovery
  16. Peer discovery begins with a list of seeds.
  17. When we don't have enough peers, we
  18. 1. ask existing peers
  19. 2. dial seeds if we're not dialing anyone currently
  20. On startup, we will also immediately dial the given list of `persistent_peers`,
  21. and will attempt to maintain persistent connections with them. If the
  22. connections die, or we fail to dial, we will redial every 5s for a few minutes,
  23. then switch to an exponential backoff schedule, and after about a day of
  24. trying, stop dialing the peer.
  25. As long as we have less than `MaxNumOutboundPeers`, we periodically request
  26. additional peers from each of our own and try seeds.
  27. ## Listening
  28. Peers listen on a configurable ListenAddr that they self-report in their
  29. NodeInfo during handshakes with other peers. Peers accept up to
  30. `MaxNumInboundPeers` incoming peers.
  31. ## Address Book
  32. Peers are tracked via their ID (their PubKey.Address()).
  33. Peers are added to the address book from the PEX when they first connect to us or
  34. when we hear about them from other peers.
  35. The address book is arranged in sets of buckets, and distinguishes between
  36. vetted (old) and unvetted (new) peers. It keeps different sets of buckets for vetted and
  37. unvetted peers. Buckets provide randomization over peer selection. Peers are put
  38. in buckets according to their IP groups.
  39. A vetted peer can only be in one bucket. An unvetted peer can be in multiple buckets, and
  40. each instance of the peer can have a different IP:PORT.
  41. If we're trying to add a new peer but there's no space in its bucket, we'll
  42. remove the worst peer from that bucket to make room.
  43. ## Vetting
  44. When a peer is first added, it is unvetted.
  45. Marking a peer as vetted is outside the scope of the `p2p` package.
  46. For Tendermint, a Peer becomes vetted once it has contributed sufficiently
  47. at the consensus layer; ie. once it has sent us valid and not-yet-known
  48. votes and/or block parts for `NumBlocksForVetted` blocks.
  49. Other users of the p2p package can determine their own conditions for when a peer is marked vetted.
  50. If a peer becomes vetted but there are already too many vetted peers,
  51. a randomly selected one of the vetted peers becomes unvetted.
  52. If a peer becomes unvetted (either a new peer, or one that was previously vetted),
  53. a randomly selected one of the unvetted peers is removed from the address book.
  54. More fine-grained tracking of peer behaviour can be done using
  55. a trust metric (see below), but it's best to start with something simple.
  56. ## Select Peers to Dial
  57. When we need more peers, we pick addresses randomly from the addrbook with some
  58. configurable bias for unvetted peers. The bias should be lower when we have
  59. fewer peers and can increase as we obtain more, ensuring that our first peers
  60. are more trustworthy, but always giving us the chance to discover new good
  61. peers.
  62. We track the last time we dialed a peer and the number of unsuccessful attempts
  63. we've made. If too many attempts are made, we mark the peer as bad.
  64. Connection attempts are made with exponential backoff (plus jitter). Because
  65. the selection process happens every `ensurePeersPeriod`, we might not end up
  66. dialing a peer for much longer than the backoff duration.
  67. If we fail to connect to the peer after 16 tries (with exponential backoff), we
  68. remove from address book completely.
  69. ## Select Peers to Exchange
  70. When we’re asked for peers, we select them as follows:
  71. - select at most `maxGetSelection` peers
  72. - try to select at least `minGetSelection` peers - if we have less than that, select them all.
  73. - select a random, unbiased `getSelectionPercent` of the peers
  74. Send the selected peers. Note we select peers for sending without bias for vetted/unvetted.
  75. ## Preventing Spam
  76. There are various cases where we decide a peer has misbehaved and we disconnect from them.
  77. When this happens, the peer is removed from the address book and black listed for
  78. some amount of time. We call this "Disconnect and Mark".
  79. Note that the bad behaviour may be detected outside the PEX reactor itself
  80. (for instance, in the mconnection, or another reactor), but it must be communicated to the PEX reactor
  81. so it can remove and mark the peer.
  82. In the PEX, if a peer sends us an unsolicited list of peers,
  83. or if the peer sends a request too soon after another one,
  84. we Disconnect and MarkBad.
  85. ## Trust Metric
  86. The quality of peers can be tracked in more fine-grained detail using a
  87. Proportional-Integral-Derivative (PID) controller that incorporates
  88. current, past, and rate-of-change data to inform peer quality.
  89. While a PID trust metric has been implemented, it remains for future work
  90. to use it in the PEX.
  91. See the [trustmetric](https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-006-trust-metric.md)
  92. and [trustmetric useage](https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-007-trust-metric-usage.md)
  93. architecture docs for more details.