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
?