Created
March 14, 2019 23:33
-
-
Save emesik/1d85a5316bd160c169152e4f72032618 to your computer and use it in GitHub Desktop.
Checks if Monero daemon is up to date by comparing height to other nodes
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 logging | |
from monero.daemon import Daemon | |
from monero.backends.jsonrpc import JSONRPCDaemon | |
import operator | |
import re | |
import sys | |
MY_DAEMON = '127.0.0.1:18081' | |
OTHER_DAEMONS = [ | |
'node.moneroworld.com:18089', | |
'node.xmr.to:18081', | |
'node.supportxmr.com:18081' | |
] | |
ACCEPTED_HEIGHT_DIFF = 3 | |
_log = logging.getLogger(__name__) | |
def url_data(url): | |
gs = re.compile( | |
r'^(?P<host>[^:\s]+)(?::(?P<port>[0-9]+))?$' | |
).match(url).groupdict() | |
return dict(filter(operator.itemgetter(1), gs.items())) | |
def get_height(daemon_url): | |
daemon = Daemon(JSONRPCDaemon(**url_data(daemon_url))) | |
info = daemon.info() | |
return info['height'] | |
if __name__ == '__main__': | |
my_height = get_height(MY_DAEMON) | |
heights = set() | |
for durl in OTHER_DAEMONS: | |
try: | |
heights.add(get_height(durl)) | |
except Exception as e: | |
_log.exception('Failed to connect to {}'.format(durl)) | |
if not heights: | |
_log.errot('No height determined (all daemons failed?)') | |
sys.exit(0) # the problem is not with MY_DAEMON | |
height_diff = my_height - max(heights) | |
if abs(height_diff) <= ACCEPTED_HEIGHT_DIFF: | |
_log.debug('Height diff within the limit (|{}| <= {})'.format( | |
height_diff, ACCEPTED_HEIGHT_DIFF)) | |
sys.exit(0) | |
_log.warning('Height diff over limit ({}). Returning code 1.'.format(height_diff)) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment