ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:24.77KB ,
资源ID:2309542      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/2309542.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(兄弟连Go语言+区块链技术培训以太坊源码分析49p2ptablego源码分析.docx)为本站会员(b****2)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

兄弟连Go语言+区块链技术培训以太坊源码分析49p2ptablego源码分析.docx

1、兄弟连Go语言+区块链技术培训以太坊源码分析49p2ptablego源码分析兄弟连Go语言+区块链技术培训以太坊源码分析(49)p2p-table.go源码分析table.go主要实现了p2p的Kademlia协议。# Kademlia协议简介(建议阅读references里面的pdf文档)Kademlia协议(以下简称Kad) 是美国纽约大学的PetarP. Maymounkov和David Mazieres.在2002年发布的一项研究结果Kademlia: A peerto -peer information system based onthe XOR metric。简单的说, Kad

2、是一种分布式哈希表( DHT) 技术, 不过和其他 DHT 实现技术比较,如Chord、 CAN、 Pastry 等, Kad 通过独特的以异或算法( XOR)为距离度量基础,建立了一种全新的 DHT 拓扑结构,相比于其他算法,大大提高了路由查询速度。# table的结构和字段const (alpha = 3 / Kademlia concurrency factorbucketSize = 16 / Kademlia bucket sizehashBits = len(common.Hash) * 8nBuckets = hashBits + 1 / Number of bucketsmax

3、BondingPingPongs = 16maxFindnodeFailures = 5autoRefreshInterval = 1 * time.HourseedCount = 30seedMaxAge = 5 * 24 * time.Hour)type Table struct mutex sync.Mutex / protects buckets, their content, and nurserybuckets nBuckets*bucket / index of known nodes by distancenursery *Node / bootstrap nodesdb *n

4、odeDB / database of known nodesrefreshReq chan chan structcloseReq chan structclosed chan structbondmu sync.Mutexbonding mapNodeID*bondprocbondslots chan struct / limits total number of active bonding processesnodeAddedHook func(*Node) / for testingnet transportself *Node / metadata of the local nod

5、e# 初始化func newTable(t transport, ourID NodeID, ourAddr *net.UDPAddr, nodeDBPath string) (*Table, error) / If no node database was given, use an in-memory one/这个在之前的database.go里面有介绍。 打开leveldb。如果path为空。那么打开一个基于内存的dbdb, err := newNodeDB(nodeDBPath, Version, ourID)if err != nil return nil, errtab := &T

6、ablenet: t,db: db,self: NewNode(ourID, ourAddr.IP, uint16(ourAddr.Port), uint16(ourAddr.Port),bonding: make(mapNodeID*bondproc),bondslots: make(chan struct, maxBondingPingPongs),refreshReq: make(chan chan struct),closeReq: make(chan struct),closed: make(chan struct),for i := 0; i cap(tab.bondslots);

7、 i+ tab.bondslots - structfor i := range tab.buckets tab.bucketsi = new(bucket)go tab.refreshLoop()return tab, nil上面的初始化启动了一个goroutine refreshLoop(),这个函数主要完成以下的工作。1.每一个小时进行一次刷新工作(autoRefreshInterval)2.如果接收到refreshReq请求。那么进行刷新工作。3.如果接收到关闭消息。那么进行关闭。所以函数主要的工作就是启动刷新工作。doRefresh/ refreshLoop schedules do

8、Refresh runs and coordinates shutdown.func (tab *Table) refreshLoop() var (timer = time.NewTicker(autoRefreshInterval)waiting chan struct / accumulates waiting callers while doRefresh runsdone chan struct / where doRefresh reports completion)loop:for select case -timer.C:if done = nil done = make(ch

9、an struct)go tab.doRefresh(done)case req := -tab.refreshReq:waiting = append(waiting, req)if done = nil done = make(chan struct)go tab.doRefresh(done)case -done:for _, ch := range waiting close(ch)waiting = nildone = nilcase -tab.closeReq:break loopif != nil .close()if done != nil -donefor _, ch :=

10、range waiting close(ch)tab.db.close()close(tab.closed)doRefresh函数/ doRefresh performs a lookup for a random target to keep buckets/ full. seed nodes are inserted if the table is empty (initial/ bootstrap or discarded faulty peers)./ doRefresh 随机查找一个目标,以便保持buckets是满的。如果table是空的,那么种子节点会插入。 (比如最开始的启动或者

11、是删除错误的节点之后)func (tab *Table) doRefresh(done chan struct) defer close(done)/ The Kademlia paper specifies that the bucket refresh should/ perform a lookup in the least recently used bucket. We cannot/ adhere to this because the findnode target is a 512bit value/ (not hash-sized) and it is not easily possible to generate a/ sha3 preimage that falls into a chosen bucket./ We perform a lookup with a random target instead./这里暂时没看懂var target NodeIDrand.Read(target:)result := tab.lookup(targ

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1