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.

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