|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import time |
| 6 | +import logging |
| 7 | + |
| 8 | + |
| 9 | +def run_vc(cmd: str): |
| 10 | + if binary: |
| 11 | + return subprocess.run([binary, '-k', client, '-p', server, '-a', address, '--cmd', cmd], |
| 12 | + stdout=subprocess.PIPE).stdout.decode() |
| 13 | + else: # use symlink |
| 14 | + return subprocess.run(['bash', 'validator-console', '--cmd', cmd], |
| 15 | + stdout=subprocess.PIPE).stdout.decode() |
| 16 | + |
| 17 | +def get_last_mc_seqno(): |
| 18 | + stats = run_vc('getstats') |
| 19 | + for line in stats.split('\n'): |
| 20 | + if line.startswith('masterchainblock'): |
| 21 | + return int(line.split()[1].split(',')[2].split(')')[0]) |
| 22 | + |
| 23 | +def restart_node(): |
| 24 | + return subprocess.run(['systemctl', 'restart', 'validator']).returncode |
| 25 | + |
| 26 | +def get_config(): |
| 27 | + with open(config_path) as f: |
| 28 | + return json.load(f) |
| 29 | + |
| 30 | +def set_config(config: dict): |
| 31 | + with open(config_path, 'w') as f: |
| 32 | + f.write(json.dumps(config, indent=4)) |
| 33 | + |
| 34 | +def set_hardforks(hfks: list): |
| 35 | + c = get_config() |
| 36 | + c['validator']['hardforks'] = hfks |
| 37 | + set_config(c) |
| 38 | + |
| 39 | +def add_hardfork(hfk: dict): |
| 40 | + c = get_config() |
| 41 | + c['validator']['hardforks'].append(hfk) |
| 42 | + set_config(c) |
| 43 | + |
| 44 | + |
| 45 | +logger = logging.getLogger(__name__) |
| 46 | +logging.basicConfig( |
| 47 | + level=logging.DEBUG, |
| 48 | + format='%(asctime)s - %(levelname)s - %(message)s', |
| 49 | + datefmt='%Y-%m-%d %H:%M:%S' |
| 50 | +) |
| 51 | + |
| 52 | +parser = argparse.ArgumentParser(description="Python script to process hardforks when syncing archive node.") |
| 53 | +parser.add_argument('--from-seqno', type=int, required=True, help='Global config path') |
| 54 | +parser.add_argument('--config-path', type=str, required=True, help='Global config path') |
| 55 | +parser.add_argument('--bin', type=str, help='VC bin') |
| 56 | +parser.add_argument('--client', type=str, help='VC Client') |
| 57 | +parser.add_argument('--server', type=str, help='VC Server') |
| 58 | +parser.add_argument('--address', type=str, help='VC Address') |
| 59 | + |
| 60 | +args = parser.parse_args() |
| 61 | +config_path = args.config_path |
| 62 | +binary = args.bin |
| 63 | +client = args.client |
| 64 | +server = args.server, |
| 65 | +address = args.address |
| 66 | +from_seqno = args.from_seqno |
| 67 | + |
| 68 | + |
| 69 | +hardforks = get_config()['validator']['hardforks'] |
| 70 | +keep_hardforks = [h for h in hardforks if h['seqno'] <= from_seqno] |
| 71 | +hardforks = [h for h in hardforks if h['seqno'] > from_seqno] |
| 72 | +if len(hardforks) == 0: |
| 73 | + logger.info("No hardforks to process.") |
| 74 | + sys.exit(0) |
| 75 | +set_hardforks(keep_hardforks) |
| 76 | + |
| 77 | +while True: |
| 78 | + if len(hardforks) == 0: |
| 79 | + break |
| 80 | + try: |
| 81 | + last_mc_seqno = get_last_mc_seqno() |
| 82 | + if not last_mc_seqno: |
| 83 | + logger.info("Waiting for last mc seqno...") |
| 84 | + time.sleep(300) |
| 85 | + continue |
| 86 | + if last_mc_seqno != hardforks[0]['seqno'] - 1: |
| 87 | + logger.info(f"Waiting for hardfork {hardforks[0]['seqno']}...") |
| 88 | + time.sleep(300) |
| 89 | + continue |
| 90 | + hardfork = hardforks.pop(0) |
| 91 | + logger.info(f"Processing hardfork {hardfork['seqno']}.") |
| 92 | + add_hardfork(hardfork) |
| 93 | + logger.info(f"Hardfork {hardfork['seqno']} has been added.") |
| 94 | + restart_node() |
| 95 | + logger.info(f"Node is restarted") |
| 96 | + except Exception as e: |
| 97 | + import traceback |
| 98 | + logger.error(f"Exception occurred: {e}\n{traceback.format_exc()}") |
| 99 | + time.sleep(60) |
| 100 | + |
| 101 | +logger.info(f"All done.") |
0 commit comments