From 622c3f69195277b91e438cd47ff575c4f582004e Mon Sep 17 00:00:00 2001 From: yungwine Date: Thu, 18 Jul 2024 17:37:40 +0800 Subject: [PATCH 1/2] add collator config --- modules/collator_config.py | 92 ++++++++++++++++++++++++++++++++++++++ mytoncore/mytoncore.py | 10 +++++ mytonctrl/mytonctrl.py | 4 ++ 3 files changed, 106 insertions(+) create mode 100644 modules/collator_config.py diff --git a/modules/collator_config.py b/modules/collator_config.py new file mode 100644 index 00000000..9596520c --- /dev/null +++ b/modules/collator_config.py @@ -0,0 +1,92 @@ +import json +import requests + +from mypylib.mypylib import color_print +from modules.module import MtcModule +from mytoncore.utils import hex2base64 + + +class CollatorConfigModule(MtcModule): + + @staticmethod + def check_config_url(url): + try: + r = requests.get(url, timeout=3) + if r.status_code != 200: + print(f'Failed to get config from {url}: {r.status_code} code; {r.text}') + return + return r.json() + except Exception as e: + print(f'Failed to get config from {url}: {e}') + return + + @staticmethod + def check_config_file(path): + try: + with open(path, 'r') as f: + return json.load(f) + except Exception as e: + print(f'Failed to read config from {path}: {e}') + return + + @staticmethod + def get_config(path): + if 'http' in path: + config = CollatorConfigModule.check_config_url(path) + else: + config = CollatorConfigModule.check_config_file(path) + if config is None: + raise Exception(f'Failed to get config') + return config + + def add_collator_config_to_vc(self, config: dict): + self.local.add_log(f"Adding collator options config to validator console", "debug") + path = self.ton.tempDir + f'/collator_config.json' + with open(path, 'w') as f: + json.dump(config, f) + result = self.ton.validatorConsole.Run(f"setcollatoroptionsjson {path}") + return 'success' in result, result + + def set_collator_config(self, args): + if len(args) != 1: + color_print("{red}Bad args. Usage:{endc} set_collator_config ") + return + location = args[0] + config = self.get_config(location) + self.ton.set_collator_config(location) + added, msg = self.add_collator_config_to_vc(config) + if not added: + print(f'Failed to add collator config to validator console: {msg}') + color_print("set_collator_config - {red}ERROR{endc}") + return + color_print("set_collator_config - {green}OK{endc}") + + def get_collator_config(self, args): + location = self.ton.get_collator_config_location() + print(f'Collator config location: {location}') + path = self.ton.tempDir + f'/current_collator_config.json' + output = self.ton.validatorConsole.Run(f'getcollatoroptionsjson {path}') + if 'saved config to' not in output: + print(f'Failed to get collator config: {output}') + color_print("get_collator_config - {red}ERROR{endc}") + return + with open(path, 'r') as f: + config = json.load(f) + print(f'Collator config:') + print(json.dumps(config, indent=4)) + color_print("get_collator_config - {green}OK{endc}") + + def update_collator_config(self, args): + location = self.ton.get_collator_config() + config = self.get_config(location) + added, msg = self.add_collator_config_to_vc(config) + if not added: + print(f'Failed to add collator config to validator console: {msg}') + color_print("update_collator_config - {red}ERROR{endc}") + return + color_print("update_collator_config - {green}OK{endc}") + + def add_console_commands(self, console): + console.AddItem("set_collator_config", self.set_collator_config, self.local.translate("set_collator_config_cmd")) + console.AddItem("update_collator_config", self.update_collator_config, self.local.translate("update_collator_config_cmd")) + console.AddItem("get_collator_config", self.get_collator_config, self.local.translate("get_collator_config_cmd")) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 726b0d2f..098d6ec9 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -3986,6 +3986,16 @@ def delete_custom_overlay(self, name: str): del self.local.db['custom_overlays'][name] self.local.save() + def set_collator_config(self, location: str): + self.local.db['collator_config'] = location + self.local.save() + + def get_collator_config_location(self): + default = 'https://raw.githubusercontent.com/ton-blockchain/ton-blockchain.github.io/main/default_collator_options.json' + location = self.local.db.get('collator_config', default) + if location is None: + location = default + return location def GetNetworkName(self): data = self.local.read_db(self.liteClient.configPath) diff --git a/mytonctrl/mytonctrl.py b/mytonctrl/mytonctrl.py index 9598716f..47e06721 100755 --- a/mytonctrl/mytonctrl.py +++ b/mytonctrl/mytonctrl.py @@ -128,6 +128,10 @@ def inject_globals(func): module = CustomOverlayModule(ton, local) module.add_console_commands(console) + from modules.collator_config import CollatorConfigModule + module = CollatorConfigModule(ton, local) + module.add_console_commands(console) + if ton.using_validator(): from modules.validator import ValidatorModule module = ValidatorModule(ton, local) From ca211e9aa747a90e8b90a26b085857501cb62509 Mon Sep 17 00:00:00 2001 From: yungwine Date: Thu, 18 Jul 2024 21:41:39 +0800 Subject: [PATCH 2/2] bugfix --- modules/collator_config.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/collator_config.py b/modules/collator_config.py index 9596520c..9234b08f 100644 --- a/modules/collator_config.py +++ b/modules/collator_config.py @@ -3,7 +3,6 @@ from mypylib.mypylib import color_print from modules.module import MtcModule -from mytoncore.utils import hex2base64 class CollatorConfigModule(MtcModule): @@ -77,7 +76,7 @@ def get_collator_config(self, args): color_print("get_collator_config - {green}OK{endc}") def update_collator_config(self, args): - location = self.ton.get_collator_config() + location = self.ton.get_collator_config_location() config = self.get_config(location) added, msg = self.add_collator_config_to_vc(config) if not added: