peer.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains the active peer-set of the downloader, maintaining both failures
  17. // as well as reputation metrics to prioritize the block retrievals.
  18. package downloader
  19. import (
  20. "errors"
  21. "math"
  22. "math/big"
  23. "sort"
  24. "sync"
  25. "sync/atomic"
  26. "time"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  29. "github.com/ethereum/go-ethereum/event"
  30. "github.com/ethereum/go-ethereum/log"
  31. )
  32. const (
  33. maxLackingHashes = 4096 // Maximum number of entries allowed on the list or lacking items
  34. measurementImpact = 0.1 // The impact a single measurement has on a peer's final throughput value.
  35. )
  36. var (
  37. errAlreadyFetching = errors.New("already fetching blocks from peer")
  38. errAlreadyRegistered = errors.New("peer is already registered")
  39. errNotRegistered = errors.New("peer is not registered")
  40. )
  41. // peerConnection represents an active peer from which hashes and blocks are retrieved.
  42. type peerConnection struct {
  43. id string // Unique identifier of the peer
  44. headerIdle int32 // Current header activity state of the peer (idle = 0, active = 1)
  45. blockIdle int32 // Current block activity state of the peer (idle = 0, active = 1)
  46. receiptIdle int32 // Current receipt activity state of the peer (idle = 0, active = 1)
  47. stateIdle int32 // Current node data activity state of the peer (idle = 0, active = 1)
  48. headerThroughput float64 // Number of headers measured to be retrievable per second
  49. blockThroughput float64 // Number of blocks (bodies) measured to be retrievable per second
  50. receiptThroughput float64 // Number of receipts measured to be retrievable per second
  51. stateThroughput float64 // Number of node data pieces measured to be retrievable per second
  52. rtt time.Duration // Request round trip time to track responsiveness (QoS)
  53. headerStarted time.Time // Time instance when the last header fetch was started
  54. blockStarted time.Time // Time instance when the last block (body) fetch was started
  55. receiptStarted time.Time // Time instance when the last receipt fetch was started
  56. stateStarted time.Time // Time instance when the last node data fetch was started
  57. lacking map[common.Hash]struct{} // Set of hashes not to request (didn't have previously)
  58. peer Peer
  59. version uint // Eth protocol version number to switch strategies
  60. log log.Logger // Contextual logger to add extra infos to peer logs
  61. lock sync.RWMutex
  62. }
  63. // LightPeer encapsulates the methods required to synchronise with a remote light peer.
  64. type LightPeer interface {
  65. Head() (common.Hash, *big.Int)
  66. RequestHeadersByHash(common.Hash, int, int, bool) error
  67. RequestHeadersByNumber(uint64, int, int, bool) error
  68. }
  69. // Peer encapsulates the methods required to synchronise with a remote full peer.
  70. type Peer interface {
  71. LightPeer
  72. RequestBodies([]common.Hash) error
  73. RequestReceipts([]common.Hash) error
  74. RequestNodeData([]common.Hash) error
  75. }
  76. // lightPeerWrapper wraps a LightPeer struct, stubbing out the Peer-only methods.
  77. type lightPeerWrapper struct {
  78. peer LightPeer
  79. }
  80. func (w *lightPeerWrapper) Head() (common.Hash, *big.Int) { return w.peer.Head() }
  81. func (w *lightPeerWrapper) RequestHeadersByHash(h common.Hash, amount int, skip int, reverse bool) error {
  82. return w.peer.RequestHeadersByHash(h, amount, skip, reverse)
  83. }
  84. func (w *lightPeerWrapper) RequestHeadersByNumber(i uint64, amount int, skip int, reverse bool) error {
  85. return w.peer.RequestHeadersByNumber(i, amount, skip, reverse)
  86. }
  87. func (w *lightPeerWrapper) RequestBodies([]common.Hash) error {
  88. panic("RequestBodies not supported in light client mode sync")
  89. }
  90. func (w *lightPeerWrapper) RequestReceipts([]common.Hash) error {
  91. panic("RequestReceipts not supported in light client mode sync")
  92. }
  93. func (w *lightPeerWrapper) RequestNodeData([]common.Hash) error {
  94. panic("RequestNodeData not supported in light client mode sync")
  95. }
  96. // newPeerConnection creates a new downloader peer.
  97. func newPeerConnection(id string, version uint, peer Peer, logger log.Logger) *peerConnection {
  98. return &peerConnection{
  99. id: id,
  100. lacking: make(map[common.Hash]struct{}),
  101. peer: peer,
  102. version: version,
  103. log: logger,
  104. }
  105. }
  106. // Reset clears the internal state of a peer entity.
  107. func (p *peerConnection) Reset() {
  108. p.lock.Lock()
  109. defer p.lock.Unlock()
  110. atomic.StoreInt32(&p.headerIdle, 0)
  111. atomic.StoreInt32(&p.blockIdle, 0)
  112. atomic.StoreInt32(&p.receiptIdle, 0)
  113. atomic.StoreInt32(&p.stateIdle, 0)
  114. p.headerThroughput = 0
  115. p.blockThroughput = 0
  116. p.receiptThroughput = 0
  117. p.stateThroughput = 0
  118. p.lacking = make(map[common.Hash]struct{})
  119. }
  120. // FetchHeaders sends a header retrieval request to the remote peer.
  121. func (p *peerConnection) FetchHeaders(from uint64, count int) error {
  122. // Short circuit if the peer is already fetching
  123. if !atomic.CompareAndSwapInt32(&p.headerIdle, 0, 1) {
  124. return errAlreadyFetching
  125. }
  126. p.headerStarted = time.Now()
  127. // Issue the header retrieval request (absolute upwards without gaps)
  128. go p.peer.RequestHeadersByNumber(from, count, 0, false)
  129. return nil
  130. }
  131. // FetchBodies sends a block body retrieval request to the remote peer.
  132. func (p *peerConnection) FetchBodies(request *fetchRequest) error {
  133. // Short circuit if the peer is already fetching
  134. if !atomic.CompareAndSwapInt32(&p.blockIdle, 0, 1) {
  135. return errAlreadyFetching
  136. }
  137. p.blockStarted = time.Now()
  138. go func() {
  139. // Convert the header set to a retrievable slice
  140. hashes := make([]common.Hash, 0, len(request.Headers))
  141. for _, header := range request.Headers {
  142. hashes = append(hashes, header.Hash())
  143. }
  144. p.peer.RequestBodies(hashes)
  145. }()
  146. return nil
  147. }
  148. // FetchReceipts sends a receipt retrieval request to the remote peer.
  149. func (p *peerConnection) FetchReceipts(request *fetchRequest) error {
  150. // Short circuit if the peer is already fetching
  151. if !atomic.CompareAndSwapInt32(&p.receiptIdle, 0, 1) {
  152. return errAlreadyFetching
  153. }
  154. p.receiptStarted = time.Now()
  155. go func() {
  156. // Convert the header set to a retrievable slice
  157. hashes := make([]common.Hash, 0, len(request.Headers))
  158. for _, header := range request.Headers {
  159. hashes = append(hashes, header.Hash())
  160. }
  161. p.peer.RequestReceipts(hashes)
  162. }()
  163. return nil
  164. }
  165. // FetchNodeData sends a node state data retrieval request to the remote peer.
  166. func (p *peerConnection) FetchNodeData(hashes []common.Hash) error {
  167. // Short circuit if the peer is already fetching
  168. if !atomic.CompareAndSwapInt32(&p.stateIdle, 0, 1) {
  169. return errAlreadyFetching
  170. }
  171. p.stateStarted = time.Now()
  172. go p.peer.RequestNodeData(hashes)
  173. return nil
  174. }
  175. // SetHeadersIdle sets the peer to idle, allowing it to execute new header retrieval
  176. // requests. Its estimated header retrieval throughput is updated with that measured
  177. // just now.
  178. func (p *peerConnection) SetHeadersIdle(delivered int, deliveryTime time.Time) {
  179. p.setIdle(deliveryTime.Sub(p.headerStarted), delivered, &p.headerThroughput, &p.headerIdle)
  180. }
  181. // SetBodiesIdle sets the peer to idle, allowing it to execute block body retrieval
  182. // requests. Its estimated body retrieval throughput is updated with that measured
  183. // just now.
  184. func (p *peerConnection) SetBodiesIdle(delivered int, deliveryTime time.Time) {
  185. p.setIdle(deliveryTime.Sub(p.blockStarted), delivered, &p.blockThroughput, &p.blockIdle)
  186. }
  187. // SetReceiptsIdle sets the peer to idle, allowing it to execute new receipt
  188. // retrieval requests. Its estimated receipt retrieval throughput is updated
  189. // with that measured just now.
  190. func (p *peerConnection) SetReceiptsIdle(delivered int, deliveryTime time.Time) {
  191. p.setIdle(deliveryTime.Sub(p.receiptStarted), delivered, &p.receiptThroughput, &p.receiptIdle)
  192. }
  193. // SetNodeDataIdle sets the peer to idle, allowing it to execute new state trie
  194. // data retrieval requests. Its estimated state retrieval throughput is updated
  195. // with that measured just now.
  196. func (p *peerConnection) SetNodeDataIdle(delivered int, deliveryTime time.Time) {
  197. p.setIdle(deliveryTime.Sub(p.stateStarted), delivered, &p.stateThroughput, &p.stateIdle)
  198. }
  199. // setIdle sets the peer to idle, allowing it to execute new retrieval requests.
  200. // Its estimated retrieval throughput is updated with that measured just now.
  201. func (p *peerConnection) setIdle(elapsed time.Duration, delivered int, throughput *float64, idle *int32) {
  202. // Irrelevant of the scaling, make sure the peer ends up idle
  203. defer atomic.StoreInt32(idle, 0)
  204. p.lock.Lock()
  205. defer p.lock.Unlock()
  206. // If nothing was delivered (hard timeout / unavailable data), reduce throughput to minimum
  207. if delivered == 0 {
  208. *throughput = 0
  209. return
  210. }
  211. // Otherwise update the throughput with a new measurement
  212. if elapsed <= 0 {
  213. elapsed = 1 // +1 (ns) to ensure non-zero divisor
  214. }
  215. measured := float64(delivered) / (float64(elapsed) / float64(time.Second))
  216. *throughput = (1-measurementImpact)*(*throughput) + measurementImpact*measured
  217. p.rtt = time.Duration((1-measurementImpact)*float64(p.rtt) + measurementImpact*float64(elapsed))
  218. p.log.Trace("Peer throughput measurements updated",
  219. "hps", p.headerThroughput, "bps", p.blockThroughput,
  220. "rps", p.receiptThroughput, "sps", p.stateThroughput,
  221. "miss", len(p.lacking), "rtt", p.rtt)
  222. }
  223. // HeaderCapacity retrieves the peers header download allowance based on its
  224. // previously discovered throughput.
  225. func (p *peerConnection) HeaderCapacity(targetRTT time.Duration) int {
  226. p.lock.RLock()
  227. defer p.lock.RUnlock()
  228. return int(math.Min(1+math.Max(1, p.headerThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxHeaderFetch)))
  229. }
  230. // BlockCapacity retrieves the peers block download allowance based on its
  231. // previously discovered throughput.
  232. func (p *peerConnection) BlockCapacity(targetRTT time.Duration) int {
  233. p.lock.RLock()
  234. defer p.lock.RUnlock()
  235. return int(math.Min(1+math.Max(1, p.blockThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxBlockFetch)))
  236. }
  237. // ReceiptCapacity retrieves the peers receipt download allowance based on its
  238. // previously discovered throughput.
  239. func (p *peerConnection) ReceiptCapacity(targetRTT time.Duration) int {
  240. p.lock.RLock()
  241. defer p.lock.RUnlock()
  242. return int(math.Min(1+math.Max(1, p.receiptThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxReceiptFetch)))
  243. }
  244. // NodeDataCapacity retrieves the peers state download allowance based on its
  245. // previously discovered throughput.
  246. func (p *peerConnection) NodeDataCapacity(targetRTT time.Duration) int {
  247. p.lock.RLock()
  248. defer p.lock.RUnlock()
  249. return int(math.Min(1+math.Max(1, p.stateThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxStateFetch)))
  250. }
  251. // MarkLacking appends a new entity to the set of items (blocks, receipts, states)
  252. // that a peer is known not to have (i.e. have been requested before). If the
  253. // set reaches its maximum allowed capacity, items are randomly dropped off.
  254. func (p *peerConnection) MarkLacking(hash common.Hash) {
  255. p.lock.Lock()
  256. defer p.lock.Unlock()
  257. for len(p.lacking) >= maxLackingHashes {
  258. for drop := range p.lacking {
  259. delete(p.lacking, drop)
  260. break
  261. }
  262. }
  263. p.lacking[hash] = struct{}{}
  264. }
  265. // Lacks retrieves whether the hash of a blockchain item is on the peers lacking
  266. // list (i.e. whether we know that the peer does not have it).
  267. func (p *peerConnection) Lacks(hash common.Hash) bool {
  268. p.lock.RLock()
  269. defer p.lock.RUnlock()
  270. _, ok := p.lacking[hash]
  271. return ok
  272. }
  273. // peerSet represents the collection of active peer participating in the chain
  274. // download procedure.
  275. type peerSet struct {
  276. peers map[string]*peerConnection
  277. newPeerFeed event.Feed
  278. peerDropFeed event.Feed
  279. lock sync.RWMutex
  280. }
  281. // newPeerSet creates a new peer set top track the active download sources.
  282. func newPeerSet() *peerSet {
  283. return &peerSet{
  284. peers: make(map[string]*peerConnection),
  285. }
  286. }
  287. // SubscribeNewPeers subscribes to peer arrival events.
  288. func (ps *peerSet) SubscribeNewPeers(ch chan<- *peerConnection) event.Subscription {
  289. return ps.newPeerFeed.Subscribe(ch)
  290. }
  291. // SubscribePeerDrops subscribes to peer departure events.
  292. func (ps *peerSet) SubscribePeerDrops(ch chan<- *peerConnection) event.Subscription {
  293. return ps.peerDropFeed.Subscribe(ch)
  294. }
  295. // Reset iterates over the current peer set, and resets each of the known peers
  296. // to prepare for a next batch of block retrieval.
  297. func (ps *peerSet) Reset() {
  298. ps.lock.RLock()
  299. defer ps.lock.RUnlock()
  300. for _, peer := range ps.peers {
  301. peer.Reset()
  302. }
  303. }
  304. // Register injects a new peer into the working set, or returns an error if the
  305. // peer is already known.
  306. //
  307. // The method also sets the starting throughput values of the new peer to the
  308. // average of all existing peers, to give it a realistic chance of being used
  309. // for data retrievals.
  310. func (ps *peerSet) Register(p *peerConnection) error {
  311. // Retrieve the current median RTT as a sane default
  312. p.rtt = ps.medianRTT()
  313. // Register the new peer with some meaningful defaults
  314. ps.lock.Lock()
  315. if _, ok := ps.peers[p.id]; ok {
  316. ps.lock.Unlock()
  317. return errAlreadyRegistered
  318. }
  319. if len(ps.peers) > 0 {
  320. p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0
  321. for _, peer := range ps.peers {
  322. peer.lock.RLock()
  323. p.headerThroughput += peer.headerThroughput
  324. p.blockThroughput += peer.blockThroughput
  325. p.receiptThroughput += peer.receiptThroughput
  326. p.stateThroughput += peer.stateThroughput
  327. peer.lock.RUnlock()
  328. }
  329. p.headerThroughput /= float64(len(ps.peers))
  330. p.blockThroughput /= float64(len(ps.peers))
  331. p.receiptThroughput /= float64(len(ps.peers))
  332. p.stateThroughput /= float64(len(ps.peers))
  333. }
  334. ps.peers[p.id] = p
  335. ps.lock.Unlock()
  336. ps.newPeerFeed.Send(p)
  337. return nil
  338. }
  339. // Unregister removes a remote peer from the active set, disabling any further
  340. // actions to/from that particular entity.
  341. func (ps *peerSet) Unregister(id string) error {
  342. ps.lock.Lock()
  343. p, ok := ps.peers[id]
  344. if !ok {
  345. ps.lock.Unlock()
  346. return errNotRegistered
  347. }
  348. delete(ps.peers, id)
  349. ps.lock.Unlock()
  350. ps.peerDropFeed.Send(p)
  351. return nil
  352. }
  353. // Peer retrieves the registered peer with the given id.
  354. func (ps *peerSet) Peer(id string) *peerConnection {
  355. ps.lock.RLock()
  356. defer ps.lock.RUnlock()
  357. return ps.peers[id]
  358. }
  359. // Len returns if the current number of peers in the set.
  360. func (ps *peerSet) Len() int {
  361. ps.lock.RLock()
  362. defer ps.lock.RUnlock()
  363. return len(ps.peers)
  364. }
  365. // AllPeers retrieves a flat list of all the peers within the set.
  366. func (ps *peerSet) AllPeers() []*peerConnection {
  367. ps.lock.RLock()
  368. defer ps.lock.RUnlock()
  369. list := make([]*peerConnection, 0, len(ps.peers))
  370. for _, p := range ps.peers {
  371. list = append(list, p)
  372. }
  373. return list
  374. }
  375. // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers
  376. // within the active peer set, ordered by their reputation.
  377. func (ps *peerSet) HeaderIdlePeers() ([]*peerConnection, int) {
  378. idle := func(p *peerConnection) bool {
  379. return atomic.LoadInt32(&p.headerIdle) == 0
  380. }
  381. throughput := func(p *peerConnection) float64 {
  382. p.lock.RLock()
  383. defer p.lock.RUnlock()
  384. return p.headerThroughput
  385. }
  386. return ps.idlePeers(eth.ETH65, eth.ETH66, idle, throughput)
  387. }
  388. // BodyIdlePeers retrieves a flat list of all the currently body-idle peers within
  389. // the active peer set, ordered by their reputation.
  390. func (ps *peerSet) BodyIdlePeers() ([]*peerConnection, int) {
  391. idle := func(p *peerConnection) bool {
  392. return atomic.LoadInt32(&p.blockIdle) == 0
  393. }
  394. throughput := func(p *peerConnection) float64 {
  395. p.lock.RLock()
  396. defer p.lock.RUnlock()
  397. return p.blockThroughput
  398. }
  399. return ps.idlePeers(eth.ETH65, eth.ETH66, idle, throughput)
  400. }
  401. // ReceiptIdlePeers retrieves a flat list of all the currently receipt-idle peers
  402. // within the active peer set, ordered by their reputation.
  403. func (ps *peerSet) ReceiptIdlePeers() ([]*peerConnection, int) {
  404. idle := func(p *peerConnection) bool {
  405. return atomic.LoadInt32(&p.receiptIdle) == 0
  406. }
  407. throughput := func(p *peerConnection) float64 {
  408. p.lock.RLock()
  409. defer p.lock.RUnlock()
  410. return p.receiptThroughput
  411. }
  412. return ps.idlePeers(eth.ETH65, eth.ETH66, idle, throughput)
  413. }
  414. // NodeDataIdlePeers retrieves a flat list of all the currently node-data-idle
  415. // peers within the active peer set, ordered by their reputation.
  416. func (ps *peerSet) NodeDataIdlePeers() ([]*peerConnection, int) {
  417. idle := func(p *peerConnection) bool {
  418. return atomic.LoadInt32(&p.stateIdle) == 0
  419. }
  420. throughput := func(p *peerConnection) float64 {
  421. p.lock.RLock()
  422. defer p.lock.RUnlock()
  423. return p.stateThroughput
  424. }
  425. return ps.idlePeers(eth.ETH65, eth.ETH66, idle, throughput)
  426. }
  427. // idlePeers retrieves a flat list of all currently idle peers satisfying the
  428. // protocol version constraints, using the provided function to check idleness.
  429. // The resulting set of peers are sorted by their measure throughput.
  430. func (ps *peerSet) idlePeers(minProtocol, maxProtocol uint, idleCheck func(*peerConnection) bool, throughput func(*peerConnection) float64) ([]*peerConnection, int) {
  431. ps.lock.RLock()
  432. defer ps.lock.RUnlock()
  433. idle, total := make([]*peerConnection, 0, len(ps.peers)), 0
  434. tps := make([]float64, 0, len(ps.peers))
  435. for _, p := range ps.peers {
  436. if p.version >= minProtocol && p.version <= maxProtocol {
  437. if idleCheck(p) {
  438. idle = append(idle, p)
  439. tps = append(tps, throughput(p))
  440. }
  441. total++
  442. }
  443. }
  444. // And sort them
  445. sortPeers := &peerThroughputSort{idle, tps}
  446. sort.Sort(sortPeers)
  447. return sortPeers.p, total
  448. }
  449. // medianRTT returns the median RTT of the peerset, considering only the tuning
  450. // peers if there are more peers available.
  451. func (ps *peerSet) medianRTT() time.Duration {
  452. // Gather all the currently measured round trip times
  453. ps.lock.RLock()
  454. defer ps.lock.RUnlock()
  455. rtts := make([]float64, 0, len(ps.peers))
  456. for _, p := range ps.peers {
  457. p.lock.RLock()
  458. rtts = append(rtts, float64(p.rtt))
  459. p.lock.RUnlock()
  460. }
  461. sort.Float64s(rtts)
  462. median := rttMaxEstimate
  463. if qosTuningPeers <= len(rtts) {
  464. median = time.Duration(rtts[qosTuningPeers/2]) // Median of our tuning peers
  465. } else if len(rtts) > 0 {
  466. median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos)
  467. }
  468. // Restrict the RTT into some QoS defaults, irrelevant of true RTT
  469. if median < rttMinEstimate {
  470. median = rttMinEstimate
  471. }
  472. if median > rttMaxEstimate {
  473. median = rttMaxEstimate
  474. }
  475. return median
  476. }
  477. // peerThroughputSort implements the Sort interface, and allows for
  478. // sorting a set of peers by their throughput
  479. // The sorted data is with the _highest_ throughput first
  480. type peerThroughputSort struct {
  481. p []*peerConnection
  482. tp []float64
  483. }
  484. func (ps *peerThroughputSort) Len() int {
  485. return len(ps.p)
  486. }
  487. func (ps *peerThroughputSort) Less(i, j int) bool {
  488. return ps.tp[i] > ps.tp[j]
  489. }
  490. func (ps *peerThroughputSort) Swap(i, j int) {
  491. ps.p[i], ps.p[j] = ps.p[j], ps.p[i]
  492. ps.tp[i], ps.tp[j] = ps.tp[j], ps.tp[i]
  493. }