Skip to content

Commit 1b12526

Browse files
authored
Merge pull request #496 from ton-blockchain/dev
Dev
2 parents 87823a0 + 775020a commit 1b12526

File tree

14 files changed

+391
-219
lines changed

14 files changed

+391
-219
lines changed

modules/backups.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import subprocess
44
import time
55

6-
import pkg_resources
7-
86
from modules.module import MtcModule
97
from mypylib.mypylib import color_print, ip2int, run_as_root, parse, MyPyClass
8+
from mytoncore.utils import get_package_resource_path
109
from mytonctrl.utils import get_current_user, pop_user_from_args
1110
from mytoninstaller.config import get_own_ip
1211

@@ -32,8 +31,8 @@ def create_tmp_ton_dir(self):
3231
def run_create_backup(args, user: str = None):
3332
if user is None:
3433
user = get_current_user()
35-
backup_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/create_backup.sh')
36-
return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5)
34+
with get_package_resource_path('mytonctrl', 'scripts/create_backup.sh') as backup_script_path:
35+
return subprocess.run(["bash", backup_script_path, "-u", user] + args, timeout=5)
3736

3837
def create_backup(self, args):
3938
if len(args) > 3:
@@ -58,8 +57,8 @@ def create_backup(self, args):
5857
def run_restore_backup(args, user: str = None):
5958
if user is None:
6059
user = get_current_user()
61-
restore_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/restore_backup.sh')
62-
return run_as_root(["bash", restore_script_path, "-u", user] + args)
60+
with get_package_resource_path('mytonctrl', 'scripts/restore_backup.sh') as restore_script_path:
61+
return run_as_root(["bash", restore_script_path, "-u", user] + args)
6362

6463
def restore_backup(self, args):
6564
if len(args) == 0 or len(args) > 5:

modules/btc_teleport.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import os
33
import subprocess
44

5-
import pkg_resources
6-
75
from modules.module import MtcModule
86
from mypylib.mypylib import run_as_root, color_print, bcolors, print_table
7+
from mytoncore.utils import get_package_resource_path
98
from mytonctrl.utils import get_current_user
109

1110

@@ -59,20 +58,20 @@ def create_env_file(self, reinit=False):
5958

6059
def add_daemon(self, user: str = None):
6160
start = f'{self.bin_dir}/oracle'
62-
script_path = pkg_resources.resource_filename('mytoninstaller', 'scripts/add2systemd.sh')
6361
if user is None:
6462
user = get_current_user()
65-
run_as_root(['bash', script_path, '-n', 'btc_teleport', '-u', user, '-g', user, '-s', start, '-w', self.bin_dir])
63+
with get_package_resource_path('mytoninstaller', 'scripts/add2systemd.sh') as script_path:
64+
run_as_root(['bash', script_path, '-n', 'btc_teleport', '-u', user, '-g', user, '-s', start, '-w', self.bin_dir])
6665

6766
def install(self, branch: str, user: str = None):
68-
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport1.sh')
6967
if user is None:
7068
user = get_current_user()
71-
exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch, "-u", user])
69+
with get_package_resource_path('mytonctrl', 'scripts/btc_teleport1.sh') as script_path:
70+
exit_code = run_as_root(["bash", script_path, "-s", '/usr/src', "-r", self.repo_name, "-b", branch, "-u", user])
7271
if exit_code != 0:
7372
raise Exception('Failed to install btc_teleport')
74-
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/btc_teleport2.sh')
75-
subprocess.run(["bash", script_path, "-s", self.src_dir])
73+
with get_package_resource_path('mytonctrl', 'scripts/btc_teleport2.sh') as script_path:
74+
subprocess.run(["bash", script_path, "-s", self.src_dir])
7675

7776
def init(self, reinstall=False, branch: str = 'master', user: str = None):
7877
if os.path.exists(self.src_dir) and not reinstall:
@@ -89,8 +88,8 @@ def init(self, reinstall=False, branch: str = 'master', user: str = None):
8988

9089
@staticmethod
9190
def run_remove_btc_teleport(args):
92-
script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/remove_btc_teleport.sh')
93-
return run_as_root(["bash", script_path] + args)
91+
with get_package_resource_path('mytonctrl', 'scripts/remove_btc_teleport.sh') as script_path:
92+
return run_as_root(["bash", script_path] + args)
9493

9594
def get_save_offers(self):
9695
bname = "saveOffersBtcTeleport"
@@ -233,5 +232,3 @@ def remove_btc_teleport(self, args: list):
233232

