Created
February 24, 2023 04:07
-
-
Save AndrewMohawk/8d75f87d213c39a455018bc835da1887 to your computer and use it in GitHub Desktop.
Poll Polygon RPCs for blockheight to look for differences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
import json | |
import time | |
async def get_block_height(session, url): | |
headers = {'Content-Type': 'application/json'} | |
try: | |
async with session.post(url,headers=headers, json={'jsonrpc':'2.0','method':'eth_blockNumber','params':[],'id':1}) as response: | |
response_txt = await response.text() | |
#print(response_txt) | |
#data = await response.json() | |
try: | |
json_data = json.loads(response_txt) | |
block_height = int(json_data['result'], 16) | |
return url, block_height | |
except Exception as e: | |
print(f"Page response for {url}: {response_txt}") | |
print("Error:", e) | |
return url, 0 | |
except Exception as e: | |
print(f"Error while connecting to {url}") | |
print("Error:", e) | |
return url, 0 | |
async def main(): | |
while True: | |
endpoint_urls = [ | |
'https://matic-mainnet.chainstacklabs.com/', | |
'https://poly-rpc.gateway.pokt.network/', | |
'https://polygon-rpc.com', | |
'https://rpc.ankr.com/polygon', | |
#'https://rpc-mainnet.maticvigil.com', | |
'https://rpc-mainnet.matic.quiknode.pro' | |
] | |
block_heights = [] | |
async with aiohttp.ClientSession() as session: | |
tasks = [] | |
for url in endpoint_urls: | |
tasks.append(asyncio.ensure_future(get_block_height(session, url))) | |
start_time = time.time() | |
results = await asyncio.gather(*tasks) | |
end_time = time.time() | |
for result in results: | |
block_heights.append(result[1]) | |
#avg_diff = sum([abs(block_heights[i]-block_heights[i-1]) for i in range(1,len(block_heights))])/len(block_heights) | |
threshhold = 5 | |
# Lets figure out if there is more then a 7 block difference between any of the block heights | |
threshhold_reached = False | |
curr_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) | |
avg_diff = sum([abs(block_heights[i]-block_heights[i-1]) for i in range(1,len(block_heights))])/len(block_heights) | |
# round avg_diff to 2 decimal places | |
avg_diff = round(avg_diff, 2) | |
for i in range(1,len(block_heights)): | |
if abs(block_heights[i]-block_heights[i-1]) > threshhold: | |
threshhold_reached = True | |
print(f"❌ {curr_time} Block heights for {endpoint_urls[i-1]} and {endpoint_urls[i]} are not within {threshhold} blocks of each other. Block heights: {block_heights[i]} and {block_heights[i-1]} Average: {avg_diff} Heights: {block_heights}") | |
if not threshhold_reached: | |
print(f"✅ {curr_time} Block heights are within {threshhold} blocks of each other. Average: {avg_diff} Heights: {block_heights}") | |
else: | |
for result in results: | |
print(f"Block height for {result[0]}: {result[1]}") | |
#print("Block heights:", block_heights) | |
#print("Average block height difference:", avg_diff) | |
elapsed_time = end_time - start_time | |
print("Elapsed time:", elapsed_time) | |
await asyncio.sleep(3) | |
if __name__ == '__main__': | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment