Skip to content

Commit c171ae5

Browse files
authored
Merge pull request #507 from ton-blockchain/dev
Dev
2 parents 1b12526 + 51e11ca commit c171ae5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3278
-1560
lines changed

.github/workflows/tests.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Lint + Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
pull_request:
8+
branches:
9+
- '**'
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-22.04
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
19+
20+
steps:
21+
- uses: actions/checkout@v5
22+
with:
23+
submodules: true
24+
25+
- name: Set up Python ${{ matrix.python-version }}
26+
uses: actions/setup-python@v5
27+
with:
28+
python-version: ${{ matrix.python-version }}
29+
30+
- name: Install dependencies
31+
run: |
32+
python -m pip install --upgrade pip
33+
pip install -r requirements.txt
34+
pip install ruff pytest pytest-mock
35+
36+
- name: Run Ruff
37+
run: |
38+
ruff check
39+
40+
- name: Build project
41+
run: |
42+
pip install -U .
43+
44+
- name: Run pytest
45+
run: |
46+
pytest --import-mode=append # to test built package

modules/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from modules.collator import CollatorModule
55
from modules.module import MtcModule
6-
from modules.pool import PoolModule
76
from modules.nominator_pool import NominatorPoolModule
87
from modules.single_pool import SingleNominatorModule
98
from modules.validator import ValidatorModule
@@ -38,7 +37,7 @@ class Setting:
3837

3938
SETTINGS = {
4039
'stake': Setting('validator', None, 'Stake amount'),
41-
'stakePercent': Setting('validator', 99, 'Stake percent if `stake` is null'),
40+
'stakePercent': Setting('validator', 100, 'Stake percent if `stake` is null'),
4241
'isSlashing': Setting('validator', None, 'Create complaints to validators'),
4342
'validatorWalletName': Setting('validator', 'wallet_001', 'Validator\'s wallet name'),
4443
'maxFactor': Setting('validator', None, 'Param send to Elector. if null will be taken from 17 config param'),

modules/alert_bot.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from modules.module import MtcModule
66
from mypylib.mypylib import get_timestamp, print_table, color_print
7-
from mytoncore import get_hostname, signed_int_to_hex64
7+
from mytoncore.utils import get_hostname, signed_int_to_hex64
8+
from mytonctrl.console_cmd import add_command, check_usage_one_arg, check_usage_two_args
89
from mytonctrl.utils import timestamp2utcdatetime
910

1011

@@ -333,15 +334,15 @@ def _is_alert_active(self, alert_name: str) -> bool:
333334
return self.get_alert_from_db(alert_name).get('active', False)
334335

335336
def enable_alert(self, args):
336-
if len(args) != 1:
337-
raise Exception("Usage: enable_alert <alert_name>")
337+
if not check_usage_one_arg("enable_alert", args):
338+
return
338339
alert_name = args[0]
339340
self.set_alert_enabled(alert_name, True)
340341
color_print("enable_alert - {green}OK{endc}")
341342

342343
def disable_alert(self, args):
343-
if len(args) != 1:
344-
raise Exception("Usage: disable_alert <alert_name>")
344+
if not check_usage_one_arg("disable_alert", args):
345+
return
345346
alert_name = args[0]
346347
self.set_alert_enabled(alert_name, False)
347348
color_print("disable_alert - {green}OK{endc}")
@@ -360,8 +361,8 @@ def test_alert(self, args):
360361
self.send_message('Test alert')
361362

362363
def setup_alert_bot(self, args):
363-
if len(args) != 2:
364-
raise Exception("Usage: setup_alert_bot <bot_token> <chat_id>")
364+
if not check_usage_two_args("setup_alert_bot", args):
365+
return
365366
self.token = args[0]
366367
self.chat_id = args[1]
367368
init_alerts()
@@ -373,7 +374,7 @@ def setup_alert_bot(self, args):
373374
color_print("setup_alert_bot - {green}OK{endc}")
374375
except Exception as e:
375376
self.local.add_log(f"Error while sending welcome message: {e}", "error")
376-
self.local.add_log(f"If you want the bot to write to a multi-person chat group, make sure the bot is added to that chat group. If it is not - do it and run the command `setup_alert_bot <bot_token> <chat_id>` again.", "info")
377+
self.local.add_log("If you want the bot to write to a multi-person chat group, make sure the bot is added to that chat group. If it is not - do it and run the command `setup_alert_bot <bot_token> <chat_id>` again.", "info")
377378
color_print("setup_alert_bot - {red}Error{endc}")
378379

379380
def send_welcome_message(self):
@@ -602,8 +603,8 @@ def check_status(self):
602603
self.local.try_function(self.check_online_collators)
603604

604605
def add_console_commands(self, console):
605-
console.AddItem("enable_alert", self.enable_alert, self.local.translate("enable_alert_cmd"))
606-
console.AddItem("disable_alert", self.disable_alert, self.local.translate("disable_alert_cmd"))
607-
console.AddItem("list_alerts", self.print_alerts, self.local.translate("list_alerts_cmd"))
608-
console.AddItem("test_alert", self.test_alert, self.local.translate("test_alert_cmd"))
609-
console.AddItem("setup_alert_bot", self.setup_alert_bot, self.local.translate("setup_alert_bot_cmd"))
606+
add_command(self.local, console, "enable_alert", self.enable_alert)
607+
add_command(self.local, console, "disable_alert", self.disable_alert)
608+
add_command(self.local, console, "list_alerts", self.print_alerts)
609+
add_command(self.local, console, "test_alert", self.test_alert)
610+
add_command(self.local, console, "setup_alert_bot", self.setup_alert_bot)

modules/backups.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
import shutil
33
import subprocess
44
import time
5+
from typing import Optional
56

67
from modules.module import MtcModule
7-
from mypylib.mypylib import color_print, ip2int, run_as_root, parse, MyPyClass
8+
from mytonctrl.console_cmd import add_command, check_usage_args_min_max_len
9+
from mypylib.mypylib import color_print, ip2int, run_as_root, parse
810
from mytoncore.utils import get_package_resource_path
911
from mytonctrl.utils import get_current_user, pop_user_from_args
1012
from mytoninstaller.config import get_own_ip
1113

1214

1315
class BackupModule(MtcModule):
1416

15-
def create_keyring(self, dir_name):
17+
def create_keyring(self, dir_name: str):
1618
keyring_dir = dir_name + '/keyring'
1719
self.ton.validatorConsole.Run(f'exportallprivatekeys {keyring_dir}')
1820

1921
def create_tmp_ton_dir(self):
2022
result = self.ton.validatorConsole.Run("getconfig")
2123
text = parse(result, "---------", "--------")
24+
if text is None:
25+
raise Exception("Could not get config from validator-console")
2226
dir_name = self.ton.tempDir + f'/ton_backup_{int(time.time() * 1000)}'
2327
dir_name_db = dir_name + '/db'
2428
os.makedirs(dir_name_db)
@@ -28,15 +32,14 @@ def create_tmp_ton_dir(self):
2832
return dir_name
2933

3034
@staticmethod
31-
def run_create_backup(args, user: str = None):
35+
def run_create_backup(args, user: Optional[str] = None):
3236
if user is None:
3337
user = get_current_user()
3438
with get_package_resource_path('mytonctrl', 'scripts/create_backup.sh') as backup_script_path:
3539
return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5)
3640

