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.

1385 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. if m.options.MaxPeers == 0 {
  385. return 0
  386. }
  387. m.mtx.Lock()
  388. defer m.mtx.Unlock()
  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 != 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. }
  491. m.dialWaker.Wake()
  492. return nil
  493. }
  494. // Dialed marks a peer as successfully dialed. Any further connections will be
  495. // rejected, and once disconnected the peer may be dialed again.
  496. func (m *PeerManager) Dialed(address NodeAddress) error {
  497. m.mtx.Lock()
  498. defer m.mtx.Unlock()
  499. delete(m.dialing, address.NodeID)
  500. var upgradeFromPeer NodeID
  501. for from, to := range m.upgrading {
  502. if to == address.NodeID {
  503. delete(m.upgrading, from)
  504. upgradeFromPeer = from
  505. // Don't break, just in case this peer was marked as upgrading for
  506. // multiple lower-scored peers (shouldn't really happen).
  507. }
  508. }
  509. if address.NodeID == m.selfID {
  510. return fmt.Errorf("rejecting connection to self (%v)", address.NodeID)
  511. }
  512. if m.connected[address.NodeID] {
  513. return fmt.Errorf("peer %v is already connected", address.NodeID)
  514. }
  515. if m.options.MaxConnected > 0 && len(m.connected) >= int(m.options.MaxConnected) {
  516. if upgradeFromPeer == "" || len(m.connected) >=
  517. int(m.options.MaxConnected)+int(m.options.MaxConnectedUpgrade) {
  518. return fmt.Errorf("already connected to maximum number of peers")
  519. }
  520. }
  521. peer, ok := m.store.Get(address.NodeID)
  522. if !ok {
  523. return fmt.Errorf("peer %q was removed while dialing", address.NodeID)
  524. }
  525. now := time.Now().UTC()
  526. peer.LastConnected = now
  527. if addressInfo, ok := peer.AddressInfo[address]; ok {
  528. addressInfo.DialFailures = 0
  529. addressInfo.LastDialSuccess = now
  530. // If not found, assume address has been removed.
  531. }
  532. if err := m.store.Set(peer); err != nil {
  533. return err
  534. }
  535. if upgradeFromPeer != "" && m.options.MaxConnected > 0 &&
  536. len(m.connected) >= int(m.options.MaxConnected) {
  537. // Look for an even lower-scored peer that may have appeared since we
  538. // started the upgrade.
  539. if p, ok := m.store.Get(upgradeFromPeer); ok {
  540. if u := m.findUpgradeCandidate(p.ID, p.Score()); u != "" {
  541. upgradeFromPeer = u
  542. }
  543. }
  544. m.evict[upgradeFromPeer] = true
  545. }
  546. m.connected[peer.ID] = true
  547. m.evictWaker.Wake()
  548. return nil
  549. }
  550. // Accepted marks an incoming peer connection successfully accepted. If the peer
  551. // is already connected or we don't allow additional connections then this will
  552. // return an error.
  553. //
  554. // If full but MaxConnectedUpgrade is non-zero and the incoming peer is
  555. // better-scored than any existing peers, then we accept it and evict a
  556. // lower-scored peer.
  557. //
  558. // NOTE: We can't take an address here, since e.g. TCP uses a different port
  559. // number for outbound traffic than inbound traffic, so the peer's endpoint
  560. // wouldn't necessarily be an appropriate address to dial.
  561. //
  562. // FIXME: When we accept a connection from a peer, we should register that
  563. // peer's address in the peer store so that we can dial it later. In order to do
  564. // that, we'll need to get the remote address after all, but as noted above that
  565. // can't be the remote endpoint since that will usually have the wrong port
  566. // number.
  567. func (m *PeerManager) Accepted(peerID NodeID) error {
  568. m.mtx.Lock()
  569. defer m.mtx.Unlock()
  570. if peerID == m.selfID {
  571. return fmt.Errorf("rejecting connection from self (%v)", peerID)
  572. }
  573. if m.connected[peerID] {
  574. return fmt.Errorf("peer %q is already connected", peerID)
  575. }
  576. if m.options.MaxConnected > 0 &&
  577. len(m.connected) >= int(m.options.MaxConnected)+int(m.options.MaxConnectedUpgrade) {
  578. return fmt.Errorf("already connected to maximum number of peers")
  579. }
  580. peer, ok := m.store.Get(peerID)
  581. if !ok {
  582. peer = m.newPeerInfo(peerID)
  583. }
  584. // If all connections slots are full, but we allow upgrades (and we checked
  585. // above that we have upgrade capacity), then we can look for a lower-scored
  586. // peer to replace and if found accept the connection anyway and evict it.
  587. var upgradeFromPeer NodeID
  588. if m.options.MaxConnected > 0 && len(m.connected) >= int(m.options.MaxConnected) {
  589. upgradeFromPeer = m.findUpgradeCandidate(peer.ID, peer.Score())
  590. if upgradeFromPeer == "" {
  591. return fmt.Errorf("already connected to maximum number of peers")
  592. }
  593. }
  594. peer.LastConnected = time.Now().UTC()
  595. if err := m.store.Set(peer); err != nil {
  596. return err
  597. }
  598. m.connected[peerID] = true
  599. if upgradeFromPeer != "" {
  600. m.evict[upgradeFromPeer] = true
  601. }
  602. m.evictWaker.Wake()
  603. return nil
  604. }
  605. // Ready marks a peer as ready, broadcasting status updates to subscribers. The
  606. // peer must already be marked as connected. This is separate from Dialed() and
  607. // Accepted() to allow the router to set up its internal queues before reactors
  608. // start sending messages.
  609. func (m *PeerManager) Ready(peerID NodeID) {
  610. m.mtx.Lock()
  611. defer m.mtx.Unlock()
  612. if m.connected[peerID] {
  613. m.ready[peerID] = true
  614. m.broadcast(PeerUpdate{
  615. NodeID: peerID,
  616. Status: PeerStatusUp,
  617. })
  618. }
  619. }
  620. // EvictNext returns the next peer to evict (i.e. disconnect). If no evictable
  621. // peers are found, the call will block until one becomes available.
  622. func (m *PeerManager) EvictNext(ctx context.Context) (NodeID, error) {
  623. for {
  624. id, err := m.TryEvictNext()
  625. if err != nil || id != "" {
  626. return id, err
  627. }
  628. select {
  629. case <-m.evictWaker.Sleep():
  630. case <-ctx.Done():
  631. return "", ctx.Err()
  632. }
  633. }
  634. }
  635. // TryEvictNext is equivalent to EvictNext, but immediately returns an empty
  636. // node ID if no evictable peers are found.
  637. func (m *PeerManager) TryEvictNext() (NodeID, error) {
  638. m.mtx.Lock()
  639. defer m.mtx.Unlock()
  640. // If any connected peers are explicitly scheduled for eviction, we return a
  641. // random one.
  642. for peerID := range m.evict {
  643. delete(m.evict, peerID)
  644. if m.connected[peerID] && !m.evicting[peerID] {
  645. m.evicting[peerID] = true
  646. return peerID, nil
  647. }
  648. }
  649. // If we're below capacity, we don't need to evict anything.
  650. if m.options.MaxConnected == 0 ||
  651. len(m.connected)-len(m.evicting) <= int(m.options.MaxConnected) {
  652. return "", nil
  653. }
  654. // If we're above capacity (shouldn't really happen), just pick the
  655. // lowest-ranked peer to evict.
  656. ranked := m.store.Ranked()
  657. for i := len(ranked) - 1; i >= 0; i-- {
  658. peer := ranked[i]
  659. if m.connected[peer.ID] && !m.evicting[peer.ID] {
  660. m.evicting[peer.ID] = true
  661. return peer.ID, nil
  662. }
  663. }
  664. return "", nil
  665. }
  666. // Disconnected unmarks a peer as connected, allowing it to be dialed or
  667. // accepted again as appropriate.
  668. func (m *PeerManager) Disconnected(peerID NodeID) {
  669. m.mtx.Lock()
  670. defer m.mtx.Unlock()
  671. ready := m.ready[peerID]
  672. delete(m.connected, peerID)
  673. delete(m.upgrading, peerID)
  674. delete(m.evict, peerID)
  675. delete(m.evicting, peerID)
  676. delete(m.ready, peerID)
  677. if ready {
  678. m.broadcast(PeerUpdate{
  679. NodeID: peerID,
  680. Status: PeerStatusDown,
  681. })
  682. }
  683. m.dialWaker.Wake()
  684. }
  685. // Errored reports a peer error, causing the peer to be evicted if it's
  686. // currently connected.
  687. //
  688. // FIXME: This should probably be replaced with a peer behavior API, see
  689. // PeerError comments for more details.
  690. //
  691. // FIXME: This will cause the peer manager to immediately try to reconnect to
  692. // the peer, which is probably not always what we want.
  693. func (m *PeerManager) Errored(peerID NodeID, err error) {
  694. m.mtx.Lock()
  695. defer m.mtx.Unlock()
  696. if m.connected[peerID] {
  697. m.evict[peerID] = true
  698. }
  699. m.evictWaker.Wake()
  700. }
  701. // Advertise returns a list of peer addresses to advertise to a peer.
  702. //
  703. // FIXME: This is fairly naïve and only returns the addresses of the
  704. // highest-ranked peers.
  705. func (m *PeerManager) Advertise(peerID NodeID, limit uint16) []NodeAddress {
  706. m.mtx.Lock()
  707. defer m.mtx.Unlock()
  708. addresses := make([]NodeAddress, 0, limit)
  709. for _, peer := range m.store.Ranked() {
  710. if peer.ID == peerID {
  711. continue
  712. }
  713. for nodeAddr, addressInfo := range peer.AddressInfo {
  714. if len(addresses) >= int(limit) {
  715. return addresses
  716. }
  717. // only add non-private NodeIDs
  718. if _, ok := m.options.PrivatePeers[nodeAddr.NodeID]; !ok {
  719. addresses = append(addresses, addressInfo.Address)
  720. }
  721. }
  722. }
  723. return addresses
  724. }
  725. // Subscribe subscribes to peer updates. The caller must consume the peer
  726. // updates in a timely fashion and close the subscription when done, otherwise
  727. // the PeerManager will halt.
  728. func (m *PeerManager) Subscribe() *PeerUpdates {
  729. // FIXME: We use a size 1 buffer here. When we broadcast a peer update
  730. // we have to loop over all of the subscriptions, and we want to avoid
  731. // having to block and wait for a context switch before continuing on
  732. // to the next subscriptions. This also prevents tail latencies from
  733. // compounding. Limiting it to 1 means that the subscribers are still
  734. // reasonably in sync. However, this should probably be benchmarked.
  735. peerUpdates := NewPeerUpdates(make(chan PeerUpdate, 1), 1)
  736. m.Register(peerUpdates)
  737. return peerUpdates
  738. }
  739. // Register allows you to inject a custom PeerUpdate instance into the
  740. // PeerManager, rather than relying on the instance constructed by the
  741. // Subscribe method, which wraps the functionality of the Register
  742. // method.
  743. //
  744. // The caller must consume the peer updates from this PeerUpdates
  745. // instance in a timely fashion and close the subscription when done,
  746. // otherwise the PeerManager will halt.
  747. func (m *PeerManager) Register(peerUpdates *PeerUpdates) {
  748. m.mtx.Lock()
  749. m.subscriptions[peerUpdates] = peerUpdates
  750. m.mtx.Unlock()
  751. go func() {
  752. for {
  753. select {
  754. case <-peerUpdates.closeCh:
  755. return
  756. case <-m.closeCh:
  757. return
  758. case pu := <-peerUpdates.routerUpdatesCh:
  759. m.processPeerEvent(pu)
  760. }
  761. }
  762. }()
  763. go func() {
  764. select {
  765. case <-peerUpdates.Done():
  766. m.mtx.Lock()
  767. delete(m.subscriptions, peerUpdates)
  768. m.mtx.Unlock()
  769. case <-m.closeCh:
  770. }
  771. }()
  772. }
  773. func (m *PeerManager) processPeerEvent(pu PeerUpdate) {
  774. m.mtx.Lock()
  775. defer m.mtx.Unlock()
  776. if _, ok := m.store.peers[pu.NodeID]; !ok {
  777. m.store.peers[pu.NodeID] = &peerInfo{}
  778. }
  779. switch pu.Status {
  780. case PeerStatusBad:
  781. m.store.peers[pu.NodeID].MutableScore--
  782. case PeerStatusGood:
  783. m.store.peers[pu.NodeID].MutableScore++
  784. }
  785. }
  786. // broadcast broadcasts a peer update to all subscriptions. The caller must
  787. // already hold the mutex lock, to make sure updates are sent in the same order
  788. // as the PeerManager processes them, but this means subscribers must be
  789. // responsive at all times or the entire PeerManager will halt.
  790. //
  791. // FIXME: Consider using an internal channel to buffer updates while also
  792. // maintaining order if this is a problem.
  793. func (m *PeerManager) broadcast(peerUpdate PeerUpdate) {
  794. for _, sub := range m.subscriptions {
  795. // We have to check closeCh separately first, otherwise there's a 50%
  796. // chance the second select will send on a closed subscription.
  797. select {
  798. case <-sub.closeCh:
  799. continue
  800. default:
  801. }
  802. select {
  803. case sub.reactorUpdatesCh <- peerUpdate:
  804. case <-sub.closeCh:
  805. }
  806. }
  807. }
  808. // Close closes the peer manager, releasing resources (i.e. goroutines).
  809. func (m *PeerManager) Close() {
  810. m.closeOnce.Do(func() {
  811. close(m.closeCh)
  812. })
  813. }
  814. // Addresses returns all known addresses for a peer, primarily for testing.
  815. // The order is arbitrary.
  816. func (m *PeerManager) Addresses(peerID NodeID) []NodeAddress {
  817. m.mtx.Lock()
  818. defer m.mtx.Unlock()
  819. addresses := []NodeAddress{}
  820. if peer, ok := m.store.Get(peerID); ok {
  821. for _, addressInfo := range peer.AddressInfo {
  822. addresses = append(addresses, addressInfo.Address)
  823. }
  824. }
  825. return addresses
  826. }
  827. // Peers returns all known peers, primarily for testing. The order is arbitrary.
  828. func (m *PeerManager) Peers() []NodeID {
  829. m.mtx.Lock()
  830. defer m.mtx.Unlock()
  831. peers := []NodeID{}
  832. for _, peer := range m.store.Ranked() {
  833. peers = append(peers, peer.ID)
  834. }
  835. return peers
  836. }
  837. // Scores returns the peer scores for all known peers, primarily for testing.
  838. func (m *PeerManager) Scores() map[NodeID]PeerScore {
  839. m.mtx.Lock()
  840. defer m.mtx.Unlock()
  841. scores := map[NodeID]PeerScore{}
  842. for _, peer := range m.store.Ranked() {
  843. scores[peer.ID] = peer.Score()
  844. }
  845. return scores
  846. }
  847. // Status returns the status for a peer, primarily for testing.
  848. func (m *PeerManager) Status(id NodeID) PeerStatus {
  849. m.mtx.Lock()
  850. defer m.mtx.Unlock()
  851. switch {
  852. case m.ready[id]:
  853. return PeerStatusUp
  854. default:
  855. return PeerStatusDown
  856. }
  857. }
  858. // findUpgradeCandidate looks for a lower-scored peer that we could evict
  859. // to make room for the given peer. Returns an empty ID if none is found.
  860. // If the peer is already being upgraded to, we return that same upgrade.
  861. // The caller must hold the mutex lock.
  862. func (m *PeerManager) findUpgradeCandidate(id NodeID, score PeerScore) NodeID {
  863. for from, to := range m.upgrading {
  864. if to == id {
  865. return from
  866. }
  867. }
  868. ranked := m.store.Ranked()
  869. for i := len(ranked) - 1; i >= 0; i-- {
  870. candidate := ranked[i]
  871. switch {
  872. case candidate.Score() >= score:
  873. return "" // no further peers can be scored lower, due to sorting
  874. case !m.connected[candidate.ID]:
  875. case m.evict[candidate.ID]:
  876. case m.evicting[candidate.ID]:
  877. case m.upgrading[candidate.ID] != "":
  878. default:
  879. return candidate.ID
  880. }
  881. }
  882. return ""
  883. }
  884. // retryDelay calculates a dial retry delay using exponential backoff, based on
  885. // retry settings in PeerManagerOptions. If retries are disabled (i.e.
  886. // MinRetryTime is 0), this returns retryNever (i.e. an infinite retry delay).
  887. // The caller must hold the mutex lock (for m.rand which is not thread-safe).
  888. func (m *PeerManager) retryDelay(failures uint32, persistent bool) time.Duration {
  889. if failures == 0 {
  890. return 0
  891. }
  892. if m.options.MinRetryTime == 0 {
  893. return retryNever
  894. }
  895. maxDelay := m.options.MaxRetryTime
  896. if persistent && m.options.MaxRetryTimePersistent > 0 {
  897. maxDelay = m.options.MaxRetryTimePersistent
  898. }
  899. delay := m.options.MinRetryTime * time.Duration(math.Pow(2, float64(failures-1)))
  900. if maxDelay > 0 && delay > maxDelay {
  901. delay = maxDelay
  902. }
  903. if m.options.RetryTimeJitter > 0 {
  904. delay += time.Duration(m.rand.Int63n(int64(m.options.RetryTimeJitter)))
  905. }
  906. return delay
  907. }
  908. // GetHeight returns a peer's height, as reported via SetHeight, or 0 if the
  909. // peer or height is unknown.
  910. //
  911. // FIXME: This is a temporary workaround to share state between the consensus
  912. // and mempool reactors, carried over from the legacy P2P stack. Reactors should
  913. // not have dependencies on each other, instead tracking this themselves.
  914. func (m *PeerManager) GetHeight(peerID NodeID) int64 {
  915. m.mtx.Lock()
  916. defer m.mtx.Unlock()
  917. peer, _ := m.store.Get(peerID)
  918. return peer.Height
  919. }
  920. // SetHeight stores a peer's height, making it available via GetHeight.
  921. //
  922. // FIXME: This is a temporary workaround to share state between the consensus
  923. // and mempool reactors, carried over from the legacy P2P stack. Reactors should
  924. // not have dependencies on each other, instead tracking this themselves.
  925. func (m *PeerManager) SetHeight(peerID NodeID, height int64) error {
  926. m.mtx.Lock()
  927. defer m.mtx.Unlock()
  928. peer, ok := m.store.Get(peerID)
  929. if !ok {
  930. peer = m.newPeerInfo(peerID)
  931. }
  932. peer.Height = height
  933. return m.store.Set(peer)
  934. }
  935. // peerStore stores information about peers. It is not thread-safe, assuming it
  936. // is only used by PeerManager which handles concurrency control. This allows
  937. // the manager to execute multiple operations atomically via its own mutex.
  938. //
  939. // The entire set of peers is kept in memory, for performance. It is loaded
  940. // from disk on initialization, and any changes are written back to disk
  941. // (without fsync, since we can afford to lose recent writes).
  942. type peerStore struct {
  943. db dbm.DB
  944. peers map[NodeID]*peerInfo
  945. ranked []*peerInfo // cache for Ranked(), nil invalidates cache
  946. }
  947. // newPeerStore creates a new peer store, loading all persisted peers from the
  948. // database into memory.
  949. func newPeerStore(db dbm.DB) (*peerStore, error) {
  950. if db == nil {
  951. return nil, errors.New("no database provided")
  952. }
  953. store := &peerStore{db: db}
  954. if err := store.loadPeers(); err != nil {
  955. return nil, err
  956. }
  957. return store, nil
  958. }
  959. // loadPeers loads all peers from the database into memory.
  960. func (s *peerStore) loadPeers() error {
  961. peers := map[NodeID]*peerInfo{}
  962. start, end := keyPeerInfoRange()
  963. iter, err := s.db.Iterator(start, end)
  964. if err != nil {
  965. return err
  966. }
  967. defer iter.Close()
  968. for ; iter.Valid(); iter.Next() {
  969. // FIXME: We may want to tolerate failures here, by simply logging
  970. // the errors and ignoring the faulty peer entries.
  971. msg := new(p2pproto.PeerInfo)
  972. if err := proto.Unmarshal(iter.Value(), msg); err != nil {
  973. return fmt.Errorf("invalid peer Protobuf data: %w", err)
  974. }
  975. peer, err := peerInfoFromProto(msg)
  976. if err != nil {
  977. return fmt.Errorf("invalid peer data: %w", err)
  978. }
  979. peers[peer.ID] = peer
  980. }
  981. if iter.Error() != nil {
  982. return iter.Error()
  983. }
  984. s.peers = peers
  985. s.ranked = nil // invalidate cache if populated
  986. return nil
  987. }
  988. // Get fetches a peer. The boolean indicates whether the peer existed or not.
  989. // The returned peer info is a copy, and can be mutated at will.
  990. func (s *peerStore) Get(id NodeID) (peerInfo, bool) {
  991. peer, ok := s.peers[id]
  992. return peer.Copy(), ok
  993. }
  994. // Set stores peer data. The input data will be copied, and can safely be reused
  995. // by the caller.
  996. func (s *peerStore) Set(peer peerInfo) error {
  997. if err := peer.Validate(); err != nil {
  998. return err
  999. }
  1000. peer = peer.Copy()
  1001. // FIXME: We may want to optimize this by avoiding saving to the database
  1002. // if there haven't been any changes to persisted fields.
  1003. bz, err := peer.ToProto().Marshal()
  1004. if err != nil {
  1005. return err
  1006. }
  1007. if err = s.db.Set(keyPeerInfo(peer.ID), bz); err != nil {
  1008. return err
  1009. }
  1010. if current, ok := s.peers[peer.ID]; !ok || current.Score() != peer.Score() {
  1011. // If the peer is new, or its score changes, we invalidate the Ranked() cache.
  1012. s.peers[peer.ID] = &peer
  1013. s.ranked = nil
  1014. } else {
  1015. // Otherwise, since s.ranked contains pointers to the old data and we
  1016. // want those pointers to remain valid with the new data, we have to
  1017. // update the existing pointer address.
  1018. *current = peer
  1019. }
  1020. return nil
  1021. }
  1022. // Delete deletes a peer, or does nothing if it does not exist.
  1023. func (s *peerStore) Delete(id NodeID) error {
  1024. if _, ok := s.peers[id]; !ok {
  1025. return nil
  1026. }
  1027. if err := s.db.Delete(keyPeerInfo(id)); err != nil {
  1028. return err
  1029. }
  1030. delete(s.peers, id)
  1031. s.ranked = nil
  1032. return nil
  1033. }
  1034. // List retrieves all peers in an arbitrary order. The returned data is a copy,
  1035. // and can be mutated at will.
  1036. func (s *peerStore) List() []peerInfo {
  1037. peers := make([]peerInfo, 0, len(s.peers))
  1038. for _, peer := range s.peers {
  1039. peers = append(peers, peer.Copy())
  1040. }
  1041. return peers
  1042. }
  1043. // Ranked returns a list of peers ordered by score (better peers first). Peers
  1044. // with equal scores are returned in an arbitrary order. The returned list must
  1045. // not be mutated or accessed concurrently by the caller, since it returns
  1046. // pointers to internal peerStore data for performance.
  1047. //
  1048. // Ranked is used to determine both which peers to dial, which ones to evict,
  1049. // and which ones to delete completely.
  1050. //
  1051. // FIXME: For now, we simply maintain a cache in s.ranked which is invalidated
  1052. // by setting it to nil, but if necessary we should use a better data structure
  1053. // for this (e.g. a heap or ordered map).
  1054. //
  1055. // FIXME: The scoring logic is currently very naïve, see peerInfo.Score().
  1056. func (s *peerStore) Ranked() []*peerInfo {
  1057. if s.ranked != nil {
  1058. return s.ranked
  1059. }
  1060. s.ranked = make([]*peerInfo, 0, len(s.peers))
  1061. for _, peer := range s.peers {
  1062. s.ranked = append(s.ranked, peer)
  1063. }
  1064. sort.Slice(s.ranked, func(i, j int) bool {
  1065. // FIXME: If necessary, consider precomputing scores before sorting,
  1066. // to reduce the number of Score() calls.
  1067. return s.ranked[i].Score() > s.ranked[j].Score()
  1068. })
  1069. return s.ranked
  1070. }
  1071. // Size returns the number of peers in the peer store.
  1072. func (s *peerStore) Size() int {
  1073. return len(s.peers)
  1074. }
  1075. // peerInfo contains peer information stored in a peerStore.
  1076. type peerInfo struct {
  1077. ID NodeID
  1078. AddressInfo map[NodeAddress]*peerAddressInfo
  1079. LastConnected time.Time
  1080. // These fields are ephemeral, i.e. not persisted to the database.
  1081. Persistent bool
  1082. Height int64
  1083. FixedScore PeerScore // mainly for tests
  1084. MutableScore int64 // updated by router
  1085. }
  1086. // peerInfoFromProto converts a Protobuf PeerInfo message to a peerInfo,
  1087. // erroring if the data is invalid.
  1088. func peerInfoFromProto(msg *p2pproto.PeerInfo) (*peerInfo, error) {
  1089. p := &peerInfo{
  1090. ID: NodeID(msg.ID),
  1091. AddressInfo: map[NodeAddress]*peerAddressInfo{},
  1092. }
  1093. if msg.LastConnected != nil {
  1094. p.LastConnected = *msg.LastConnected
  1095. }
  1096. for _, a := range msg.AddressInfo {
  1097. addressInfo, err := peerAddressInfoFromProto(a)
  1098. if err != nil {
  1099. return nil, err
  1100. }
  1101. p.AddressInfo[addressInfo.Address] = addressInfo
  1102. }
  1103. return p, p.Validate()
  1104. }
  1105. // ToProto converts the peerInfo to p2pproto.PeerInfo for database storage. The
  1106. // Protobuf type only contains persisted fields, while ephemeral fields are
  1107. // discarded. The returned message may contain pointers to original data, since
  1108. // it is expected to be serialized immediately.
  1109. func (p *peerInfo) ToProto() *p2pproto.PeerInfo {
  1110. msg := &p2pproto.PeerInfo{
  1111. ID: string(p.ID),
  1112. LastConnected: &p.LastConnected,
  1113. }
  1114. for _, addressInfo := range p.AddressInfo {
  1115. msg.AddressInfo = append(msg.AddressInfo, addressInfo.ToProto())
  1116. }
  1117. if msg.LastConnected.IsZero() {
  1118. msg.LastConnected = nil
  1119. }
  1120. return msg
  1121. }
  1122. // Copy returns a deep copy of the peer info.
  1123. func (p *peerInfo) Copy() peerInfo {
  1124. if p == nil {
  1125. return peerInfo{}
  1126. }
  1127. c := *p
  1128. for i, addressInfo := range c.AddressInfo {
  1129. addressInfoCopy := addressInfo.Copy()
  1130. c.AddressInfo[i] = &addressInfoCopy
  1131. }
  1132. return c
  1133. }
  1134. // Score calculates a score for the peer. Higher-scored peers will be
  1135. // preferred over lower scores.
  1136. func (p *peerInfo) Score() PeerScore {
  1137. if p.FixedScore > 0 {
  1138. return p.FixedScore
  1139. }
  1140. if p.Persistent {
  1141. return PeerScorePersistent
  1142. }
  1143. if p.MutableScore <= 0 {
  1144. return 0
  1145. }
  1146. if p.MutableScore >= math.MaxUint8 {
  1147. return PeerScore(math.MaxUint8)
  1148. }
  1149. return PeerScore(p.MutableScore)
  1150. }
  1151. // Validate validates the peer info.
  1152. func (p *peerInfo) Validate() error {
  1153. if p.ID == "" {
  1154. return errors.New("no peer ID")
  1155. }
  1156. return nil
  1157. }
  1158. // peerAddressInfo contains information and statistics about a peer address.
  1159. type peerAddressInfo struct {
  1160. Address NodeAddress
  1161. LastDialSuccess time.Time
  1162. LastDialFailure time.Time
  1163. DialFailures uint32 // since last successful dial
  1164. }
  1165. // peerAddressInfoFromProto converts a Protobuf PeerAddressInfo message
  1166. // to a peerAddressInfo.
  1167. func peerAddressInfoFromProto(msg *p2pproto.PeerAddressInfo) (*peerAddressInfo, error) {
  1168. address, err := ParseNodeAddress(msg.Address)
  1169. if err != nil {
  1170. return nil, fmt.Errorf("invalid address %q: %w", address, err)
  1171. }
  1172. addressInfo := &peerAddressInfo{
  1173. Address: address,
  1174. DialFailures: msg.DialFailures,
  1175. }
  1176. if msg.LastDialSuccess != nil {
  1177. addressInfo.LastDialSuccess = *msg.LastDialSuccess
  1178. }
  1179. if msg.LastDialFailure != nil {
  1180. addressInfo.LastDialFailure = *msg.LastDialFailure
  1181. }
  1182. return addressInfo, addressInfo.Validate()
  1183. }
  1184. // ToProto converts the address into to a Protobuf message for serialization.
  1185. func (a *peerAddressInfo) ToProto() *p2pproto.PeerAddressInfo {
  1186. msg := &p2pproto.PeerAddressInfo{
  1187. Address: a.Address.String(),
  1188. LastDialSuccess: &a.LastDialSuccess,
  1189. LastDialFailure: &a.LastDialFailure,
  1190. DialFailures: a.DialFailures,
  1191. }
  1192. if msg.LastDialSuccess.IsZero() {
  1193. msg.LastDialSuccess = nil
  1194. }
  1195. if msg.LastDialFailure.IsZero() {
  1196. msg.LastDialFailure = nil
  1197. }
  1198. return msg
  1199. }
  1200. // Copy returns a copy of the address info.
  1201. func (a *peerAddressInfo) Copy() peerAddressInfo {
  1202. return *a
  1203. }
  1204. // Validate validates the address info.
  1205. func (a *peerAddressInfo) Validate() error {
  1206. return a.Address.Validate()
  1207. }
  1208. // Database key prefixes.
  1209. const (
  1210. prefixPeerInfo int64 = 1
  1211. )
  1212. // keyPeerInfo generates a peerInfo database key.
  1213. func keyPeerInfo(id NodeID) []byte {
  1214. key, err := orderedcode.Append(nil, prefixPeerInfo, string(id))
  1215. if err != nil {
  1216. panic(err)
  1217. }
  1218. return key
  1219. }
  1220. // keyPeerInfoRange generates start/end keys for the entire peerInfo key range.
  1221. func keyPeerInfoRange() ([]byte, []byte) {
  1222. start, err := orderedcode.Append(nil, prefixPeerInfo, "")
  1223. if err != nil {
  1224. panic(err)
  1225. }
  1226. end, err := orderedcode.Append(nil, prefixPeerInfo, orderedcode.Infinity)
  1227. if err != nil {
  1228. panic(err)
  1229. }
  1230. return start, end
  1231. }