前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >以太坊源码分析-同步之Syncing接口

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

作者头像
程序新视界
发布2022-05-09 17:49:10
3900
发布2022-05-09 17:49:10
举报
文章被收录于专栏:丑胖侠丑胖侠

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

Syncing方法源代码

代码语言:javascript
复制
// Syncing returns false in case the node is currently not syncing with the network. It can be up to date or has not
// yet received the latest block headers from its pears. In case it is synchronizing:
// - startingBlock: block number this node started to synchronise from
// - currentBlock:  block number this node is currently importing
// - highestBlock:  block number of the highest block header this node has received from peers
// - pulledStates:  number of state entries processed until now
// - knownStates:   number of known state entries that still need to be pulled
func (s *PublicEthereumAPI) Syncing() (interface{}, error) {
    progress := s.b.Downloader().Progress()

    // Return not syncing if the synchronisation already completed
    if progress.CurrentBlock >= progress.HighestBlock {
        return false, nil
    }
    // Otherwise gather the block sync stats
    return map[string]interface{}{
        "startingBlock": hexutil.Uint64(progress.StartingBlock),
        "currentBlock":  hexutil.Uint64(progress.CurrentBlock),
        "highestBlock":  hexutil.Uint64(progress.HighestBlock),
        "pulledStates":  hexutil.Uint64(progress.PulledStates),
        "knownStates":   hexutil.Uint64(progress.KnownStates),
    }, nil
}

Syncing方法的源代码很简单,注释说明也已经很清楚了。通过这段源代码我们可以得知一下信息: - 当然CurrentBlock大于等于HighestBlock时返回false,这也正是通常所说的同步完成之后,再执行eth.syncing()函数会返回false的原因。 - startingBlock:开始同步的起始区块编号; - currentBlock:当前正在导入的区块编号; - highestBlock:通过所链接的节点获得的当前最高的区块高度; - pulledStates:当前已经拉取的状态条目数; - knownStates:当前已知的待拉取的总状态条目数;

对应的结构体代码

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

代码语言:javascript
复制
// SyncProgress gives progress indications when the node is synchronising with
// the Ethereum network.
type SyncProgress struct {
    StartingBlock uint64 // Block number where sync began
    CurrentBlock  uint64 // Current block number where sync is at
    HighestBlock  uint64 // Highest alleged block number in the chain
    PulledStates  uint64 // Number of state trie entries already downloaded
    KnownStates   uint64 // Total number of state trie entries known about
}

结构体信息计算

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

代码语言:javascript
复制
progress := s.b.Downloader().Progress()

可以看到如下代码:

代码语言:javascript
复制
// Progress retrieves the synchronisation boundaries, specifically the origin
// block where synchronisation started at (may have failed/suspended); the block
// or header sync is currently at; and the latest known block which the sync targets.
//
// In addition, during the state download phase of fast synchronisation the number
// of processed and the total number of known states are also returned. Otherwise
// these are zero.
func (d *Downloader) Progress() ethereum.SyncProgress {
    // Lock the current stats and return the progress
    d.syncStatsLock.RLock()
    defer d.syncStatsLock.RUnlock()

    current := uint64(0)
    switch d.mode {
    case FullSync:
        current = d.blockchain.CurrentBlock().NumberU64()
    case FastSync:
        current = d.blockchain.CurrentFastBlock().NumberU64()
    case LightSync:
        current = d.lightchain.CurrentHeader().Number.Uint64()
    }
    return ethereum.SyncProgress{
        StartingBlock: d.syncStatsChainOrigin,
        CurrentBlock:  current,
        HighestBlock:  d.syncStatsChainHeight,
        PulledStates:  d.syncStatsState.processed,
        KnownStates:   d.syncStatsState.processed + d.syncStatsState.pending,
    }
}

从这端代码我们可以分析得出,current的值在不同的同步模式下是有所不同的: - full模式:返回当前区块的高度; - fast模式:返回fast区块的高度; - light模式:返回当前的header编号; - 而KnownStates又是由PulledStates的值加上当前处于pending装的值获得。

总结

通过上面源代码分析,我们已经可以明了的看到当我们执行eth.sycing时返回不同的结果信息所代表的含义。欢迎大家关注微信公众号,获取最新的相关技术分享。

原文链接:http://www.choupangxia.com/topic/detail/13

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Syncing方法源代码
  • 对应的结构体代码
  • 结构体信息计算
  • 总结
相关产品与服务
腾讯云代码分析
腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档