以太坊区块广播、fetcher消息接收过滤、downloader的Total Difficulty的代码流程解析

16 篇文章 2 订阅
9 篇文章 0 订阅

 

一、定义

   Total Difficulty - integer of the total difficulty of the chain until this block. 从区块链的第一个区块到此区块的难度的

二、作用

    主要用于区块广播和同步fetcher、Insertblock时判断区块新旧,值越大,处理优先级越高。

三、代码引用流程

1.广播与p2p部分分析

  •     minedBroadcastLoop
// Mined broadcast loop
func (self *ProtocolManager) minedBroadcastLoop() {
	// automatically stops if unsubscribe
	for obj := range self.minedBlockSub.Chan() {
		switch ev := obj.Data.(type) {
		case core.NewMinedBlockEvent:
			self.BroadcastBlock(ev.Block, true)  // First propagate block to peers
			self.BroadcastBlock(ev.Block, false) // Only then announce to the rest
		}
	}
}

       minedBroadcastLoop()持续等待本个体的新挖掘出区块事件,然后立即广播给需要的相邻个体。当不再订阅新挖掘区块事件时,这个函数才会结束等待并返回。

       在收到新挖掘出区块事件后,minedBroadcastLoop()会连续调用两次BroadcastBlock(),两次调用仅仅一个bool型参数@propagate不一样:

            a. 当该参数为true时,会将整个新区块依次发给相邻区块中的一小部分;

            b. 而当其为false时,仅仅将新区块的Hash值和Number发送给所有相邻列表。

// BroadcastBlock will either propagate a block to a subset of it's peers, or
// will only announce it's availability (depending what's requested).
func (pm *ProtocolManager) BroadcastBlock(block *types.Block, propagate bool) {
	hash := block.Hash()
	peers := pm.peers.PeersWithoutBlock(hash)

	// If propagation is requested, send to a subset of the peer
	//propagate为真,发送NewBlockMsg消息,附带加上了此block的difficulty的td
	if propagate {
		// Calculate the TD of the block (it's not imported yet, so block.Td is not valid)
		var td *big.Int
		if parent := pm.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1); parent != nil {
			td = new(big.Int).Add(block.Difficulty(), pm.blockchain.GetTd(block.ParentHash(), block.NumberU64()-1))
		} else {
			log.Error("Propagating dangling block", "number", block.Number(), "hash", hash)
			return
		}
		// Send the block to a subset of our peers
		//发送块给相邻peers的数目子集,数目为原长度开根号值。
		// 由于一个块的的数据量不小,如果每个peer都发送新区块的话,会照成网络拥堵,于是取Logn这么一个数量时间复杂度
		transfer := peers[:int(math.Sqrt(float64(len(peers))))]
		for _, peer := range transfer {
			peer.SendNewBlock(block, td)
		}
		log.Trace("Propagated block", "hash", hash, "recipients", len(transfer), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
		return
	}
	// Otherwise if the block is indeed in out own chain, announce it
	//propagate为否,发送NewBlockHashes消息,仅仅发送hash和block number,不带td.
	if pm.blockchain.HasBlock(hash, block.NumberU64()) {
		for _, peer := range peers {
			peer.SendNewBlockHashes([]common.Hash{hash}, []uint64{block.NumberU64()})
		}
		log.Trace("Announced block", "hash", hash, "recipients", len(peers), "duration", common.PrettyDuration(time.Since(block.ReceivedAt)))
	}
}
  •     run

        当p2p的server启动的时候,会主动的找节点去连接,或者被其他的节点连接。 连接的过程是首先进行加密信道的握手,然后进行协议的握手。 最后为每个协议启动goroutine 执行Run方法来把控制交给最终的协议。 这个run方法首先创建了一个peer对象,然后调用了handle方法来处理这个peer

	manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{
			Name:    ProtocolName,
			Version: version,
			Length:  ProtocolLengths[i],
			Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
				peer := manager.newPeer(int(version), p, rw)
				select {
				case manager.newPeerCh <- peer:  //把peer发送到newPeerCh通道
					manager.wg.Add(1)
					defer manager.wg.Done()
					return manager.handle(peer)
				case <-manager.quitSync:        // 调用handle方法
					return p2p.DiscQuitting
				}
			},
			NodeInfo: func() interface{} {
				return manager.NodeInfo()
			},
			PeerInfo: func(id discover.NodeID) interface{} {
				if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
					return p.Info()
				}
				return nil
			},
		})
	}
  •   handle 
 // handle is the callback invoked to manage the life cycle of an eth peer. When
    // this function terminates, the peer is disconnected.
    // handle是一个回调方法,用来管理eth的peer的生命周期管理。 当这个方法退出的时候,peer的连接也会断开。
    func (pm *ProtocolManager) handle(p *peer) error {
        if pm.peers.Len() >= pm.maxPeers {
            return p2p.DiscTooManyPeers
        }
        p.Log().Debug("Ethereum peer connected", "name", p.Name())
    
        // Execute the Ethereum handshake
        td, head, genesis := pm.blockchain.Status()
        // td是total difficult, head是当前的区块头,genesis是创世区块的信息。 只有创世区块相同才能握手成功。
        if err := p.Handshake(pm.networkId, td, head, genesis); err != nil {
            p.Log().Debug("Ethereum handshake failed", "err", err)
            return err
        }
        if rw, ok := p.rw.(*meteredMsgReadWriter); ok {
            rw.Init(p.version)
        }
        // Register the peer locally
        // 把peer注册到本地
        if err := pm.peers.Register(p); err != nil {
            p.Log().Error("Ethereum peer registration failed", "err", err)
            return err
        }
        defer pm.removePeer(p.id)
    
        // Register the peer in the downloader. If the downloader considers it banned, we disconnect
        // 把peer注册给downloader. 如果downloader认为这个peer被禁,那么断开连接。
        if err := pm.downloader.RegisterPeer(p.id, p.version, p); err != nil {
            return err
        }
        // Propagate existing transactions. new transactions appearing
        // after this will be sent via broadcasts.
        // 把当前pending的交易发送给对方,这个只在连接刚建立的时候发生
        pm.syncTransactions(p)
    
        // If we're DAO hard-fork aware, validate any remote peer with regard to the hard-fork
        // 验证peer的DAO硬分叉
        if daoBlock := pm.chainconfig.DAOForkBlock; daoBlock != nil {
            // Request the peer's DAO fork header for extra-data validation
            if err := p.RequestHeadersByNumber(daoBlock.Uint64(), 1, 0, false); err != nil {
                return err
            }
            // Start a timer to disconnect if the peer doesn't reply in time
            // 如果15秒内没有接收到回应。那么断开连接。
            p.forkDrop = time.AfterFunc(daoChallengeTimeout, func() {
                p.Log().Debug("Timed out DAO fork-check, dropping")
                pm.removePeer(p.id)
            })
            // Make sure it's cleaned up if the peer dies off
            defer func() {
                if p.forkDrop != nil {
                    p.forkDrop.Stop()
                    p.forkDrop = nil
                }
            }()
        }
        // main loop. handle incoming messages.
        // 主循环。 处理进入的消息。
        for {
            if err := pm.handleMsg(p); err != nil {
                p.Log().Debug("Ethereum message handling failed", "err", err)
                return err
            }
        }
    }

       a. 首先进行握手,只有创世区块相同才能握手成功。

       b. 然后将这个网络节点加入到缓存的节点列表中pm.peers.Register(p)。

       c.  把本地的产生的未打包的交易发送给网络节点。

       d.  最后进入pm.handleMsg(p)主循环,不停的监听网络节点发过来的消息,并处理。

  • Handshake

      与远程网络节点握手Handshake()方法

    // Handshake executes the eth protocol handshake, negotiating version number,
    // network IDs, difficulties, head and genesis blocks.
    func (p *peer) Handshake(network uint64, td *big.Int, head common.Hash, genesis common.Hash) error {
        // Send out own handshake in a new thread
        // error的channel的大小是2, 就是为了一次性处理下面的两个goroutine方法
        errc := make(chan error, 2)
        var status statusData // safe to read after two values have been received from errc
        go func() {
            errc <- p2p.Send(p.rw, StatusMsg, &statusData{
                ProtocolVersion: uint32(p.version),
                NetworkId: network,
                TD: td,
                CurrentBlock: head,
                GenesisBlock: genesis,
            })

        }()

        go func() {
            errc <- p.readStatus(network, &status, genesis)
        }()

        timeout := time.NewTimer(handshakeTimeout)
        defer timeout.Stop()
        // 如果接收到任何一个错误(发送,接收),或者是超时, 那么就断开连接。
        for i := 0; i < 2; i++ {
            select {
            case err := <-errc:
               if err != nil {
                    return err
                }
            case <-timeout.C:
                return p2p.DiscReadTimeout

            }
        }
        p.td, p.head = status.TD, status.CurrentBlock
        return nil
    }

       把本地节点的状态发送给远程节点包括ProtocolVersion、NetworkId、TD、CurrentBlock、GenesisBlock,然后读取返回的状态数据,并做对比,如果都满足条件就握手成功。   

  2. 区块同步

     区块同步分为主动尝试发起和被动接受消息发起。

  2.1 fetcher被动接受消息发起同步

    fetcher是用来辅助同步区块数据的,累积所有其他个体发送来的有关新数据的宣布消息,并在自身对照后,安排相应的获取请        求,但它并不做真正下载区块数据的事情,下载的事情交由downloader来做。

        a.接收NewBlockHashesMsg广播notify通知并处理,这里不涉及td。 

         b.接收BlockHeadersMsg消息,涉及td的判断。

  •    fetcher NewBlockHashesMsg消息

      在收到 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)

}

