12

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 ?

  • 1
    And how do you retrieve your client's last block # ? – Ben Aug 17 '17 at 12:55
11

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

  • 1
    I'm experiencing a caveat where my client connects to a geth that is on, but not in connected to any peers in the network yet. In that case web3.eth.syncing also returns false, altho it's not in sync. Any ideas to cover this case? – Fabiano Soriani Aug 22 '17 at 22:37
  • @FabianoPS to determine whether the node has not yet synced or finished syncing, you can look at the timestamp of the last block. If it is "old", syncing has not started yet – Bruno Grieder Jun 20 '18 at 3:28
3

Why not querying eth.syncing? From the doc:

web3.eth.syncing // or async web3.eth.getSyncing(callback(error, result){ ... }) This property is read only and returns the either a sync object, when the node is syncing or false.

Returns

Object|Boolean - A sync object as follows, when the node is currently syncing or false:

startingBlock: Number - The block number where the sync started. currentBlock: Number - The block number where at which block the node currently synced to already. highestBlock: Number - The estimated block number to sync to.

Example

var sync = web3.eth.syncing;
console.log(sync);
/*
{
   startingBlock: 300,
   currentBlock: 312,
   highestBlock: 512
}
*/
0

After having a lot of problems with syncing myself, I looked into how to get an ETA of the sync process.

Seems Geth can run a javascript file as part of it's startup in the console, so I added to a script found here and came up with the script below. This needs to be saved into a .js file and referenced at geth startup.

To get it running, add the argument --preload sync_stats.js to your geth startup. This will load the script and start it along with the console, so it gets interleaved with the normal console output. By default, it inserts stats every 10 seconds. Change timeInterval to alter this.

eg.

/home/$USER/go-ethereum/build/bin/geth --rpc console --preload sync_stats.js

Here is the script to go into sync_stats.js:

var lastBlocksToGo = 0;var timeInterval = 10000;var blocksSoFar = 0;var timeSoFar = 0;
setInterval(function(){
if (eth.syncing === false && eth.blockNumber != 0) {
    console.log('In Sync. Current Block: '+eth.blockNumber);
    lastBlocksToGo = 0;
    blocksSoFar = 0;
    timeSoFar = 0;
}
else {
var percentage = (eth.syncing.currentBlock/eth.syncing.highestBlock)*100;
percentage = Math.floor(percentage *100) / 100;
var blocksToGo = eth.syncing.highestBlock - eth.syncing.currentBlock;
var blocksThisTime = (lastBlocksToGo - blocksToGo);

if (blocksThisTime > 0) blocksSoFar += blocksThisTime;
timeSoFar += timeInterval;

var bps = blocksSoFar / (timeSoFar / 1000);
var etas = blocksToGo / bps;

var etaM = parseInt(etas/60,10);
var runtM = parseInt(timeSoFar/60000, 10);
if (isNaN(etaM)) etaM = '???';
    console.log('Syncing: '+percentage.toFixed(2)+'% ETA: '+etaM+' minutes @ '+bps.toFixed(1)+' blocks/sec    [runtime: '+runtM+' mins blocksThisRun: '+blocksSoFar+']');

lastBlocksToGo = blocksToGo;

console.log('Eth.Syncing.CurrentBlock: '+eth.syncing.currentBlock);
console.log('Eth.Syncing.HighestBlock: '+eth.syncing.highestBlock);
}
},timeInterval);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.