234233
def add_console_commands(self, console):
235234
console.AddItem("remove_btc_teleport", self.remove_btc_teleport, self.local.translate("remove_btc_teleport_cmd"))
236-
console.AddItem("vote_offer_btc_teleport", self.vote_offer_btc_teleport, self.local.translate("vote_offer_btc_teleport_cmd"))
237-
console.AddItem("print_offers_btc_teleport_list", self.print_offers_btc_teleport_list, self.local.translate("print_offers_btc_teleport_list_cmd"))

modules/single_pool.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import os
22

3-
import pkg_resources
4-
53
from mypylib.mypylib import color_print
64
from modules.pool import PoolModule
5+
from mytoncore.utils import get_package_resource_path
76

87

98
class SingleNominatorModule(PoolModule):
@@ -21,12 +20,11 @@ def do_create_single_pool(self, pool_name, owner_address):
2120
self.ton.local.add_log("create_single_pool warning: Pool already exists: " + file_path, "warning")
2221
return
2322

24-
fift_script = pkg_resources.resource_filename('mytoncore', 'contracts/single-nominator-pool/init.fif')
25-
code_boc = pkg_resources.resource_filename('mytoncore',
26-
'contracts/single-nominator-pool/single-nominator-code.hex')
2723
validator_wallet = self.ton.GetValidatorWallet()
28-
args = [fift_script, code_boc, owner_address, validator_wallet.addrB64, file_path]
29-
result = self.ton.fift.Run(args)
24+
with get_package_resource_path('mytoncore', 'contracts/single-nominator-pool/init.fif') as fift_script:
25+
with get_package_resource_path('mytoncore', 'contracts/single-nominator-pool/single-nominator-code.hex') as code_boc:
26+
args = [fift_script, code_boc, owner_address, validator_wallet.addrB64, file_path]
27+
result = self.ton.fift.Run(args)
3028
if "Saved single nominator pool" not in result:
3129
raise Exception("create_single_pool error: " + result)
3230

mytoncore/functions.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ def Telemetry(local, ton):
499499
data["cpuInfo"] = {'cpuName': local.try_function(get_cpu_name), 'virtual': local.try_function(is_host_virtual)}
500500
data["validatorDiskName"] = local.try_function(get_validator_disk_name)
501501
data["pings"] = local.try_function(get_pings_values)
502+
data["pythonVersion"] = sys.version
502503

503504
# Get git hashes
504505
gitHashes = dict()
@@ -649,24 +650,34 @@ def gc_import(local, ton):
649650
if not files:
650651
local.add_log("No files left to import", "debug")
651652
ton.local.db['importGc'] = False
653+
ton.local.save()
652654
return
653655
try:
654656
status = ton.GetValidatorStatus()
655657
node_seqno = int(status.shardclientmasterchainseqno)
656658
except Exception as e:
657659
local.add_log(f"Failed to get shardclientmasterchainseqno: {e}", "warning")
658660
return
659-
removed = 0
660-
for file in files:
661-
file_seqno = int(file.split('.')[1])
662-
if node_seqno > file_seqno + 101:
663-
try:
664-
os.remove(os.path.join(import_path, file))
665-
removed += 1
666-
except PermissionError:
667-
local.add_log(f"Failed to remove file {file}: Permission denied", "error")
668-
continue
669-
local.add_log(f"Removed {removed} import files up to {node_seqno} seqno", "debug")
661+
to_delete = []
662+
to_delete_dirs = []
663+
for root, dirs, files in os.walk(import_path):
664+
if root != import_path and not dirs and not files:
665+
to_delete_dirs.append(root)
666+
for file in files:
667+
file_seqno = int(file.split('.')[1])
668+
if node_seqno > file_seqno + 101:
669+
to_delete.append(os.path.join(root, file))
670+
for file_path in to_delete:
671+
try:
672+
os.remove(file_path)
673+
except Exception as e:
674+
local.add_log(f"Failed to remove file {file_path}: {e}", "error")
675+
for dir_path in to_delete_dirs:
676+
try:
677+
os.rmdir(dir_path)
678+
except Exception as e:
679+
local.add_log(f"Failed to remove dir {dir_path}: {e}", "error")
680+
local.add_log(f"Removed {len(to_delete)} import files and {len(to_delete_dirs)} import dirs up to {node_seqno} seqno", "debug")
670681

671682

672683
def backup_mytoncore_logs(local: MyPyClass, ton: MyTonCore):
@@ -741,8 +752,7 @@ def General(local):
741752
if ton.in_initial_sync():
742753
local.start_cycle(check_initial_sync, sec=120, args=(local, ton))
743754

744-
if ton.local.db.get('importGc'):
745-
local.start_cycle(gc_import, sec=300, args=(local, ton))
755+
local.start_cycle(gc_import, sec=600, args=(local, ton))
746756

747757
thr_sleep()
748758
# end define

mytoncore/mytoncore.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
import struct
88
import psutil
99
import subprocess
10-
import pkg_resources
1110
import requests
1211
from fastcrc import crc16
1312

1413
from modules import MODES
1514
from modules.btc_teleport import BtcTeleportModule
16-
from mytoncore.utils import xhex2hex, ng2g
15+
from mytoncore.utils import xhex2hex, ng2g, get_package_resource_path
1716
from mytoncore.liteclient import LiteClient
1817
from mytoncore.validator_console import ValidatorConsole
1918
from mytoncore.fift import Fift
@@ -1115,9 +1114,9 @@ def CreateComplaintRequest(self, electionId, complaintHash, validatorIndex):
11151114
def remove_proofs_from_complaint(self, input_file_name: str):
11161115
self.local.add_log("start remove_proofs_from_complaint function", "debug")
11171116
output_file_name = self.tempDir + "complaint-new.boc"
1118-
fift_script = pkg_resources.resource_filename('mytoncore', 'complaints/remove-proofs-v2.fif')
1119-
args = [fift_script, input_file_name, output_file_name]
1120-
result = self.fift.Run(args)
1117+
with get_package_resource_path('mytoncore', 'complaints/remove-proofs-v2.fif') as fift_script:
1118+
args = [fift_script, input_file_name, output_file_name]
1119+
result = self.fift.Run(args)
11211120
return output_file_name
11221121

11231122

@@ -1304,7 +1303,7 @@ def GetStake(self, account, args=None):
13041303
stake = self.local.db.get("stake")
13051304
usePool = self.using_pool()
13061305
useController = self.using_liquid_staking()
1307-
stakePercent = self.local.db.get("stakePercent", 99)
1306+
stakePercent = self.local.db.get("stakePercent", 100)
13081307
vconfig = self.GetValidatorConfig()
13091308
validators = vconfig.get("validators")
13101309
config17 = self.GetConfig17()
@@ -1343,6 +1342,8 @@ def GetStake(self, account, args=None):
13431342
stake = int(account.balance*sp)
13441343
elif len(vconfig.validators) > 0:
13451344
stake = int(account.balance*sp)
1345+
if stakePercent == 100:
1346+
stake -= 20
13461347

13471348
# Check if we have enough coins
13481349
if stake > config17["maxStake"]:

mytoncore/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
import json
33
import re
44
import subprocess
5+
try:
6+
# Python 3.9+
7+
from importlib.resources import files, as_file
8+
except ImportError:
9+
# Python < 3.9
10+
from importlib_resources import files, as_file
511

612

713
def str2b64(s):
@@ -177,3 +183,8 @@ def shard_is_ancestor(parent: int, child: int) -> bool:
177183
y = _lower_bit64(uc)
178184
mask = (_bits_negate64(x) << 1) & _MASK64
179185
return x >= y and not ((up ^ uc) & mask)
186+
187+
188+
def get_package_resource_path(package: str, resource: str):
189+
ref = files(package) / resource
190+
return as_file(ref)

mytonctrl/migrate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
2-
import pkg_resources
32

43
from mypylib.mypylib import MyPyClass
4+
from mytoncore.utils import get_package_resource_path
55
from mytoncore.mytoncore import MyTonCore
66

77
from mypylib.mypylib import (
@@ -13,9 +13,9 @@
1313

1414
def migrate_to_version_1(local: MyPyClass, ton: MyTonCore):
1515
# get script path
16-
migrate_script_path = pkg_resources.resource_filename('mytonctrl', 'migrations/migration_001.sh')
17-
args = ["/bin/bash", migrate_script_path]
18-
exit_code = run_as_root(args)
16+
with get_package_resource_path('mytonctrl', 'migrations/migration_001.sh') as migrate_script_path:
17+
args = ["/bin/bash", migrate_script_path]
18+
exit_code = run_as_root(args)
1919
if exit_code != 0:
2020
raise RuntimeError(f'Failed to run migration error. Exit code: {exit_code}')
2121
return

0 commit comments

Comments
 (0)