Skip to content

Commit 401e0e2

Browse files
authored
Merge pull request #271 from ton-blockchain/mytonctrl2_testnet
Mytonctrl2 testnet
2 parents f97cfff + f1d4947 commit 401e0e2

File tree

15 files changed

+343
-135
lines changed

15 files changed

+343
-135
lines changed

modules/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
from modules.single_pool import SingleNominatorModule
88
from modules.validator import ValidatorModule
99
from modules.controller import ControllerModule
10+
from modules.liteserver import LiteserverModule
1011

1112

1213
MODES = {
1314
'validator': ValidatorModule,
1415
'nominator-pool': NominatorPoolModule,
1516
'single-nominator': SingleNominatorModule,
1617
'liquid-staking': ControllerModule,
18+
'liteserver': LiteserverModule
1719
}
1820

1921

@@ -44,6 +46,7 @@ class Setting:
4446
'overlayTelemetryUrl': Setting(None, 'https://telemetry.toncenter.com/report_overlays', 'Overlay telemetry url'),
4547
'duplicateApi': Setting(None, 'sendTelemetry', 'Duplicate external to Toncenter'),
4648
'duplicateApiUrl': Setting(None, 'https://[testnet.]toncenter.com/api/v2/sendBoc', 'Toncenter api url for duplicate'),
49+
'checkAdnl': Setting(None, 'sendTelemetry', 'Check local udp port and adnl connection'),
4750
'liteclient_timeout': Setting(None, 3, 'Liteclient default timeout'),
4851
'console_timeout': Setting(None, 3, 'Validator console default timeout'),
4952
'fift_timeout': Setting(None, 3, 'Fift default timeout'),

modules/liteserver.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import psutil
2+
3+
from modules.module import MtcModule
4+
5+
6+
class LiteserverModule(MtcModule):
7+
8+
description = 'For liteserver usage only without validator.'
9+
default_value = False
10+
11+
def add_console_commands(self, console):
12+
...

modules/nominator_pool.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,6 @@ def activate_pool(self, args):
7878
self.do_activate_pool(pool)
7979
color_print("ActivatePool - {green}OK{endc}")
8080

81-
def do_deposit_to_pool(self, pool_addr, amount):
82-
wallet = self.ton.GetValidatorWallet()
83-
bocPath = self.ton.local.buffer.my_temp_dir + wallet.name + "validator-deposit-query.boc"
84-
fiftScript = self.ton.contractsDir + "nominator-pool/func/validator-deposit.fif"
85-
args = [fiftScript, bocPath]
86-
result = self.ton.fift.Run(args)
87-
resultFilePath = self.ton.SignBocWithWallet(wallet, bocPath, pool_addr, amount)
88-
self.ton.SendFile(resultFilePath, wallet)
89-
90-
def deposit_to_pool(self, args):
91-
try:
92-
poll_addr = args[0]
93-
amount = float(args[1])
94-
except:
95-
color_print("{red}Bad args. Usage:{endc} deposit_to_pool <pool-addr> <amount>")
96-
return
97-
self.do_deposit_to_pool(poll_addr, amount)
98-
color_print("DepositToPool - {green}OK{endc}")
99-
100-
def do_withdraw_from_pool(self, pool_addr, amount):
101-
pool_data = self.ton.GetPoolData(pool_addr)
102-
if pool_data["state"] == 0:
103-
self.ton.WithdrawFromPoolProcess(pool_addr, amount)
104-
else:
105-
self.ton.PendWithdrawFromPool(pool_addr, amount)
106-
107-
def withdraw_from_pool(self, args):
108-
try:
109-
pool_addr = args[0]
110-
amount = float(args[1])
111-
except:
112-
color_print("{red}Bad args. Usage:{endc} withdraw_from_pool <pool-addr> <amount>")
113-
return
114-
self.do_withdraw_from_pool(pool_addr, amount)
115-
color_print("WithdrawFromPool - {green}OK{endc}")
116-
11781
def update_validator_set(self, args):
11882
try:
11983
pool_addr = args[0]
@@ -127,8 +91,4 @@ def update_validator_set(self, args):
12791
def add_console_commands(self, console):
12892
console.AddItem("new_pool", self.new_pool, self.local.translate("new_pool_cmd"))
12993
console.AddItem("activate_pool", self.activate_pool, self.local.translate("activate_pool_cmd"))
130-
console.AddItem("deposit_to_pool", self.deposit_to_pool, self.local.translate("deposit_to_pool_cmd"))
131-
console.AddItem("withdraw_from_pool", self.withdraw_from_pool, self.local.translate("withdraw_from_pool_cmd"))
13294
console.AddItem("update_validator_set", self.update_validator_set, self.local.translate("update_validator_set_cmd"))
133-
134-

modules/pool.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,45 @@ def check_download_pool_contract_scripts(self):
5858
if not os.path.isdir(contract_path):
5959
self.ton.DownloadContract("https://github.com/ton-blockchain/nominator-pool")
6060

61+
def do_deposit_to_pool(self, pool_addr, amount):
62+
wallet = self.ton.GetValidatorWallet()
63+
bocPath = self.ton.local.buffer.my_temp_dir + wallet.name + "validator-deposit-query.boc"
64+
fiftScript = self.ton.contractsDir + "nominator-pool/func/validator-deposit.fif"
65+
args = [fiftScript, bocPath]
66+
result = self.ton.fift.Run(args)
67+
resultFilePath = self.ton.SignBocWithWallet(wallet, bocPath, pool_addr, amount)
68+
self.ton.SendFile(resultFilePath, wallet)
69+
70+
def deposit_to_pool(self, args):
71+
try:
72+
poll_addr = args[0]
73+
amount = float(args[1])
74+
except:
75+
color_print("{red}Bad args. Usage:{endc} deposit_to_pool <pool-addr> <amount>")
76+
return
77+
self.do_deposit_to_pool(poll_addr, amount)
78+
color_print("DepositToPool - {green}OK{endc}")
79+
80+
def do_withdraw_from_pool(self, pool_addr, amount):
81+
pool_data = self.ton.GetPoolData(pool_addr)
82+
if pool_data["state"] == 0:
83+
self.ton.WithdrawFromPoolProcess(pool_addr, amount)
84+
else:
85+
self.ton.PendWithdrawFromPool(pool_addr, amount)
86+
87+
def withdraw_from_pool(self, args):
88+
try:
89+
pool_addr = args[0]
90+
amount = float(args[1])
91+
except:
92+
color_print("{red}Bad args. Usage:{endc} withdraw_from_pool <pool-addr> <amount>")
93+
return
94+
self.do_withdraw_from_pool(pool_addr, amount)
95+
color_print("WithdrawFromPool - {green}OK{endc}")
96+
6197
def add_console_commands(self, console):
6298
console.AddItem("pools_list", self.print_pools_list, self.local.translate("pools_list_cmd"))
6399
console.AddItem("delete_pool", self.delete_pool, self.local.translate("delete_pool_cmd"))
64100
console.AddItem("import_pool", self.import_pool, self.local.translate("import_pool_cmd"))
101+
console.AddItem("deposit_to_pool", self.deposit_to_pool, self.local.translate("deposit_to_pool_cmd"))
102+
console.AddItem("withdraw_from_pool", self.withdraw_from_pool, self.local.translate("withdraw_from_pool_cmd"))

mytoncore/functions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import subprocess
1111

1212
from mytoncore.mytoncore import MyTonCore
13+
from mytonctrl.utils import fix_git_config
1314
from mytoninstaller.config import GetConfig
1415
from mypylib.mypylib import (
1516
b2mb,
@@ -54,6 +55,8 @@ def Event(local, event_name):
5455
ValidatorDownEvent(local)
5556
elif event_name == "enable_ton_storage_provider":
5657
enable_ton_storage_provider_event(local)
58+
elif event_name == "enable_liteserver_mode":
59+
enable_liteserver_mode(local)
5760
local.exit()
5861
# end define
5962

@@ -90,6 +93,13 @@ def enable_ton_storage_provider_event(local):
9093
#end define
9194

9295

96+
def enable_liteserver_mode(local):
97+
ton = MyTonCore(local)
98+
ton.disable_mode('validator')
99+
ton.enable_mode('liteserver')
100+
#end define
101+
102+
93103
def Elections(local, ton):
94104
use_pool = ton.using_pool()
95105
use_liquid_staking = ton.using_liquid_staking()
@@ -411,7 +421,9 @@ def Telemetry(local, ton):
411421

412422
# Get git hashes
413423
gitHashes = dict()
414-
gitHashes["mytonctrl"] = get_git_hash("/usr/src/mytonctrl")
424+
mtc_path = "/usr/src/mytonctrl"
425+
local.try_function(fix_git_config, args=[mtc_path])
426+
gitHashes["mytonctrl"] = get_git_hash(mtc_path)
415427
gitHashes["validator"] = GetBinGitHash(
416428
"/usr/bin/ton/validator-engine/validator-engine")
417429
data["gitHashes"] = gitHashes

mytoncore/mytoncore.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,8 @@ def SignBocWithWallet(self, wallet, boc_path, dest, coins, **kwargs):
12081208
elif "v3" in wallet.version:
12091209
fift_script = "wallet-v3.fif"
12101210
args = [fift_script, wallet.path, dest, subwallet, seqno, coins, boc_mode, boc_path, result_file_path]
1211+
else:
1212+
raise Exception(f"SignBocWithWallet error: Wallet version '{wallet.version}' is not supported")
12111213
if flags:
12121214
args += flags
12131215
result = self.fift.Run(args)
@@ -1912,6 +1914,8 @@ def MoveCoins(self, wallet, dest, coins, **kwargs):
19121914
elif "v3" in wallet.version:
19131915
fiftScript = "wallet-v3.fif"
19141916
args = [fiftScript, wallet.path, dest, subwallet, seqno, coins, "-m", mode, resultFilePath]
1917+
else:
1918+
raise Exception(f"MoveCoins error: Wallet version '{wallet.version}' is not supported")
19151919
if flags:
19161920
args += flags
19171921
result = self.fift.Run(args)
@@ -2276,6 +2280,9 @@ def GetComplaints(self, electionId=None, past=False):
22762280
complaints[chash] = item
22772281
#end for
22782282

2283+
# sort complaints by their creation time and hash
2284+
complaints = dict(sorted(complaints.items(), key=lambda item: (item[1]["createdTime"], item[0])))
2285+
22792286
# Set buffer
22802287
self.SetFunctionBuffer(bname, complaints)
22812288

@@ -2707,6 +2714,43 @@ def GetDbSize(self, exceptions="log"):
27072714
return result
27082715
#end define
27092716

2717+
def check_adnl(self):
2718+
telemetry = self.local.db.get("sendTelemetry", False)
2719+
check_adnl = self.local.db.get("checkAdnl", telemetry)
2720+
if not check_adnl:
2721+
return
2722+
url = 'http://45.129.96.53/adnl_check'
2723+
try:
2724+
data = self.get_local_adnl_data()
2725+
response = requests.post(url, json=data, timeout=5).json()
2726+
except Exception as e:
2727+
self.local.add_log(f'Failed to check adnl connection: {type(e)}: {e}', 'error')
2728+
return False
2729+
result = response.get("ok")
2730+
if not result:
2731+
self.local.add_log(f'Failed to check adnl connection to local node: {response.get("message")}', 'error')
2732+
return result
2733+
#end define
2734+
2735+
def get_local_adnl_data(self):
2736+
2737+
def int2ip(dec):
2738+
import socket
2739+
return socket.inet_ntoa(struct.pack("!i", dec))
2740+
2741+
vconfig = self.GetValidatorConfig()
2742+
2743+
data = {"host": int2ip(vconfig["addrs"][0]["ip"]), "port": vconfig["addrs"][0]["port"]}
2744+
2745+
dht_id = vconfig["dht"][0]["id"]
2746+
dht_id_hex = base64.b64decode(dht_id).hex().upper()
2747+
2748+
result = self.validatorConsole.Run(f"exportpub {dht_id_hex}")
2749+
pubkey = parse(result, "got public key: ", "\n")
2750+
data["pubkey"] = base64.b64encode(base64.b64decode(pubkey)[4:]).decode()
2751+
return data
2752+
#end define
2753+
27102754
def Result2List(self, text):
27112755
buff = parse(text, "result:", "\n")
27122756
if buff is None or "error" in buff:
@@ -3220,9 +3264,20 @@ def get_modes(self):
32203264
current_modes[name] = mode.default_value # assign default mode value
32213265
return current_modes
32223266

3267+
def check_enable_mode(self, name):
3268+
if name == 'liteserver':
3269+
if self.using_validator():
3270+
raise Exception(f'Cannot enable liteserver mode while validator mode is enabled. '
3271+
f'Use `disable_mode validator` first.')
3272+
if name == 'validator':
3273+
if self.using_liteserver():
3274+
raise Exception(f'Cannot enable validator mode while liteserver mode is enabled. '
3275+
f'Use `disable_mode liteserver` first.')
3276+
32233277
def enable_mode(self, name):
32243278
if name not in MODES:
32253279
raise Exception(f'Unknown module name: {name}. Available modes: {", ".join(MODES)}')
3280+
self.check_enable_mode(name)
32263281
current_modes = self.get_modes()
32273282
current_modes[name] = True
32283283
self.local.save()
@@ -3255,6 +3310,9 @@ def using_pool(self) -> bool:
32553310
def using_validator(self):
32563311
return self.get_mode_value('validator')
32573312

3313+
def using_liteserver(self):
3314+
return self.get_mode_value('liteserver')
3315+
32583316
def Tlb2Json(self, text):
32593317
# Заменить скобки
32603318
start = 0

mytonctrl.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
#
66
import os
77
import sys
8+
import subprocess
9+
10+
requirements_path = "/usr/src/mytonctrl/requirements.txt"
11+
if os.path.isfile(requirements_path):
12+
args = ["pip3", "install", "-r", requirements_path]
13+
subprocess.run(args)
14+
#end if
815

916
sys.path.insert(0, '/usr/bin/mytonctrl') # Add path to mytonctrl module
1017

0 commit comments

Comments
 (0)