|
| 1 | +import json |
| 2 | +import requests |
| 3 | + |
| 4 | +from mypylib.mypylib import color_print |
| 5 | +from modules.module import MtcModule |
| 6 | + |
| 7 | + |
| 8 | +class CollatorConfigModule(MtcModule): |
| 9 | + |
| 10 | + @staticmethod |
| 11 | + def check_config_url(url): |
| 12 | + try: |
| 13 | + r = requests.get(url, timeout=3) |
| 14 | + if r.status_code != 200: |
| 15 | + print(f'Failed to get config from {url}: {r.status_code} code; {r.text}') |
| 16 | + return |
| 17 | + return r.json() |
| 18 | + except Exception as e: |
| 19 | + print(f'Failed to get config from {url}: {e}') |
| 20 | + return |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def check_config_file(path): |
| 24 | + try: |
| 25 | + with open(path, 'r') as f: |
| 26 | + return json.load(f) |
| 27 | + except Exception as e: |
| 28 | + print(f'Failed to read config from {path}: {e}') |
| 29 | + return |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def get_config(path): |
| 33 | + if 'http' in path: |
| 34 | + config = CollatorConfigModule.check_config_url(path) |
| 35 | + else: |
| 36 | + config = CollatorConfigModule.check_config_file(path) |
| 37 | + if config is None: |
| 38 | + raise Exception(f'Failed to get config') |
| 39 | + return config |
| 40 | + |
| 41 | + def add_collator_config_to_vc(self, config: dict): |
| 42 | + self.local.add_log(f"Adding collator options config to validator console", "debug") |
| 43 | + path = self.ton.tempDir + f'/collator_config.json' |
| 44 | + with open(path, 'w') as f: |
| 45 | + json.dump(config, f) |
| 46 | + result = self.ton.validatorConsole.Run(f"setcollatoroptionsjson {path}") |
| 47 | + return 'success' in result, result |
| 48 | + |
| 49 | + def set_collator_config(self, args): |
| 50 | + if len(args) != 1: |
| 51 | + color_print("{red}Bad args. Usage:{endc} set_collator_config <path/url>") |
| 52 | + return |
| 53 | + location = args[0] |
| 54 | + config = self.get_config(location) |
| 55 | + self.ton.set_collator_config(location) |
| 56 | + added, msg = self.add_collator_config_to_vc(config) |
| 57 | + if not added: |
| 58 | + print(f'Failed to add collator config to validator console: {msg}') |
| 59 | + color_print("set_collator_config - {red}ERROR{endc}") |
| 60 | + return |
| 61 | + color_print("set_collator_config - {green}OK{endc}") |
| 62 | + |
| 63 | + def get_collator_config(self, args): |
| 64 | + location = self.ton.get_collator_config_location() |
| 65 | + print(f'Collator config location: {location}') |
| 66 | + path = self.ton.tempDir + f'/current_collator_config.json' |
| 67 | + output = self.ton.validatorConsole.Run(f'getcollatoroptionsjson {path}') |
| 68 | + if 'saved config to' not in output: |
| 69 | + print(f'Failed to get collator config: {output}') |
| 70 | + color_print("get_collator_config - {red}ERROR{endc}") |
| 71 | + return |
| 72 | + with open(path, 'r') as f: |
| 73 | + config = json.load(f) |
| 74 | + print(f'Collator config:') |
| 75 | + print(json.dumps(config, indent=4)) |
| 76 | + color_print("get_collator_config - {green}OK{endc}") |
| 77 | + |
| 78 | + def update_collator_config(self, args): |
| 79 | + location = self.ton.get_collator_config_location() |
| 80 | + config = self.get_config(location) |
| 81 | + added, msg = self.add_collator_config_to_vc(config) |
| 82 | + if not added: |
| 83 | + print(f'Failed to add collator config to validator console: {msg}') |
| 84 | + color_print("update_collator_config - {red}ERROR{endc}") |
| 85 | + return |
| 86 | + color_print("update_collator_config - {green}OK{endc}") |
| 87 | + |
| 88 | + def add_console_commands(self, console): |
| 89 | + console.AddItem("set_collator_config", self.set_collator_config, self.local.translate("set_collator_config_cmd")) |
| 90 | + console.AddItem("update_collator_config", self.update_collator_config, self.local.translate("update_collator_config_cmd")) |
| 91 | + console.AddItem("get_collator_config", self.get_collator_config, self.local.translate("get_collator_config_cmd")) |
0 commit comments