Skip to content

Commit 74147cb

Browse files
committed
add ton_storage_provider
1 parent b4ba97b commit 74147cb

File tree

11 files changed

+491
-62
lines changed

11 files changed

+491
-62
lines changed

docs/ru/ton-storage-provider.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# TON storage provider
2+
3+
MyTonCtrl поддерживает установку дополнительных компонентов таких как `TON storage` и `TON storage provider`. С их помощью вы можете хранить пользовательские файлы у себя на сервере и получать за это вознаграждение.
4+
5+
## Установка дополнительных компонентов
6+
7+
Войдите в MyTonCtrl
8+
```
9+
mytonctrl
10+
```
11+
12+
Затем войдите в режим установщика
13+
```
14+
installer
15+
```
16+
17+
После включите нужный функционал TON storage provider:
18+
```
19+
enable TSP
20+
```
21+
22+
## Активация контракта провайдера
23+
24+
В ходе установки дополнительных компонетов был создан специальный кошелек провайдера `provider_wallet_001` откуда будут отправляться доказательства и куда будет приходить награда.
25+
Для его работы сначала нужно пополнить этот кошелек на 1 монету и активировать его командой внутри MyTonCtrl, в ходе которого он так же зарегистрируется в списке провайдеров:
26+
```
27+
activate_ton_storage_provider
28+
```
29+
30+
## Панель управления TON storage provider
31+
32+
TODO

mytoncore/functions.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import psutil
66
import time
77
import json
8+
import base64
89
import requests
910
import subprocess
1011

1112
from mytoncore.mytoncore import MyTonCore
1213
from mytoncore.utils import parse_db_stats
14+
from mytoninstaller.config import GetConfig
1315
from mypylib.mypylib import (
1416
b2mb,
1517
get_timestamp,
@@ -26,8 +28,8 @@ def Init(local):
2628
# Event reaction
2729
if ("-e" in sys.argv):
2830
x = sys.argv.index("-e")
29-
eventName = sys.argv[x+1]
30-
Event(local, eventName)
31+
event_name = sys.argv[x+1]
32+
Event(local, event_name)
3133
# end if
3234

3335
local.run()
@@ -46,11 +48,13 @@ def Init(local):
4648
# end define
4749

4850

49-
def Event(local, eventName):
50-
if eventName == "enableVC":
51+
def Event(local, event_name):
52+
if event_name == "enableVC":
5153
EnableVcEvent(local)
52-
elif eventName == "validator down":
54+
elif event_name == "validator down":
5355
ValidatorDownEvent(local)
56+
elif event_name == "enable_ton_storage_provider":
57+
enable_ton_storage_provider_event(local)
5458
local.exit()
5559
# end define
5660

@@ -78,6 +82,15 @@ def ValidatorDownEvent(local):
7882
# end define
7983

8084

85+
def enable_ton_storage_provider_event(local):
86+
config_path = local.db.ton_storage.provider.config_path
87+
config = GetConfig(path=config_path)
88+
key_bytes = base64.b64decode(config.ProviderKey)
89+
ton = MyTonCore(local)
90+
ton.import_wallet_with_version(key_bytes[:32], version="v3r2", wallet_name="provider_wallet_001")
91+
#end define
92+
93+
8194
def Elections(local, ton):
8295
use_pool = ton.using_pool()
8396
use_liquid_staking = ton.using_liquid_staking()

mytoncore/mytoncore.py

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,17 +1656,11 @@ def CreateWallet(self, name, workchain=0, version="v1", **kwargs):
16561656
if os.path.isfile(walletPath + ".pk") and "v3" not in version:
16571657
self.local.add_log("CreateWallet error: Wallet already exists: " + name, "warning")
16581658
else:
1659-
if "v1" in version:
1660-
fiftScript = "new-wallet.fif"
1661-
args = [fiftScript, workchain, walletPath]
1662-
if "v2" in version:
1663-
fiftScript = "new-wallet-v2.fif"
1664-
args = [fiftScript, workchain, walletPath]
1665-
if "v3" in version:
1666-
fiftScript = "new-wallet-v3.fif"
1667-
args = [fiftScript, workchain, subwallet, walletPath]
1668-
result = self.fift.Run(args)
1659+
fift_args = self.get_new_wallet_fift_args(version, workchain=workchain,
1660+
wallet_path=wallet_path, subwallet=subwallet)
1661+
result = self.fift.Run(fift_args)
16691662
if "Creating new" not in result:
1663+
print(result)
16701664
raise Exception("CreateWallet error")
16711665
#end if
16721666
wallet = self.GetLocalWallet(name, version)
@@ -1717,6 +1711,49 @@ def ImportWallet(self, addr_b64, key):
17171711
return wallet_name
17181712
#end define
17191713

1714+
def import_wallet_with_version(self, key, version, **kwargs):
1715+
wallet_name = kwargs.get("wallet_name")
1716+
workchain = kwargs.get("workchain", 0)
1717+
subwallet_default = 698983191 + workchain # 0x29A9A317 + workchain
1718+
subwallet = kwargs.get("subwallet", subwallet_default)
1719+
if type(key) == bytes:
1720+
pk_bytes = key
1721+
else:
1722+
pk_bytes = base64.b64decode(key)
1723+
if wallet_name == None:
1724+
wallet_name = self.GenerateWalletName()
1725+
wallet_path = self.walletsDir + wallet_name
1726+
with open(wallet_path + ".pk", 'wb') as file:
1727+
file.write(pk_bytes)
1728+
fift_args = self.get_new_wallet_fift_args(version, workchain=workchain,
1729+
wallet_path=wallet_path, subwallet=subwallet)
1730+
result = self.fift.Run(fift_args)
1731+
if "Creating new" not in result:
1732+
print(result)
1733+
raise Exception("import_wallet_with_version error")
1734+
wallet = self.GetLocalWallet(wallet_name, version)
1735+
self.SetWalletVersion(wallet.addrB64, version)
1736+
return wallet
1737+
#end define
1738+
1739+
def get_new_wallet_fift_args(self, version, **kwargs):
1740+
workchain = kwargs.get("workchain")
1741+
wallet_path = kwargs.get("wallet_path")
1742+
subwallet = kwargs.get("subwallet")
1743+
if "v1" in version:
1744+
fift_script = "new-wallet.fif"
1745+
args = [fift_script, workchain, wallet_path]
1746+
elif "v2" in version:
1747+
fift_script = "new-wallet-v2.fif"
1748+
args = [fift_script, workchain, wallet_path]
1749+
elif "v3" in version:
1750+
fift_script = "new-wallet-v3.fif"
1751+
args = [fift_script, workchain, subwallet, wallet_path]
1752+
else:
1753+
raise Exception(f"get_wallet_fift error: fift script for `{version}` not found")
1754+
return args
1755+
#end define
1756+
17201757
def addr_b64_to_bytes(self, addr_b64):
17211758
workchain, addr, bounceable = self.ParseAddrB64(addr_b64)
17221759
workchain_bytes = int.to_bytes(workchain, 4, "big", signed=True)

mytonctrl/mytonctrl.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def inject_globals(func):
147147

148148
console.AddItem("cleanup", inject_globals(cleanup_validator_db), local.translate("cleanup_cmd"))
149149
console.AddItem("benchmark", inject_globals(run_benchmark), local.translate("benchmark_cmd"))
150+
console.AddItem("activate_ton_storage_provider", inject_globals(activate_ton_storage_provider), local.translate("activate_ton_storage_provider_cmd"))
150151

151152
# Process input parameters
152153
opts, args = getopt.getopt(argv,"hc:w:",["config=","wallets="])
@@ -176,6 +177,24 @@ def inject_globals(func):
176177
local.db.config.logLevel = "debug" if console.debug else "info"
177178
local.db.config.isLocaldbSaving = False
178179
local.run()
180+
#end define
181+
182+
183+
def activate_ton_storage_provider(local, ton, args):
184+
wallet_name = "provider_wallet_001"
185+
wallet = ton.GetLocalWallet(wallet_name)
186+
account = ton.GetAccount(wallet.addrB64)
187+
if account.status == "active":
188+
color_print("activate_ton_storage_provider - {green}Already activated{endc}")
189+
#return
190+
ton.ActivateWallet(wallet)
191+
destination = "0:7777777777777777777777777777777777777777777777777777777777777777"
192+
ton_storage = ton.GetSettings("ton_storage")
193+
comment = f"tsp-{ton_storage.provider.pubkey}"
194+
flags = ["-n", "-C", comment]
195+
ton.MoveCoins(wallet, destination, 0.01, flags=flags)
196+
color_print("activate_ton_storage_provider - {green}OK{endc}")
197+
#end define
179198

180199

181200
def check_installer_user():

mytoninstaller/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def SetConfig(**kwargs):
3434
#end define
3535

3636

37+
def backup_config(local, config_path):
38+
backup_path = f"{config_path}.backup"
39+
local.add_log(f"Backup config file '{config_path}' to '{backup_path}'", "debug")
40+
args = ["cp", config_path, backup_path]
41+
subprocess.run(args)
42+
#end define
43+
44+
3745
def BackupVconfig(local):
3846
local.add_log("Backup validator config file 'config.json' to 'config.json.backup'", "debug")
3947
vconfig_path = local.buffer.vconfig_path

mytoninstaller/mytoninstaller.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
EnableTonHttpApi,
2626
DangerousRecoveryValidatorConfigFile,
2727
CreateSymlinks,
28-
enable_ls_proxy
28+
enable_ls_proxy,
29+
enable_ton_storage,
30+
enable_ton_storage_provider
2931
)
3032
from mytoninstaller.config import (
3133
CreateLocalConfig,
@@ -172,6 +174,7 @@ def Enable(local, args):
172174
print("'JR' - jsonrpc")
173175
print("'THA' - ton-http-api")
174176
print("'LSP' - ls-proxy")
177+
print("'TSP' - ton-storage + ton-storage-provider")
175178
print("Example: 'enable FN'")
176179
return
177180
if name == "THA":
@@ -236,6 +239,9 @@ def Event(local, name):
236239
EnableTonHttpApi(local)
237240
if name == "enableLSP":
238241
enable_ls_proxy(local)
242+
if name == "enableTSP":
243+
enable_ton_storage(local)
244+
enable_ton_storage_provider(local)
239245
if name == "clc":
240246
ix = sys.argv.index("-i")
241247
initBlock_b64 = sys.argv[ix+1]
Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/bin/bash
22
set -e
33

4+
# import functions: check_superuser, get_cpu_number, check_go_version
5+
my_dir=$(dirname $(realpath ${0}))
6+
. ${my_dir}/utils.sh
7+
48
# Проверить sudo
5-
if [ "$(id -u)" != "0" ]; then
6-
echo "Please run script as root"
7-
exit 1
8-
fi
9+
check_superuser
910

1011
# install parameters
1112
src_path=/usr/src
@@ -40,64 +41,46 @@ ENDC='\033[0m'
4041
echo -e "${COLOR}[1/4]${ENDC} Cloning github repository"
4142
echo "https://github.com/${author}/${repo}.git -> ${branch}"
4243

44+
45+
package_src_path=${src_path}/${repo}
46+
rm -rf ${package_src_path}
47+
4348
cd ${src_path}
44-
rm -rf ${repo}
4549
git clone --branch=${branch} --recursive https://github.com/${author}/${repo}.git
4650

4751
# Установка компонентов
4852
echo -e "${COLOR}[2/4]${ENDC} Installing required packages"
49-
50-
arc=$(dpkg --print-architecture)
51-
go_version_url=https://go.dev/VERSION?m=text
52-
go_version=$(curl -s ${go_version_url} | head -n 1)
53-
go_url=https://go.dev/dl/${go_version}.linux-${arc}.tar.gz
5453
go_path=/usr/local/go/bin/go
55-
rm -rf /usr/local/go
56-
wget -c ${go_url} -O - | tar -C /usr/local -xz
57-
58-
# Расчитываем количество процессоров для сборки
59-
if [[ "$OSTYPE" =~ darwin.* ]]; then
60-
cpu_number=$(sysctl -n hw.logicalcpu)
61-
else
62-
memory=$(cat /proc/meminfo | grep MemAvailable | awk '{print $2}')
63-
cpu_number=$(($memory/2100000))
64-
max_cpu_number=$(nproc)
65-
if [ ${cpu_number} -gt ${max_cpu_number} ]; then
66-
cpu_number=$((${max_cpu_number}-1))
67-
fi
68-
if [ ${cpu_number} == 0 ]; then
69-
echo "Warning! insufficient RAM"
70-
cpu_number=1
71-
fi
72-
fi
54+
check_go_version "${package_src_path}/go.mod" ${go_path}
7355

7456
# Компилируем из исходников
57+
cpu_number=$(get_cpu_number)
7558
echo -e "${COLOR}[3/4]${ENDC} Source compilation, use ${cpu_number} cpus"
7659

77-
proxy_src_path=${src_path}/${repo}
78-
ton_src_path=${proxy_src_path}/ton
79-
proxy_internal_path=${proxy_src_path}/internal/emulate/lib
60+
ton_src_path=${package_src_path}/ton
61+
proxy_internal_path=${package_src_path}/internal/emulate/lib
8062

8163
proxy_build_path=${bin_path}/${bin_name}
8264
ton_build_path=${proxy_build_path}/ton
83-
proxy_db_path=/var/${bin_name}
84-
proxy_lib_path=${proxy_db_path}/lib
65+
db_path=/var/${bin_name}
66+
lib_path=${db_path}/lib
8567

86-
mkdir -p ${proxy_lib_path}
68+
mkdir -p ${lib_path}
8769
mkdir -p ${ton_build_path} && cd ${ton_build_path}
8870
cmake -DCMAKE_BUILD_TYPE=Release -DOPENSSL_FOUND=1 -DOPENSSL_INCLUDE_DIR=${openssl_path}/include -DOPENSSL_CRYPTO_LIBRARY=${openssl_path}/libcrypto.a ${ton_src_path}
8971
make emulator -j ${cpu_number}
90-
cp ${ton_build_path}/emulator/libemulator.so ${proxy_lib_path}
72+
cp ${ton_build_path}/emulator/libemulator.so ${lib_path}
9173
cp ${ton_build_path}/emulator/libemulator.so ${proxy_internal_path}
9274
cp ${ton_build_path}/emulator/emulator_export.h ${proxy_internal_path}
9375

9476
# Компилируем
95-
cd ${proxy_src_path}
96-
CGO_ENABLED=1 ${go_path} build -o ${proxy_db_path}/${bin_name} ${proxy_src_path}/cmd/main.go
77+
cd ${package_src_path}
78+
entry_point=$(find ${package_src_path} -name "main.go" | head -n 1)
79+
CGO_ENABLED=1 ${go_path} build -o ${db_path}/${bin_name} ${entry_point}
9780

9881
# Настроить директорию работы
99-
chown -R ${user}:${user} ${proxy_db_path}
82+
chown -R ${user}:${user} ${db_path}
10083

10184
# Выход из программы
10285
echo -e "${COLOR}[4/4]${ENDC} ${bin_name} installation complete"
103-
exit 0
86+
exit 0

0 commit comments

Comments
 (0)