From 305117f54afe3269bc2cdd140a9e0541254f7f72 Mon Sep 17 00:00:00 2001 From: yungwine Date: Mon, 10 Feb 2025 09:33:20 +0400 Subject: [PATCH 1/4] fix CreateLocalConfigFile --- mytoninstaller/mytoninstaller.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mytoninstaller/mytoninstaller.py b/mytoninstaller/mytoninstaller.py index aaca6ca0..c0827cae 100644 --- a/mytoninstaller/mytoninstaller.py +++ b/mytoninstaller/mytoninstaller.py @@ -195,7 +195,8 @@ def PrintLiteServerConfig(local, args): def CreateLocalConfigFile(local, args): initBlock = GetInitBlock() initBlock_b64 = dict2b64(initBlock) - args = ["python3", "-m", "mytoninstaller", "-u", local.buffer.user, "-e", "clc", "-i", initBlock_b64] + user = local.buffer.user or os.environ.get("USER", "root") + args = ["python3", "-m", "mytoninstaller", "-u", user, "-e", "clc", "-i", initBlock_b64] run_as_root(args) #end define From 22993b83ca1e44e253abaf44d1276d42e76ab800 Mon Sep 17 00:00:00 2001 From: yungwine Date: Mon, 10 Feb 2025 09:39:30 +0400 Subject: [PATCH 2/4] check if tha exists before enabling --- mytoncore/mytoncore.py | 5 +++-- mytoninstaller/utils.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 22d42bd7..bdc89aea 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -33,6 +33,8 @@ Dict, int2ip ) +from mytoninstaller.utils import enable_tha + class MyTonCore(): def __init__(self, local): @@ -3079,8 +3081,7 @@ def check_enable_mode(self, name): raise Exception(f'Cannot enable validator mode while liteserver mode is enabled. ' f'Use `disable_mode liteserver` first.') if name == 'liquid-staking': - from mytoninstaller.settings import enable_ton_http_api - enable_ton_http_api(self.local) + enable_tha(self.local) def enable_mode(self, name): if name not in MODES: diff --git a/mytoninstaller/utils.py b/mytoninstaller/utils.py index a2cd817f..4c2fdf0f 100644 --- a/mytoninstaller/utils.py +++ b/mytoninstaller/utils.py @@ -2,6 +2,8 @@ import json import time import subprocess + +import requests from nacl.signing import SigningKey @@ -50,3 +52,25 @@ def get_ed25519_pubkey(privkey): pubkey = privkey_obj.verify_key.encode() return pubkey #end define + + +def tha_exists(): + try: + resp = requests.get('http://127.0.0.1:8801/healthcheck', timeout=3) + except: + return False + if resp.status_code == 200 and resp.text == '"OK"': + return True + return False + + +def enable_tha(local): + try: + if not tha_exists(): + local.add_log("Enabling TON HTTP API", "debug") + from mytoninstaller.settings import enable_ton_http_api + enable_ton_http_api(local) + local.add_log("Enabled TON HTTP API", "debug") + except Exception as e: + local.add_log(f"Error in enable_tha: {e}", "warning") + pass From 371a4a477e810c383d4194f9ce628ae501471e72 Mon Sep 17 00:00:00 2001 From: yungwine Date: Thu, 13 Feb 2025 13:22:47 +0400 Subject: [PATCH 3/4] refactor enable tha --- mytoncore/mytoncore.py | 5 ++--- mytoninstaller/settings.py | 25 ++++++++++++++++++++++--- mytoninstaller/utils.py | 24 ------------------------ 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index bdc89aea..22d42bd7 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -33,8 +33,6 @@ Dict, int2ip ) -from mytoninstaller.utils import enable_tha - class MyTonCore(): def __init__(self, local): @@ -3081,7 +3079,8 @@ def check_enable_mode(self, name): raise Exception(f'Cannot enable validator mode while liteserver mode is enabled. ' f'Use `disable_mode liteserver` first.') if name == 'liquid-staking': - enable_tha(self.local) + from mytoninstaller.settings import enable_ton_http_api + enable_ton_http_api(self.local) def enable_mode(self, name): if name not in MODES: diff --git a/mytoninstaller/settings.py b/mytoninstaller/settings.py index c9b667b3..d81f572d 100644 --- a/mytoninstaller/settings.py +++ b/mytoninstaller/settings.py @@ -485,17 +485,36 @@ def EnableJsonRpc(local): color_print(text) #end define +def tha_exists(): + try: + resp = requests.get('http://127.0.0.1:8801/healthcheck', timeout=3) + except: + return False + if resp.status_code == 200 and resp.text == '"OK"': + return True + return False +#end define + def enable_ton_http_api(local): - local.add_log("start EnableTonHttpApi function", "debug") + try: + if not tha_exists(): + enable_ton_http_api(local) + except Exception as e: + local.add_log(f"Error in enable_ton_http_api: {e}", "warning") + pass +#end define + +def do_enable_ton_http_api(local): + local.add_log("start do_enable_ton_http_api function", "debug") if not os.path.exists('/usr/bin/ton/local.config.json'): from mytoninstaller.mytoninstaller import CreateLocalConfigFile CreateLocalConfigFile(local, []) ton_http_api_installer_path = pkg_resources.resource_filename('mytoninstaller.scripts', 'ton_http_api_installer.sh') exit_code = run_as_root(["bash", ton_http_api_installer_path]) if exit_code == 0: - text = "EnableTonHttpApi - {green}OK{endc}" + text = "do_enable_ton_http_api - {green}OK{endc}" else: - text = "EnableTonHttpApi - {red}Error{endc}" + text = "do_enable_ton_http_api - {red}Error{endc}" color_print(text) #end define diff --git a/mytoninstaller/utils.py b/mytoninstaller/utils.py index 4c2fdf0f..a2cd817f 100644 --- a/mytoninstaller/utils.py +++ b/mytoninstaller/utils.py @@ -2,8 +2,6 @@ import json import time import subprocess - -import requests from nacl.signing import SigningKey @@ -52,25 +50,3 @@ def get_ed25519_pubkey(privkey): pubkey = privkey_obj.verify_key.encode() return pubkey #end define - - -def tha_exists(): - try: - resp = requests.get('http://127.0.0.1:8801/healthcheck', timeout=3) - except: - return False - if resp.status_code == 200 and resp.text == '"OK"': - return True - return False - - -def enable_tha(local): - try: - if not tha_exists(): - local.add_log("Enabling TON HTTP API", "debug") - from mytoninstaller.settings import enable_ton_http_api - enable_ton_http_api(local) - local.add_log("Enabled TON HTTP API", "debug") - except Exception as e: - local.add_log(f"Error in enable_tha: {e}", "warning") - pass From faf34895a8192edf6081747508bb3123d727026c Mon Sep 17 00:00:00 2001 From: yungwine Date: Thu, 13 Feb 2025 13:27:52 +0400 Subject: [PATCH 4/4] fix refactor enable tha --- mytoninstaller/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mytoninstaller/settings.py b/mytoninstaller/settings.py index d81f572d..f8452dba 100644 --- a/mytoninstaller/settings.py +++ b/mytoninstaller/settings.py @@ -498,7 +498,7 @@ def tha_exists(): def enable_ton_http_api(local): try: if not tha_exists(): - enable_ton_http_api(local) + do_enable_ton_http_api(local) except Exception as e: local.add_log(f"Error in enable_ton_http_api: {e}", "warning") pass