Geth Synchronization – How to Know if Database is Synced

go-ethereumsynchronization

In order to answer this question there is a need to get the timestamp of when the database is synced with geth.

I can think of two ways of doing that :

  1. find the 1st occurrence of 1 block in the geth client, usually when syncing it retrieves more blocks I0523 13:32:06.653226 core/blockchain.go:959] imported 1 block(s) (0 queued 0 ignored) including 9 txs in 12.87853ms. #1569731 [341d073a / 341d073a] but if for some readson the syncronisation imports only one block and isn't yet synced that would be a false positive. maybe when you got twice ina row could be safe enough
  2. retrieve the last block number from any block explorer and compare it with your client's last block #

but maybe there is a built-in method ?

Best Answer

From my experience #2 is the best way to check the status of a node.

eth.syncing returns false even when the blocks are not synced. When geth is starting it's false, and often while not all blocks have synced it still returns false.

> web3.eth.syncing
false
> I0607 08:09:04.878618 eth/downloader/downloader.go:320] Block synchronisation started
> web3.eth.syncing
{
  currentBlock: 1086723,
  highestBlock: 1087298,
  knownStates: 0,
  pulledStates: 0,
  startingBlock: 1086723
}

later

I0607 08:09:14.008694 core/blockchain.go:964] imported 1 block(s) (0 queued 0 ignored) including 0 txs in 4.647406ms. #1087304 [501df7da / 501df7da]
> web3.eth.syncing
false

Note the false, but the block number isn't at the syncing.highestBlock yet.

There is a callback for when sync starts and stops.. just remember that the highestBlock will change as new blocks are mined.

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethissyncing