peer.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. 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 int // 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 int, 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. // Sanity check the protocol version
  123. if p.version < 62 {
  124. panic(fmt.Sprintf("header fetch [eth/62+] requested on eth/%d", p.version))
  125. }
  126. // Short circuit if the peer is already fetching
  127. if !atomic.CompareAndSwapInt32(&p.headerIdle, 0, 1) {
  128. return errAlreadyFetching
  129. }
  130. p.headerStarted = time.Now()
  131. // Issue the header retrieval request (absolute upwards without gaps)
  132. go p.peer.RequestHeadersByNumber(from, count, 0, false)
  133. return nil
  134. }
  135. // FetchBodies sends a block body retrieval request to the remote peer.
  136. func (p *peerConnection) FetchBodies(request *fetchRequest) error {
  137. // Sanity check the protocol version
  138. if p.version < 62 {
  139. panic(fmt.Sprintf("body fetch [eth/62+] requested on eth/%d", p.version))
  140. }
  141. // Short circuit if the peer is already fetching
  142. if !atomic.CompareAndSwapInt32(&p.blockIdle, 0, 1) {
  143. return errAlreadyFetching
  144. }
  145. p.blockStarted = time.Now()
  146. // Convert the header set to a retrievable slice
  147. hashes := make([]common.Hash, 0, len(request.Headers))
  148. for _, header := range request.Headers {
  149. hashes = append(hashes, header.Hash())
  150. }
  151. go p.peer.RequestBodies(hashes)
  152. return nil
  153. }
  154. // FetchReceipts sends a receipt retrieval request to the remote peer.
  155. func (p *peerConnection) FetchReceipts(request *fetchRequest) error {
  156. // Sanity check the protocol version
  157. if p.version < 63 {
  158. panic(fmt.Sprintf("body fetch [eth/63+] requested on eth/%d", p.version))
  159. }
  160. // Short circuit if the peer is already fetching
  161. if !atomic.CompareAndSwapInt32(&p.receiptIdle, 0, 1) {
  162. return errAlreadyFetching
  163. }
  164. p.receiptStarted = time.Now()
  165. // Convert the header set to a retrievable slice
  166. hashes := make([]common.Hash, 0, len(request.Headers))
  167. for _, header := range request.Headers {
  168. hashes = append(hashes, header.Hash())
  169. }
  170. go p.peer.RequestReceipts(hashes)
  171. return nil
  172. }
  173. // FetchNodeData sends a node state data retrieval request to the remote peer.
  174. func (p *peerConnection) FetchNodeData(hashes []common.Hash) error {
  175. // Sanity check the protocol version
  176. if p.version < 63 {
  177. panic(fmt.Sprintf("node data fetch [eth/63+] requested on eth/%d", p.version))
  178. }
  179. // Short circuit if the peer is already fetching
  180. if !atomic.CompareAndSwapInt32(&p.stateIdle, 0, 1) {
  181. return errAlreadyFetching
  182. }
  183. p.stateStarted = time.Now()
  184. go p.peer.RequestNodeData(hashes)
  185. return nil
  186. }
  187. // SetHeadersIdle sets the peer to idle, allowing it to execute new header retrieval
  188. // requests. Its estimated header retrieval throughput is updated with that measured
  189. // just now.
  190. func (p *peerConnection) SetHeadersIdle(delivered int) {
  191. p.setIdle(p.headerStarted, delivered, &p.headerThroughput, &p.headerIdle)
  192. }
  193. // SetBodiesIdle sets the peer to idle, allowing it to execute block body retrieval
  194. // requests. Its estimated body retrieval throughput is updated with that measured
  195. // just now.
  196. func (p *peerConnection) SetBodiesIdle(delivered int) {
  197. p.setIdle(p.blockStarted, delivered, &p.blockThroughput, &p.blockIdle)
  198. }
  199. // SetReceiptsIdle sets the peer to idle, allowing it to execute new receipt
  200. // retrieval requests. Its estimated receipt retrieval throughput is updated
  201. // with that measured just now.
  202. func (p *peerConnection) SetReceiptsIdle(delivered int) {
  203. p.setIdle(p.receiptStarted, delivered, &p.receiptThroughput, &p.receiptIdle)
  204. }
  205. // SetNodeDataIdle sets the peer to idle, allowing it to execute new state trie
  206. // data retrieval requests. Its estimated state retrieval throughput is updated
  207. // with that measured just now.
  208. func (p *peerConnection) SetNodeDataIdle(delivered int) {
  209. p.setIdle(p.stateStarted, delivered, &p.stateThroughput, &p.stateIdle)
  210. }
  211. // setIdle sets the peer to idle, allowing it to execute new retrieval requests.
  212. // Its estimated retrieval throughput is updated with that measured just now.
  213. func (p *peerConnection) setIdle(started time.Time, delivered int, throughput *float64, idle *int32) {
  214. // Irrelevant of the scaling, make sure the peer ends up idle
  215. defer atomic.StoreInt32(idle, 0)
  216. p.lock.Lock()
  217. defer p.lock.Unlock()
  218. // If nothing was delivered (hard timeout / unavailable data), reduce throughput to minimum
  219. if delivered == 0 {
  220. *throughput = 0
  221. return
  222. }
  223. // Otherwise update the throughput with a new measurement
  224. elapsed := time.Since(started) + 1 // +1 (ns) to ensure non-zero divisor
  225. measured := float64(delivered) / (float64(elapsed) / float64(time.Second))
  226. *throughput = (1-measurementImpact)*(*throughput) + measurementImpact*measured
  227. p.rtt = time.Duration((1-measurementImpact)*float64(p.rtt) + measurementImpact*float64(elapsed))
  228. p.log.Trace("Peer throughput measurements updated",
  229. "hps", p.headerThroughput, "bps", p.blockThroughput,
  230. "rps", p.receiptThroughput, "sps", p.stateThroughput,
  231. "miss", len(p.lacking), "rtt", p.rtt)
  232. }
  233. // HeaderCapacity retrieves the peers header download allowance based on its
  234. // previously discovered throughput.
  235. func (p *peerConnection) HeaderCapacity(targetRTT time.Duration) int {
  236. p.lock.RLock()
  237. defer p.lock.RUnlock()
  238. return int(math.Min(1+math.Max(1, p.headerThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxHeaderFetch)))
  239. }
  240. // BlockCapacity retrieves the peers block download allowance based on its
  241. // previously discovered throughput.
  242. func (p *peerConnection) BlockCapacity(targetRTT time.Duration) int {
  243. p.lock.RLock()
  244. defer p.lock.RUnlock()
  245. return int(math.Min(1+math.Max(1, p.blockThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxBlockFetch)))
  246. }
  247. // ReceiptCapacity retrieves the peers receipt download allowance based on its
  248. // previously discovered throughput.
  249. func (p *peerConnection) ReceiptCapacity(targetRTT time.Duration) int {
  250. p.lock.RLock()
  251. defer p.lock.RUnlock()
  252. return int(math.Min(1+math.Max(1, p.receiptThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxReceiptFetch)))
  253. }
  254. // NodeDataCapacity retrieves the peers state download allowance based on its
  255. // previously discovered throughput.
  256. func (p *peerConnection) NodeDataCapacity(targetRTT time.Duration) int {
  257. p.lock.RLock()
  258. defer p.lock.RUnlock()
  259. return int(math.Min(1+math.Max(1, p.stateThroughput*float64(targetRTT)/float64(time.Second)), float64(MaxStateFetch)))
  260. }
  261. // MarkLacking appends a new entity to the set of items (blocks, receipts, states)
  262. // that a peer is known not to have (i.e. have been requested before). If the
  263. // set reaches its maximum allowed capacity, items are randomly dropped off.
  264. func (p *peerConnection) MarkLacking(hash common.Hash) {
  265. p.lock.Lock()
  266. defer p.lock.Unlock()
  267. for len(p.lacking) >= maxLackingHashes {
  268. for drop := range p.lacking {
  269. delete(p.lacking, drop)
  270. break
  271. }
  272. }
  273. p.lacking[hash] = struct{}{}
  274. }
  275. // Lacks retrieves whether the hash of a blockchain item is on the peers lacking
  276. // list (i.e. whether we know that the peer does not have it).
  277. func (p *peerConnection) Lacks(hash common.Hash) bool {
  278. p.lock.RLock()
  279. defer p.lock.RUnlock()
  280. _, ok := p.lacking[hash]
  281. return ok
  282. }
  283. // peerSet represents the collection of active peer participating in the chain
  284. // download procedure.
  285. type peerSet struct {
  286. peers map[string]*peerConnection
  287. newPeerFeed event.Feed
  288. peerDropFeed event.Feed
  289. lock sync.RWMutex
  290. }
  291. // newPeerSet creates a new peer set top track the active download sources.
  292. func newPeerSet() *peerSet {
  293. return &peerSet{
  294. peers: make(map[string]*peerConnection),
  295. }
  296. }
  297. // SubscribeNewPeers subscribes to peer arrival events.
  298. func (ps *peerSet) SubscribeNewPeers(ch chan<- *peerConnection) event.Subscription {
  299. return ps.newPeerFeed.Subscribe(ch)
  300. }
  301. // SubscribePeerDrops subscribes to peer departure events.
  302. func (ps *peerSet) SubscribePeerDrops(ch chan<- *peerConnection) event.Subscription {
  303. return ps.peerDropFeed.Subscribe(ch)
  304. }
  305. // Reset iterates over the current peer set, and resets each of the known peers
  306. // to prepare for a next batch of block retrieval.
  307. func (ps *peerSet) Reset() {
  308. ps.lock.RLock()
  309. defer ps.lock.RUnlock()
  310. for _, peer := range ps.peers {
  311. peer.Reset()
  312. }
  313. }
  314. // Register injects a new peer into the working set, or returns an error if the
  315. // peer is already known.
  316. //
  317. // The method also sets the starting throughput values of the new peer to the
  318. // average of all existing peers, to give it a realistic chance of being used
  319. // for data retrievals.
  320. func (ps *peerSet) Register(p *peerConnection) error {
  321. // Retrieve the current median RTT as a sane default
  322. p.rtt = ps.medianRTT()
  323. // Register the new peer with some meaningful defaults
  324. ps.lock.Lock()
  325. if _, ok := ps.peers[p.id]; ok {
  326. ps.lock.Unlock()
  327. return errAlreadyRegistered
  328. }
  329. if len(ps.peers) > 0 {
  330. p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0
  331. for _, peer := range ps.peers {
  332. peer.lock.RLock()
  333. p.headerThroughput += peer.headerThroughput
  334. p.blockThroughput += peer.blockThroughput
  335. p.receiptThroughput += peer.receiptThroughput
  336. p.stateThroughput += peer.stateThroughput
  337. peer.lock.RUnlock()
  338. }
  339. p.headerThroughput /= float64(len(ps.peers))
  340. p.blockThroughput /= float64(len(ps.peers))
  341. p.receiptThroughput /= float64(len(ps.peers))
  342. p.stateThroughput /= float64(len(ps.peers))
  343. }
  344. ps.peers[p.id] = p
  345. ps.lock.Unlock()
  346. ps.newPeerFeed.Send(p)
  347. return nil
  348. }
  349. // Unregister removes a remote peer from the active set, disabling any further
  350. // actions to/from that particular entity.
  351. func (ps *peerSet) Unregister(id string) error {
  352. ps.lock.Lock()
  353. p, ok := ps.peers[id]
  354. if !ok {
  355. defer ps.lock.Unlock()
  356. return errNotRegistered
  357. }
  358. delete(ps.peers, id)
  359. ps.lock.Unlock()
  360. ps.peerDropFeed.Send(p)
  361. return nil
  362. }
  363. // Peer retrieves the registered peer with the given id.
  364. func (ps *peerSet) Peer(id string) *peerConnection {
  365. ps.lock.RLock()
  366. defer ps.lock.RUnlock()
  367. return ps.peers[id]
  368. }
  369. // Len returns if the current number of peers in the set.
  370. func (ps *peerSet) Len() int {
  371. ps.lock.RLock()
  372. defer ps.lock.RUnlock()
  373. return len(ps.peers)
  374. }
  375. // AllPeers retrieves a flat list of all the peers within the set.
  376. func (ps *peerSet) AllPeers() []*peerConnection {
  377. ps.lock.RLock()
  378. defer ps.lock.RUnlock()
  379. list := make([]*peerConnection, 0, len(ps.peers))
  380. for _, p := range ps.peers {
  381. list = append(list, p)
  382. }
  383. return list
  384. }
  385. // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers
  386. // within the active peer set, ordered by their reputation.
  387. func (ps *peerSet) HeaderIdlePeers() ([]*peerConnection, int) {
  388. idle := func(p *peerConnection) bool {
  389. return atomic.LoadInt32(&p.headerIdle) == 0
  390. }
  391. throughput := func(p *peerConnection) float64 {
  392. p.lock.RLock()
  393. defer p.lock.RUnlock()
  394. return p.headerThroughput
  395. }
  396. return ps.idlePeers(62, 65, idle, throughput)
  397. }
  398. // BodyIdlePeers retrieves a flat list of all the currently body-idle peers within
  399. // the active peer set, ordered by their reputation.
  400. func (ps *peerSet) BodyIdlePeers() ([]*peerConnection, int) {
  401. idle := func(p *peerConnection) bool {
  402. return atomic.LoadInt32(&p.blockIdle) == 0
  403. }
  404. throughput := func(p *peerConnection) float64 {
  405. p.lock.RLock()
  406. defer p.lock.RUnlock()
  407. return p.blockThroughput
  408. }
  409. return ps.idlePeers(62, 65, idle, throughput)
  410. }
  411. // ReceiptIdlePeers retrieves a flat list of all the currently receipt-idle peers
  412. // within the active peer set, ordered by their reputation.
  413. func (ps *peerSet) ReceiptIdlePeers() ([]*peerConnection, int) {
  414. idle := func(p *peerConnection) bool {
  415. return atomic.LoadInt32(&p.receiptIdle) == 0
  416. }
  417. throughput := func(p *peerConnection) float64 {
  418. p.lock.RLock()
  419. defer p.lock.RUnlock()
  420. return p.receiptThroughput
  421. }
  422. return ps.idlePeers(63, 65, idle, throughput)
  423. }
  424. // NodeDataIdlePeers retrieves a flat list of all the currently node-data-idle
  425. // peers within the active peer set, ordered by their reputation.
  426. func (ps *peerSet) NodeDataIdlePeers() ([]*peerConnection, int) {
  427. idle := func(p *peerConnection) bool {
  428. return atomic.LoadInt32(&p.stateIdle) == 0
  429. }
  430. throughput := func(p *peerConnection) float64 {
  431. p.lock.RLock()
  432. defer p.lock.RUnlock()
  433. return p.stateThroughput
  434. }
  435. return ps.idlePeers(63, 65, idle, throughput)
  436. }
  437. // idlePeers retrieves a flat list of all currently idle peers satisfying the
  438. // protocol version constraints, using the provided function to check idleness.
  439. // The resulting set of peers are sorted by their measure throughput.
  440. func (ps *peerSet) idlePeers(minProtocol, maxProtocol int, idleCheck func(*peerConnection) bool, throughput func(*peerConnection) float64) ([]*peerConnection, int) {
  441. ps.lock.RLock()
  442. defer ps.lock.RUnlock()
  443. idle, total := make([]*peerConnection, 0, len(ps.peers)), 0
  444. for _, p := range ps.peers {
  445. if p.version >= minProtocol && p.version <= maxProtocol {
  446. if idleCheck(p) {
  447. idle = append(idle, p)
  448. }
  449. total++
  450. }
  451. }
  452. for i := 0; i < len(idle); i++ {
  453. for j := i + 1; j < len(idle); j++ {
  454. if throughput(idle[i]) < throughput(idle[j]) {
  455. idle[i], idle[j] = idle[j], idle[i]
  456. }
  457. }
  458. }
  459. return idle, total
  460. }
  461. // medianRTT returns the median RTT of the peerset, considering only the tuning
  462. // peers if there are more peers available.
  463. func (ps *peerSet) medianRTT() time.Duration {
  464. // Gather all the currently measured round trip times
  465. ps.lock.RLock()
  466. defer ps.lock.RUnlock()
  467. rtts := make([]float64, 0, len(ps.peers))
  468. for _, p := range ps.peers {
  469. p.lock.RLock()
  470. rtts = append(rtts, float64(p.rtt))
  471. p.lock.RUnlock()
  472. }
  473. sort.Float64s(rtts)
  474. median := rttMaxEstimate
  475. if qosTuningPeers <= len(rtts) {
  476. median = time.Duration(rtts[qosTuningPeers/2]) // Median of our tuning peers
  477. } else if len(rtts) > 0 {
  478. median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos)
  479. }
  480. // Restrict the RTT into some QoS defaults, irrelevant of true RTT
  481. if median < rttMinEstimate {
  482. median = rttMinEstimate
  483. }
  484. if median > rttMaxEstimate {
  485. median = rttMaxEstimate
  486. }
  487. return median
  488. }