Skip to content

Commit 14be068

Browse files
authored
Merge pull request #584 from ton-blockchain/dev
Dev
2 parents 24edb4a + 5873a07 commit 14be068

5 files changed

Lines changed: 129 additions & 2 deletions

File tree

modules/general.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,47 @@ def Upgrade(self, args: list[str]):
901901
text = "Upgrade - {red}Error{endc}"
902902
color_print(text)
903903

904+
def reload_global_config(self, args: list[str]):
905+
if not check_usage_args_min_max_len("reload_global_config", args, 0, 1):
906+
return
907+
if len(args) == 1:
908+
url = args[0]
909+
else:
910+
network_name = self.ton.GetNetworkName()
911+
if network_name == "mainnet":
912+
url = "https://ton-blockchain.github.io/global.config.json"
913+
elif network_name == "testnet":
914+
url = "https://ton-blockchain.github.io/testnet-global.config.json"
915+
else:
916+
raise Exception(
917+
"could not detect the network, please provide the config url explicitly"
918+
)
919+
920+
self.local.add_log(f"Downloading global config from {url}", "info")
921+
try:
922+
response = requests.get(url, timeout=30)
923+
response.raise_for_status()
924+
config_text = response.text
925+
json.loads(config_text) # make sure the downloaded config is valid json before replacing
926+
except Exception as e:
927+
color_print(
928+
f"reload_global_config error: {{red}}failed to download config from {url}: {e}{{endc}}"
929+
)
930+
return
931+
932+
global_config_path = self.ton.get_paths().global_config_path
933+
with tempfile.NamedTemporaryFile("w", suffix=".json") as tmp_file:
934+
tmp_file.write(config_text)
935+
tmp_file.flush()
936+
exit_code = run_as_root(
937+
["install", "-m", "0644", tmp_file.name, str(global_config_path)]
938+
)
939+
940+
if exit_code == 0:
941+
color_print("reload_global_config - {green}OK{endc}")
942+
else:
943+
color_print("reload_global_config - {red}Error{endc}")
944+
904945
def run_benchmark(self, args: list[str]):
905946
if shutil.which("uv") is None:
906947
answer = input("uv is not installed. Install it? [y/n] ").strip().lower()
@@ -1047,6 +1088,9 @@ def run_installer(self, args: list[str]):
10471088
def add_console_commands(self, console):
10481089
add_command(self.local, console, "update", self.Update)
10491090
add_command(self.local, console, "upgrade", self.Upgrade)
1091+
add_command(
1092+
self.local, console, "reload_global_config", self.reload_global_config
1093+
)
10501094
add_command(self.local, console, "installer", self.run_installer)
10511095
add_command(self.local, console, "status", self.print_status)
10521096
add_command(self.local, console, "status_modes", self.mode_status)

modules/utilities.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,15 @@ def check_adnl_connection(self):
373373
if not check_adnl:
374374
return True, ''
375375
self.local.add_log('Checking ADNL connection to local node', 'info')
376-
hosts = ['45.129.96.53', '5.154.181.153', '91.194.11.68', '45.12.134.214', '103.106.3.171']
376+
hosts = ['45.129.96.53', '5.154.181.153', '45.12.134.214']
377377
hosts = random.sample(hosts, k=3)
378378
data = self.ton.get_local_adnl_data()
379379
error = ''
380380
ok = True
381381
for host in hosts:
382382
url = f'http://{host}/adnl_check'
383383
try:
384-
response = requests.post(url, json=data, timeout=5).json()
384+
response = requests.post(url, json=data, timeout=3).json()
385385
except Exception as e:
386386
ok = False
387387
error = f'Failed to check ADNL connection to local node: {type(e)}: {e}'