从广播通知里会获取到一个newBlockHashesData的列表。newBlockHashesData只包括block的hash值和block的number值。然后每个newBlockHashesData调用pm.fetcher.Notify(p.id, block.Hash, block.Number, time.Now(), p.RequestOneHeader, p.RequestBodies)方法,除了传入block的hash值和block的number值,还需要传入当前的时间戳,peer.go的两个函数指针。

  •  fetcher NewBlockMsg消息同步
            case msg.Code == NewBlockMsg:
            // Retrieve and decode the propagated block
            var request newBlockData
            if err := msg.Decode(&request); err != nil {
                return errResp(ErrDecode, "%v: %v", msg, err)
            }
            request.Block.ReceivedAt = msg.ReceivedAt
            request.Block.ReceivedFrom = p
    
            // Mark the peer as owning the block and schedule it for import
            p.MarkBlock(request.Block.Hash())
            pm.fetcher.Enqueue(p.id, request.Block)
    
            // Assuming the block is importable by the peer, but possibly not yet done so,
            // calculate the head hash and TD that the peer truly must have.
            var (
                trueHead = request.Block.ParentHash()
                trueTD   = new(big.Int).Sub(request.TD, request.Block.Difficulty())
            )
            // Update the peers total difficulty if better than the previous
            if _, td := p.Head(); trueTD.Cmp(td) > 0 {
                // 如果peer的真实的TD和head和我们这边记载的不同, 设置peer真实的head和td,
                p.SetHead(trueHead, trueTD)
    
                // Schedule a sync if above ours. Note, this will not fire a sync for a gap of
                // a singe block (as the true TD is below the propagated block), however this
                // scenario should easily be covered by the fetcher.
                // 如果真实的TD比我们的TD大,那么请求和这个peer同步。
                currentBlock := pm.blockchain.CurrentBlock()
                if trueTD.Cmp(pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())) > 0 {
                    go pm.synchronise(p)
                }
            }

    最后调用go pm.synchronise(p) 尝试发起同步并调用downloader下载。

 2.2主动尝试发起同步

  •    syncer

   节点定时与相邻个体进行区块全链的强制同步。syncer()首先启动fetcher成员,然后进入一个无限循环,每次循环中都会向相邻peer列表中“最优”的那个peer作一次区块全链同步。

