文档章节

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

兄弟连区块链入门教程
 兄弟连区块链入门教程
发布于 10/19 10:54
字数 1084
阅读 7
收藏 0

  兄弟连区块链教程以太坊源码分析downloader-peer源码分析,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。
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. }

© 著作权归作者所有

共有 人打赏支持
兄弟连区块链入门教程
粉丝 5
博文 174
码字总数 182165
作品 0
延庆
私信 提问
兄弟连区块链入门教程以太坊源码分析p2p-peer.go源码分析

区块链入门教程以太坊源码分析p2p-peer.go源码分析,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退...

兄弟连区块链入门教程
10/24
0
0
区块链入门教程以太坊源码分析node源码分析

兄弟连区块链入门教程以太坊源码分析node源码分析,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,...

兄弟连区块链入门教程
10/24
0
0
区块链入门教程以太坊源码分析ethdb源码分析

兄弟连区块链入门教程以太坊源码分析ethdb源码分析,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退...

兄弟连区块链入门教程
10/23
0
0
区块链从入门到放弃-区块链入门汇总整理

一个完整的入门整理帖,所以到处搜罗搬运了一些觉得挺不错的来,直接贴链接应该不算侵权吧 入门介绍与原理: 一、比特币 1.比特币白皮书 这是一切的开始 2.精通比特币 讲比特币很详细的一本书...

雪花又一年
04/13
0
0
区块链教程以太坊源码分析core-state源码分析(一)

兄弟连区块链教程以太坊源码分析core-state源码分析,core/state 包主要为以太坊的state trie提供了一层缓存层(cache) database主要提供了trie树的抽象,提供trie树的缓存和合约代码长度的缓...

兄弟连区块链入门教程
10/22
0
0

没有更多内容

加载失败,请刷新页面

加载更多

十月开源项目推荐:专为 Python 初学者准备的 IDE 你用过吗?

每月新增开源项目。顾名思义,每月更新一期。我们会从社区上个月新收录的开源项目中,挑选出有价值的、有用的、优秀的、或者好玩的开源项目来和大家分享。数量不多,但我们力求推荐的都是精品...

编辑部的故事
10分钟前
5
0
Java/Android 获取文件夹的文件列表(file.listFiles())并按名称排序,中文优先

排序规则 因为是中国人,习惯性看中文文件夹放前面比较顺眼,所以在别人博客(https://blog.csdn.net/da_caoyuan/article/details/56664673)的基础上,加上了自己的排序规则。 默认排序规则...

她叫我小渝
11分钟前
0
0
RabbitMQ通过shovel插件迁移数据

前言 生产环境中会遇到RabbitMQ数据迁移的场景,例如:切换云服务厂商、不同Region之间数据迁移、新搭建RabbitMQ实例,数据需要同步至新的RabbitMQ实例。 前提条件: 源RabbitMQ实例打开了s...

中间件小哥
14分钟前
0
0
kubernetes 环境搭建

kubernetes 简介:Kubernetes是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful)。 点击此处查看官网详情。...

MrPei
28分钟前
1
0
关于scala macro的example

http://www.bbartosz.com/blog/2016/09/24/fun-with-scalameta-examples-part1/

Littlebox
30分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部