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.

94 lines
3.9 KiB

7 years ago
  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.
  8. Some such peers can additional be marked as `private`, which means
  9. we will not gossip them to others.
  10. All others peers are tracked using an address book.
  11. ## Discovery
  12. Peer discovery begins with a list of seeds.
  13. When we have no peers, or have been unable to find enough peers from existing ones,
  14. we dial a randomly selected seed to get a list of peers to dial.
  15. So long as we have less than `MaxPeers`, we periodically request additional peers
  16. from each of our own. If sufficient time goes by and we still can't find enough peers,
  17. we try the seeds again.
  18. ## Address Book
  19. Peers are tracked via their ID (their PubKey.Address()).
  20. For each ID, the address book keeps the most recent IP:PORT.
  21. Peers are added to the address book from the PEX when they first connect to us or
  22. when we hear about them from other peers.
  23. The address book is arranged in sets of buckets, and distinguishes between
  24. vetted and unvetted peers. It keeps different sets of buckets for vetted and
  25. unvetted peers. Buckets provide randomization over peer selection.
  26. A vetted peer can only be in one bucket. An unvetted peer can be in multiple buckets.
  27. ## Vetting
  28. When a peer is first added, it is unvetted.
  29. Marking a peer as vetted is outside the scope of the `p2p` package.
  30. For Tendermint, a Peer becomes vetted once it has contributed sufficiently
  31. at the consensus layer; ie. once it has sent us valid and not-yet-known
  32. votes and/or block parts for `NumBlocksForVetted` blocks.
  33. Other users of the p2p package can determine their own conditions for when a peer is marked vetted.
  34. If a peer becomes vetted but there are already too many vetted peers,
  35. a randomly selected one of the vetted peers becomes unvetted.
  36. If a peer becomes unvetted (either a new peer, or one that was previously vetted),
  37. a randomly selected one of the unvetted peers is removed from the address book.
  38. More fine-grained tracking of peer behaviour can be done using
  39. a Trust Metric, but it's best to start with something simple.
  40. ## Select Peers to Dial
  41. When we need more peers, we pick them randomly from the addrbook with some
  42. configurable bias for unvetted peers. The bias should be lower when we have fewer peers,
  43. and can increase as we obtain more, ensuring that our first peers are more trustworthy,
  44. but always giving us the chance to discover new good peers.
  45. ## Select Peers to Exchange
  46. When we’re asked for peers, we select them as follows:
  47. - select at most `maxGetSelection` peers
  48. - try to select at least `minGetSelection` peers - if we have less than that, select them all.
  49. - select a random, unbiased `getSelectionPercent` of the peers
  50. Send the selected peers. Note we select peers for sending without bias for vetted/unvetted.
  51. ## Preventing Spam
  52. There are various cases where we decide a peer has misbehaved and we disconnect from them.
  53. When this happens, the peer is removed from the address book and black listed for
  54. some amount of time. We call this "Disconnect and Mark".
  55. Note that the bad behaviour may be detected outside the PEX reactor itseld
  56. (for instance, in the mconnection, or another reactor), but it must be communicated to the PEX reactor
  57. so it can remove and mark the peer.
  58. In the PEX, if a peer sends us unsolicited lists of peers,
  59. or if the peer sends too many requests for more peers in a given amount of time,
  60. we Disconnect and Mark.
  61. ## Trust Metric
  62. The quality of peers can be tracked in more fine-grained detail using a
  63. Proportional-Integral-Derrivative (PID) controller that incorporates
  64. current, past, and rate-of-change data to inform peer quality.
  65. While a PID trust metric has been implemented, it remains for future work
  66. to use it in the PEX.