Anyone who has been on the launch of a new Blockchain will know that the process can be quite stressful. I have personally been maintaining a testnet node for EOS Argentina and the multiple chain forks inspired me to build this little script that checks your node´s Blockchain head number and compare it to another node, ideally to one that you know is on the main chain, but you could also compare two of you own nodes to check if they are on the same head_block_num.
Our node is in the jungle testnet so I compare my block with @cryptolions node since they are the BPs who maintain this chain and are usually in the latest block. This is what I initially came up with (bare in mind I have had very little sleep in the last few days):
import urllib.request
import json
url = urllib.request.urlopen('https://n2.eosargentina.io/v1/chain/get_info')
arg = url.read()
eosarg = json.loads(arg)
eosargentina = (eosarg["head_block_num"])
eosargentina1 = str(eosargentina)
print ("eosargentina node is at block " + eosargentina1)
url = urllib.request.urlopen('http://dev.cryptolions.io:8888/v1/chain/get_info')
lion = url.read()
cryptolions = json.loads(lion)
lionblock = (cryptolions["head_block_num"])
lionblock1 = str(lionblock)
print ("cryptolions node is at block " + lionblock1)
if eosargentina == lionblock:
print ("is all good")
else :
print ("we are forked!!!!")
A good friend of mine cleaned this code a little and made it more readable and scalable, I recommend you use his version:
import requests as r
import json
def get_head_block_from(url):
res = json.loads(r.get('{0}/v1/chain/get_info'.format(url)).text)
return int(res["head_block_num"])
eosargentina = get_head_block_from("https://n2.eosargentina.io")
cryptolions = get_head_block_from("http://dev.cryptolions.io:8888")
if eosargentina == cryptolions:
print ("is all good")
else :
print ("we are forked!!!!")
print (" - eosargentina : {0}".format(eosargentina))
print (" - cryptolions : {0}".format(cryptolions))
I hope this script can help others too, it certainly saved me a lot of time. I will be publishing more tools as we make them.
Until the next time.