在節點同步的過程當中,咱們常常須要執行eth.syncing來查看當前的同步狀況,本篇博客帶領你們看一下syncing api的源代碼實現。node
// 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` }` </pre>
Syncing方法的源代碼很簡單,註釋說明也已經很清楚了。經過這段源代碼咱們能夠得知一下信息:api
固然CurrentBlock大於等於HighestBlock時返回false,這也正是一般所說的同步完成以後,再執行eth.syncing()函數會返回false的緣由。微信
startingBlock:開始同步的起始區塊編號;函數
currentBlock:當前正在導入的區塊編號;學習
highestBlock:經過所連接的節點得到的當前最高的區塊高度;ui
pulledStates:當前已經拉取的狀態條目數;this
knownStates:當前已知的待拉取的總狀態條目數。code
下面是同步信息對應的結構體的代碼及註釋。ci
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. `}` </pre>
上面看到當執行eth.syncing返回結果信息的代碼,再延伸一下看看這些數據從哪裏來。進入下面Progress方法的內部實現:get
progress := s.b.Downloader().Progress() </pre>
能夠看到以下代碼:
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. `}` </pre>
從這端代碼咱們能夠分析得出,curren
full模式:返回當前區塊的高度;
fast模式:返回fast區塊的高度;
light模式:返回當前的header編號;
而KnownStates又是由PulledStates的值加上當前處於pending裝的值得到。
經過上面源代碼分析,咱們已經能夠明瞭的看到當咱們執行eth.sycing時返回不一樣的結果信息所表明的含義。歡迎你們關注微信公衆號,獲取最新的相關技術分享。
內容來源:程序新視界
做者:二師兄
如下是咱們的社區介紹,歡迎各類合做、交流、學習:)