Skip to content

Commit f483fc3

Browse files
authored
Merge pull request #421 from ton-blockchain/node_args
Node args
2 parents 50e79f3 + d153e6f commit f483fc3

File tree

6 files changed

+63
-40
lines changed

6 files changed

+63
-40
lines changed

mytonctrl/mytonctrl.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,6 @@ def Upgrade(ton, args):
370370
upgrade_script_path = pkg_resources.resource_filename('mytonctrl', 'scripts/upgrade.sh')
371371
runArgs = ["bash", upgrade_script_path, "-a", author, "-r", repo, "-b", branch]
372372
exitCode = run_as_root(runArgs)
373-
if ton.using_validator():
374-
try:
375-
from mytoninstaller.mytoninstaller import set_node_argument, get_node_args
376-
node_args = get_node_args()
377-
if node_args.get('--state-ttl') == '604800':
378-
set_node_argument(ton.local, ["--state-ttl", "-d"])
379-
except Exception as e:
380-
color_print(f"{{red}}Failed to set node argument: {e} {{endc}}")
381373
if exitCode == 0:
382374
text = "Upgrade - {green}OK{endc}"
383375
else:

mytoninstaller/mytoninstaller.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,18 @@ def Status(local, args):
132132
node_args = get_node_args()
133133
color_print("{cyan}===[ Node arguments ]==={endc}")
134134
for key, value in node_args.items():
135-
print(f"{key}: {value}")
135+
for v in value:
136+
print(f"{key}: {v}")
136137
#end define
137138

138139

139140
def set_node_argument(local, args):
140141
if len(args) < 1:
141-
color_print("{red}Bad args. Usage:{endc} set_node_argument <arg-name> [arg-value] [-d (to delete)]")
142+
color_print("{red}Bad args. Usage:{endc} set_node_argument <arg-name> [arg-value] [-d (to delete)].\n"
143+
"Examples: 'set_node_argument --archive-ttl 86400' or 'set_node_argument --archive-ttl -d' or 'set_node_argument -M' or 'set_node_argument --add-shard 0:2000000000000000 0:a000000000000000'")
142144
return
143145
arg_name = args[0]
144-
args = [arg_name, args[1] if len(args) > 1 else ""]
146+
args = [arg_name, " ".join(args[1:])]
145147
script_path = pkg_resources.resource_filename('mytoninstaller.scripts', 'set_node_argument.py')
146148
run_as_root(['python3', script_path] + args)
147149
color_print("set_node_argument - {green}OK{endc}")

mytoninstaller/node_args.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
def get_validator_service():
44
path = '/etc/systemd/system/validator.service'
5-
with open(path, 'r') as f:
6-
return f.read()
5+
with open(path, 'r') as file:
6+
return file.read()
77
#end define
88

99

@@ -14,22 +14,19 @@ def get_node_start_command():
1414
return line.split('=')[1].strip()
1515
#end define
1616

17+
def get_node_args(start_command: str = None):
18+
if start_command is None:
19+
start_command = get_node_start_command()
20+
#end if
1721

18-
def get_node_args(command: str = None):
19-
if command is None:
20-
command = get_node_start_command()
21-
result = {}
22-
key = ''
23-
for c in command.split(' ')[1:]:
24-
if c.startswith('--') or c.startswith('-'):
25-
if key:
26-
result[key] = ''
27-
key = c
28-
elif key:
29-
result[key] = c
30-
key = ''
31-
if key:
32-
result[key] = ''
22+
result = dict() # {key: [value1, value2]}
23+
node_args = start_command.split(' ')[1:]
24+
key = None
25+
for item in node_args:
26+
if item.startswith('-'):
27+
key = item
28+
result[key] = list()
29+
else:
30+
result[key].append(item)
3331
return result
3432
#end define
35-

