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.

1406 lines
42 KiB

  1. package p2p
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "math"
  7. "math/rand"
  8. "sort"
  9. "sync"
  10. "time"
  11. "github.com/gogo/protobuf/proto"
  12. "github.com/google/orderedcode"
  13. dbm "github.com/tendermint/tm-db"
  14. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  15. p2pproto "github.com/tendermint/tendermint/proto/tendermint/p2p"
  16. "github.com/tendermint/tendermint/types"
  17. )
  18. const (
  19. // retryNever is returned by retryDelay() when retries are disabled.
  20. retryNever time.Duration = math.MaxInt64
  21. )
  22. // PeerStatus is a peer status.
  23. //
  24. // The peer manager has many more internal states for a peer (e.g. dialing,
  25. // connected, evicting, and so on), which are tracked separately. PeerStatus is
  26. // for external use outside of the peer manager.
  27. type PeerStatus string
  28. const (
  29. PeerStatusUp PeerStatus = "up" // connected and ready
  30. PeerStatusDown PeerStatus = "down" // disconnected
  31. PeerStatusGood PeerStatus = "good" // peer observed as good
  32. PeerStatusBad PeerStatus = "bad" // peer observed as bad
  33. )
  34. // PeerScore is a numeric score assigned to a peer (higher is better).
  35. type PeerScore uint8
  36. const (
  37. PeerScorePersistent PeerScore = math.MaxUint8 // persistent peers
  38. )
  39. // PeerUpdate is a peer update event sent via PeerUpdates.
  40. type PeerUpdate struct {
  41. NodeID types.NodeID
  42. Status PeerStatus
  43. }
  44. // PeerUpdates is a peer update subscription with notifications about peer
  45. // events (currently just status changes).
  46. type PeerUpdates struct {
  47. routerUpdatesCh chan PeerUpdate
  48. reactorUpdatesCh chan PeerUpdate
  49. closeCh chan struct{}
  50. closeOnce sync.Once
  51. }
  52. // NewPeerUpdates creates a new PeerUpdates subscription. It is primarily for
  53. // internal use, callers should typically use PeerManager.Subscribe(). The
  54. // subscriber must call Close() when done.
  55. func NewPeerUpdates(updatesCh chan PeerUpdate, buf int) *PeerUpdates {
  56. return &PeerUpdates{
  57. reactorUpdatesCh: updatesCh,
  58. routerUpdatesCh: make(chan PeerUpdate, buf),
  59. closeCh: make(chan struct{}),
  60. }
  61. }
  62. // Updates returns a channel for consuming peer updates.
  63. func (pu *PeerUpdates) Updates() <-chan PeerUpdate {
  64. return pu.reactorUpdatesCh
  65. }
  66. // SendUpdate pushes information about a peer into the routing layer,
  67. // presumably from a peer.
  68. func (pu *PeerUpdates) SendUpdate(update PeerUpdate) {
  69. select {
  70. case <-pu.closeCh:
  71. case pu.routerUpdatesCh <- update:
  72. }
  73. }
  74. // Close closes the peer updates subscription.
  75. func (pu *PeerUpdates) Close() {
  76. pu.closeOnce.Do(func() {
  77. // NOTE: We don't close updatesCh since multiple goroutines may be
  78. // sending on it. The PeerManager senders will select on closeCh as well
  79. // to avoid blocking on a closed subscription.
  80. close(pu.closeCh)
  81. })
  82. }
  83. // Done returns a channel that is closed when the subscription is closed.
  84. func (pu *PeerUpdates) Done() <-chan struct{} {
  85. return pu.closeCh
  86. }
  87. // PeerManagerOptions specifies options for a PeerManager.
  88. type PeerManagerOptions struct {
  89. // PersistentPeers are peers that we want to maintain persistent connections
  90. // to. These will be scored higher than other peers, and if
  91. // MaxConnectedUpgrade is non-zero any lower-scored peers will be evicted if
  92. // necessary to make room for these.
  93. PersistentPeers []types.NodeID
  94. // MaxPeers is the maximum number of peers to track information about, i.e.
  95. // store in the peer store. When exceeded, the lowest-scored unconnected peers
  96. // will be deleted. 0 means no limit.
  97. MaxPeers uint16
  98. // MaxConnected is the maximum number of connected peers (inbound and
  99. // outbound). 0 means no limit.
  100. MaxConnected uint16
  101. // MaxConnectedUpgrade is the maximum number of additional connections to
  102. // use for probing any better-scored peers to upgrade to when all connection
  103. // slots are full. 0 disables peer upgrading.
  104. //
  105. // For example, if we are already connected to MaxConnected peers, but we
  106. // know or learn about better-scored peers (e.g. configured persistent
  107. // peers) that we are not connected too, then we can probe these peers by
  108. // using up to MaxConnectedUpgrade connections, and once connected evict the
  109. // lowest-scored connected peers. This also works for inbound connections,
  110. // i.e. if a higher-scored peer attempts to connect to us, we can accept
  111. // the connection and evict a lower-scored peer.
  112. MaxConnectedUpgrade uint16
  113. // MinRetryTime is the minimum time to wait between retries. Retry times
  114. // double for each retry, up to MaxRetryTime. 0 disables retries.
  115. MinRetryTime time.Duration
  116. // MaxRetryTime is the maximum time to wait between retries. 0 means
  117. // no maximum, in which case the retry time will keep doubling.
  118. MaxRetryTime time.Duration
  119. // MaxRetryTimePersistent is the maximum time to wait between retries for
  120. // peers listed in PersistentPeers. 0 uses MaxRetryTime instead.
  121. MaxRetryTimePersistent time.Duration
  122. // RetryTimeJitter is the upper bound of a random interval added to
  123. // retry times, to avoid thundering herds. 0 disables jitter.
  124. RetryTimeJitter time.Duration
  125. // PeerScores sets fixed scores for specific peers. It is mainly used
  126. // for testing. A score of 0 is ignored.
  127. PeerScores map[types.NodeID]PeerScore
  128. // PrivatePeerIDs defines a set of NodeID objects which the PEX reactor will
  129. // consider private and never gossip.
  130. PrivatePeers map[types.NodeID]struct{}
  131. // persistentPeers provides fast PersistentPeers lookups. It is built
  132. // by optimize().
  133. persistentPeers map[types.NodeID]bool
  134. }
  135. // Validate validates the options.
  136. func (o *PeerManagerOptions) Validate() error {
  137. for _, id := range o.PersistentPeers {
  138. if err := id.Validate(); err != nil {
  139. return fmt.Errorf("invalid PersistentPeer ID %q: %w", id, err)
  140. }
  141. }
  142. for id := range o.PrivatePeers {
  143. if err := id.Validate(); err != nil {
  144. return fmt.Errorf("invalid private peer ID %q: %w", id, err)
  145. }
  146. }
  147. if o.MaxConnected > 0 && len(o.PersistentPeers) > int(o.MaxConnected) {
  148. return fmt.Errorf("number of persistent peers %v can't exceed MaxConnected %v",
  149. len(o.PersistentPeers), o.MaxConnected)
  150. }
  151. if o.MaxPeers > 0 {
  152. if o.MaxConnected == 0 || o.MaxConnected+o.MaxConnectedUpgrade > o.MaxPeers {
  153. return fmt.Errorf(
  154. "MaxConnected %v and MaxConnectedUpgrade %v can't exceed MaxPeers %v",
  155. o.MaxConnected, o.MaxConnectedUpgrade, o.MaxPeers)
  156. }
  157. }
  158. if o.MaxRetryTime > 0 {
  159. if o.MinRetryTime == 0 {
  160. return errors.New("can't set MaxRetryTime without MinRetryTime")
  161. }
  162. if o.MinRetryTime > o.MaxRetryTime {
  163. return fmt.Errorf("MinRetryTime %v is greater than MaxRetryTime %v",
  164. o.MinRetryTime, o.MaxRetryTime)
  165. }
  166. }
  167. if o.MaxRetryTimePersistent > 0 {
  168. if o.MinRetryTime == 0 {
  169. return errors.New("can't set MaxRetryTimePersistent without MinRetryTime")
  170. }
  171. if o.MinRetryTime > o.MaxRetryTimePersistent {
  172. return fmt.Errorf(
  173. "MinRetryTime %v is greater than MaxRetryTimePersistent %v",
  174. o.MinRetryTime, o.MaxRetryTimePersistent)
  175. }
  176. }
  177. return nil
  178. }
  179. // isPersistentPeer checks if a peer is in PersistentPeers. It will panic
  180. // if called before optimize().
  181. func (o *PeerManagerOptions) isPersistent(id types.NodeID) bool {
  182. if o.persistentPeers == nil {
  183. panic("isPersistentPeer() called before optimize()")
  184. }
  185. return o.persistentPeers[id]
  186. }
  187. // optimize optimizes operations by pregenerating lookup structures. It's a
  188. // separate method instead of memoizing during calls to avoid dealing with
  189. // concurrency and mutex overhead.
  190. func (o *PeerManagerOptions) optimize() {
  191. o.persistentPeers = make(map[types.NodeID]bool, len(o.PersistentPeers))
  192. for _, p := range o.PersistentPeers {
  193. o.persistentPeers[p] = true
  194. }
  195. }
  196. // PeerManager manages peer lifecycle information, using a peerStore for
  197. // underlying storage. Its primary purpose is to determine which peer to connect
  198. // to next (including retry timers), make sure a peer only has a single active
  199. // connection (either inbound or outbound), and evict peers to make room for
  200. // higher-scored peers. It does not manage actual connections (this is handled
  201. // by the Router), only the peer lifecycle state.
  202. //
  203. // For an outbound connection, the flow is as follows:
  204. // - DialNext: return a peer address to dial, mark peer as dialing.
  205. // - DialFailed: report a dial failure, unmark as dialing.
  206. // - Dialed: report a dial success, unmark as dialing and mark as connected
  207. // (errors if already connected, e.g. by Accepted).
  208. // - Ready: report routing is ready, mark as ready and broadcast PeerStatusUp.
  209. // - Disconnected: report peer disconnect, unmark as connected and broadcasts
  210. // PeerStatusDown.
  211. //
  212. // For an inbound connection, the flow is as follows:
  213. // - Accepted: report inbound connection success, mark as connected (errors if
  214. // already connected, e.g. by Dialed).
  215. // - Ready: report routing is ready, mark as ready and broadcast PeerStatusUp.
  216. // - Disconnected: report peer disconnect, unmark as connected and broadcasts
  217. // PeerStatusDown.
  218. //
  219. // When evicting peers, either because peers are explicitly scheduled for
  220. // eviction or we are connected to too many peers, the flow is as follows:
  221. // - EvictNext: if marked evict and connected, unmark evict and mark evicting.
  222. // If beyond MaxConnected, pick lowest-scored peer and mark evicting.
  223. // - Disconnected: unmark connected, evicting, evict, and broadcast a
  224. // PeerStatusDown peer update.
  225. //
  226. // If all connection slots are full (at MaxConnections), we can use up to
  227. // MaxConnectionsUpgrade additional connections to probe any higher-scored
  228. // unconnected peers, and if we reach them (or they reach us) we allow the
  229. // connection and evict a lower-scored peer. We mark the lower-scored peer as
  230. // upgrading[from]=to to make sure no other higher-scored peers can claim the
  231. // same one for an upgrade. The flow is as follows:
  232. // - Accepted: if upgrade is possible, mark connected and add lower-scored to evict.
  233. // - DialNext: if upgrade is possible, mark upgrading[from]=to and dialing.
  234. // - DialFailed: unmark upgrading[from]=to and dialing.
  235. // - Dialed: unmark upgrading[from]=to and dialing, mark as connected, add
  236. // lower-scored to evict.
  237. // - EvictNext: pick peer from evict, mark as evicting.
  238. // - Disconnected: unmark connected, upgrading[from]=to, evict, evicting.
  239. type PeerManager struct {
  240. selfID types.NodeID
  241. options PeerManagerOptions
  242. rand *rand.Rand
  243. dialWaker *tmsync.Waker // wakes up DialNext() on relevant peer changes
  244. evictWaker *tmsync.Waker // wakes up EvictNext() on relevant peer changes
  245. closeCh chan struct{} // signal channel for Close()
  246. closeOnce sync.Once
  247. mtx sync.Mutex
  248. store *peerStore
  249. subscriptions map[*PeerUpdates]*PeerUpdates // keyed by struct identity (address)
  250. dialing map[types.NodeID]bool // peers being dialed (DialNext → Dialed/DialFail)
  251. upgrading map[types.NodeID]types.NodeID // peers claimed for upgrade (DialNext → Dialed/DialFail)
  252. connected map[types.NodeID]bool // connected peers (Dialed/Accepted → Disconnected)
  253. ready map[types.NodeID]bool // ready peers (Ready → Disconnected)
  254. evict map[types.NodeID]bool // peers scheduled for eviction (Connected → EvictNext)
  255. evicting map[types.NodeID]bool // peers being evicted (EvictNext → Disconnected)
  256. }
  257. // NewPeerManager creates a new peer manager.
  258. func NewPeerManager(selfID types.NodeID, peerDB dbm.DB, options PeerManagerOptions) (*PeerManager, error) {
  259. if selfID == "" {
  260. return nil, errors.New("self ID not given")
  261. }
  262. if err := options.Validate(); err != nil {
  263. return nil, err
  264. }
  265. options.optimize()
  266. store, err := newPeerStore(peerDB)
  267. if err != nil {
  268. return nil, err
  269. }
  270. peerManager := &PeerManager{
  271. selfID: selfID,
  272. options: options,
  273. rand: rand.New(rand.NewSource(time.Now().UnixNano())), // nolint:gosec
  274. dialWaker: tmsync.NewWaker(),
  275. evictWaker: tmsync.NewWaker(),
  276. closeCh: make(chan struct{}),
  277. store: store,
  278. dialing: map[types.NodeID]bool{},
  279. upgrading: map[types.NodeID]types.NodeID{},
  280. connected: map[types.NodeID]bool{},
  281. ready: map[types.NodeID]bool{},
  282. evict: map[types.NodeID]bool{},
  283. evicting: map[types.NodeID]bool{},
  284. subscriptions: map[*PeerUpdates]*PeerUpdates{},
  285. }
  286. if err = peerManager.configurePeers(); err != nil {
  287. return nil, err
  288. }
  289. if err = peerManager.prunePeers(); err != nil {
  290. return nil, err
  291. }
  292. return peerManager, nil
  293. }
  294. // configurePeers configures peers in the peer store with ephemeral runtime
  295. // configuration, e.g. PersistentPeers. It also removes ourself, if we're in the
  296. // peer store. The caller must hold the mutex lock.
  297. func (m *PeerManager) configurePeers() error {
  298. if err := m.store.Delete(m.selfID); err != nil {
  299. return err
  300. }
  301. configure := map[types.NodeID]bool{}
  302. for _, id := range m.options.PersistentPeers {
  303. configure[id] = true
  304. }
  305. for id := range m.options.PeerScores {
  306. configure[id] = true
  307. }
  308. for id := range configure {
  309. if peer, ok := m.store.Get(id); ok {
  310. if err := m.store.Set(m.configurePeer(peer)); err != nil {
  311. return err
  312. }
  313. }
  314. }
  315. return nil
  316. }
  317. // configurePeer configures a peer with ephemeral runtime configuration.
  318. func (m *PeerManager) configurePeer(peer peerInfo) peerInfo {
  319. peer.Persistent = m.options.isPersistent(peer.ID)
  320. peer.FixedScore = m.options.PeerScores[peer.ID]
  321. return peer
  322. }
  323. // newPeerInfo creates a peerInfo for a new peer.
  324. func (m *PeerManager) newPeerInfo(id types.NodeID) peerInfo {
  325. peerInfo := peerInfo{
  326. ID: id,
  327. AddressInfo: map[NodeAddress]*peerAddressInfo{},
  328. }
  329. return m.configurePeer(peerInfo)
  330. }
  331. // prunePeers removes low-scored peers from the peer store if it contains more
  332. // than MaxPeers peers. The caller must hold the mutex lock.
  333. func (m *PeerManager) prunePeers() error {
  334. if m.options.MaxPeers == 0 || m.store.Size() <= int(m.options.MaxPeers) {
  335. return nil
  336. }
  337. ranked := m.store.Ranked()
  338. for i := len(ranked) - 1; i >= 0; i-- {
  339. peerID := ranked[i].ID
  340. switch {
  341. case m.store.Size() <= int(m.options.MaxPeers):
  342. return nil
  343. case m.dialing[peerID]:
  344. case m.connected[peerID]:
  345. default:
  346. if err := m.store.Delete(peerID); err != nil {
  347. return err
  348. }
  349. }
  350. }
  351. return nil
  352. }
  353. // Add adds a peer to the manager, given as an address. If the peer already
  354. // exists, the address is added to it if it isn't already present. This will push
  355. // low scoring peers out of the address book if it exceeds the maximum size.
  356. func (m *PeerManager) Add(address NodeAddress) (bool, error) {
  357. if err := address.Validate(); err != nil {
  358. return false, err
  359. }
  360. if address.NodeID == m.selfID {
  361. return false, fmt.Errorf("can't add self (%v) to peer store", m.selfID)
  362. }
  363. m.mtx.Lock()
  364. defer m.mtx.Unlock()
  365. peer, ok := m.store.Get(address.NodeID)
  366. if !ok {
  367. peer = m.newPeerInfo(address.NodeID)
  368. }
  369. _, ok = peer.AddressInfo[address]
  370. // if we already have the peer address, there's no need to continue
  371. if ok {
  372. return false, nil
  373. }
  374. // else add the new address
  375. peer.AddressInfo[address] = &peerAddressInfo{Address: address}
  376. if err := m.store.Set(peer); err != nil {
  377. return false, err
  378. }
  379. if err := m.prunePeers(); err != nil {
  380. return true, err
  381. }
  382. m.dialWaker.Wake()
  383. return true, nil
  384. }
  385. // PeerRatio returns the ratio of peer addresses stored to the maximum size.
  386. func (m *PeerManager) PeerRatio() float64 {
  387. m.mtx.Lock()
  388. defer m.mtx.Unlock()
  389. if m.options.MaxPeers == 0 {
  390. return 0
  391. }
  392. return float64(m.store.Size()) / float64(m.options.MaxPeers)
  393. }
  394. // DialNext finds an appropriate peer address to dial, and marks it as dialing.
  395. // If no peer is found, or all connection slots are full, it blocks until one
  396. // becomes available. The caller must call Dialed() or DialFailed() for the
  397. // returned peer.
  398. func (m *PeerManager) DialNext(ctx context.Context) (NodeAddress, error) {
  399. for {
  400. address, err := m.TryDialNext()
  401. if err != nil || (address != NodeAddress{}) {
  402. return address, err
  403. }
  404. select {
  405. case <-m.dialWaker.Sleep():
  406. case <-ctx.Done():
  407. return NodeAddress{}, ctx.Err()
  408. }
  409. }
  410. }
  411. // TryDialNext is equivalent to DialNext(), but immediately returns an empty
  412. // address if no peers or connection slots are available.
  413. func (m *PeerManager) TryDialNext() (NodeAddress, error) {
  414. m.mtx.Lock()
  415. defer m.mtx.Unlock()
  416. // We allow dialing MaxConnected+MaxConnectedUpgrade peers. Including
  417. // MaxConnectedUpgrade allows us to probe additional peers that have a
  418. // higher score than any other peers, and if successful evict it.
  419. if m.options.MaxConnected > 0 && len(m.connected)+len(m.dialing) >=
  420. int(m.options.MaxConnected)+int(m.options.MaxConnectedUpgrade) {
  421. return NodeAddress{}, nil
  422. }
  423. for _, peer := range m.store.Ranked() {
  424. if m.dialing[peer.ID] || m.connected[peer.ID] {
  425. continue
  426. }
  427. for _, addressInfo := range peer.AddressInfo {
  428. if time.Since(addressInfo.LastDialFailure) < m.retryDelay(addressInfo.DialFailures, peer.Persistent) {
  429. continue
  430. }
  431. // We now have an eligible address to dial. If we're full but have
  432. // upgrade capacity (as checked above), we find a lower-scored peer
  433. // we can replace and mark it as upgrading so noone else claims it.
  434. //
  435. // If we don't find one, there is no point in trying additional
  436. // peers, since they will all have the same or lower score than this
  437. // peer (since they're ordered by score via peerStore.Ranked).
  438. if m.options.MaxConnected > 0 && len(m.connected) >= int(m.options.MaxConnected) {
  439. upgradeFromPeer := m.findUpgradeCandidate(peer.ID, peer.Score())
  440. if upgradeFromPeer == "" {
  441. return NodeAddress{}, nil
  442. }
  443. m.upgrading[upgradeFromPeer] = peer.ID
  444. }
  445. m.dialing[peer.ID] = true
  446. return addressInfo.Address, nil
  447. }
  448. }
  449. return NodeAddress{}, nil
  450. }
  451. // DialFailed reports a failed dial attempt. This will make the peer available
  452. // for dialing again when appropriate (possibly after a retry timeout).
  453. //
  454. // FIXME: This should probably delete or mark bad addresses/peers after some time.
  455. func (m *PeerManager) DialFailed(address NodeAddress) error {
  456. m.mtx.Lock()
  457. defer m.mtx.Unlock()
  458. delete(m.dialing, address.NodeID)
  459. for from, to := range m.upgrading {
  460. if to == address.NodeID {
  461. delete(m.upgrading, from) // Unmark failed upgrade attempt.
  462. }
  463. }
  464. peer, ok := m.store.Get(address.NodeID)
  465. if !ok { // Peer may have been removed while dialing, ignore.
  466. return nil
  467. }
  468. addressInfo, ok := peer.AddressInfo[address]
  469. if !ok {
  470. return nil // Assume the address has been removed, ignore.
  471. }
  472. addressInfo.LastDialFailure = time.Now().UTC()
  473. addressInfo.DialFailures++
  474. if err := m.store.Set(peer); err != nil {
  475. return err
  476. }
  477. // We spawn a goroutine that notifies DialNext() again when the retry
  478. // timeout has elapsed, so that we can consider dialing it again. We
  479. // calculate the retry delay outside the goroutine, since it must hold
  480. // the mutex lock.
  481. if d := m.retryDelay(addressInfo.DialFailures, peer.Persistent); d != 0 && d != retryNever {
  482. go func() {
  483. // Use an explicit timer with deferred cleanup instead of
  484. // time.After(), to avoid leaking goroutines on PeerManager.Close().
  485. timer := time.NewTimer(d)
  486. defer timer.Stop()
  487. select {
  488. case <-timer.C:
  489. m.dialWaker.Wake()
  490. case <-m.closeCh:
  491. }
  492. }()
  493. } else {
  494. m.dialWaker.Wake()
  495. }
  496. return nil
  497. }
  498. // Dialed marks a peer as successfully dialed. Any further connections will be
  499. // rejected, and once disconnected the peer may be dialed again.
  500. func (m *PeerManager) Dialed(address NodeAddress) error {
  501. m.mtx.Lock()
  502. defer m.mtx.Unlock()
  503. delete(m.dialing, address.NodeID)
  504. var upgradeFromPeer types.NodeID
  505. for from, to := range m.upgrading {
  506. if to == address.NodeID {
  507. delete(m.upgrading, from)
  508. upgradeFromPeer = from
  509. // Don't break, just in case this peer was marked as upgrading for
  510. // multiple lower-scored peers (shouldn't really happen).
  511. }
  512. }
  513. if address.NodeID == m.selfID {
  514. return fmt.Errorf("rejecting connection to self (%v)", address.NodeID)
  515. }
  516. if m.connected[address.NodeID] {
  517. return fmt.Errorf("peer %v is already connected", address.NodeID)
  518. }
  519. if m.options.MaxConnected > 0 && len(m.connected) >= int(m.options.MaxConnected) {
  520. if upgradeFromPeer == "" || len(m.connected) >=
  521. int(m.options.MaxConnected)+int(m.options.MaxConnectedUpgrade) {
  522. return fmt.Errorf("already connected to maximum number of peers")
  523. }
  524. }
  525. peer, ok := m.store.Get(address.NodeID)
  526. if !ok {
  527. return fmt.Errorf("peer %q was removed while dialing", address.NodeID)
  528. }
  529. now := time.Now().UTC()
  530. peer.LastConnected = now
  531. if addressInfo, ok := peer.AddressInfo[address]; ok {
  532. addressInfo.DialFailures = 0
  533. addressInfo.LastDialSuccess = now
  534. // If not found, assume address has been removed.
  535. }
  536. if err := m.store.Set(peer); err != nil {
  537. return err
  538. }
  539. if upgradeFromPeer != "" && m.options.MaxConnected > 0 &&
  540. len(m.connected) >= int(m.options.MaxConnected) {
  541. // Look for an even lower-scored peer that may have appeared since we
  542. // started the upgrade.
  543. if p, ok := m.store.Get(upgradeFromPeer); ok {
  544. if u := m.findUpgradeCandidate(p.ID, p.Score()); u != "" {
  545. upgradeFromPeer = u
  546. }
  547. }
  548. m.evict[upgradeFromPeer] = true
  549. }
  550. m.connected[peer.ID] = true
  551. m.evictWaker.Wake()
  552. return nil
  553. }
  554. // Accepted marks an incoming peer connection successfully accepted. If the peer
  555. // is already connected or we don't allow additional connections then this will
  556. // return an error.
  557. //
  558. // If full but MaxConnectedUpgrade is non-zero and the incoming peer is
  559. // better-scored than any existing peers, then we accept it and evict a
  560. // lower-scored peer.
  561. //
  562. // NOTE: We can't take an address here, since e.g. TCP uses a different port
  563. // number for outbound traffic than inbound traffic, so the peer's endpoint
  564. // wouldn't necessarily be an appropriate address to dial.
  565. //
  566. // FIXME: When we accept a connection from a peer, we should register that
  567. // peer's address in the peer store so that we can dial it later. In order to do
  568. // that, we'll need to get the remote address after all, but as noted above that
  569. // can't be the remote endpoint since that will usually have the wrong port
  570. // number.
  571. func (m *PeerManager) Accepted(peerID types.NodeID) error {
  572. m.mtx.Lock()
  573. defer m.mtx.Unlock()
  574. if peerID == m.selfID {
  575. return fmt.Errorf("rejecting connection from self (%v)", peerID)
  576. }
  577. if m.connected[peerID] {
  578. return fmt.Errorf("peer %q is already connected", peerID)
  579. }
  580. if m.options.MaxConnected > 0 &&
  581. len(m.connected) >= int(m.options.MaxConnected)+int(m.options.MaxConnectedUpgrade) {
  582. return fmt.Errorf("already connected to maximum number of peers")
  583. }
  584. peer, ok := m.store.Get(peerID)
  585. if !ok {
  586. peer = m.newPeerInfo(peerID)
  587. }
  588. // reset this to avoid penalizing peers for their past transgressions
  589. for _, addr := range peer.AddressInfo {
  590. addr.DialFailures = 0
  591. }
  592. // If all connections slots are full, but we allow upgrades (and we checked
  593. // above that we have upgrade capacity), then we can look for a lower-scored
  594. // peer to replace and if found accept the connection anyway and evict it.
  595. var upgradeFromPeer types.NodeID
  596. if m.options.MaxConnected > 0 && len(m.connected) >= int(m.options.MaxConnected) {
  597. upgradeFromPeer = m.findUpgradeCandidate(peer.ID, peer.Score())
  598. if upgradeFromPeer == "" {
  599. return fmt.Errorf("already connected to maximum number of peers")
  600. }
  601. }
  602. peer.LastConnected = time.Now().UTC()
  603. if err := m.store.Set(peer); err != nil {
  604. return err
  605. }
  606. m.connected[peerID] = true
  607. if upgradeFromPeer != "" {
  608. m.evict[upgradeFromPeer] = true
  609. }
  610. m.evictWaker.Wake()
  611. return nil
  612. }
  613. // Ready marks a peer as ready, broadcasting status updates to subscribers. The
  614. // peer must already be marked as connected. This is separate from Dialed() and
  615. // Accepted() to allow the router to set up its internal queues before reactors
  616. // start sending messages.
  617. func (m *PeerManager) Ready(peerID types.NodeID) {
  618. m.mtx.Lock()
  619. defer m.mtx.Unlock()
  620. if m.connected[peerID] {
  621. m.ready[peerID] = true
  622. m.broadcast(PeerUpdate{
  623. NodeID: peerID,
  624. Status: PeerStatusUp,
  625. })
  626. }
  627. }
  628. // EvictNext returns the next peer to evict (i.e. disconnect). If no evictable
  629. // peers are found, the call will block until one becomes available.
  630. func (m *PeerManager) EvictNext(ctx context.Context) (types.NodeID, error) {
  631. for {
  632. id, err := m.TryEvictNext()
  633. if err != nil || id != "" {
  634. return id, err
  635. }
  636. select {
  637. case <-m.evictWaker.Sleep():
  638. case <-ctx.Done():
  639. return "", ctx.Err()
  640. }
  641. }
  642. }
  643. // TryEvictNext is equivalent to EvictNext, but immediately returns an empty
  644. // node ID if no evictable peers are found.
  645. func (m *PeerManager) TryEvictNext() (types.NodeID, error) {
  646. m.mtx.Lock()
  647. defer m.mtx.Unlock()
  648. // If any connected peers are explicitly scheduled for eviction, we return a
  649. // random one.
  650. for peerID := range m.evict {
  651. delete(m.evict, peerID)
  652. if m.connected[peerID] && !m.evicting[peerID] {
  653. m.evicting[peerID] = true
  654. return peerID, nil
  655. }
  656. }
  657. // If we're below capacity, we don't need to evict anything.
  658. if m.options.MaxConnected == 0 ||
  659. len(m.connected)-len(m.evicting) <= int(m.options.MaxConnected) {
  660. return "", nil
  661. }
  662. // If we're above capacity (shouldn't really happen), just pick the
  663. // lowest-ranked peer to evict.
  664. ranked := m.store.Ranked()
  665. for i := len(ranked) - 1; i >= 0; i-- {
  666. peer := ranked[i]
  667. if m.connected[peer.ID] && !m.evicting[peer.ID] {
  668. m.evicting[peer.ID] = true
  669. return peer.ID, nil
  670. }
  671. }
  672. return "", nil
  673. }
  674. // Disconnected unmarks a peer as connected, allowing it to be dialed or
  675. // accepted again as appropriate.
  676. func (m *PeerManager) Disconnected(peerID types.NodeID) {
  677. m.mtx.Lock()
  678. defer m.mtx.Unlock()
  679. ready := m.ready[peerID]
  680. delete(m.connected, peerID)
  681. delete(m.upgrading, peerID)
  682. delete(m.evict, peerID)
  683. delete(m.evicting, peerID)
  684. delete(m.ready, peerID)
  685. if ready {
  686. m.broadcast(PeerUpdate{
  687. NodeID: peerID,
  688. Status: PeerStatusDown,
  689. })
  690. }
  691. m.dialWaker.Wake()
  692. }
  693. // Errored reports a peer error, causing the peer to be evicted if it's
  694. // currently connected.
  695. //
  696. // FIXME: This should probably be replaced with a peer behavior API, see
  697. // PeerError comments for more details.
  698. //
  699. // FIXME: This will cause the peer manager to immediately try to reconnect to
  700. // the peer, which is probably not always what we want.
  701. func (m *PeerManager) Errored(peerID types.NodeID, err error) {
  702. m.mtx.Lock()
  703. defer m.mtx.Unlock()
  704. if m.connected[peerID] {
  705. m.evict[peerID] = true
  706. }
  707. m.evictWaker.Wake()
  708. }
  709. // Advertise returns a list of peer addresses to advertise to a peer.
  710. //
  711. // FIXME: This is fairly naïve and only returns the addresses of the
  712. // highest-ranked peers.
  713. func (m *PeerManager) Advertise(peerID types.NodeID, limit uint16) []NodeAddress {
  714. m.mtx.Lock()
  715. defer m.mtx.Unlock()
  716. addresses := make([]NodeAddress, 0, limit)
  717. for _, peer := range m.store.Ranked() {
  718. if peer.ID == peerID {
  719. continue
  720. }
  721. for nodeAddr, addressInfo := range peer.AddressInfo {
  722. if len(addresses) >= int(limit) {
  723. return addresses
  724. }
  725. // only add non-private NodeIDs
  726. if _, ok := m.options.PrivatePeers[nodeAddr.NodeID]; !ok {
  727. addresses = append(addresses, addressInfo.Address)
  728. }
  729. }
  730. }
  731. return addresses
  732. }
  733. // Subscribe subscribes to peer updates. The caller must consume the peer
  734. // updates in a timely fashion and close the subscription when done, otherwise
  735. // the PeerManager will halt.
  736. func (m *PeerManager) Subscribe() *PeerUpdates {
  737. // FIXME: We use a size 1 buffer here. When we broadcast a peer update
  738. // we have to loop over all of the subscriptions, and we want to avoid
  739. // having to block and wait for a context switch before continuing on
  740. // to the next subscriptions. This also prevents tail latencies from
  741. // compounding. Limiting it to 1 means that the subscribers are still
  742. // reasonably in sync. However, this should probably be benchmarked.
  743. peerUpdates := NewPeerUpdates(make(chan PeerUpdate, 1), 1)
  744. m.Register(peerUpdates)
  745. return peerUpdates
  746. }
  747. // Register allows you to inject a custom PeerUpdate instance into the
  748. // PeerManager, rather than relying on the instance constructed by the
  749. // Subscribe method, which wraps the functionality of the Register
  750. // method.
  751. //
  752. // The caller must consume the peer updates from this PeerUpdates
  753. // instance in a timely fashion and close the subscription when done,
  754. // otherwise the PeerManager will halt.
  755. func (m *PeerManager) Register(peerUpdates *PeerUpdates) {
  756. m.mtx.Lock()
  757. m.subscriptions[peerUpdates] = peerUpdates
  758. m.mtx.Unlock()
  759. go func() {
  760. for {
  761. select {
  762. case <-peerUpdates.closeCh:
  763. return
  764. case <-m.closeCh:
  765. return
  766. case pu := <-peerUpdates.routerUpdatesCh:
  767. m.processPeerEvent(pu)
  768. }
  769. }
  770. }()
  771. go func() {
  772. select {
  773. case <-peerUpdates.Done():
  774. m.mtx.Lock()
  775. delete(m.subscriptions, peerUpdates)
  776. m.mtx.Unlock()
  777. case <-m.closeCh:
  778. }
  779. }()
  780. }
  781. func (m *PeerManager) processPeerEvent(pu PeerUpdate) {
  782. m.mtx.Lock()
  783. defer m.mtx.Unlock()
  784. if _, ok := m.store.peers[pu.NodeID]; !ok {
  785. m.store.peers[pu.NodeID] = &peerInfo{}
  786. }
  787. switch pu.Status {
  788. case PeerStatusBad:
  789. m.store.peers[pu.NodeID].MutableScore--
  790. case PeerStatusGood:
  791. m.store.peers[pu.NodeID].MutableScore++
  792. }
  793. }
  794. // broadcast broadcasts a peer update to all subscriptions. The caller must
  795. // already hold the mutex lock, to make sure updates are sent in the same order
  796. // as the PeerManager processes them, but this means subscribers must be
  797. // responsive at all times or the entire PeerManager will halt.
  798. //
  799. // FIXME: Consider using an internal channel to buffer updates while also
  800. // maintaining order if this is a problem.
  801. func (m *PeerManager) broadcast(peerUpdate PeerUpdate) {
  802. for _, sub := range m.subscriptions {
  803. // We have to check closeCh separately first, otherwise there's a 50%
  804. // chance the second select will send on a closed subscription.
  805. select {
  806. case <-sub.closeCh:
  807. continue
  808. default:
  809. }
  810. select {
  811. case sub.reactorUpdatesCh <- peerUpdate:
  812. case <-sub.closeCh:
  813. }
  814. }
  815. }
  816. // Close closes the peer manager, releasing resources (i.e. goroutines).
  817. func (m *PeerManager) Close() {
  818. m.closeOnce.Do(func() {
  819. close(m.closeCh)
  820. })
  821. }
  822. // Addresses returns all known addresses for a peer, primarily for testing.
  823. // The order is arbitrary.
  824. func (m *PeerManager) Addresses(peerID types.NodeID) []NodeAddress {
  825. m.mtx.Lock()
  826. defer m.mtx.Unlock()
  827. addresses := []NodeAddress{}
  828. if peer, ok := m.store.Get(peerID); ok {
  829. for _, addressInfo := range peer.AddressInfo {
  830. addresses = append(addresses, addressInfo.Address)
  831. }
  832. }
  833. return addresses
  834. }
  835. // Peers returns all known peers, primarily for testing. The order is arbitrary.
  836. func (m *PeerManager) Peers() []types.NodeID {
  837. m.mtx.Lock()
  838. defer m.mtx.Unlock()
  839. peers := []types.NodeID{}
  840. for _, peer := range m.store.Ranked() {
  841. peers = append(peers, peer.ID)
  842. }
  843. return peers
  844. }
  845. // Scores returns the peer scores for all known peers, primarily for testing.
  846. func (m *PeerManager) Scores() map[types.NodeID]PeerScore {
  847. m.mtx.Lock()
  848. defer m.mtx.Unlock()
  849. scores := map[types.NodeID]PeerScore{}
  850. for _, peer := range m.store.Ranked() {
  851. scores[peer.ID] = peer.Score()
  852. }
  853. return scores
  854. }
  855. // Status returns the status for a peer, primarily for testing.
  856. func (m *PeerManager) Status(id types.NodeID) PeerStatus {
  857. m.mtx.Lock()
  858. defer m.mtx.Unlock()
  859. switch {
  860. case m.ready[id]:
  861. return PeerStatusUp
  862. default:
  863. return PeerStatusDown
  864. }
  865. }
  866. // findUpgradeCandidate looks for a lower-scored peer that we could evict
  867. // to make room for the given peer. Returns an empty ID if none is found.
  868. // If the peer is already being upgraded to, we return that same upgrade.
  869. // The caller must hold the mutex lock.
  870. func (m *PeerManager) findUpgradeCandidate(id types.NodeID, score PeerScore) types.NodeID {
  871. for from, to := range m.upgrading {
  872. if to == id {
  873. return from
  874. }
  875. }
  876. ranked := m.store.Ranked()
  877. for i := len(ranked) - 1; i >= 0; i-- {
  878. candidate := ranked[i]
  879. switch {
  880. case candidate.Score() >= score:
  881. return "" // no further peers can be scored lower, due to sorting
  882. case !m.connected[candidate.ID]:
  883. case m.evict[candidate.ID]:
  884. case m.evicting[candidate.ID]:
  885. case m.upgrading[candidate.ID] != "":
  886. default:
  887. return candidate.ID
  888. }
  889. }
  890. return ""
  891. }
  892. // retryDelay calculates a dial retry delay using exponential backoff, based on
  893. // retry settings in PeerManagerOptions. If retries are disabled (i.e.
  894. // MinRetryTime is 0), this returns retryNever (i.e. an infinite retry delay).
  895. // The caller must hold the mutex lock (for m.rand which is not thread-safe).
  896. func (m *PeerManager) retryDelay(failures uint32, persistent bool) time.Duration {
  897. if failures == 0 {
  898. return 0
  899. }
  900. if m.options.MinRetryTime == 0 {
  901. return retryNever
  902. }
  903. maxDelay := m.options.MaxRetryTime
  904. if persistent && m.options.MaxRetryTimePersistent > 0 {
  905. maxDelay = m.options.MaxRetryTimePersistent
  906. }
  907. delay := m.options.MinRetryTime * time.Duration(math.Pow(2, float64(failures-1)))
  908. if maxDelay > 0 && delay > maxDelay {
  909. delay = maxDelay
  910. }
  911. if m.options.RetryTimeJitter > 0 {
  912. delay += time.Duration(m.rand.Int63n(int64(m.options.RetryTimeJitter)))
  913. }
  914. return delay
  915. }
  916. // GetHeight returns a peer's height, as reported via SetHeight, or 0 if the
  917. // peer or height is unknown.
  918. //
  919. // FIXME: This is a temporary workaround to share state between the consensus
  920. // and mempool reactors, carried over from the legacy P2P stack. Reactors should
  921. // not have dependencies on each other, instead tracking this themselves.
  922. func (m *PeerManager) GetHeight(peerID types.NodeID) int64 {
  923. m.mtx.Lock()
  924. defer m.mtx.Unlock()
  925. peer, _ := m.store.Get(peerID)
  926. return peer.Height
  927. }
  928. // SetHeight stores a peer's height, making it available via GetHeight.
  929. //
  930. // FIXME: This is a temporary workaround to share state between the consensus
  931. // and mempool reactors, carried over from the legacy P2P stack. Reactors should
  932. // not have dependencies on each other, instead tracking this themselves.
  933. func (m *PeerManager) SetHeight(peerID types.NodeID, height int64) error {
  934. m.mtx.Lock()
  935. defer m.mtx.Unlock()
  936. peer, ok := m.store.Get(peerID)
  937. if !ok {
  938. peer = m.newPeerInfo(peerID)
  939. }
  940. peer.Height = height
  941. return m.store.Set(peer)
  942. }
  943. // peerStore stores information about peers. It is not thread-safe, assuming it
  944. // is only used by PeerManager which handles concurrency control. This allows
  945. // the manager to execute multiple operations atomically via its own mutex.
  946. //
  947. // The entire set of peers is kept in memory, for performance. It is loaded
  948. // from disk on initialization, and any changes are written back to disk
  949. // (without fsync, since we can afford to lose recent writes).
  950. type peerStore struct {
  951. db dbm.DB
  952. peers map[types.NodeID]*peerInfo
  953. ranked []*peerInfo // cache for Ranked(), nil invalidates cache
  954. }
  955. // newPeerStore creates a new peer store, loading all persisted peers from the
  956. // database into memory.
  957. func newPeerStore(db dbm.DB) (*peerStore, error) {
  958. if db == nil {
  959. return nil, errors.New("no database provided")
  960. }
  961. store := &peerStore{db: db}
  962. if err := store.loadPeers(); err != nil {
  963. return nil, err
  964. }
  965. return store, nil
  966. }
  967. // loadPeers loads all peers from the database into memory.
  968. func (s *peerStore) loadPeers() error {
  969. peers := map[types.NodeID]*peerInfo{}
  970. start, end := keyPeerInfoRange()
  971. iter, err := s.db.Iterator(start, end)
  972. if err != nil {
  973. return err
  974. }
  975. defer iter.Close()
  976. for ; iter.Valid(); iter.Next() {
  977. // FIXME: We may want to tolerate failures here, by simply logging
  978. // the errors and ignoring the faulty peer entries.
  979. msg := new(p2pproto.PeerInfo)
  980. if err := proto.Unmarshal(iter.Value(), msg); err != nil {
  981. return fmt.Errorf("invalid peer Protobuf data: %w", err)
  982. }
  983. peer, err := peerInfoFromProto(msg)
  984. if err != nil {
  985. return fmt.Errorf("invalid peer data: %w", err)
  986. }
  987. peers[peer.ID] = peer
  988. }
  989. if iter.Error() != nil {
  990. return iter.Error()
  991. }
  992. s.peers = peers
  993. s.ranked = nil // invalidate cache if populated
  994. return nil
  995. }
  996. // Get fetches a peer. The boolean indicates whether the peer existed or not.
  997. // The returned peer info is a copy, and can be mutated at will.
  998. func (s *peerStore) Get(id types.NodeID) (peerInfo, bool) {
  999. peer, ok := s.peers[id]
  1000. return peer.Copy(), ok
  1001. }
  1002. // Set stores peer data. The input data will be copied, and can safely be reused
  1003. // by the caller.
  1004. func (s *peerStore) Set(peer peerInfo) error {
  1005. if err := peer.Validate(); err != nil {
  1006. return err
  1007. }
  1008. peer = peer.Copy()
  1009. // FIXME: We may want to optimize this by avoiding saving to the database
  1010. // if there haven't been any changes to persisted fields.
  1011. bz, err := peer.ToProto().Marshal()
  1012. if err != nil {
  1013. return err
  1014. }
  1015. if err = s.db.Set(keyPeerInfo(peer.ID), bz); err != nil {
  1016. return err
  1017. }
  1018. if current, ok := s.peers[peer.ID]; !ok || current.Score() != peer.Score() {
  1019. // If the peer is new, or its score changes, we invalidate the Ranked() cache.
  1020. s.peers[peer.ID] = &peer
  1021. s.ranked = nil
  1022. } else {
  1023. // Otherwise, since s.ranked contains pointers to the old data and we
  1024. // want those pointers to remain valid with the new data, we have to
  1025. // update the existing pointer address.
  1026. *current = peer
  1027. }
  1028. return nil
  1029. }
  1030. // Delete deletes a peer, or does nothing if it does not exist.
  1031. func (s *peerStore) Delete(id types.NodeID) error {
  1032. if _, ok := s.peers[id]; !ok {
  1033. return nil
  1034. }
  1035. if err := s.db.Delete(keyPeerInfo(id)); err != nil {
  1036. return err
  1037. }
  1038. delete(s.peers, id)
  1039. s.ranked = nil
  1040. return nil
  1041. }
  1042. // List retrieves all peers in an arbitrary order. The returned data is a copy,
  1043. // and can be mutated at will.
  1044. func (s *peerStore) List() []peerInfo {
  1045. peers := make([]peerInfo, 0, len(s.peers))
  1046. for _, peer := range s.peers {
  1047. peers = append(peers, peer.Copy())
  1048. }
  1049. return peers
  1050. }
  1051. // Ranked returns a list of peers ordered by score (better peers first). Peers
  1052. // with equal scores are returned in an arbitrary order. The returned list must
  1053. // not be mutated or accessed concurrently by the caller, since it returns
  1054. // pointers to internal peerStore data for performance.
  1055. //
  1056. // Ranked is used to determine both which peers to dial, which ones to evict,
  1057. // and which ones to delete completely.
  1058. //
  1059. // FIXME: For now, we simply maintain a cache in s.ranked which is invalidated
  1060. // by setting it to nil, but if necessary we should use a better data structure
  1061. // for this (e.g. a heap or ordered map).
  1062. //
  1063. // FIXME: The scoring logic is currently very naïve, see peerInfo.Score().
  1064. func (s *peerStore) Ranked() []*peerInfo {
  1065. if s.ranked != nil {
  1066. return s.ranked
  1067. }
  1068. s.ranked = make([]*peerInfo, 0, len(s.peers))
  1069. for _, peer := range s.peers {
  1070. s.ranked = append(s.ranked, peer)
  1071. }
  1072. sort.Slice(s.ranked, func(i, j int) bool {
  1073. // FIXME: If necessary, consider precomputing scores before sorting,
  1074. // to reduce the number of Score() calls.
  1075. return s.ranked[i].Score() > s.ranked[j].Score()
  1076. })
  1077. return s.ranked
  1078. }
  1079. // Size returns the number of peers in the peer store.
  1080. func (s *peerStore) Size() int {
  1081. return len(s.peers)
  1082. }
  1083. // peerInfo contains peer information stored in a peerStore.
  1084. type peerInfo struct {
  1085. ID types.NodeID
  1086. AddressInfo map[NodeAddress]*peerAddressInfo
  1087. LastConnected time.Time
  1088. // These fields are ephemeral, i.e. not persisted to the database.
  1089. Persistent bool
  1090. Height int64
  1091. FixedScore PeerScore // mainly for tests
  1092. MutableScore int64 // updated by router
  1093. }
  1094. // peerInfoFromProto converts a Protobuf PeerInfo message to a peerInfo,
  1095. // erroring if the data is invalid.
  1096. func peerInfoFromProto(msg *p2pproto.PeerInfo) (*peerInfo, error) {
  1097. p := &peerInfo{
  1098. ID: types.NodeID(msg.ID),
  1099. AddressInfo: map[NodeAddress]*peerAddressInfo{},
  1100. }
  1101. if msg.LastConnected != nil {
  1102. p.LastConnected = *msg.LastConnected
  1103. }
  1104. for _, a := range msg.AddressInfo {
  1105. addressInfo, err := peerAddressInfoFromProto(a)
  1106. if err != nil {
  1107. return nil, err
  1108. }
  1109. p.AddressInfo[addressInfo.Address] = addressInfo
  1110. }
  1111. return p, p.Validate()
  1112. }
  1113. // ToProto converts the peerInfo to p2pproto.PeerInfo for database storage. The
  1114. // Protobuf type only contains persisted fields, while ephemeral fields are
  1115. // discarded. The returned message may contain pointers to original data, since
  1116. // it is expected to be serialized immediately.
  1117. func (p *peerInfo) ToProto() *p2pproto.PeerInfo {
  1118. msg := &p2pproto.PeerInfo{
  1119. ID: string(p.ID),
  1120. LastConnected: &p.LastConnected,
  1121. }
  1122. for _, addressInfo := range p.AddressInfo {
  1123. msg.AddressInfo = append(msg.AddressInfo, addressInfo.ToProto())
  1124. }
  1125. if msg.LastConnected.IsZero() {
  1126. msg.LastConnected = nil
  1127. }
  1128. return msg
  1129. }
  1130. // Copy returns a deep copy of the peer info.
  1131. func (p *peerInfo) Copy() peerInfo {
  1132. if p == nil {
  1133. return peerInfo{}
  1134. }
  1135. c := *p
  1136. for i, addressInfo := range c.AddressInfo {
  1137. addressInfoCopy := addressInfo.Copy()
  1138. c.AddressInfo[i] = &addressInfoCopy
  1139. }
  1140. return c
  1141. }
  1142. // Score calculates a score for the peer. Higher-scored peers will be
  1143. // preferred over lower scores.
  1144. func (p *peerInfo) Score() PeerScore {
  1145. if p.FixedScore > 0 {
  1146. return p.FixedScore
  1147. }
  1148. if p.Persistent {
  1149. return PeerScorePersistent
  1150. }
  1151. score := p.MutableScore
  1152. for _, addr := range p.AddressInfo {
  1153. // DialFailures is reset when dials succeed, so this
  1154. // is either the number of dial failures or 0.
  1155. score -= int64(addr.DialFailures)
  1156. }
  1157. if score <= 0 {
  1158. return 0
  1159. }
  1160. if score >= math.MaxUint8 {
  1161. return PeerScore(math.MaxUint8)
  1162. }
  1163. return PeerScore(score)
  1164. }
  1165. // Validate validates the peer info.
  1166. func (p *peerInfo) Validate() error {
  1167. if p.ID == "" {
  1168. return errors.New("no peer ID")
  1169. }
  1170. return nil
  1171. }
  1172. // peerAddressInfo contains information and statistics about a peer address.
  1173. type peerAddressInfo struct {
  1174. Address NodeAddress
  1175. LastDialSuccess time.Time
  1176. LastDialFailure time.Time
  1177. DialFailures uint32 // since last successful dial
  1178. }
  1179. // peerAddressInfoFromProto converts a Protobuf PeerAddressInfo message
  1180. // to a peerAddressInfo.
  1181. func peerAddressInfoFromProto(msg *p2pproto.PeerAddressInfo) (*peerAddressInfo, error) {
  1182. address, err := ParseNodeAddress(msg.Address)
  1183. if err != nil {
  1184. return nil, fmt.Errorf("invalid address %q: %w", address, err)
  1185. }
  1186. addressInfo := &peerAddressInfo{
  1187. Address: address,
  1188. DialFailures: msg.DialFailures,
  1189. }
  1190. if msg.LastDialSuccess != nil {
  1191. addressInfo.LastDialSuccess = *msg.LastDialSuccess
  1192. }
  1193. if msg.LastDialFailure != nil {
  1194. addressInfo.LastDialFailure = *msg.LastDialFailure
  1195. }
  1196. return addressInfo, addressInfo.Validate()
  1197. }
  1198. // ToProto converts the address into to a Protobuf message for serialization.
  1199. func (a *peerAddressInfo) ToProto() *p2pproto.PeerAddressInfo {
  1200. msg := &p2pproto.PeerAddressInfo{
  1201. Address: a.Address.String(),
  1202. LastDialSuccess: &a.LastDialSuccess,
  1203. LastDialFailure: &a.LastDialFailure,
  1204. DialFailures: a.DialFailures,
  1205. }
  1206. if msg.LastDialSuccess.IsZero() {
  1207. msg.LastDialSuccess = nil
  1208. }
  1209. if msg.LastDialFailure.IsZero() {
  1210. msg.LastDialFailure = nil
  1211. }
  1212. return msg
  1213. }
  1214. // Copy returns a copy of the address info.
  1215. func (a *peerAddressInfo) Copy() peerAddressInfo {
  1216. return *a
  1217. }
  1218. // Validate validates the address info.
  1219. func (a *peerAddressInfo) Validate() error {
  1220. return a.Address.Validate()
  1221. }
  1222. // Database key prefixes.
  1223. const (
  1224. prefixPeerInfo int64 = 1
  1225. )
  1226. // keyPeerInfo generates a peerInfo database key.
  1227. func keyPeerInfo(id types.NodeID) []byte {
  1228. key, err := orderedcode.Append(nil, prefixPeerInfo, string(id))
  1229. if err != nil {
  1230. panic(err)
  1231. }
  1232. return key
  1233. }
  1234. // keyPeerInfoRange generates start/end keys for the entire peerInfo key range.
  1235. func keyPeerInfoRange() ([]byte, []byte) {
  1236. start, err := orderedcode.Append(nil, prefixPeerInfo, "")
  1237. if err != nil {
  1238. panic(err)
  1239. }
  1240. end, err := orderedcode.Append(nil, prefixPeerInfo, orderedcode.Infinity)
  1241. if err != nil {
  1242. panic(err)
  1243. }
  1244. return start, end
  1245. }