peer.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. "fmt"
  22. "math"
  23. "math/big"
  24. "sort"
  25. "sync"
  26. "sync/atomic"
  27. "time"
  28. "github.com/ethereum/go-ethereum/common"
  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. // Head hash and total difficulty retriever for
  37. type currentHeadRetrievalFn func() (common.Hash, *big.Int)
  38. // Block header and body fetchers belonging to eth/62 and above
  39. type relativeHeaderFetcherFn func(common.Hash, int, int, bool) error
  40. type absoluteHeaderFetcherFn func(uint64, int, int, bool) error
  41. type blockBodyFetcherFn func([]common.Hash) error
  42. type receiptFetcherFn func([]common.Hash) error
  43. type stateFetcherFn func([]common.Hash) error
  44. var (
  45. errAlreadyFetching = errors.New("already fetching blocks from peer")
  46. errAlreadyRegistered = errors.New("peer is already registered")
  47. errNotRegistered = errors.New("peer is not registered")
  48. )
  49. // peer represents an active peer from which hashes and blocks are retrieved.
  50. type peer struct {
  51. id string // Unique identifier of the peer
  52. headerIdle int32 // Current header activity state of the peer (idle = 0, active = 1)
  53. blockIdle int32 // Current block activity state of the peer (idle = 0, active = 1)
  54. receiptIdle int32 // Current receipt activity state of the peer (idle = 0, active = 1)
  55. stateIdle int32 // Current node data activity state of the peer (idle = 0, active = 1)
  56. headerThroughput float64 // Number of headers measured to be retrievable per second
  57. blockThroughput float64 // Number of blocks (bodies) measured to be retrievable per second
  58. receiptThroughput float64 // Number of receipts measured to be retrievable per second
  59. stateThroughput float64 // Number of node data pieces measured to be retrievable per second
  60. rtt time.Duration // Request round trip time to track responsiveness (QoS)
  61. headerStarted time.Time // Time instance when the last header fetch was started
  62. blockStarted time.Time // Time instance when the last block (body) fetch was started
  63. receiptStarted time.Time // Time instance when the last receipt fetch was started
  64. stateStarted time.Time // Time instance when the last node data fetch was started
  65. lacking map[common.Hash]struct{} // Set of hashes not to request (didn't have previously)
  66. currentHead currentHeadRetrievalFn // Method to fetch the currently known head of the peer
  67. getRelHeaders relativeHeaderFetcherFn // [eth/62] Method to retrieve a batch of headers from an origin hash
  68. getAbsHeaders absoluteHeaderFetcherFn // [eth/62] Method to retrieve a batch of headers from an absolute position
  69. getBlockBodies blockBodyFetcherFn // [eth/62] Method to retrieve a batch of block bodies
  70. getReceipts receiptFetcherFn // [eth/63] Method to retrieve a batch of block transaction receipts
  71. getNodeData stateFetcherFn // [eth/63] Method to retrieve a batch of state trie data
  72. version int // Eth protocol version number to switch strategies
  73. log log.Logger // Contextual logger to add extra infos to peer logs
  74. lock sync.RWMutex
  75. }
  76. // newPeer create a new downloader peer, with specific hash and block retrieval
  77. // mechanisms.
  78. func newPeer(id string, version int, currentHead currentHeadRetrievalFn,
  79. getRelHeaders relativeHeaderFetcherFn, getAbsHeaders absoluteHeaderFetcherFn, getBlockBodies blockBodyFetcherFn,
  80. getReceipts receiptFetcherFn, getNodeData stateFetcherFn, logger log.Logger) *peer {
  81. return &peer{
  82. id: id,
  83. lacking: make(map[common.Hash]struct{}),
  84. currentHead: currentHead,
  85. getRelHeaders: getRelHeaders,
  86. getAbsHeaders: getAbsHeaders,
  87. getBlockBodies: getBlockBodies,
  88. getReceipts: getReceipts,
  89. getNodeData: getNodeData,
  90. version: version,
  91. log: logger,
  92. }
  93. }
  94. // Reset clears the internal state of a peer entity.
  95. func (p *peer) Reset() {
  96. p.lock.Lock()
  97. defer p.lock.Unlock()
  98. atomic.StoreInt32(&p.headerIdle, 0)
  99. atomic.StoreInt32(&p.blockIdle, 0)
  100. atomic.StoreInt32(&p.receiptIdle, 0)
  101. atomic.StoreInt32(&p.stateIdle, 0)
  102. p.headerThroughput = 0
  103. p.blockThroughput = 0
  104. p.receiptThroughput = 0
  105. p.stateThroughput = 0
  106. p.lacking = make(map[common.Hash]struct{})
  107. }
  108. // FetchHeaders sends a header retrieval request to the remote peer.
  109. func (p *peer) FetchHeaders(from uint64, count int) error {
  110. // Sanity check the protocol version
  111. if p.version < 62 {
  112. panic(fmt.Sprintf("header fetch [eth/62+] requested on eth/%d", p.version))
  113. }
  114. // Short circuit if the peer is already fetching
  115. if !atomic.CompareAndSwapInt32(&p.headerIdle, 0, 1) {
  116. return errAlreadyFetching
  117. }
  118. p.headerStarted = time.Now()
  119. // Issue the header retrieval request (absolut upwards without gaps)
  120. go p.getAbsHeaders(from, count, 0, false)
  121. return nil
  122. }
  123. // FetchBodies sends a block body retrieval request to the remote peer.
  124. func (p *peer) FetchBodies(request *fetchRequest) error {
  125. // Sanity check the protocol version
  126. if p.version < 62 {
  127. panic(fmt.Sprintf("body fetch [eth/62+] requested on eth/%d", p.version))
  128. }
  129. // Short circuit if the peer is already fetching
  130. if !atomic.CompareAndSwapInt32(&p.blockIdle, 0, 1) {
  131. return errAlreadyFetching
  132. }
  133. p.blockStarted = time.Now()
  134. // Convert the header set to a retrievable slice
  135. hashes := make([]common.Hash, 0, len(request.Headers))
  136. for _, header := range request.Headers {
  137. hashes = append(hashes, header.Hash())
  138. }
  139. go p.getBlockBodies(hashes)
  140. return nil
  141. }
  142. // FetchReceipts sends a receipt retrieval request to the remote peer.
  143. func (p *peer) FetchReceipts(request *fetchRequest) error {
  144. // Sanity check the protocol version
  145. if p.version < 63 {
  146. panic(fmt.Sprintf("body fetch [eth/63+] requested on eth/%d", p.version))
  147. }
  148. // Short circuit if the peer is already fetching
  149. if !atomic.CompareAndSwapInt32(&p.receiptIdle, 0, 1) {
  150. return errAlreadyFetching
  151. }
  152. p.receiptStarted = time.Now()
  153. // Convert the header set to a retrievable slice
  154. hashes := make([]common.Hash, 0, len(request.Headers))
  155. for _, header := range request.Headers {
  156. hashes = append(hashes, header.Hash())
  157. }
  158. go p.getReceipts(hashes)
  159. return nil
  160. }
  161. // FetchNodeData sends a node state data retrieval request to the remote peer.
  162. func (p *peer) FetchNodeData(hashes []common.Hash) error {
  163. // Sanity check the protocol version
  164. if p.version < 63 {
  165. panic(fmt.Sprintf("node data fetch [eth/63+] requested on eth/%d", p.version))
  166. }
  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.getNodeData(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 *peer) SetHeadersIdle(delivered int) {
  179. p.setIdle(p.headerStarted, delivered, &p.headerThroughput, &p.headerIdle)
  180. }
  181. // SetBlocksIdle sets the peer to idle, allowing it to execute new block retrieval
  182. // requests. Its estimated block retrieval throughput is updated with that measured
  183. // just now.
  184. func (p *peer) SetBlocksIdle(delivered int) {
  185. p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle)
  186. }
  187. // SetBodiesIdle sets the peer to idle, allowing it to execute block body retrieval
  188. // requests. Its estimated body retrieval throughput is updated with that measured
  189. // just now.
  190. func (p *peer) SetBodiesIdle(delivered int) {
  191. p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle)
  192. }
  193. // SetReceiptsIdle sets the peer to idle, allowing it to execute new receipt
  194. // retrieval requests. Its estimated receipt retrieval throughput is updated
  195. // with that measured just now.
  196. func (p *peer) SetReceiptsIdle(delivered int) {
  197. p.setIdle(p.receiptStarted, delivered, &p.receiptThroughput, &p.receiptIdle)
  198. }
  199. // SetNodeDataIdle sets the peer to idle, allowing it to execute new state trie
  200. // data retrieval requests. Its estimated state retrieval throughput is updated
  201. // with that measured just now.
  202. func (p *peer) SetNodeDataIdle(delivered int) {
  203. p.setIdle(p.stateStarted, delivered, &p.stateThroughput, &p.stateIdle)
  204. }
  205. // setIdle sets the peer to idle, allowing it to execute new retrieval requests.
  206. // Its estimated retrieval throughput is updated with that measured just now.
  207. func (p *peer) setIdle(started time.Time, delivered int, throughput *float64, idle *int32) {
  208. // Irrelevant of the scaling, make sure the peer ends up idle
  209. defer atomic.StoreInt32(idle, 0)
  210. p.lock.Lock()
  211. defer p.lock.Unlock()
  212. // If nothing was delivered (hard timeout / unavailable data), reduce throughput to minimum
  213. if delivered == 0 {
  214. *throughput = 0
  215. return
  216. }
  217. // Otherwise update the throughput with a new measurement
  218. elapsed := time.Since(started) + 1 // +1 (ns) to ensure non-zero divisor
  219. measured := float64(delivered) / (float64(elapsed) / float64(time.Second))
  220. *throughput = (1-measurementImpact)*(*throughput) + measurementImpact*measured
  221. p.rtt = time.Duration((1-measurementImpact)*float64(p.rtt) + measurementImpact*float64(elapsed))
  222. p.log.Trace("Peer throughput measurements updated",
  223. "hps", p.headerThroughput, "bps", p.blockThroughput,
  224. "rps", p.receiptThroughput, "sps", p.stateThroughput,
  225. "miss", len(p.lacking), "rtt", p.rtt)
  226. }
  227. // HeaderCapacity retrieves the peers header download allowance based on its
  228. // previously discovered throughput.
  229. func (p *peer) HeaderCapacity(targetRTT time.Duration) int {
  230. p.lock.RLock()
  231. defer p.lock.RUnlock()
  232. return int(math.Min(1+math.Max(1, p.headerThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxHeaderFetch)))
  233. }
  234. // BlockCapacity retrieves the peers block download allowance based on its
  235. // previously discovered throughput.
  236. func (p *peer) BlockCapacity(targetRTT time.Duration) int {
  237. p.lock.RLock()
  238. defer p.lock.RUnlock()
  239. return int(math.Min(1+math.Max(1, p.blockThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxBlockFetch)))
  240. }
  241. // ReceiptCapacity retrieves the peers receipt download allowance based on its
  242. // previously discovered throughput.
  243. func (p *peer) ReceiptCapacity(targetRTT time.Duration) int {
  244. p.lock.RLock()
  245. defer p.lock.RUnlock()
  246. return int(math.Min(1+math.Max(1, p.receiptThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxReceiptFetch)))
  247. }
  248. // NodeDataCapacity retrieves the peers state download allowance based on its
  249. // previously discovered throughput.
  250. func (p *peer) NodeDataCapacity(targetRTT time.Duration) int {
  251. p.lock.RLock()
  252. defer p.lock.RUnlock()
  253. return int(math.Min(1+math.Max(1, p.stateThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxStateFetch)))
  254. }
  255. // MarkLacking appends a new entity to the set of items (blocks, receipts, states)
  256. // that a peer is known not to have (i.e. have been requested before). If the
  257. // set reaches its maximum allowed capacity, items are randomly dropped off.
  258. func (p *peer) MarkLacking(hash common.Hash) {
  259. p.lock.Lock()
  260. defer p.lock.Unlock()
  261. for len(p.lacking) >= maxLackingHashes {
  262. for drop := range p.lacking {
  263. delete(p.lacking, drop)
  264. break
  265. }
  266. }
  267. p.lacking[hash] = struct{}{}
  268. }
  269. // Lacks retrieves whether the hash of a blockchain item is on the peers lacking
  270. // list (i.e. whether we know that the peer does not have it).
  271. func (p *peer) Lacks(hash common.Hash) bool {
  272. p.lock.RLock()
  273. defer p.lock.RUnlock()
  274. _, ok := p.lacking[hash]
  275. return ok
  276. }
  277. // peerSet represents the collection of active peer participating in the chain
  278. // download procedure.
  279. type peerSet struct {
  280. peers map[string]*peer
  281. newPeerFeed event.Feed
  282. lock sync.RWMutex
  283. }
  284. // newPeerSet creates a new peer set top track the active download sources.
  285. func newPeerSet() *peerSet {
  286. return &peerSet{
  287. peers: make(map[string]*peer),
  288. }
  289. }
  290. func (ps *peerSet) SubscribeNewPeers(ch chan<- *peer) event.Subscription {
  291. return ps.newPeerFeed.Subscribe(ch)
  292. }
  293. // Reset iterates over the current peer set, and resets each of the known peers
  294. // to prepare for a next batch of block retrieval.
  295. func (ps *peerSet) Reset() {
  296. ps.lock.RLock()
  297. defer ps.lock.RUnlock()
  298. for _, peer := range ps.peers {
  299. peer.Reset()
  300. }
  301. }
  302. // Register injects a new peer into the working set, or returns an error if the
  303. // peer is already known.
  304. //
  305. // The method also sets the starting throughput values of the new peer to the
  306. // average of all existing peers, to give it a realistic chance of being used
  307. // for data retrievals.
  308. func (ps *peerSet) Register(p *peer) error {
  309. // Retrieve the current median RTT as a sane default
  310. p.rtt = ps.medianRTT()
  311. // Register the new peer with some meaningful defaults
  312. ps.lock.Lock()
  313. if _, ok := ps.peers[p.id]; ok {
  314. ps.lock.Unlock()
  315. return errAlreadyRegistered
  316. }
  317. if len(ps.peers) > 0 {
  318. p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0
  319. for _, peer := range ps.peers {
  320. peer.lock.RLock()
  321. p.headerThroughput += peer.headerThroughput
  322. p.blockThroughput += peer.blockThroughput
  323. p.receiptThroughput += peer.receiptThroughput
  324. p.stateThroughput += peer.stateThroughput
  325. peer.lock.RUnlock()
  326. }
  327. p.headerThroughput /= float64(len(ps.peers))
  328. p.blockThroughput /= float64(len(ps.peers))
  329. p.receiptThroughput /= float64(len(ps.peers))
  330. p.stateThroughput /= float64(len(ps.peers))
  331. }
  332. ps.peers[p.id] = p
  333. ps.lock.Unlock()
  334. ps.newPeerFeed.Send(p)
  335. return nil
  336. }
  337. // Unregister removes a remote peer from the active set, disabling any further
  338. // actions to/from that particular entity.
  339. func (ps *peerSet) Unregister(id string) error {
  340. ps.lock.Lock()
  341. defer ps.lock.Unlock()
  342. if _, ok := ps.peers[id]; !ok {
  343. return errNotRegistered
  344. }
  345. delete(ps.peers, id)
  346. return nil
  347. }
  348. // Peer retrieves the registered peer with the given id.
  349. func (ps *peerSet) Peer(id string) *peer {
  350. ps.lock.RLock()
  351. defer ps.lock.RUnlock()
  352. return ps.peers[id]
  353. }
  354. // Len returns if the current number of peers in the set.
  355. func (ps *peerSet) Len() int {
  356. ps.lock.RLock()
  357. defer ps.lock.RUnlock()
  358. return len(ps.peers)
  359. }
  360. // AllPeers retrieves a flat list of all the peers within the set.
  361. func (ps *peerSet) AllPeers() []*peer {
  362. ps.lock.RLock()
  363. defer ps.lock.RUnlock()
  364. list := make([]*peer, 0, len(ps.peers))
  365. for _, p := range ps.peers {
  366. list = append(list, p)
  367. }
  368. return list
  369. }
  370. // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers
  371. // within the active peer set, ordered by their reputation.
  372. func (ps *peerSet) HeaderIdlePeers() ([]*peer, int) {
  373. idle := func(p *peer) bool {
  374. return atomic.LoadInt32(&p.headerIdle) == 0
  375. }
  376. throughput := func(p *peer) float64 {
  377. p.lock.RLock()
  378. defer p.lock.RUnlock()
  379. return p.headerThroughput
  380. }
  381. return ps.idlePeers(62, 64, idle, throughput)
  382. }
  383. // BodyIdlePeers retrieves a flat list of all the currently body-idle peers within
  384. // the active peer set, ordered by their reputation.
  385. func (ps *peerSet) BodyIdlePeers() ([]*peer, int) {
  386. idle := func(p *peer) bool {
  387. return atomic.LoadInt32(&p.blockIdle) == 0
  388. }
  389. throughput := func(p *peer) float64 {
  390. p.lock.RLock()
  391. defer p.lock.RUnlock()
  392. return p.blockThroughput
  393. }
  394. return ps.idlePeers(62, 64, idle, throughput)
  395. }
  396. // ReceiptIdlePeers retrieves a flat list of all the currently receipt-idle peers
  397. // within the active peer set, ordered by their reputation.
  398. func (ps *peerSet) ReceiptIdlePeers() ([]*peer, int) {
  399. idle := func(p *peer) bool {
  400. return atomic.LoadInt32(&p.receiptIdle) == 0
  401. }
  402. throughput := func(p *peer) float64 {
  403. p.lock.RLock()
  404. defer p.lock.RUnlock()
  405. return p.receiptThroughput
  406. }
  407. return ps.idlePeers(63, 64, idle, throughput)
  408. }
  409. // NodeDataIdlePeers retrieves a flat list of all the currently node-data-idle
  410. // peers within the active peer set, ordered by their reputation.
  411. func (ps *peerSet) NodeDataIdlePeers() ([]*peer, int) {
  412. idle := func(p *peer) bool {
  413. return atomic.LoadInt32(&p.stateIdle) == 0
  414. }
  415. throughput := func(p *peer) float64 {
  416. p.lock.RLock()
  417. defer p.lock.RUnlock()
  418. return p.stateThroughput
  419. }
  420. return ps.idlePeers(63, 64, idle, throughput)
  421. }
  422. // idlePeers retrieves a flat list of all currently idle peers satisfying the
  423. // protocol version constraints, using the provided function to check idleness.
  424. // The resulting set of peers are sorted by their measure throughput.
  425. func (ps *peerSet) idlePeers(minProtocol, maxProtocol int, idleCheck func(*peer) bool, throughput func(*peer) float64) ([]*peer, int) {
  426. ps.lock.RLock()
  427. defer ps.lock.RUnlock()
  428. idle, total := make([]*peer, 0, len(ps.peers)), 0
  429. for _, p := range ps.peers {
  430. if p.version >= minProtocol && p.version <= maxProtocol {
  431. if idleCheck(p) {
  432. idle = append(idle, p)
  433. }
  434. total++
  435. }
  436. }
  437. for i := 0; i < len(idle); i++ {
  438. for j := i + 1; j < len(idle); j++ {
  439. if throughput(idle[i]) < throughput(idle[j]) {
  440. idle[i], idle[j] = idle[j], idle[i]
  441. }
  442. }
  443. }
  444. return idle, total
  445. }
  446. // medianRTT returns the median RTT of te peerset, considering only the tuning
  447. // peers if there are more peers available.
  448. func (ps *peerSet) medianRTT() time.Duration {
  449. // Gather all the currnetly measured round trip times
  450. ps.lock.RLock()
  451. defer ps.lock.RUnlock()
  452. rtts := make([]float64, 0, len(ps.peers))
  453. for _, p := range ps.peers {
  454. p.lock.RLock()
  455. rtts = append(rtts, float64(p.rtt))
  456. p.lock.RUnlock()
  457. }
  458. sort.Float64s(rtts)
  459. median := rttMaxEstimate
  460. if qosTuningPeers <= len(rtts) {
  461. median = time.Duration(rtts[qosTuningPeers/2]) // Median of our tuning peers
  462. } else if len(rtts) > 0 {
  463. median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos)
  464. }
  465. // Restrict the RTT into some QoS defaults, irrelevant of true RTT
  466. if median < rttMinEstimate {
  467. median = rttMinEstimate
  468. }
  469. if median > rttMaxEstimate {
  470. median = rttMaxEstimate
  471. }
  472. return median
  473. }