mytoninstaller/scripts/set_node_argument.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,35 @@ def set_node_arg(arg_name: str, arg_value: str = ''):
1111
"""
1212
assert arg_name.startswith('-'), 'arg_name must start with "-" or "--"'
1313
service = get_validator_service()
14-
command = get_node_start_command()
15-
if command.split(' ')[0] != '/usr/bin/ton/validator-engine/validator-engine':
16-
raise Exception('Invalid node start command in service file')
17-
if command is None:
14+
start_command = get_node_start_command()
15+
if start_command is None:
1816
raise Exception('Cannot find node start command in service file')
19-
args = get_node_args(command)
17+
first_arg = start_command.split(' ')[0]
18+
if first_arg != '/usr/bin/ton/validator-engine/validator-engine':
19+
raise Exception('Invalid node start command in service file')
20+
#end if
21+
22+
node_args = get_node_args(start_command)
2023
if arg_value == '-d':
21-
args.pop(arg_name, None)
24+
node_args.pop(arg_name, None)
2225
else:
23-
args[arg_name] = arg_value
24-
new_command = command.split(' ')[0] + ' ' + ' '.join([f'{k} {v}' for k, v in args.items()])
25-
new_service = service.replace(command, new_command)
26-
with open('/etc/systemd/system/validator.service', 'w') as f:
27-
f.write(new_service)
26+
if ' ' in arg_value:
27+
node_args[arg_name] = arg_value.split()
28+
else:
29+
node_args[arg_name] = [arg_value]
30+
#end if
31+
32+
buffer = list()
33+
buffer.append(first_arg)
34+
for key, value_list in node_args.items():
35+
if len(value_list) == 0:
36+
buffer.append(f"{key}")
37+
for value in value_list:
38+
buffer.append(f"{key} {value}")
39+
new_start_command = ' '.join(buffer)
40+
new_service = service.replace(start_command, new_start_command)
41+
with open('/etc/systemd/system/validator.service', 'w') as file:
42+
file.write(new_service)
2843
restart_node()
2944
#end define
3045

mytoninstaller/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def FirstNodeSettings(local):
6666
# Прописать автозагрузку
6767
cpus = psutil.cpu_count() - 1
6868
cmd = f"{validatorAppPath} --threads {cpus} --daemonize --global-config {globalConfigPath} --db {ton_db_dir} --logname {tonLogPath} --archive-ttl {archive_ttl} --verbosity 1"
69+
70+
if os.getenv('ADD_SHARD'):
71+
add_shard = os.getenv('ADD_SHARD')
72+
cmd += f' -M'
73+
for shard in add_shard.split():
74+
cmd += f' --add-shard {shard}'
75+
6976
add2systemd(name="validator", user=vuser, start=cmd) # post="/usr/bin/python3 /usr/src/mytonctrl/mytoncore.py -e \"validator down\""
7077

7178
# Получить внешний ip адрес

scripts/install.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def run_cli():
3838
"dump",
3939
message="Do you want to download blockchain's dump? "
4040
"This reduces synchronization time but requires to download a large file",
41+
),
42+
inquirer.Text(
43+
"add-shard",
44+
message="Set shards node will sync. Skip to sync all shards. "
45+
"Format: <workchain>:<shard>. Divide multiple shards with space. "
46+
"Example: `0:2000000000000000 0:6000000000000000`",
47+
validate=lambda _, x: not x or all([":" in i for i in x.split()])
4148
)
4249
]
4350

@@ -51,6 +58,7 @@ def parse_args(answers: dict):
5158
network = answers["network"].lower()
5259
config = answers["config"]
5360
archive_ttl = answers["archive-ttl"]
61+
add_shard = answers["add-shard"]
5462
validator_mode = answers["validator-mode"]
5563
dump = answers["dump"]
5664

@@ -61,6 +69,8 @@ def parse_args(answers: dict):
6169

6270
if archive_ttl:
6371
os.putenv('ARCHIVE_TTL', archive_ttl) # set env variable
72+
if add_shard:
73+
os.putenv('ADD_SHARD', add_shard)
6474

6575
if validator_mode and validator_mode not in ('Skip', 'Validator wallet'):
6676
if validator_mode == 'Nominator pool':

0 commit comments

Comments
 (0)