在节点同步的过程中,我们经常需要执行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()`11.  `// Return not syncing if the synchronisation already completed`12.  `if progress.CurrentBlock  >= progress.HighestBlock  {`13.  `return  false,  nil`14.  `}`15.  `// Otherwise gather the block sync stats`16.  `return map[string]interface{}{`17.  `"startingBlock": hexutil.Uint64(progress.StartingBlock),`18.  `"currentBlock": hexutil.Uint64(progress.CurrentBlock),`19.  `"highestBlock": hexutil.Uint64(progress.HighestBlock),`20.  `"pulledStates": hexutil.Uint64(progress.PulledStates),`21.  `"knownStates": hexutil.Uint64(progress.KnownStates),`22.  `},  nil`23.  `}`

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()`13.  `current := uint64(0)`14.  `switch d.mode {`15.  `case  FullSync:`16.  `current = d.blockchain.CurrentBlock().NumberU64()`17.  `case  FastSync:`18.  `current = d.blockchain.CurrentFastBlock().NumberU64()`19.  `case  LightSync:`20.  `current = d.lightchain.CurrentHeader().Number.Uint64()`21.  `}`22.  `return ethereum.SyncProgress{`23.  `StartingBlock: d.syncStatsChainOrigin,`24.  `CurrentBlock: current,`25.  `HighestBlock: d.syncStatsChainHeight,`26.  `PulledStates: d.syncStatsState.processed,`27.  `KnownStates: d.syncStatsState.processed + d.syncStatsState.pending,`28.  `}`29.  `}`

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

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

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

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

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

4

总  结

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

内容来源:程序新视界

作者:二师兄

以下是我们的社区介绍,欢迎各种合作、交流、学习:)

以太坊源码分析-同步之Syncing接口相关推荐

  1. 3 v4 中心节点固定_死磕以太坊源码分析之p2p节点发现

    死磕以太坊源码分析之p2p节点发现 在阅读节点发现源码之前必须要理解kadmilia算法,可以参考:KAD算法详解. 节点发现概述 节点发现,使本地节点得知其他节点的信息,进而加入到p2p网络中. 以 ...

  2. 以太坊源码分析-交易

    以太坊源码分析-交易 机理 先说一点区块链转账的基本概念和流程 用户输入转账的地址和转入的地址和转出的金额 系统通过转出的地址的私钥对转账信息进行签名(用于证明这 笔交易确实有本人进行) 系统对交易信 ...

  3. php区块链以太坊,兄弟连区块链教程以太坊源码分析CMD深入分析(一)

    兄弟连区块链教程以太坊源码分析CMD深入分析. cmd包分析 cmd下面总共有13个子包,除了util包之外,每个子包都有一个主函数,每个主函数的init方法中都定义了该主函数支持的命令,如 geth ...

  4. kademlia java_死磕以太坊源码分析之Kademlia算法

    死磕以太坊源码分析之Kademlia算法 KAD 算法概述 Kademlia是一种点对点分布式哈希表(DHT),它在容易出错的环境中也具有可证明的一致性和性能.使用一种基于异或指标的拓扑结构来路由查询 ...

  5. 以太坊源码分析(2)——以太坊APP对象

    前言 从这一节开始,我将开始以太坊代码全覆盖讲解,讲解的流程是: 以太坊程序入口 基本框架 以太坊协议 发送一笔交易后发生了什么 启动挖矿 以太坊共识 p2p 网络 阅读本系列文章,将默认读者具备一定 ...

  6. go-ethereum-code-analysis 以太坊源码分析

    分析go-ethereum的过程,我希望从依赖比较少的底层技术组件开始,慢慢深入到核心逻辑. 目录 go-ethereum代码阅读环境搭建 以太坊黄皮书 符号索引 rlp源码解析 trie源码分析 e ...

  7. 以太坊源码分析之随心笔记

    链客,专为开发者而生,有问必答! 此文章来自区块链技术社区,未经允许拒绝转载. 以太坊索引 table.go 定期随机选取一些节点找他们要他们的节点,放到本地,也就是一个随机找节点的table 里头的 ...

  8. 以太坊源码分析:fetcher模块和区块传播

    前言 这篇文章从区块传播策略入手,介绍新区块是如何传播到远端节点,以及新区块加入到远端节点本地链的过程,同时会介绍fetcher模块,fetcher的功能是处理Peer通知的区块信息.在介绍过程中,还 ...

  9. 46.以太坊源码分析(46)p2p-peer.go源码分析

    nat是网络地址转换的意思. 这部分的源码比较独立而且单一,这里就暂时不分析了. 大家了解基本的功能就行了. nat下面有upnp和pmp两种网络协议. upnp的应用场景(pmp是和upnp类似的协 ...

最新文章

  1. 计算机二级第十九套题电子表格,2012年计算机二级Access第十九套上机试题及答案详解...
  2. 转 awk 使用方法
  3. ubuntu上wordpress安装的前置工作
  4. AppFabric Caching Admin Tool
  5. spring-security-学习笔记-02-基于Session的认证方式
  6. Docker(4)-容器互联与端口映射
  7. 06MySQL基本函数的使用
  8. 整理了 25 个前端相关的学习网站和一些靠谱的小工具,都来看看吧
  9. java程序崩溃查询,java – 有程序识别它上次崩溃了吗?
  10. mysql5.7 解压版 中文乱码_MySQL 5.7解压版安装、卸载及乱码问题的图文解决方法...
  11. 解读 | 数据分析师(含转行)的面试简历如何写?
  12. oracle 依据日期查询,Oracle中日期作为条件的查询
  13. SpringBoot_01_初窥门径
  14. 项目八学好英语网html,学好英语的八种方法-英语文章阅读-大耳朵英语 - 免费在线英语学习 口语练习 四级听力资料 在线翻译 网络课堂 英语社区...
  15. 教育资源平台空间装扮html代码,一看就会—— 河南省基础教育资源公共服务平台“人人通空间”...
  16. VM虚拟机:VMware Kernel Module Updater
  17. 云米Q2财报:复苏表象下有苦衷
  18. 老僧长谈设计模式-10-桥接模式
  19. 使用MATLAB求图像的形心、外接矩形顶点坐标并标记
  20. C#调用TSC条码打印机打印条码(转)

热门文章

  1. 在 Flutter 中实现文字动画
  2. 增加firefox的搜索选项
  3. 2022/08/04、05 day01-2/02:Redis数据类型
  4. ffmpeg获取rtsp h265_用FFmpeg将rtsp视频流保存成H264、h265文件
  5. 31款早餐,一个月不重样
  6. 收藏!AI从业者必备表情包!
  7. mysql是怎么实现多对多的_mysql复习篇及一对多和多对多的总结(17.6.26 )
  8. 用计算机解开手机密码,手机忘记锁屏密码?维修老师傅教你一招,按下这里就能解开...
  9. CSDN周刊:Google Cloud大规模宕机;中国正式进入 5G 商用元年!苹果发布SwiftUI
  10. MP2451 VOUT计算公式 表