ITPub博客

首页 > 区块链 > 区块链 > 区块链教程以太坊源码分析downloader-peer源码分析

区块链教程以太坊源码分析downloader-peer源码分析

原创 区块链 作者:兄弟连区块链入门教程 时间:2018-10-19 10:55:08 0 删除 编辑

peer模块包含了downloader使用的peer节点,封装了吞吐量,是否空闲,并记录了之前失败的信息。

peer

 

  1. // peerConnection represents an active peer from which hashes and blocks are retrieved.

  2. type peerConnection struct {

  3. id string // Unique identifier of the peer

  4.  

  5. headerIdle int32 // Current header activity state of the peer (idle = 0, active = 1) 当前的header获取的工作状态。

  6. blockIdle int32 // Current block activity state of the peer (idle = 0, active = 1) 当前的区块获取的工作状态

  7. receiptIdle int32 // Current receipt activity state of the peer (idle = 0, active = 1) 当前的收据获取的工作状态

  8. stateIdle int32 // Current node data activity state of the peer (idle = 0, active = 1) 当前节点状态的工作状态

  9.  

  10. headerThroughput float64 // Number of headers measured to be retrievable per second //记录每秒能够接收多少个区块头的度量值

  11. blockThroughput float64 // Number of blocks (bodies) measured to be retrievable per second //记录每秒能够接收多少个区块的度量值

  12. receiptThroughput float64 // Number of receipts measured to be retrievable per second 记录每秒能够接收多少个收据的度量值

  13. stateThroughput float64 // Number of node data pieces measured to be retrievable per second 记录每秒能够接收多少个账户状态的度量值

  14.  

  15. rtt time.Duration // Request round trip time to track responsiveness (QoS) 请求回应时间

  16.  

  17. headerStarted time.Time // Time instance when the last header fetch was started 记录最后一个header fetch的请求时间

  18. blockStarted time.Time // Time instance when the last block (body) fetch was started

  19. receiptStarted time.Time // Time instance when the last receipt fetch was started

  20. stateStarted time.Time // Time instance when the last node data fetch was started

  21.  

  22. lacking map[common.Hash]struct{} // Set of hashes not to request (didn't have previously) 记录的Hash值不会去请求,一般是因为之前的请求失败

  23.  

  24. peer Peer // eth的peer

  25.  

  26. version int // Eth protocol version number to switch strategies

  27. log log.Logger // Contextual logger to add extra infos to peer logs

  28. lock sync.RWMutex

  29. }

FetchXXX
FetchHeaders FetchBodies等函数 主要调用了eth.peer的功能来进行发送数据请求。

 

  1. // FetchHeaders sends a header retrieval request to the remote peer.

  2. func (p *peerConnection) FetchHeaders(from uint64, count int) error {

  3. // Sanity check the protocol version

  4. if p.version = maxLackingHashes {

  5. for drop := range p.lacking {

  6. delete(p.lacking, drop)

  7. break

  8. }

  9. }

  10. p.lacking[hash] = struct{}{}

  11. }

  12.  

  13. // Lacks retrieves whether the hash of a blockchain item is on the peers lacking

  14. // list (i.e. whether we know that the peer does not have it).

  15. func (p *peerConnection) Lacks(hash common.Hash) bool {

  16. p.lock.RLock()

  17. defer p.lock.RUnlock()

  18.  

  19. _, ok := p.lacking[hash]

  20. return ok

  21. }

peerSet

 

  1. // peerSet represents the collection of active peer participating in the chain

  2. // download procedure.

  3. type peerSet struct {

  4. peers map[string]*peerConnection

  5. newPeerFeed event.Feed

  6. peerDropFeed event.Feed

  7. lock sync.RWMutex

  8. }

Register 和 UnRegister

 

  1. // Register injects a new peer into the working set, or returns an error if the

  2. // peer is already known.

  3. //

  4. // The method also sets the starting throughput values of the new peer to the

  5. // average of all existing peers, to give it a realistic chance of being used

  6. // for data retrievals.

  7. func (ps *peerSet) Register(p *peerConnection) error {

  8. // Retrieve the current median RTT as a sane default

  9. p.rtt = ps.medianRTT()

  10.  

  11. // Register the new peer with some meaningful defaults

  12. ps.lock.Lock()

  13. if _, ok := ps.peers[p.id]; ok {

  14. ps.lock.Unlock()

  15. return errAlreadyRegistered

  16. }

  17. if len(ps.peers) > 0 {

  18. p.headerThroughput, p.blockThroughput, p.receiptThroughput, p.stateThroughput = 0, 0, 0, 0

  19.  

  20. for _, peer := range ps.peers {

  21. peer.lock.RLock()

  22. p.headerThroughput += peer.headerThroughput

  23. p.blockThroughput += peer.blockThroughput

  24. p.receiptThroughput += peer.receiptThroughput

  25. p.stateThroughput += peer.stateThroughput

  26. peer.lock.RUnlock()

  27. }

  28. p.headerThroughput /= float64(len(ps.peers))

  29. p.blockThroughput /= float64(len(ps.peers))

  30. p.receiptThroughput /= float64(len(ps.peers))

  31. p.stateThroughput /= float64(len(ps.peers))

  32. }

  33. ps.peers[p.id] = p

  34. ps.lock.Unlock()

  35.  

  36. ps.newPeerFeed.Send(p)

  37. return nil

  38. }

  39.  

  40. // Unregister removes a remote peer from the active set, disabling any further

  41. // actions to/from that particular entity.

  42. func (ps *peerSet) Unregister(id string) error {

  43. ps.lock.Lock()

  44. p, ok := ps.peers[id]

  45. if !ok {

  46. defer ps.lock.Unlock()

  47. return errNotRegistered

  48. }

  49. delete(ps.peers, id)

  50. ps.lock.Unlock()

  51.  

  52. ps.peerDropFeed.Send(p)

  53. return nil

  54. }

XXXIdlePeers

 

  1. // HeaderIdlePeers retrieves a flat list of all the currently header-idle peers

  2. // within the active peer set, ordered by their reputation.

  3. func (ps *peerSet) HeaderIdlePeers() ([]*peerConnection, int) {

  4. idle := func(p *peerConnection) bool {

  5. return atomic.LoadInt32(&p.headerIdle) == 0

  6. }

  7. throughput := func(p *peerConnection) float64 {

  8. p.lock.RLock()

  9. defer p.lock.RUnlock()

  10. return p.headerThroughput

  11. }

  12. return ps.idlePeers(62, 64, idle, throughput)

  13. }

  14.  

  15. // idlePeers retrieves a flat list of all currently idle peers satisfying the

  16. // protocol version constraints, using the provided function to check idleness.

  17. // The resulting set of peers are sorted by their measure throughput.

  18. func (ps *peerSet) idlePeers(minProtocol, maxProtocol int, idleCheck func(*peerConnection) bool, throughput func(*peerConnection) float64) ([]*peerConnection, int) {

  19. ps.lock.RLock()

  20. defer ps.lock.RUnlock()

  21.  

  22. idle, total := make([]*peerConnection, 0, len(ps.peers)), 0

  23. for _, p := range ps.peers { //首先抽取idle的peer

  24. if p.version >= minProtocol && p.version 0 {

  25. median = time.Duration(rtts[len(rtts)/2]) // Median of our connected peers (maintain even like this some baseline qos)

  26. }

  27. // Restrict the RTT into some QoS defaults, irrelevant of true RTT

  28. if median rttMaxEstimate {

  29. median = rttMaxEstimate

  30. }

  31. return median

  32. }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31557831/viewspace-2216873/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-10-11

  • 博文量
    79
  • 访问量
    59488