3741
def create_backup(self, args):
38-
if len(args) > 3:
39-
color_print("{red}Bad args. Usage:{endc} create_backup [filename] [-u <user>]")
42+
if not check_usage_args_min_max_len("create_backup", args, 0, 3):
4043
return
4144
tmp_dir = self.create_tmp_ton_dir()
4245
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-t", tmp_dir]
@@ -51,23 +54,21 @@ def create_backup(self, args):
5154
color_print("create_backup - {red}Error{endc}")
5255
shutil.rmtree(tmp_dir)
5356
return process.returncode
54-
# end define
5557

5658
@staticmethod
57-
def run_restore_backup(args, user: str = None):
59+
def run_restore_backup(args, user: Optional[str] = None):
5860
if user is None:
5961
user = get_current_user()
6062
with get_package_resource_path('mytonctrl', 'scripts/restore_backup.sh') as restore_script_path:
6163
return run_as_root(["bash", restore_script_path, "-u", user] + args)
6264

6365
def restore_backup(self, args):
64-
if len(args) == 0 or len(args) > 5:
65-
color_print("{red}Bad args. Usage:{endc} restore_backup <filename> [-y] [--skip-create-backup] [-u <user>]")
66+
if not check_usage_args_min_max_len('restore_backup', args, 1, 5):
6667
return
6768
user = pop_user_from_args(args)
6869
if '-y' not in args:
6970
res = input(
70-
f'This action will overwrite existing configuration with contents of backup archive, please make sure that donor node is not in operation prior to this action. Proceed [y/n]')
71+
'This action will overwrite existing configuration with contents of backup archive, please make sure that donor node is not in operation prior to this action. Proceed [y/n]')
7172
if res.lower() != 'y':
7273
print('aborted.')
7374
return
@@ -79,8 +80,8 @@ def restore_backup(self, args):
7980
print('Before proceeding, mtc will create a backup of current configuration.')
8081
try:
8182
self.create_backup([])
82-
except:
83-
color_print("{red}Could not create backup{endc}")
83+
except Exception as e:
84+
color_print(f"{{red}}Could not create backup: {e}{{endc}}")
8485

8586
ip = str(ip2int(get_own_ip()))
8687
command_args = ["-m", self.ton.local.buffer.my_work_dir, "-n", args[0], "-i", ip]
@@ -94,8 +95,7 @@ def restore_backup(self, args):
9495
self.local.exit()
9596
else:
9697
color_print("restore_backup - {red}Error{endc}")
97-
# end define
9898

9999
def add_console_commands(self, console):
100-
console.AddItem("create_backup", self.create_backup, self.local.translate("create_backup_cmd"))
101-
console.AddItem("restore_backup", self.restore_backup, self.local.translate("restore_backup_cmd"))
100+
add_command(self.local, console, "create_backup", self.create_backup)
101+
add_command(self.local, console, "restore_backup", self.restore_backup)

0 commit comments

Comments
 (0)