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.

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