// syncer is responsible for periodically synchronising with the network, both
// downloading hashes and blocks as well as handling the announcement handler.
func (pm *ProtocolManager) syncer() {
	// Start and ensure cleanup of sync mechanisms
	pm.fetcher.Start()
	defer pm.fetcher.Stop()
	defer pm.downloader.Terminate()

	// Wait for different events to fire synchronisation operations
	forceSync := time.NewTicker(forceSyncCycle)
	defer forceSync.Stop()

	for {
		select {
		case <-pm.newPeerCh:
			// Make sure we have peers to select from, then sync
            //如果有新登记(加入)的相邻个体,则在整个peer列表数目大于5时,发起同步
			if pm.peers.Len() < minDesiredPeerCount {
				break
			}
			go pm.synchronise(pm.peers.BestPeer())

		case <-forceSync.C:
			// Force a sync even if not enough peers are present
            //如果没有新peer到达,则以10s为间隔超时,强制开始一次同步。
			go pm.synchronise(pm.peers.BestPeer())

		case <-pm.noMorePeers:
			return
		}
	}
}

     发起上述同步的理由分两种:

              a. 如果有新登记(加入)的相邻个体,则在整个peer列表数目大于5时,发起之;

                    当P2P server执行 ProtocolManager 的p2p.Protocol 的Run指针的时候会send pm.newPeerCh,这时候选择最优的网                      络节点,启动pm.synchronise(pm.peers.BestPeer()) goroutine。

              b. 如果没有新peer到达,则以10s为间隔定时的发起之。

这里所谓"最优"指的是peer中所维护区块链的TotalDifficulty(td)最高,由于Td是全链中从创世块到最新头块的Difficulty值总和,所以Td值最高就意味着它的区块链是最新的,跟这样的peer作区块全链同步,显然改动量是最小的,此即"最优"。

Synchronise

// synchronise tries to sync up our local block chain with a remote peer.
//尝试同步本地区块链与远程对等点,此节点主动尝试发起的同步
func (pm *ProtocolManager) synchronise(peer *peer) {
	// Short circuit if no peers are available
	if peer == nil {
		return
	}
	// Make sure the peer's TD is higher than our own
	//peer的TD必须比本地的TD值大,意即比本地区块更新
	currentBlock := pm.blockchain.CurrentBlock()
	td := pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64())

	pHead, pTd := peer.Head()
	if pTd.Cmp(td) <= 0 {
		return
	}
	// Otherwise try to sync with the downloader
	//开始downloader同步流程
	mode := downloader.FullSync
	if atomic.LoadUint32(&pm.fastSync) == 1 {
		// Fast sync was explicitly requested, and explicitly granted
		mode = downloader.FastSync
	} else if currentBlock.NumberU64() == 0 && pm.blockchain.CurrentFastBlock().NumberU64() > 0 {
		// The database seems empty as the current block is the genesis. Yet the fast
		// block is ahead, so fast sync was enabled for this node at a certain point.
		// The only scenario where this can happen is if the user manually (or via a
		// bad block) rolled back a fast sync node below the sync point. In this case
		// however it's safe to reenable fast sync.
		atomic.StoreUint32(&pm.fastSync, 1)
		mode = downloader.FastSync
	}

	if mode == downloader.FastSync {
		// Make sure the peer's total difficulty we are synchronizing is higher.
		//远程peer的td必须是最高的
		if pm.blockchain.GetTdByHash(pm.blockchain.CurrentFastBlock().Hash()).Cmp(pTd) >= 0 {
			return
		}
	}

	// Run the sync cycle, and disable fast sync if we've went past the pivot block
	//调用downloader的Synchronise开始同步循环,如果我们通了主块验证关闭快速同步模式
	if err := pm.downloader.Synchronise(peer.id, pHead, pTd, mode); err != nil {
		return
	}
	if atomic.LoadUint32(&pm.fastSync) == 1 {
		log.Info("Fast sync complete, auto disabling")
		atomic.StoreUint32(&pm.fastSync, 0)
	}
	atomic.StoreUint32(&pm.acceptTxs, 1) // Mark initial sync done
	if head := pm.blockchain.CurrentBlock(); head.NumberU64() > 0 {
		// We've completed a sync cycle, notify all peers of new state. This path is
		// essential in star-topology networks where a gateway node needs to notify
		// all its out-of-date peers of the availability of a new block. This failure
		// scenario will most often crop up in private and hackathon networks with
		// degenerate connectivity, but it should be healthy for the mainnet too to
		// more reliably update peers or the local TD state.
		//我们完成了一个同步周期,通知所有的对等点新的状态。
		//在星形拓扑网络中,网关节点需要通知其所有过时的对等节点一个新块的可用性这条路径是必不可少的。
		//这种失败场景通常会出现在带有退化连接的私有和hackathon网络中,但对于主流网络来说,更可靠地更新对等点或本地TD状态也应该是健康的。
		go pm.BroadcastBlock(head, false)
	}
}

2.3 Downloader

    2.3.1  启动Downloader

ProtocolManager收到新的区块消息广播或者有新的P2P网络节点加入的时候会调用ProtocolManager的 synchronise(peer *peer)方法,这时候会调用Downloader的Synchronise(peer.id, pHead, pTd, mode)方法。

Synchronise方法,重置d.queue和d.peers,清空d.bodyWakeCh, d.receiptWakeCh,d.headerCh, d.bodyCh, d.receiptCh,d.headerProcCh。调用d.syncWithPeer(p, hash, td)方法:

// syncWithPeer starts a block synchronization based on the hash chain from the
// specified peer and head hash.
func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.Int) (err error) {
	d.mux.Post(StartEvent{})
	defer func() {
		// reset on error
		if err != nil {
			d.mux.Post(FailedEvent{err})
		} else {
			d.mux.Post(DoneEvent{})
		}
	}()
	if p.version < 62 {
		return errTooOld
	}

	log.Debug("Synchronising with the network", "peer", p.id, "eth", p.version, "head", hash, "td", td, "mode", d.mode)
	defer func(start time.Time) {
		log.Debug("Synchronisation terminated", "elapsed", time.Since(start))
	}(time.Now())

	// Look up the sync boundaries: the common ancestor and the target block
	latest, err := d.fetchHeight(p)
	if err != nil {
		return err
	}
	height := latest.Number.Uint64()

	origin, err := d.findAncestor(p, height)
	if err != nil {
		return err
	}
	d.syncStatsLock.Lock()
	if d.syncStatsChainHeight <= origin || d.syncStatsChainOrigin > origin {
		d.syncStatsChainOrigin = origin
	}
	d.syncStatsChainHeight = height
	d.syncStatsLock.Unlock()

	// Ensure our origin point is below any fast sync pivot point
	pivot := uint64(0)
	if d.mode == FastSync {
		if height <= uint64(fsMinFullBlocks) {
			origin = 0
		} else {
			pivot = height - uint64(fsMinFullBlocks)
			if pivot <= origin {
				origin = pivot - 1
			}
		}
	}
	d.committed = 1
	if d.mode == FastSync && pivot != 0 {
		d.committed = 0
	}
	// Initiate the sync using a concurrent header and content retrieval algorithm
	d.queue.Prepare(origin+1, d.mode)
	if d.syncInitHook != nil {
		d.syncInitHook(origin, height)
	}

	fetchers := []func() error{
		func() error { return d.fetchHeaders(p, origin+1, pivot) }, // Headers are always retrieved
		func() error { return d.fetchBodies(origin + 1) },          // Bodies are retrieved during normal and fast sync
		func() error { return d.fetchReceipts(origin + 1) },        // Receipts are retrieved during fast sync
		func() error { return d.processHeaders(origin+1, pivot, td) },
	}
	if d.mode == FastSync {
		fetchers = append(fetchers, func() error { return d.processFastSyncContent(latest) })
	} else if d.mode == FullSync {
		fetchers = append(fetchers, d.processFullSyncContent)
	}
	return d.spawnSync(fetchers)
}

首先调用latest, err := d.fetchHeight(p)获取到peer节点最新的区块头

func (d *Downloader) fetchHeight(p *peerConnection) (*types.Header, error) {
    p.log.Debug("Retrieving remote chain height")

    // Request the advertised remote head block and wait for the response
    head, _ := p.peer.Head()
    go p.peer.RequestHeadersByHash(head, 1, 0, false)

    ttl := d.requestTTL()
    timeout := time.After(ttl)
    for {
        select {
        case <-d.cancelCh:
            return nil, errCancelBlockFetch

        case packet := <-d.headerCh:
            // Discard anything not from the origin peer
            if packet.PeerId() != p.id {
                log.Debug("Received headers from incorrect peer", "peer", packet.PeerId())
                break
            }
            // Make sure the peer actually gave something valid
            headers := packet.(*headerPack).headers
            if len(headers) != 1 {
                p.log.Debug("Multiple headers for single request", "headers", len(headers))
                return nil, errBadPeer
            }
            head := headers[0]
            p.log.Debug("Remote head header identified", "number", head.Number, "hash", head.Hash())
            return head, nil

        case <-timeout:
            p.log.Debug("Waiting for head header timed out", "elapsed", ttl)
            return nil, errTimeout

        case <-d.bodyCh:
        case <-d.receiptCh:
            // Out of bounds delivery, ignore
        }
    }
}
  • 1,调用peer.RequestHeadersByHash(head, 1, 0, false),给网络节点发送一个GetBlockHeadersMsg的消息
  • 2,然后阻塞住线程,直到收到d.headerCh或者timeout
  • 3,本地节点会收到网络节点的BlockHeadersMsg的消息返回
  • 4,调用downloader.DeliverHeaders(p.id, headers)
  • 5,这时候会把p.id和headers打包发送给d.headerCh
  • 6,这时候select收到d.headerCh,阻塞打开,并返回header内容

syncWithPeer() 方法接着调用 d.findAncestor(p, height)来获取本地节点和网络节点共同的祖先:

func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, error) {
    // Figure out the valid ancestor range to prevent rewrite attacks
    floor, ceil := int64(-1), d.lightchain.CurrentHeader().Number.Uint64()

    if d.mode == FullSync {
        ceil = d.blockchain.CurrentBlock().NumberU64()
    } else if d.mode == FastSync {
        ceil = d.blockchain.CurrentFastBlock().NumberU64()
    }
    if ceil >= MaxForkAncestry {
        floor = int64(ceil - MaxForkAncestry)
    }
    p.log.Debug("Looking for common ancestor", "local", ceil, "remote", height)

    // Request the topmost blocks to short circuit binary ancestor lookup
    head := ceil
    if head > height {
        head = height
    }
    from := int64(head) - int64(MaxHeaderFetch)
    if from < 0 {
        from = 0
    }
    // Span out with 15 block gaps into the future to catch bad head reports
    limit := 2 * MaxHeaderFetch / 16
    count := 1 + int((int64(ceil)-from)/16)
    if count > limit {
        count = limit
    }
    go p.peer.RequestHeadersByNumber(uint64(from), count, 15, false)

    // Wait for the remote response to the head fetch
    number, hash := uint64(0), common.Hash{}

    ttl := d.requestTTL()
    timeout := time.After(ttl)

    for finished := false; !finished; {
        select {
        case <-d.cancelCh:
            return 0, errCancelHeaderFetch

        case packet := <-d.headerCh:
            // Discard anything not from the origin peer
            if packet.PeerId() != p.id {
                log.Debug("Received headers from incorrect peer", "peer", packet.PeerId())
                break
            }
            // Make sure the peer actually gave something valid
            headers := packet.(*headerPack).headers
            if len(headers) == 0 {
                p.log.Warn("Empty head header set")
                return 0, errEmptyHeaderSet
            }
            // Make sure the peer's reply conforms to the request
            for i := 0; i < len(headers); i++ {
                if number := headers[i].Number.Int64(); number != from+int64(i)*16 {
                    p.log.Warn("Head headers broke chain ordering", "index", i, "requested", from+int64(i)*16, "received", number)
                    return 0, errInvalidChain
                }
            }
            // Check if a common ancestor was found
            finished = true
            for i := len(headers) - 1; i >= 0; i-- {
                // Skip any headers that underflow/overflow our requested set
                if headers[i].Number.Int64() < from || headers[i].Number.Uint64() > ceil {
                    continue
                }
                // Otherwise check if we already know the header or not
                if (d.mode == FullSync && d.blockchain.HasBlock(headers[i].Hash(), headers[i].Number.Uint64())) || (d.mode != FullSync && d.lightchain.HasHeader(headers[i].Hash(), headers[i].Number.Uint64())) {
                    number, hash = headers[i].Number.Uint64(), headers[i].Hash()

                    // If every header is known, even future ones, the peer straight out lied about its head
                    if number > height && i == limit-1 {
                        p.log.Warn("Lied about chain head", "reported", height, "found", number)
                        return 0, errStallingPeer
                    }
                    break
                }
            }

        case <-timeout:
            p.log.Debug("Waiting for head header timed out", "elapsed", ttl)
            return 0, errTimeout

        case <-d.bodyCh:
        case <-d.receiptCh:
            // Out of bounds delivery, ignore
        }
    }
    // If the head fetch already found an ancestor, return
    if !common.EmptyHash(hash) {
        if int64(number) <= floor {
            p.log.Warn("Ancestor below allowance", "number", number, "hash", hash, "allowance", floor)
            return 0, errInvalidAncestor
        }
        p.log.Debug("Found common ancestor", "number", number, "hash", hash)
        return number, nil
    }
    // Ancestor not found, we need to binary search over our chain
    start, end := uint64(0), head
    if floor > 0 {
        start = uint64(floor)
    }
    for start+1 < end {
        // Split our chain interval in two, and request the hash to cross check
        check := (start + end) / 2

        ttl := d.requestTTL()
        timeout := time.After(ttl)

        go p.peer.RequestHeadersByNumber(check, 1, 0, false)

        // Wait until a reply arrives to this request
        for arrived := false; !arrived; {
            select {
            case <-d.cancelCh:
                return 0, errCancelHeaderFetch

            case packer := <-d.headerCh:
                // Discard anything not from the origin peer
                if packer.PeerId() != p.id {
                    log.Debug("Received headers from incorrect peer", "peer", packer.PeerId())
                    break
                }
                // Make sure the peer actually gave something valid
                headers := packer.(*headerPack).headers
                if len(headers) != 1 {
                    p.log.Debug("Multiple headers for single request", "headers", len(headers))
                    return 0, errBadPeer
                }
                arrived = true

                // Modify the search interval based on the response
                if (d.mode == FullSync && !d.blockchain.HasBlock(headers[0].Hash(), headers[0].Number.Uint64())) || (d.mode != FullSync && !d.lightchain.HasHeader(headers[0].Hash(), headers[0].Number.Uint64())) {
                    end = check
                    break
                }
                header := d.lightchain.GetHeaderByHash(headers[0].Hash()) // Independent of sync mode, header surely exists
                if header.Number.Uint64() != check {
                    p.log.Debug("Received non requested header", "number", header.Number, "hash", header.Hash(), "request", check)
                    return 0, errBadPeer
                }
                start = check

            case <-timeout:
                p.log.Debug("Waiting for search header timed out", "elapsed", ttl)
                return 0, errTimeout

            case <-d.bodyCh:
            case <-d.receiptCh:
                // Out of bounds delivery, ignore
            }
        }
    }
    // Ensure valid ancestry and return
    if int64(start) <= floor {
        p.log.Warn("Ancestor below allowance", "number", start, "hash", hash, "allowance", floor)
        return 0, errInvalidAncestor
    }
    p.log.Debug("Found common ancestor", "number", start, "hash", hash)
    return start, nil
}
  • 1,调用peer.RequestHeadersByNumber(uint64(from), count, 15, false),获取header。这里传入 count和 15,指从本地最高的header往前数192个区块的头,每16个区块取一个区块头。为了后面select收到d.headerCh时加以验证。
  • 2,select收到了headers,遍历header,看是否在本地是否存在这个header,如果有,并且不为空,就说明找到共同的祖先,返回祖先number
  • 3,如果没有找到共同的祖先,再重新从本地的区块链MaxForkAncestry起的一半的位置开始取区块头,一一验证是否跟网络节点返回的header一致,如果有就说明有共同的祖先,并返回,没有的话就返回0.

继续syncWithPeer()方法,找到同步的轴心的pivot,最后把要同步的数据和同步的方法传给d.spawnSync(fetchers),并执行。d.spawnSync(fetchers)挨个执行传入的同步方法。

2.3.2 Downloader同步数据方法

    fetchHeaders(),fetchBodies() , fetchReceipts()

func (d *Downloader) fetchHeaders(p *peerConnection, from uint64, pivot uint64) error {
    p.log.Debug("Directing header downloads", "origin", from)
    defer p.log.Debug("Header download terminated")

    // Create a timeout timer, and the associated header fetcher
    skeleton := true            // Skeleton assembly phase or finishing up
    request := time.Now()       // time of the last skeleton fetch request
    timeout := time.NewTimer(0) // timer to dump a non-responsive active peer
    <-timeout.C                 // timeout channel should be initially empty
    defer timeout.Stop()

    var ttl time.Duration
    getHeaders := func(from uint64) {
        request = time.Now()

        ttl = d.requestTTL()
        timeout.Reset(ttl)

        if skeleton {
            p.log.Trace("Fetching skeleton headers", "count", MaxHeaderFetch, "from", from)
            go p.peer.RequestHeadersByNumber(from+uint64(MaxHeaderFetch)-1, MaxSkeletonSize, MaxHeaderFetch-1, false)
        } else {
            p.log.Trace("Fetching full headers", "count", MaxHeaderFetch, "from", from)
            go p.peer.RequestHeadersByNumber(from, MaxHeaderFetch, 0, false)
        }
    }
    // Start pulling the header chain skeleton until all is done
    getHeaders(from)

    for {
        select {
        case <-d.cancelCh:
            return errCancelHeaderFetch

        case packet := <-d.headerCh:
            // Make sure the active peer is giving us the skeleton headers
            if packet.PeerId() != p.id {
                log.Debug("Received skeleton from incorrect peer", "peer", packet.PeerId())
                break
            }
            headerReqTimer.UpdateSince(request)
            timeout.Stop()

            // If the skeleton's finished, pull any remaining head headers directly from the origin
            if packet.Items() == 0 && skeleton {
                skeleton = false
                getHeaders(from)
                continue
            }
            // If no more headers are inbound, notify the content fetchers and return
            if packet.Items() == 0 {
                // Don't abort header fetches while the pivot is downloading
                if atomic.LoadInt32(&d.committed) == 0 && pivot <= from {
                    p.log.Debug("No headers, waiting for pivot commit")
                    select {
                    case <-time.After(fsHeaderContCheck):
                        getHeaders(from)
                        continue
                    case <-d.cancelCh:
                        return errCancelHeaderFetch
                    }
                }
                // Pivot done (or not in fast sync) and no more headers, terminate the process
                p.log.Debug("No more headers available")
                select {
                case d.headerProcCh <- nil:
                    return nil
                case <-d.cancelCh:
                    return errCancelHeaderFetch
                }
            }
            headers := packet.(*headerPack).headers

            // If we received a skeleton batch, resolve internals concurrently
            if skeleton {
                filled, proced, err := d.fillHeaderSkeleton(from, headers)
                if err != nil {
                    p.log.Debug("Skeleton chain invalid", "err", err)
                    return errInvalidChain
                }
                headers = filled[proced:]
                from += uint64(proced)
            }
            // Insert all the new headers and fetch the next batch
            if len(headers) > 0 {
                p.log.Trace("Scheduling new headers", "count", len(headers), "from", from)
                select {
                case d.headerProcCh <- headers:
                case <-d.cancelCh:
                    return errCancelHeaderFetch
                }
                from += uint64(len(headers))
            }
            getHeaders(from)

        case <-timeout.C:
            if d.dropPeer == nil {
                // The dropPeer method is nil when `--copydb` is used for a local copy.
                // Timeouts can occur if e.g. compaction hits at the wrong time, and can be ignored
                p.log.Warn("Downloader wants to drop peer, but peerdrop-function is not set", "peer", p.id)
                break
            }
            // Header retrieval timed out, consider the peer bad and drop
            p.log.Debug("Header request timed out", "elapsed", ttl)
            headerTimeoutMeter.Mark(1)
            d.dropPeer(p.id)

            // Finish the sync gracefully instead of dumping the gathered data though
            for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
                select {
                case ch <- false:
                case <-d.cancelCh:
                }
            }
            select {
            case d.headerProcCh <- nil:
            case <-d.cancelCh:
            }
            return errBadPeer
        }
    }
}
  • 1,getHeaders()调用peer.RequestHeadersByNumber()方法 获取网络节点的headers。
  • 2,有两种获取方式,首先走的是skeleton方式,从查找到的共同祖先区块+192个区块位置开始,每隔192个区块,获取128个区块头。非skeleton方式,从共同祖先区块开始,获取192个区块头。
  • 3,如果第一种方式获取不到区块头,则执行第二种获取方式,如果第二种方式还是没有获取到区块头的话,直接返回
  • 4,如果是skeleton获取到的,调用fillHeaderSkeleton()方法加入到skeleton header chain
  • 5,然后调整from值,再递归调用getHeaders()方法
func (d *Downloader) fillHeaderSkeleton(from uint64, skeleton []*types.Header) ([]*types.Header, int, error) {
    log.Debug("Filling up skeleton", "from", from)
    d.queue.ScheduleSkeleton(from, skeleton)

    var (
        deliver = func(packet dataPack) (int, error) {
            pack := packet.(*headerPack)
            return d.queue.DeliverHeaders(pack.peerId, pack.headers, d.headerProcCh)
        }
        expire   = func() map[string]int { return d.queue.ExpireHeaders(d.requestTTL()) }
        throttle = func() bool { return false }
        reserve  = func(p *peerConnection, count int) (*fetchRequest, bool, error) {
            return d.queue.ReserveHeaders(p, count), false, nil
        }
        fetch    = func(p *peerConnection, req *fetchRequest) error { return p.FetchHeaders(req.From, MaxHeaderFetch) }
        capacity = func(p *peerConnection) int { return p.HeaderCapacity(d.requestRTT()) }
        setIdle  = func(p *peerConnection, accepted int) { p.SetHeadersIdle(accepted) }
    )
    err := d.fetchParts(errCancelHeaderFetch, d.headerCh, deliver, d.queue.headerContCh, expire,
        d.queue.PendingHeaders, d.queue.InFlightHeaders, throttle, reserve,
        nil, fetch, d.queue.CancelHeaders, capacity, d.peers.HeaderIdlePeers, setIdle, "headers")

    log.Debug("Skeleton fill terminated", "err", err)

    filled, proced := d.queue.RetrieveHeaders()
    return filled, proced, err
}

     a) 把skeleton的headers加入queue.ScheduleSkeleton调度队列,

     b) 然后执行d.fetchParts()方法。

d.fetchParts()方法主要做了这几件事情

  • 1,对收到的headers执行d.queue.DeliverHeaders()方法。
  • 2,如果d.queue.PendingHeaders有pending的headers,调用d.peers.HeaderIdlePeers获取到idle的peers
  • 3,调用d.queue.ReserveHeaders把pending的headers储备到idle的peers里面
  • 4,用idle的peers调用p.FetchHeaders(req.From, MaxHeaderFetch)去获取headers

c) 最后执行d.queue.RetrieveHeaders(),获取到filled进去的headers

其他同步区块数据的方法d.fetchBodies() , d.fetchReceipts() 和fetchHeaders()流程类似,还更简单一些。

  2.3.3 Downloader同步数据过程

  d.processHeaders(), d.processFastSyncContent(latest) , d.processFullSyncContent

     2.3.3.1    d.processHeaders() 方法

func (d *Downloader) processHeaders(origin uint64, pivot uint64, td *big.Int) error {
    // Keep a count of uncertain headers to roll back
    rollback := []*types.Header{}
    defer func() {
        if len(rollback) > 0 {
            // Flatten the headers and roll them back
            hashes := make([]common.Hash, len(rollback))
            for i, header := range rollback {
                hashes[i] = header.Hash()
            }
            lastHeader, lastFastBlock, lastBlock := d.lightchain.CurrentHeader().Number, common.Big0, common.Big0
            if d.mode != LightSync {
                lastFastBlock = d.blockchain.CurrentFastBlock().Number()
                lastBlock = d.blockchain.CurrentBlock().Number()
            }
            d.lightchain.Rollback(hashes)
            curFastBlock, curBlock := common.Big0, common.Big0
            if d.mode != LightSync {
                curFastBlock = d.blockchain.CurrentFastBlock().Number()
                curBlock = d.blockchain.CurrentBlock().Number()
            }
            log.Warn("Rolled back headers", "count", len(hashes),
                "header", fmt.Sprintf("%d->%d", lastHeader, d.lightchain.CurrentHeader().Number),
                "fast", fmt.Sprintf("%d->%d", lastFastBlock, curFastBlock),
                "block", fmt.Sprintf("%d->%d", lastBlock, curBlock))
        }
    }()

    // Wait for batches of headers to process
    gotHeaders := false

    for {
        select {
        case <-d.cancelCh:
            return errCancelHeaderProcessing

        case headers := <-d.headerProcCh:
            // Terminate header processing if we synced up
            if len(headers) == 0 {
                // Notify everyone that headers are fully processed
                for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
                    select {
                    case ch <- false:
                    case <-d.cancelCh:
                    }
                }
                if d.mode != LightSync {
                    head := d.blockchain.CurrentBlock()
                    if !gotHeaders && td.Cmp(d.blockchain.GetTd(head.Hash(), head.NumberU64())) > 0 {
                        return errStallingPeer
                    }
                }
                if d.mode == FastSync || d.mode == LightSync {
                    head := d.lightchain.CurrentHeader()
                    if td.Cmp(d.lightchain.GetTd(head.Hash(), head.Number.Uint64())) > 0 {
                        return errStallingPeer
                    }
                }
                // Disable any rollback and return
                rollback = nil
                return nil
            }
            // Otherwise split the chunk of headers into batches and process them
            gotHeaders = true

            for len(headers) > 0 {
                // Terminate if something failed in between processing chunks
                select {
                case <-d.cancelCh:
                    return errCancelHeaderProcessing
                default:
                }
                // Select the next chunk of headers to import
                limit := maxHeadersProcess
                if limit > len(headers) {
                    limit = len(headers)
                }
                chunk := headers[:limit]

                // In case of header only syncing, validate the chunk immediately
                if d.mode == FastSync || d.mode == LightSync {
                    // Collect the yet unknown headers to mark them as uncertain
                    unknown := make([]*types.Header, 0, len(headers))
                    for _, header := range chunk {
                        if !d.lightchain.HasHeader(header.Hash(), header.Number.Uint64()) {
                            unknown = append(unknown, header)
                        }
                    }
                    // If we're importing pure headers, verify based on their recentness
                    frequency := fsHeaderCheckFrequency
                    if chunk[len(chunk)-1].Number.Uint64()+uint64(fsHeaderForceVerify) > pivot {
                        frequency = 1
                    }
                    if n, err := d.lightchain.InsertHeaderChain(chunk, frequency); err != nil {
                        // If some headers were inserted, add them too to the rollback list
                        if n > 0 {
                            rollback = append(rollback, chunk[:n]...)
                        }
                        log.Debug("Invalid header encountered", "number", chunk[n].Number, "hash", chunk[n].Hash(), "err", err)
                        return errInvalidChain
                    }
                    // All verifications passed, store newly found uncertain headers
                    rollback = append(rollback, unknown...)
                    if len(rollback) > fsHeaderSafetyNet {
                        rollback = append(rollback[:0], rollback[len(rollback)-fsHeaderSafetyNet:]...)
                    }
                }
                // Unless we're doing light chains, schedule the headers for associated content retrieval
                if d.mode == FullSync || d.mode == FastSync {
                    // If we've reached the allowed number of pending headers, stall a bit
                    for d.queue.PendingBlocks() >= maxQueuedHeaders || d.queue.PendingReceipts() >= maxQueuedHeaders {
                        select {
                        case <-d.cancelCh:
                            return errCancelHeaderProcessing
                        case <-time.After(time.Second):
                        }
                    }
                    // Otherwise insert the headers for content retrieval
                    inserts := d.queue.Schedule(chunk, origin)
                    if len(inserts) != len(chunk) {
                        log.Debug("Stale headers")
                        return errBadPeer
                    }
                }
                headers = headers[limit:]
                origin += uint64(limit)
            }
            // Signal the content downloaders of the availablility of new tasks
            for _, ch := range []chan bool{d.bodyWakeCh, d.receiptWakeCh} {
                select {
                case ch <- true:
                default:
                }
            }
        }
    }
}
  • 1,收到从fetchHeaders()方法 中d.headerProcCh发送过来的headers
  • 2,如果是FastSync或者LightSync模式,直接调用lightchain.InsertHeaderChain(chunk, frequency)插入到headerChain。
  • 3,如果是FullSync或者FastSyn模式,调用d.queue.Schedule(chunk, origin),放入downloader.queue来调度

2.3.3.2 processFastSyncContent() 方法

func (d *Downloader) processFastSyncContent(latest *types.Header) error {
    // Start syncing state of the reported head block. This should get us most of
    // the state of the pivot block.
    stateSync := d.syncState(latest.Root)
    defer stateSync.Cancel()
    go func() {
        if err := stateSync.Wait(); err != nil && err != errCancelStateFetch {
            d.queue.Close() // wake up WaitResults
        }
    }()
    // Figure out the ideal pivot block. Note, that this goalpost may move if the
    // sync takes long enough for the chain head to move significantly.
    pivot := uint64(0)
    if height := latest.Number.Uint64(); height > uint64(fsMinFullBlocks) {
        pivot = height - uint64(fsMinFullBlocks)
    }
    // To cater for moving pivot points, track the pivot block and subsequently
    // accumulated download results separatey.
    var (
        oldPivot *fetchResult   // Locked in pivot block, might change eventually
        oldTail  []*fetchResult // Downloaded content after the pivot
    )
    for {
        // Wait for the next batch of downloaded data to be available, and if the pivot
        // block became stale, move the goalpost
        results := d.queue.Results(oldPivot == nil) // Block if we're not monitoring pivot staleness
        if len(results) == 0 {
            // If pivot sync is done, stop
            if oldPivot == nil {
                return stateSync.Cancel()
            }
            // If sync failed, stop
            select {
            case <-d.cancelCh:
                return stateSync.Cancel()
            default:
            }
        }
        if d.chainInsertHook != nil {
            d.chainInsertHook(results)
        }
        if oldPivot != nil {
            results = append(append([]*fetchResult{oldPivot}, oldTail...), results...)
        }
        // Split around the pivot block and process the two sides via fast/full sync
        if atomic.LoadInt32(&d.committed) == 0 {
            latest = results[len(results)-1].Header
            if height := latest.Number.Uint64(); height > pivot+2*uint64(fsMinFullBlocks) {
                log.Warn("Pivot became stale, moving", "old", pivot, "new", height-uint64(fsMinFullBlocks))
                pivot = height - uint64(fsMinFullBlocks)
            }
        }
        P, beforeP, afterP := splitAroundPivot(pivot, results)
        if err := d.commitFastSyncData(beforeP, stateSync); err != nil {
            return err
        }
        if P != nil {
            // If new pivot block found, cancel old state retrieval and restart
            if oldPivot != P {
                stateSync.Cancel()

                stateSync = d.syncState(P.Header.Root)
                defer stateSync.Cancel()
                go func() {
                    if err := stateSync.Wait(); err != nil && err != errCancelStateFetch {
                        d.queue.Close() // wake up WaitResults
                    }
                }()
                oldPivot = P
            }
            // Wait for completion, occasionally checking for pivot staleness
            select {
            case <-stateSync.done:
                if stateSync.err != nil {
                    return stateSync.err
                }
                if err := d.commitPivotBlock(P); err != nil {
                    return err
                }
                oldPivot = nil

            case <-time.After(time.Second):
                oldTail = afterP
                continue
            }
        }
        // Fast sync done, pivot commit done, full import
        if err := d.importBlockResults(afterP); err != nil {
            return err
        }
    }
}
  • 1,同步最新的状态信息,的到最新的pivot值
  • 2,不停的从d.queue 的result缓存中获取要处理的result数据
  • 3,如果results数据为空,同时pivot也为空的时候,说明同步完成了,并返回
  • 4,根据pivot值和results计算:pivot值对应的result,和pivot值之前的results和pivot值之后的results
  • 5,调用commitFastSyncData把pivot值之前的results 插入本地区块链中,带上收据和交易数据
  • 6,更新同步状态信息后,把pivot值对应的result 调用commitPivotBlock插入本地区块链中,并调用FastSyncCommitHead,记录这个pivot的hash值
  • 7,调用d.importBlockResults把pivot值之后的results插入本地区块链中,这时候不插入区块交易收据数据。

2.3.3.3,processFullSyncContent()方法

func (d *Downloader) processFullSyncContent() error {
    for {
        results := d.queue.Results(true)
        if len(results) == 0 {
            return nil
        }
        if d.chainInsertHook != nil {
            d.chainInsertHook(results)
        }
        if err := d.importBlockResults(results); err != nil {
            return err
        }
    }
}

func (d *Downloader) importBlockResults(results []*fetchResult) error {
    // Check for any early termination requests
    if len(results) == 0 {
        return nil
    }
    select {
    case <-d.quitCh:
        return errCancelContentProcessing
    default:
    }
    // Retrieve the a batch of results to import
    first, last := results[0].Header, results[len(results)-1].Header
    log.Debug("Inserting downloaded chain", "items", len(results),
        "firstnum", first.Number, "firsthash", first.Hash(),
        "lastnum", last.Number, "lasthash", last.Hash(),
    )
    blocks := make([]*types.Block, len(results))
    for i, result := range results {
        blocks[i] = types.NewBlockWithHeader(result.Header).WithBody(result.Transactions, result.Uncles)
    }
    if index, err := d.blockchain.InsertChain(blocks); err != nil {
        log.Debug("Downloaded item processing failed", "number", results[index].Header.Number, "hash", results[index].Header.Hash(), "err", err)
        return errInvalidChain
    }
    return nil
}

processFullSyncContent方法比较简单:直接获取缓存的results数据,并插入到本地区块链中。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值