|
#!/usr/bin/env python3 |
|
import argparse |
|
|
|
from time import time, sleep |
|
from datetime import datetime, timedelta |
|
|
|
from pyhmy import blockchain |
|
|
|
parser = argparse.ArgumentParser(description="Harmony Validator Node Block Height Difference Watcher") |
|
parser.add_argument("-i", "--interval", type=int, default=60, help="Seconds between queries. (Default: 60)") |
|
parser.add_argument("-b", "--beacon", type=str, default='https://rpc.s0.t.hmny.io', |
|
help="Beacon RPC URL to use. (Default: 'https://rpc.s0.t.hmny.io')") |
|
parser.add_argument("-l", "--local", type=str, default='http://localhost:9500', |
|
help="Beacon RPC URL to use. (Default: 'http://localhost:9500')") |
|
args = parser.parse_args() |
|
|
|
INTERVAL = args.interval |
|
|
|
beaconURL = args.beacon |
|
localURL = args.local |
|
|
|
BEACON_FIRST_RECORDED = 0 |
|
LOCAL_FIRST_RECORDED = 0 |
|
|
|
TIME_STARTED = time() |
|
DATETIME_STARTED = datetime.now() |
|
|
|
TOTAL_TIME_PASSED = 0 |
|
|
|
beacon_last_block = 0 |
|
local_last_block = 0 |
|
|
|
|
|
def sep(): |
|
print("", '-' * 87) |
|
|
|
|
|
def get_blocks(): |
|
_now = datetime.now().strftime("%Y/%m/%d %H:%M:%S") |
|
try: |
|
_beacon_latest_block = blockchain.get_latest_header(beaconURL)['blockNumber'] |
|
except Exception: |
|
_beacon_latest_block = beacon_latest_block |
|
_local_latest_block = blockchain.get_latest_header(localURL)['blockNumber'] |
|
return _now, _beacon_latest_block, _local_latest_block |
|
|
|
|
|
sep() |
|
print("", "TIME".center(21), "Local".center(10), "Remote".center(10), "Behind".center(10), |
|
"Gained".center(10), "Gained/s".center(10), "ETA".center(10), "", sep="|") |
|
sep() |
|
|
|
while True: |
|
t, beacon_latest_block, local_latest_block = get_blocks() |
|
|
|
if BEACON_FIRST_RECORDED == 0 or LOCAL_FIRST_RECORDED == 0: |
|
BEACON_FIRST_RECORDED = beacon_latest_block |
|
LOCAL_FIRST_RECORDED = local_latest_block |
|
|
|
blocks_until_in_sync = beacon_latest_block - local_latest_block |
|
|
|
total_beacon_new_blocks = beacon_latest_block - BEACON_FIRST_RECORDED |
|
total_local_new_blocks = local_latest_block - LOCAL_FIRST_RECORDED |
|
|
|
total_local_blocks_progress = total_local_new_blocks - total_beacon_new_blocks |
|
|
|
time_passed_seconds = (datetime.now() - DATETIME_STARTED).seconds |
|
|
|
if BEACON_FIRST_RECORDED == beacon_latest_block: |
|
total_local_blocks_progress = 0 |
|
block_progress_per_second = 0 |
|
seconds_until_synced = "TBD" |
|
else: |
|
block_progress_per_second = total_local_blocks_progress / time_passed_seconds |
|
seconds_until_synced = str(timedelta(seconds=blocks_until_in_sync / block_progress_per_second)).split(".")[0] |
|
|
|
print("", t.center(21), str(local_latest_block).center(10), str(beacon_latest_block).center(10), |
|
str(blocks_until_in_sync).center(10), str(total_local_blocks_progress).center(10), |
|
str(round(block_progress_per_second, 2)).center(10), seconds_until_synced.center(10), "", sep="|") |
|
|
|
beacon_last_block = beacon_latest_block |
|
local_last_block = local_latest_block |
|
|
|
sleep(INTERVAL - ((time() - TIME_STARTED) % INTERVAL)) |