I have a four node setup and have get method in smartcontract to get the status of certain application. Currently the api invocation is happening at any node ( any of four nodes). For example if one of the node is syncing to other nodes ( was down for some time) in the network and if my api call is triggered at the syncing node the data which I am getting won't be the latest one. So is there anyway to identify the node is in sync with network and then perform the api get call or am I wrong with my understatnding.
4 Answers
Is my node connected to peers?
web3.net.peerCount
Is my node syncing?
web3.eth.syncing
Is my node fully synced? (should be 0)
web3.eth.syncing.highestBlock - web3.eth.syncing.currentBlock
You may use method web3.eth.isSyncing
to know whether node is still synchronizing. You may use web3.eth.getBlockNumber
to know what block node is currently at.
You better check both, because node may think that it is synchronized, but may actually be disconnected form the network.
As say in other answer you should use web3.eth.isSyncing
but just to be precise this function :
- if is synced, returns
false
- if is syncing, returns
startingBlock
,currentBlock
,highestBlock
,knownStates
pulledStates
- if is just started / not connected to other nodes, returns
false
The last case can be tricky as it gives you false positive.
You can use trustray open source
-
1Hi there. Can you include some more information about how to use the application in a way that answers the question? Commented Apr 10, 2019 at 8:09
-