继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

以太坊之Fetcher(收到BlockHash的处理)

紫衣仙女
关注TA
已关注
手记 231
粉丝 71
获赞 334

以太坊节点广播block的时候一部分节点广播整个block内容,其余节点广播block的hash,

本篇分析一下节点收到block hash后的处理

收到NewBlockHash

eth/handler.go中收到NewBlockHashesMsg消息,看代码的处理:

[plain] view plain copy

  1. case msg.Code == NewBlockHashesMsg:  

  2.     var announces newBlockHashesData  

  3.     if err := msg.Decode(&announces); err != nil {  

  4.     return errResp(ErrDecode, "%v: %v", msg, err)  

  5.     }  

  6.     // Mark the hashes as present at the remote node  

  7.     for _, block := range announces {  

  8.     p.MarkBlock(block.Hash)  

  9.     }  

  10.     // Schedule all the unknown hashes for retrieval  

  11.     unknown := make(newBlockHashesData, 0, len(announces))  

  12.     for _, block := range announces {  

  13.     if !pm.blockchain.HasBlock(block.Hash, block.Number) {  

  14.         unknown = append(unknown, block)  

  15.     }  

  16.     }  

  17.     for _, block := range unknown {  

  18.     pm.fetcher.Notify(p.id, block.Hash, block.Number, time.Now(), p.RequestOneHeader, p.RequestBodies)  

  19.     }  

收到消息后:

1 遍历收到的block hashes,mark下对端节点拥有此block

2 再遍历收到的hashes,与本地对比看是否已有该block,没有的append到unknown

3 遍历unknown,然后调用fetcher的Notify,带hash,block num,request header函数和request body函数

[plain] view plain copy

  1. func (f *Fetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time,  

  2.     headerFetcher headerRequesterFn, bodyFetcher bodyRequesterFn) error {  

  3.     block := &announce{  

  4.         hash:        hash,  

  5.         number:      number,  

  6.         time:        time,  

  7.         origin:      peer,  

  8.         fetchHeader: headerFetcher,  

  9.         fetchBodies: bodyFetcher,  

  10.     }  

  11.     select {  

  12.     case f.notify <- block:  

  13.         return nil  

  14.     case <-f.quit:  

  15.         return errTerminated  

  16.     }  

  17. }  

Notify做的事情也很简单,new了一个announce结构,然后写到channel notify,实现了notify的作用,想必是有个地方一直在监听notify channel做事情,找到监听notify的地方

Fetcher获取header和body

(代码比较长,暂时先贴着了,要么分段帖也不好贴)

[plain] view plain copy

  1. func (f *Fetcher) loop() {  

  2.     // Iterate the block fetching until a quit is requested  

  3.     fetchTimer := time.NewTimer(0)  

  4.     completeTimer := time.NewTimer(0)  

  5.   

  6.     for {  

  7.         // Clean up any expired block fetches  

  8.         for hash, announce := range f.fetching {  

  9.             if time.Since(announce.time) > fetchTimeout {  

  10.                 f.forgetHash(hash)  

  11.             }  

  12.         }  

  13.         // Import any queued blocks that could potentially fit  

  14.         height := f.chainHeight()  

  15.         for !f.queue.Empty() {  

  16.             op := f.queue.PopItem().(*inject)  

  17.             if f.queueChangeHook != nil {  

  18.                 f.queueChangeHook(op.block.Hash(), false)  

  19.             }  

  20.             // If too high up the chain or phase, continue later  

  21.             number := op.block.NumberU64()  

  22.             if number > height+1 {  

  23.                 f.queue.Push(op, -float32(op.block.NumberU64()))  

  24.                 if f.queueChangeHook != nil {  

  25.                     f.queueChangeHook(op.block.Hash(), true)  

  26.                 }  

  27.                 break  

  28.             }  

  29.             // Otherwise if fresh and still unknown, try and import  

  30.             hash := op.block.Hash()  

  31.             if number+maxUncleDist < height || f.getBlock(hash) != nil {  

  32.                 f.forgetBlock(hash)  

  33.                 continue  

  34.             }  

  35.             f.insert(op.origin, op.block)  

  36.         }  

  37.         // Wait for an outside event to occur  

  38.         select {  

  39.         case <-f.quit:  

  40.             // Fetcher terminating, abort all operations  

  41.             return  

  42.   

  43.         case notification := <-f.notify:  

  44.             // A block was announced, make sure the peer isn't DOSing us  

  45.             propAnnounceInMeter.Mark(1)  

  46.   

  47.             count := f.announces[notification.origin] + 1  

  48.             if count > hashLimit {  

  49.                 log.Debug("Peer exceeded outstanding announces", "peer", notification.origin, "limit", hashLimit)  

  50.                 propAnnounceDOSMeter.Mark(1)  

  51.                 break  

  52.             }  

  53.             // If we have a valid block number, check that it's potentially useful  

  54.             if notification.number > 0 {  

  55.                 if dist := int64(notification.number) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {  

  56.                     log.Debug("Peer discarded announcement", "peer", notification.origin, "number", notification.number, "hash", notification.hash, "distance", dist)  

  57.                     propAnnounceDropMeter.Mark(1)  

  58.                     break  

  59.                 }  

  60.             }  

  61.             // All is well, schedule the announce if block's not yet downloading  

  62.             if _, ok := f.fetching[notification.hash]; ok {  

  63.                 break  

  64.             }  

  65.             if _, ok := f.completing[notification.hash]; ok {  

  66.                 break  

  67.             }  

  68.             f.announces[notification.origin] = count  

  69.             f.announced[notification.hash] = append(f.announced[notification.hash], notification)  

  70.             if f.announceChangeHook != nil && len(f.announced[notification.hash]) == 1 {  

  71.                 f.announceChangeHook(notification.hash, true)  

  72.             }  

  73.             if len(f.announced) == 1 {  

  74.                 f.rescheduleFetch(fetchTimer)  

  75.             }  

  76.   

  77.         case op := <-f.inject:  

  78.             // A direct block insertion was requested, try and fill any pending gaps  

  79.             propBroadcastInMeter.Mark(1)  

  80.             f.enqueue(op.origin, op.block)  

  81.   

  82.         case hash := <-f.done:  

  83.             // A pending import finished, remove all traces of the notification  

  84.             f.forgetHash(hash)  

  85.             f.forgetBlock(hash)  

  86.   

  87.         case <-fetchTimer.C:  

  88.             // At least one block's timer ran out, check for needing retrieval  

  89.             request := make(map[string][]common.Hash)  

  90.   

  91.             for hash, announces := range f.announced {  

  92.                 if time.Since(announces[0].time) > arriveTimeout-gatherSlack {  

  93.                     // Pick a random peer to retrieve from, reset all others  

  94.                     announce := announces[rand.Intn(len(announces))]  

  95.                     f.forgetHash(hash)  

  96.   

  97.                     // If the block still didn't arrive, queue for fetching  

  98.                     if f.getBlock(hash) == nil {  

  99.                         request[announce.origin] = append(request[announce.origin], hash)  

  100.                         f.fetching[hash] = announce  

  101.                     }  

  102.                 }  

  103.             }  

  104.             // Send out all block header requests  

  105.             for peer, hashes := range request {  

  106.                 log.Trace("Fetching scheduled headers", "peer", peer, "list", hashes)  

  107.   

  108.                 // Create a closure of the fetch and schedule in on a new thread  

  109.                 fetchHeader, hashes := f.fetching[hashes[0]].fetchHeader, hashes  

  110.                 go func() {  

  111.                     if f.fetchingHook != nil {  

  112.                         f.fetchingHook(hashes)  

  113.                     }  

  114.                     for _, hash := range hashes {  

  115.                         headerFetchMeter.Mark(1)  

  116.                         fetchHeader(hash) // Suboptimal, but protocol doesn't allow batch header retrievals  

  117.                     }  

  118.                 }()  

  119.             }  

  120.             // Schedule the next fetch if blocks are still pending  

  121.             f.rescheduleFetch(fetchTimer)  

  122.   

  123.         case <-completeTimer.C:  

  124.             // At least one header's timer ran out, retrieve everything  

  125.             request := make(map[string][]common.Hash)  

  126.   

  127.             for hash, announces := range f.fetched {  

  128.                 // Pick a random peer to retrieve from, reset all others  

  129.                 announce := announces[rand.Intn(len(announces))]  

  130.                 f.forgetHash(hash)  

  131.   

  132.                 // If the block still didn't arrive, queue for completion  

  133.                 if f.getBlock(hash) == nil {  

  134.                     request[announce.origin] = append(request[announce.origin], hash)  

  135.                     f.completing[hash] = announce  

  136.                 }  

  137.             }  

  138.             // Send out all block body requests  

  139.             for peer, hashes := range request {  

  140.                 log.Trace("Fetching scheduled bodies", "peer", peer, "list", hashes)  

  141.   

  142.                 // Create a closure of the fetch and schedule in on a new thread  

  143.                 if f.completingHook != nil {  

  144.                     f.completingHook(hashes)  

  145.                 }  

  146.                 bodyFetchMeter.Mark(int64(len(hashes)))  

  147.                 go f.completing[hashes[0]].fetchBodies(hashes)  

  148.             }  

  149.             // Schedule the next fetch if blocks are still pending  

  150.             f.rescheduleComplete(completeTimer)  

  151.   

  152.         case filter := <-f.headerFilter:  

  153.             // Headers arrived from a remote peer. Extract those that were explicitly  

  154.             // requested by the fetcher, and return everything else so it's delivered  

  155.             // to other parts of the system.  

  156.             var task *headerFilterTask  

  157.             select {  

  158.             case task = <-filter:  

  159.             case <-f.quit:  

  160.                 return  

  161.             }  

  162.             headerFilterInMeter.Mark(int64(len(task.headers)))  

  163.   

  164.             // Split the batch of headers into unknown ones (to return to the caller),  

  165.             // known incomplete ones (requiring body retrievals) and completed blocks.  

  166.             unknown, incomplete, complete := []*types.Header{}, []*announce{}, []*types.Block{}  

  167.             for _, header := range task.headers {  

  168.                 hash := header.Hash()  

  169.   

  170.                 // Filter fetcher-requested headers from other synchronisation algorithms  

  171.                 if announce := f.fetching[hash]; announce != nil && announce.origin == task.peer && f.fetched[hash] == nil && f.completing[hash] == nil && f.queued[hash] == nil {  

  172.                     // If the delivered header does not match the promised number, drop the announcer  

  173.                     if header.Number.Uint64() != announce.number {  

  174.                         log.Trace("Invalid block number fetched", "peer", announce.origin, "hash", header.Hash(), "announced", announce.number, "provided", header.Number)  

  175.                         f.dropPeer(announce.origin)  

  176.                         f.forgetHash(hash)  

  177.                         continue  

  178.                     }  

  179.                     // Only keep if not imported by other means  

  180.                     if f.getBlock(hash) == nil {  

  181.                         announce.header = header  

  182.                         announce.time = task.time  

  183.   

  184.                         // If the block is empty (header only), short circuit into the final import queue  

  185.                         if header.TxHash == types.DeriveSha(types.Transactions{}) && header.UncleHash == types.CalcUncleHash([]*types.Header{}) {  

  186.                             log.Trace("Block empty, skipping body retrieval", "peer", announce.origin, "number", header.Number, "hash", header.Hash())  

  187.   

  188.                             block := types.NewBlockWithHeader(header)  

  189.                             block.ReceivedAt = task.time  

  190.   

  191.                             complete = append(complete, block)  

  192.                             f.completing[hash] = announce  

  193.                             continue  

  194.                         }  

  195.                         // Otherwise add to the list of blocks needing completion  

  196.                         incomplete = append(incomplete, announce)  

  197.                     } else {  

  198.                         log.Trace("Block already imported, discarding header", "peer", announce.origin, "number", header.Number, "hash", header.Hash())  

  199.                         f.forgetHash(hash)  

  200.                     }  

  201.                 } else {  

  202.                     // Fetcher doesn't know about it, add to the return list  

  203.                     unknown = append(unknown, header)  

  204.                 }  

  205.             }  

  206.             headerFilterOutMeter.Mark(int64(len(unknown)))  

  207.             select {  

  208.             case filter <- &headerFilterTask{headers: unknown, time: task.time}:  

  209.             case <-f.quit:  

  210.                 return  

  211.             }  

  212.             // Schedule the retrieved headers for body completion  

  213.             for _, announce := range incomplete {  

  214.                 hash := announce.header.Hash()  

  215.                 if _, ok := f.completing[hash]; ok {  

  216.                     continue  

  217.                 }  

  218.                 f.fetched[hash] = append(f.fetched[hash], announce)  

  219.                 if len(f.fetched) == 1 {  

  220.                     f.rescheduleComplete(completeTimer)  

  221.                 }  

  222.             }  

  223.             // Schedule the header-only blocks for import  

  224.             for _, block := range complete {  

  225.                 if announce := f.completing[block.Hash()]; announce != nil {  

  226.                     f.enqueue(announce.origin, block)  

  227.                 }  

  228.             }  

  229.   

  230.         case filter := <-f.bodyFilter:  

  231.             // Block bodies arrived, extract any explicitly requested blocks, return the rest  

  232.             var task *bodyFilterTask  

  233.             select {  

  234.             case task = <-filter:  

  235.             case <-f.quit:  

  236.                 return  

  237.             }  

  238.             bodyFilterInMeter.Mark(int64(len(task.transactions)))  

  239.   

  240.             blocks := []*types.Block{}  

  241.             for i := 0; i < len(task.transactions) && i < len(task.uncles); i++ {  

  242.                 // Match up a body to any possible completion request  

  243.                 matched := false  

  244.   

  245.                 for hash, announce := range f.completing {  

  246.                     if f.queued[hash] == nil {  

  247.                         txnHash := types.DeriveSha(types.Transactions(task.transactions[i]))  

  248.                         uncleHash := types.CalcUncleHash(task.uncles[i])  

  249.   

  250.                         if txnHash == announce.header.TxHash && uncleHash == announce.header.UncleHash && announce.origin == task.peer {  

  251.                             // Mark the body matched, reassemble if still unknown  

  252.                             matched = true  

  253.   

  254.                             if f.getBlock(hash) == nil {  

  255.                                 block := types.NewBlockWithHeader(announce.header).WithBody(task.transactions[i], task.uncles[i])  

  256.                                 block.ReceivedAt = task.time  

  257.   

  258.                                 blocks = append(blocks, block)  

  259.                             } else {  

  260.                                 f.forgetHash(hash)  

  261.                             }  

  262.                         }  

  263.                     }  

  264.                 }  

  265.                 if matched {  

  266.                     task.transactions = append(task.transactions[:i], task.transactions[i+1:]...)  

  267.                     task.uncles = append(task.uncles[:i], task.uncles[i+1:]...)  

  268.                     i--  

  269.                     continue  

  270.                 }  

  271.             }  

  272.   

  273.             bodyFilterOutMeter.Mark(int64(len(task.transactions)))  

  274.             select {  

  275.             case filter <- task:  

  276.             case <-f.quit:  

  277.                 return  

  278.             }  

  279.             // Schedule the retrieved blocks for ordered import  

  280.             for _, block := range blocks {  

  281.                 if announce := f.completing[block.Hash()]; announce != nil {  

  282.                     f.enqueue(announce.origin, block)  

  283.                 }  

  284.             }  

  285.         }  

  286.     }  

  287. }  