mytonctrl/console_cmd.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
USAGES = {
77
"update": "[repo_url|repo_owner] [branch]",
88
"upgrade": "[repo_url|repo_owner] [branch]",
9+
"reload_global_config": "[url]",
910
"installer": "[command]",
1011
"status": "[fast]",
1112
"enable_mode": "<mode_name>",

mytonctrl/resources/translate.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"ru": "Подтянуть исходный код и перекомпилировать компоненты TON",
1010
"zh_TW": "拉取源碼並重新編譯 TON 組件"
1111
},
12+
"reload_global_config_cmd": {
13+
"en": "Re-download the network global config",
14+
"ru": "Переустановить глобальный конфиг сети",
15+
"zh_TW": "重新下載網路全域設定檔"
16+
},
1217
"status_cmd": {
1318
"en": "Show TON status",
1419
"ru": "Показать статус TON",

tests/integration/test_basic_commands.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,83 @@ def fake_SetSettings(self, name, value):
130130
assert "Upgrade - Error" in output
131131

132132

133+
def test_reload_global_config(cli, monkeypatch):
134+
# the cli fixture pins GetNetworkName to "mainnet"
135+
valid_config = json.dumps({"validator": {}, "liteservers": []})
136+
137+
class FakeResponse:
138+
def __init__(self, text, status=200):
139+
self.text = text
140+
self._status = status
141+
142+
def raise_for_status(self):
143+
if self._status >= 400:
144+
raise Exception(f"HTTP {self._status}")
145+
146+
requested = {}
147+
148+
def fake_get(url, timeout=None):
149+
requested["url"] = url
150+
requested["timeout"] = timeout
151+
return FakeResponse(valid_config)
152+
153+
monkeypatch.setattr(general_module.requests, "get", fake_get)
154+
155+
copied = {}
156+
157+
def fake_run_as_root(run_args):
158+
copied["args"] = run_args
159+
# the temp source file must still exist when we install it into place
160+
assert pathlib.Path(run_args[3]).is_file()
161+
return 0
162+
163+
monkeypatch.setattr(general_module, "run_as_root", fake_run_as_root)
164+
165+
# no url -> mainnet default url, installed to the global config path
166+
output = cli.execute("reload_global_config", no_color=True)
167+
assert requested["url"] == "https://ton-blockchain.github.io/global.config.json"
168+
assert copied["args"][:3] == ["install", "-m", "0644"]
169+
assert copied["args"][4] == "/usr/bin/ton/global.config.json"
170+
assert not pathlib.Path(copied["args"][3]).exists() # temp file cleaned up
171+
assert "reload_global_config - OK" in output
172+
173+
# explicit url arg overrides the default
174+
output = cli.execute(
175+
"reload_global_config https://example.com/custom.json", no_color=True
176+
)
177+
assert requested["url"] == "https://example.com/custom.json"
178+
assert "reload_global_config - OK" in output
179+
180+
# too many args -> usage error, nothing downloaded
181+
requested.clear()
182+
output = cli.execute("reload_global_config a b", no_color=True)
183+
assert "Bad args" in output
184+
assert requested == {}
185+
186+
# non-json response -> error, config is not touched
187+
copied.clear()
188+
monkeypatch.setattr(
189+
general_module.requests,
190+
"get",
191+
lambda url, timeout=None: FakeResponse("<html>not json</html>"),
192+
)
193+
output = cli.execute("reload_global_config", no_color=True)
194+
assert "reload_global_config error" in output
195+
assert copied == {}
196+
197+
# network unknown and no url -> ask for explicit url
198+
monkeypatch.setattr(general_module.requests, "get", fake_get)
199+
monkeypatch.setattr(MyTonCore, "GetNetworkName", lambda self: "unknown")
200+
output = cli.execute("reload_global_config", no_color=True)
201+
assert "could not detect the network" in output
202+
203+
# root install fails -> Error
204+
monkeypatch.setattr(MyTonCore, "GetNetworkName", lambda self: "mainnet")
205+
monkeypatch.setattr(general_module, "run_as_root", lambda _: 1)
206+
output = cli.execute("reload_global_config", no_color=True)
207+
assert "reload_global_config - Error" in output
208+
209+
133210
def test_installer(cli, monkeypatch):
134211
calls = {}
135212
def fake_run(self, cmd):

0 commit comments

Comments
 (0)