During node synchronization, you often need to perform eth. Syncing to check the current synchronization. This blog takes a look at the source code implementation of the Syncing API.

1

Syncing method source code

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.  `}`

The source code for the Syncing method is simple, and the annotations are already clear. From this source code, we can get some information:

  • Of course, CurrentBlock greater than or equal to HighestBlock returns false, which is why it is common to say that executing eth. Syncing () after synchronization returns false.

  • StartingBlock: start block number for synchronization.

  • CurrentBlock: indicates the id of the block being imported.

  • HighestBlock: the current highestBlock height obtained by the linked node;

  • PulledStates: number of state items that have been pulled;

  • KnownStates: Indicates the total number of known state entries to be pulled.

2

Corresponding structure code

Below is the code and comments for the structure corresponding to the synchronization information.

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

Structure information calculation

See above the code that returns the result information when executing eth. Syncing. Extend it to see where the data comes from. Enter the following internal implementation of the Progress method:

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

You can see the following code:

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.  `}`

From this piece of code, we can analyze curren

  • Full mode: returns the height of the current block;

  • Fast mode: Return the height of the FAST block;

  • Light mode: Returns the current header number;

  • KnownStates is obtained from the PulledStates value plus the value currently mounted pending.

4

Total knot

From the above source code analysis, we can already see what it means to return different results when we execute eth. Sycing. Welcome to follow the wechat public account to get the latest technology sharing.

Content source: Program new horizon

Author: Brother Two

The following is our community introduction, welcome all kinds of cooperation, exchange, learning 🙂