加入hash到announced

果然这里有个loop(),内部是一个for循环,在监听读取一些channel的数据

1 判断f.announces map内从对端peer发来的hash个数是否大于hashLimit(256),如果大于,那么对端peer可能在Dos攻击,break出去

2 判断block的num与自身chain高度的差值,如果小于-maxUncleDist(7)或者大于maxQueueDist(32),也break出去

3 判断是否在f.fetching,如果是break

4 判断是否在f.completing,如果是break

5 加入到f.announced, f.announces加1(此个数用来判断节点是否在Dos攻击)

6 一旦f.announced有元素了,就开始rescheduleFetch(fetchTimer),内容是reset下fetchTimer,遍历announced中的条目,拿到最早的时间,然后reset fetchTimer为500ms减去最早的条目加入到announced后耗费的时间,目的是让fetchTimer尽快执行而又不浪费资源空转

获取headers

所以接下来看f.announced的处理在哪里

还在本函数中,case <- fetchTimer.C

这个timer一直在跑,每次跑完会reset一下,刚刚上面有讲过它的原理

1 遍历f.announced中的announce,对于同一个block的hash,可能有若干个节点广播过来

    if time.Since(announces[0].time) > arriveTimeOut(500ms) - gatherSlack(100ms)

这里判断最早加入的hash的时间要大于400ms(500-100)才去处理,这个逻辑应该是为了等够一定的时间积攒一些节点广播同一个block然后随机挑选一个节点去同步,避免所有节点都向源节点同步造成源节点网络拥堵

把这些要请求的加入到request map内

同时把此hash从f.announced中移除并加入到f.fetching map内

2 遍历request map,启动goroutine去获取指定hash的block header,有几个header就几个goroutine

然后等待对端节点返回header

返回headers

还在本loop()函数内,返回header是写入到channel f.headerFilter

1 遍历处理收到的headers,从f.fetching内拿到anounce(上一步放入到该map内),并且返回的peer也是获取的peer,不在fetched内,不在completing内,不在queued内才通过判断;否则加入unkonwn map内

2 如果不是要获取的高度的block header,continue

3 如果block是空的:既没有tx也没有uncle,那么直接组成block然后加入到complete map内并且加入f.completing,否则加入到incomplete map内

4 把unkonwn放入到filter处理一下(具体怎么处理再看一下)

5 遍历incomplete map,然后放入到f.fetched map内,表示拿到了header还需要继续拿body。同时rescheduleComplete(),同上面的rescheduleFetch,尽快取block,时间不大于100ms

6 遍历complete map,调用f.enqueue(),目的就是把完成的block加入到f.queued内,处理等后面拿到body一起说下

获取bodies

上面拿到header后把hash加入到f.fetched map内,接着拿body

还是在本loop()内,等completeTimer到达:case <-completeTimer.C

1 遍历f.fetched,随机选一个去获取body

2 把hash加入到f.completing map内和request内

3 遍历request,启动gorountine去对端节点获取body内容

返回bodies

对端节点处理完返回对应的body后, 还是写入到本loop()内的channel f.bodyFilter: 

1 遍历收到的tx和uncle

2 如果hash不在f.queued(没有处理完成),并且判断收到的tx hash、unclue hash、peer与completing中的一致就加入到blocks map内

3遍历blocks,调用f.enqueue()加入到f.queue

处理完成

接着处理f.queue map,还是在本loop()内循环处理的

如果f.queue不为空pop一个出来:f.queue.PopItem(),经过简单的check,f.insert()加入到本地

  1. func (f *Fetcher) insert(peer string, block *types.Block) {  

  2.     hash := block.Hash()  

  3.   

  4.     // Run the import on a new thread  

  5.     log.Debug("Importing propagated block", "peer", peer, "number", block.Number(), "hash", hash)  

  6.     go func() {  

  7.         defer func() { f.done <- hash }()  

  8.   

  9.         // If the parent's unknown, abort insertion  

  10.         parent := f.getBlock(block.ParentHash())  

  11.         if parent == nil {  

  12.             log.Debug("Unknown parent of propagated block", "peer", peer, "number", block.Number(), "hash", hash, "parent", block.ParentHash())  

  13.             return  

  14.         }  

  15.         // Quickly validate the header and propagate the block if it passes  

  16.         switch err := f.verifyHeader(block.Header()); err {  

  17.         case nil:  

  18.             // All ok, quickly propagate to our peers  

  19.             propBroadcastOutTimer.UpdateSince(block.ReceivedAt)  

  20.             go f.broadcastBlock(block, true)  

  21.   

  22.         case consensus.ErrFutureBlock:  

  23.             // Weird future block, don't fail, but neither propagate  

  24.   

  25.         default:  

  26.             // Something went very wrong, drop the peer  

  27.             log.Debug("Propagated block verification failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)  

  28.             f.dropPeer(peer)  

  29.             return  

  30.         }  

  31.         // Run the actual import and log any issues  

  32.         if _, err := f.insertChain(types.Blocks{block}); err != nil {  

  33.             log.Debug("Propagated block import failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)  

  34.             return  

  35.         }  

  36.         // If import succeeded, broadcast the block  

  37.         propAnnounceOutTimer.UpdateSince(block.ReceivedAt)  

  38.         go f.broadcastBlock(block, false)  

  39.   

  40.         // Invoke the testing hook if needed  

  41.         if f.importedHook != nil {  

  42.             f.importedHook(block)  

  43.         }  

  44.     }()  

  45. }  

启动gorountine插入到本地:

1 先VerifyHeader,通过后广播block到若干节点

2 f.insertChain()就是写入到本地的leveldb

3 插入成功后,广播block hash到其他节点

有人要问了,为什么广播hash放在最后呢,要做到尽快广播到全网是不是放在1也可以:

1是广播整个区块,3是广播hash,只有等2插入到本地后,3广播出去后,别的节点来获取自己才有东西给别人,这也是3放在2后面的原因

不过其实也是可以放在第1步,只是这么做的话需要做另外的处理,不利于代码的统一性

总结

自此就分析完了节点收到blockhash后的处理,大致如下:

1 交给fetcher处理,先去拿header

2 等拿到header后,如果body为空,直接组织成block;否则接着拿body

3 拿到body后跟header组织成block,然后插入到本地的leveldb

中间有一些小细节:

1 获取header或者body的时候会等一段时间(400ms、100ms)等收到若干个节点广播的hash,

然后随机选一个去获取,这也是网络负载均衡的设计;代价就是要等一段时间才能同步完成一个区块

2 防Dos攻击:会记录同一个节点同时有多少hash在处理,如果大于给定的256就认为是节点在Dos攻击

Fetcher和downloader的区别

看完了fetcher和downloader的代码就知道了二者的区别:

1 downloader是用来当本地与其他节点相差太多的时候同步到最新的区块

2 fetcher是收到其他节点广播的新区块hashes后去获取这些给定的区块的

原文出处

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP