以太坊源码分析-同步之Syncing接口

Posted 区块链社区HiBlock

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以太坊源码分析-同步之Syncing接口相关的知识,希望对你有一定的参考价值。


内容来源:程序新视界


以太坊源码分析-同步之Syncing接口


在节点同步的过程中,我们经常需要执行eth.syncing来查看当前的同步情况,本篇博客带领大家看一下syncing api的源代码实现。


1

Syncing方法源代码


 
   
   
 
  1. // Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not

  2. // yet received the latest block headers from its pears. In case it is synchronizing:

  3. // - startingBlock: block number this node started to synchronise from

  4. // - currentBlock:  block number this node is currently importing

  5. // - highestBlock:  block number of the highest block header this node has received from peers

  6. // - pulledStates:  number of state entries processed until now

  7. // - knownStates:   number of known state entries that still need to be pulled

  8. func (s *PublicEthereumAPI) Syncing() (interface{}, error) {

  9.    progress := s.b.Downloader().Progress()

  10.    // Return not syncing if the synchronisation already completed

  11.    if progress.CurrentBlock >= progress.HighestBlock {

  12.        return false, nil

  13.    }

  14.    // Otherwise gather the block sync stats

  15.    return map[string]interface{}{

  16.        "startingBlock": hexutil.Uint64(progress.StartingBlock),

  17.        "currentBlock":  hexutil.Uint64(progress.CurrentBlock),

  18.        "highestBlock":  hexutil.Uint64(progress.HighestBlock),

  19.        "pulledStates":  hexutil.Uint64(progress.PulledStates),

  20.        "knownStates":   hexutil.Uint64(progress.KnownStates),

  21.    }, nil

  22. }


Syncing方法的源代码很简单,注释说明也已经很清楚了。通过这段源代码我们可以得知一下信息:


  • 当然CurrentBlock大于等于HighestBlock时返回false,这也正是通常所说的同步完成之后,再执行eth.syncing()函数会返回false的原因。

  • startingBlock:开始同步的起始区块编号;

  • currentBlock:当前正在导入的区块编号;

  • highestBlock:通过所链接的节点获得的当前最高的区块高度;

  • pulledStates:当前已经拉取的状态条目数;

  • knownStates:当前已知的待拉取的总状态条目数。


2

对应的结构体代码


下面是同步信息对应的结构体的代码及注释。


 
   
   
 
  1. // SyncProgress gives progress indications when the node is synchronising with

  2. // the Ethereum network.

  3. type SyncProgress struct {

  4.    StartingBlock uint64 // Block number where sync began

  5.    CurrentBlock  uint64 // Current block number where sync is at

  6.    HighestBlock  uint64 // Highest alleged block number in the chain

  7.    PulledStates  uint64 // Number of state trie entries already downloaded

  8.    KnownStates   uint64 // Total number of state trie entries known about

  9. }


3

结构体信息计算


上面看到当执行eth.syncing返回结果信息的代码,再延伸一下看看这些数据从哪里来。进入下面Progress方法的内部实现:


 
   
   
 
  1. progress := s.b.Downloader().Progress()


可以看到如下代码:


 
   
   
 
  1. // Progress retrieves the synchronisation boundaries, specifically the origin

  2. // block where synchronisation started at (may have failed/suspended); the block

  3. // or header sync is currently at; and the latest known block which the sync targets.

  4. //

  5. // In addition, during the state download phase of fast synchronisation the number

  6. // of processed and the total number of known states are also returned. Otherwise

  7. // these are zero.

  8. func (d *Downloader) Progress() ethereum.SyncProgress {

  9.    // Lock the current stats and return the progress

  10.    d.syncStatsLock.RLock()

  11.    defer d.syncStatsLock.RUnlock()

  12.    current := uint64(0)

  13.    switch d.mode {

  14.    case FullSync:

  15.        current = d.blockchain.CurrentBlock().NumberU64()

  16.    case FastSync:

  17.        current = d.blockchain.CurrentFastBlock().NumberU64()

  18.    case LightSync:

  19.        current = d.lightchain.CurrentHeader().Number.Uint64()

  20.    }

  21.    return ethereum.SyncProgress{

  22.        StartingBlock: d.syncStatsChainOrigin,

  23.        CurrentBlock:  current,

  24.        HighestBlock:  d.syncStatsChainHeight,

  25.        PulledStates:  d.syncStatsState.processed,

  26.        KnownStates:   d.syncStatsState.processed + d.syncStatsState.pending,

  27.    }

  28. }


从这端代码我们可以分析得出,curren


  • full模式:返回当前区块的高度;

  • fast模式:返回fast区块的高度;

  • light模式:返回当前的header编号;

  • 而KnownStates又是由PulledStates的值加上当前处于pending装的值获得。


4

总  结



延伸阅读




以上是关于以太坊源码分析-同步之Syncing接口的主要内容,如果未能解决你的问题,请参考以下文章

区块链 | 智能合约Ethereum源代码- 以太坊RPC通信实例和原理代码分析(上)

区块链入门教程以太坊源码分析fast sync算法一

区块链教程以太坊源码分析core-state源码分析

以太坊源码分析--p2p节点发现

以太坊控制台源码分析

[Ethereum] 以太坊源码分析p2p+eth