peer.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/big"
  22. "sync"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  26. "github.com/ethereum/go-ethereum/event"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/p2p/msgrate"
  29. )
  30. const (
  31. maxLackingHashes = 4096 // Maximum number of entries allowed on the list or lacking items
  32. )
  33. var (
  34. errAlreadyRegistered = errors.New("peer is already registered")
  35. errNotRegistered = errors.New("peer is not registered")
  36. )
  37. // peerConnection represents an active peer from which hashes and blocks are retrieved.
  38. type peerConnection struct {
  39. id string // Unique identifier of the peer
  40. rates *msgrate.Tracker // Tracker to hone in on the number of items retrievable per second
  41. lacking map[common.Hash]struct{} // Set of hashes not to request (didn't have previously)
  42. peer Peer
  43. version uint // Eth protocol version number to switch strategies
  44. log log.Logger // Contextual logger to add extra infos to peer logs
  45. lock sync.RWMutex
  46. }
  47. // LightPeer encapsulates the methods required to synchronise with a remote light peer.
  48. type LightPeer interface {
  49. Head() (common.Hash, *big.Int)
  50. RequestHeadersByHash(common.Hash, int, int, bool, chan *eth.Response) (*eth.Request, error)
  51. RequestHeadersByNumber(uint64, int, int, bool, chan *eth.Response) (*eth.Request, error)
  52. }
  53. // Peer encapsulates the methods required to synchronise with a remote full peer.
  54. type Peer interface {
  55. LightPeer
  56. RequestBodies([]common.Hash, chan *eth.Response) (*eth.Request, error)
  57. RequestReceipts([]common.Hash, chan *eth.Response) (*eth.Request, error)
  58. }
  59. // lightPeerWrapper wraps a LightPeer struct, stubbing out the Peer-only methods.
  60. type lightPeerWrapper struct {
  61. peer LightPeer
  62. }
  63. func (w *lightPeerWrapper) Head() (common.Hash, *big.Int) { return w.peer.Head() }
  64. func (w *lightPeerWrapper) RequestHeadersByHash(h common.Hash, amount int, skip int, reverse bool, sink chan *eth.Response) (*eth.Request, error) {
  65. return w.peer.RequestHeadersByHash(h, amount, skip, reverse, sink)
  66. }
  67. func (w *lightPeerWrapper) RequestHeadersByNumber(i uint64, amount int, skip int, reverse bool, sink chan *eth.Response) (*eth.Request, error) {
  68. return w.peer.RequestHeadersByNumber(i, amount, skip, reverse, sink)
  69. }
  70. func (w *lightPeerWrapper) RequestBodies([]common.Hash, chan *eth.Response) (*eth.Request, error) {
  71. panic("RequestBodies not supported in light client mode sync")
  72. }
  73. func (w *lightPeerWrapper) RequestReceipts([]common.Hash, chan *eth.Response) (*eth.Request, error) {
  74. panic("RequestReceipts not supported in light client mode sync")
  75. }
  76. // newPeerConnection creates a new downloader peer.
  77. func newPeerConnection(id string, version uint, peer Peer, logger log.Logger) *peerConnection {
  78. return &peerConnection{
  79. id: id,
  80. lacking: make(map[common.Hash]struct{}),
  81. peer: peer,
  82. version: version,
  83. log: logger,
  84. }
  85. }
  86. // Reset clears the internal state of a peer entity.
  87. func (p *peerConnection) Reset() {
  88. p.lock.Lock()
  89. defer p.lock.Unlock()
  90. p.lacking = make(map[common.Hash]struct{})
  91. }
  92. // UpdateHeaderRate updates the peer's estimated header retrieval throughput with
  93. // the current measurement.
  94. func (p *peerConnection) UpdateHeaderRate(delivered int, elapsed time.Duration) {
  95. p.rates.Update(eth.BlockHeadersMsg, elapsed, delivered)
  96. }
  97. // UpdateBodyRate updates the peer's estimated body retrieval throughput with the
  98. // current measurement.
  99. func (p *peerConnection) UpdateBodyRate(delivered int, elapsed time.Duration) {
  100. p.rates.Update(eth.BlockBodiesMsg, elapsed, delivered)
  101. }
  102. // UpdateReceiptRate updates the peer's estimated receipt retrieval throughput
  103. // with the current measurement.
  104. func (p *peerConnection) UpdateReceiptRate(delivered int, elapsed time.Duration) {
  105. p.rates.Update(eth.ReceiptsMsg, elapsed, delivered)
  106. }
  107. // HeaderCapacity retrieves the peer's header download allowance based on its
  108. // previously discovered throughput.
  109. func (p *peerConnection) HeaderCapacity(targetRTT time.Duration) int {
  110. cap := p.rates.Capacity(eth.BlockHeadersMsg, targetRTT)
  111. if cap > MaxHeaderFetch {
  112. cap = MaxHeaderFetch
  113. }
  114. return cap
  115. }
  116. // BodyCapacity retrieves the peer's body download allowance based on its
  117. // previously discovered throughput.
  118. func (p *peerConnection) BodyCapacity(targetRTT time.Duration) int {
  119. cap := p.rates.Capacity(eth.BlockBodiesMsg, targetRTT)
  120. if cap > MaxBlockFetch {
  121. cap = MaxBlockFetch
  122. }
  123. return cap
  124. }
  125. // ReceiptCapacity retrieves the peers receipt download allowance based on its
  126. // previously discovered throughput.
  127. func (p *peerConnection) ReceiptCapacity(targetRTT time.Duration) int {
  128. cap := p.rates.Capacity(eth.ReceiptsMsg, targetRTT)
  129. if cap > MaxReceiptFetch {
  130. cap = MaxReceiptFetch
  131. }
  132. return cap
  133. }
  134. // MarkLacking appends a new entity to the set of items (blocks, receipts, states)
  135. // that a peer is known not to have (i.e. have been requested before). If the
  136. // set reaches its maximum allowed capacity, items are randomly dropped off.
  137. func (p *peerConnection) MarkLacking(hash common.Hash) {
  138. p.lock.Lock()
  139. defer p.lock.Unlock()
  140. for len(p.lacking) >= maxLackingHashes {
  141. for drop := range p.lacking {
  142. delete(p.lacking, drop)
  143. break
  144. }
  145. }
  146. p.lacking[hash] = struct{}{}
  147. }
  148. // Lacks retrieves whether the hash of a blockchain item is on the peers lacking
  149. // list (i.e. whether we know that the peer does not have it).
  150. func (p *peerConnection) Lacks(hash common.Hash) bool {
  151. p.lock.RLock()
  152. defer p.lock.RUnlock()
  153. _, ok := p.lacking[hash]
  154. return ok
  155. }
  156. // peeringEvent is sent on the peer event feed when a remote peer connects or
  157. // disconnects.
  158. type peeringEvent struct {
  159. peer *peerConnection
  160. join bool
  161. }
  162. // peerSet represents the collection of active peer participating in the chain
  163. // download procedure.
  164. type peerSet struct {
  165. peers map[string]*peerConnection
  166. rates *msgrate.Trackers // Set of rate trackers to give the sync a common beat
  167. events event.Feed // Feed to publish peer lifecycle events on
  168. lock sync.RWMutex
  169. }
  170. // newPeerSet creates a new peer set top track the active download sources.
  171. func newPeerSet() *peerSet {
  172. return &peerSet{
  173. peers: make(map[string]*peerConnection),
  174. rates: msgrate.NewTrackers(log.New("proto", "eth")),
  175. }
  176. }
  177. // SubscribeEvents subscribes to peer arrival and departure events.
  178. func (ps *peerSet) SubscribeEvents(ch chan<- *peeringEvent) event.Subscription {
  179. return ps.events.Subscribe(ch)
  180. }
  181. // Reset iterates over the current peer set, and resets each of the known peers
  182. // to prepare for a next batch of block retrieval.
  183. func (ps *peerSet) Reset() {
  184. ps.lock.RLock()
  185. defer ps.lock.RUnlock()
  186. for _, peer := range ps.peers {
  187. peer.Reset()
  188. }
  189. }
  190. // Register injects a new peer into the working set, or returns an error if the
  191. // peer is already known.
  192. //
  193. // The method also sets the starting throughput values of the new peer to the
  194. // average of all existing peers, to give it a realistic chance of being used
  195. // for data retrievals.
  196. func (ps *peerSet) Register(p *peerConnection) error {
  197. // Register the new peer with some meaningful defaults
  198. ps.lock.Lock()
  199. if _, ok := ps.peers[p.id]; ok {
  200. ps.lock.Unlock()
  201. return errAlreadyRegistered
  202. }
  203. p.rates = msgrate.NewTracker(ps.rates.MeanCapacities(), ps.rates.MedianRoundTrip())
  204. if err := ps.rates.Track(p.id, p.rates); err != nil {
  205. ps.lock.Unlock()
  206. return err
  207. }
  208. ps.peers[p.id] = p
  209. ps.lock.Unlock()
  210. ps.events.Send(&peeringEvent{peer: p, join: true})
  211. return nil
  212. }
  213. // Unregister removes a remote peer from the active set, disabling any further
  214. // actions to/from that particular entity.
  215. func (ps *peerSet) Unregister(id string) error {
  216. ps.lock.Lock()
  217. p, ok := ps.peers[id]
  218. if !ok {
  219. ps.lock.Unlock()
  220. return errNotRegistered
  221. }
  222. delete(ps.peers, id)
  223. ps.rates.Untrack(id)
  224. ps.lock.Unlock()
  225. ps.events.Send(&peeringEvent{peer: p, join: false})
  226. return nil
  227. }
  228. // Peer retrieves the registered peer with the given id.
  229. func (ps *peerSet) Peer(id string) *peerConnection {
  230. ps.lock.RLock()
  231. defer ps.lock.RUnlock()
  232. return ps.peers[id]
  233. }
  234. // Len returns if the current number of peers in the set.
  235. func (ps *peerSet) Len() int {
  236. ps.lock.RLock()
  237. defer ps.lock.RUnlock()
  238. return len(ps.peers)
  239. }
  240. // AllPeers retrieves a flat list of all the peers within the set.
  241. func (ps *peerSet) AllPeers() []*peerConnection {
  242. ps.lock.RLock()
  243. defer ps.lock.RUnlock()
  244. list := make([]*peerConnection, 0, len(ps.peers))
  245. for _, p := range ps.peers {
  246. list = append(list, p)
  247. }
  248. return list
  249. }
  250. // peerCapacitySort implements sort.Interface.
  251. // It sorts peer connections by capacity (descending).
  252. type peerCapacitySort struct {
  253. peers []*peerConnection
  254. caps []int
  255. }
  256. func (ps *peerCapacitySort) Len() int {
  257. return len(ps.peers)
  258. }
  259. func (ps *peerCapacitySort) Less(i, j int) bool {
  260. return ps.caps[i] > ps.caps[j]
  261. }
  262. func (ps *peerCapacitySort) Swap(i, j int) {
  263. ps.peers[i], ps.peers[j] = ps.peers[j], ps.peers[i]
  264. ps.caps[i], ps.caps[j] = ps.caps[j], ps.caps[i]
  265. }