Skip to content

Instantly share code, notes, and snippets.

@dirtycajunrice
Last active January 25, 2022 12:46
Show Gist options
  • Save dirtycajunrice/63cb9e8d1ddc232b33a7136e83d9f5fe to your computer and use it in GitHub Desktop.
Save dirtycajunrice/63cb9e8d1ddc232b33a7136e83d9f5fe to your computer and use it in GitHub Desktop.
Script to watch the block diff while syncing Harmony Validator Nodes

Install

  1. pip install git+https://github.com/harmony-one/pyhmy.git@refs/pull/24/merge#egg=pyhmy (They need the above merge request so use that merged shadow branch)
  2. curl/wget the main file raw.
  3. chmod +x and move it within your $PATH
watchBlockDiff -h
usage: watchBlockDiff [-h] [-i INTERVAL] [-b BEACON] [-l LOCAL]

Harmony Validator Node Block Height Difference Watcher

optional arguments:
  -h, --help            show this help message and exit
  -i INTERVAL, --interval INTERVAL
                        Seconds between queries. (Default: 60)
  -b BEACON, --beacon BEACON
                        Beacon RPC URL to use. (Default: 'https://rpc.s0.t.hmny.io')
  -l LOCAL, --local LOCAL
                        Beacon RPC URL to use. (Default: 'http://localhost:9500')
git+https://github.com/harmony-one/pyhmy.git@refs/pull/24/merge#egg=pyhmy
#!/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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment