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

本文详细解析了以太坊节点在收到NewBlockHashesMsg消息后,如何通过Fetcher进行BlockHeader和BlockBody的获取,包括加入announced、处理fetching和completing、防DoS攻击等步骤,以及Fetcher与downloader的区别。
摘要由CSDN通过智能技术生成

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

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

收到NewBlockHash

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

case msg.Code == NewBlockHashesMsg:
    var announces newBlockHashesData
    if err := msg.Decode(&announces); err != nil {
	return errResp(ErrDecode, "%v: %v", msg, err)
    }
    // Mark the hashes as present at the remote node
    for _, block := range announces {
	p.MarkBlock(block.Hash)
    }
    // Schedule all the unknown hashes for retrieval
    unknown := make(newBlockHashesData, 0, len(announces))
    for _, block := range announces {
    if !pm.blockchain.HasBlock(block.Hash, block.Number) {
	    unknown = append(unknown, block)
	}
    }
    for _, block := range unknown {
	pm.fetcher.Notify(p.id, block.Hash, block.Number, time.Now(), p.RequestOneHeader, p.RequestBodies)
    }

收到消息后:

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

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

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

func (f *Fetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time,
	headerFetcher headerRequesterFn, bodyFetcher bodyRequesterFn) error {
	block := &announce{
		hash:        hash,
		number:      number,
		time:        time,
		origin:      peer,
		fetchHeader: headerFetcher,
		fetchBodies: bodyFetcher,
	}
	select {
	case f.notify <- block:
		return nil
	case <-f.quit:
		return errTerminated
	}
}

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

Fetcher获取header和body

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

func (f *Fetcher) loop() {
	// Iterate the block fetching until a quit is requested
	fetchTimer := time.NewTimer(0)
	completeTimer := time.NewTimer(0)

	for {
		// Clean up any expired block fetches
		for hash, announce := range f.fetching {
			if time.Since(announce.time) > fetchTimeout {
				f.forgetHash(hash)
			}
		}
		// Import any queued blocks that could potentially fit
		height := f.chainHeight()
		for !f.queue.Empty() {
			op := f.queue.PopItem().(*inject)
			if f.queueChangeHook != nil {
				f.queueChangeHook(op.block.Hash(), false)
			}
			// If too high up the chain or phase, continue later
			number := op.block.NumberU64()
			if number > height+1 {
				f.queue.Push(op, -float32(op.block.NumberU64()))
				if f.queueChangeHook != nil {
					f.queueChangeHook(op.block.Hash(), true)
				}
				break
			}
			// Otherwise if fresh and still unknown, try and import
			hash := op.block.Hash()
			if number+maxUncleDist < height || f.getBlock(hash) != nil {
				f.forgetBlock(hash)
				continue
			}
			f.insert(op.origin, op.block)
		}
		// Wait for an outside event to occur
		select {
		case <-f.quit:
			// Fetcher terminating, abort all operations
			return

		case notification := <-f.notify:
			// A block was announced, make sure the peer isn't DOSing us
			propAnnounceInMeter.Mark(1)

			count := f.announces[notification.origin] + 1
			if count > hashLimit {
				log.Debug("Peer exceeded outstanding announces", "peer", notification.origin, "limit", hashLimit)
				propAnnounceDOSMeter.Mark(1)
				break
			}
			// If we have a valid block number, check that it's potentially useful
			if notification.nu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值