3

How to view latest block number of a geth node in the console: Answer follows:

> var sync = web3.eth.syncing; 
undefined
> console.log(sync);
[object Object]
undefined
> sync
{
  currentBlock: 1486592,
  highestBlock: 1488447,
  startingBlock: 1486592
}

Please note that I am working on a private blockchain. There are few nodes and network is fragile.

My goal is to get the highestBlockNumber right away no matter what from my local node. But this function sync = web3.eth.syncing does return false on many cases and I may need to wait few minutes to get a valid information. And when it does return a valid block number, it has already head of the time.

    while [ $startBlockNum == $undefined ]
    do
        startBlockNum=$(echo "$header; mylib.highestBlock()"   | sudo nodejs);
        echo $startBlockNum;
    done

    exports.highestBlock = function() {
       var sync = web3.eth.syncing;
       console.log( sync.highestBlock );
       return sync.highestBlock;
    };

For example. When I called: sync = web3.eth.syncing highestBlock should be 100 but it returns false. I have check sync value inside a loop until it returns a valid information, and when it does it returns 2 minutes more later and returned blockNumber=108 (which highest block# already updated, and actually 8 blocks ahead).

[Q] What may be the reason web3.eth.syncing returns undefined or false for multiple minutes even sync is done on the background?

Is there any way to prevent this from happening? If sync = web3.eth.syncing returns undefined could I trust the value returned by eth.blockNumber?

2 Answers 2

2

For me with the latest web3 eth.getSyncing was not available. but isSyncing works great. (maybe it has since been renamed ?).

const Eth = require('web3-eth');
web3.eth.isSyncing().then(function (r)
{
  console.log("r" + JSON.stringify(r) );
  console.log("currentBlock: " + r['currentBlock']);
  console.log("highestBlock: " + r['highestBlock']);
});
1

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

You should use a callback function:

web3.eth.getSyncing(callback(error, result){
if(!err) {
   console.log(result);
   console.log(result.highestBlock);
}
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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