Skip to content

Commit 69c6331

Browse files
committed
add state ttl setting to cli installer
1 parent 0ddc4e2 commit 69c6331

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

mytoninstaller/settings.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def FirstNodeSettings(local):
4949
archive_ttl = int(os.getenv('ARCHIVE_TTL'))
5050
else:
5151
archive_ttl = 2592000 if local.buffer.mode == 'liteserver' else 86400
52+
state_ttl = None
53+
if os.getenv('STATE_TTL'):
54+
state_ttl = int(os.getenv('STATE_TTL'))
55+
archive_ttl -= state_ttl
5256

5357
# Проверить конфигурацию
5458
if os.path.isfile(vconfig_path):
@@ -74,12 +78,15 @@ def FirstNodeSettings(local):
7478
cpus = psutil.cpu_count() - 1
7579

7680
ttl_cmd = ''
77-
if archive_ttl == -1 and local.buffer.mode == 'liteserver':
81+
if archive_ttl == -1:
7882
archive_ttl = 10**9
79-
ttl_cmd = f' --state-ttl {10**9} --permanent-celldb'
83+
state_ttl = 10**9
84+
ttl_cmd += f' --permanent-celldb'
85+
if state_ttl is not None:
86+
ttl_cmd += f' --state-ttl {state_ttl}'
87+
ttl_cmd += f' --archive-ttl {archive_ttl}'
8088

81-
82-
cmd = f"{validatorAppPath} --threads {cpus} --daemonize --global-config {globalConfigPath} --db {ton_db_dir} --logname {tonLogPath} --archive-ttl {archive_ttl} --verbosity 1"
89+
cmd = f"{validatorAppPath} --threads {cpus} --daemonize --global-config {globalConfigPath} --db {ton_db_dir} --logname {tonLogPath} --verbosity 1"
8390
cmd += ttl_cmd
8491

8592
if os.getenv('ADD_SHARD'):
@@ -189,12 +196,6 @@ def parse_block_value(local, block: str):
189196
return int(data['seqno'])
190197

191198

192-
def process_hardforks(local):
193-
194-
pass
195-
196-
197-
198199
def download_archive_from_ts(local):
199200
archive_blocks = os.getenv('ARCHIVE_BLOCKS')
200201
if archive_blocks is None:
@@ -203,7 +204,7 @@ def download_archive_from_ts(local):
203204
if len(archive_blocks.split()) > 1:
204205
block_from, block_to = archive_blocks.split()
205206
block_from, block_to = parse_block_value(local, block_from), parse_block_value(local, block_to)
206-
block_from = max(0, block_from - 100) # to download previous package as node may require some blocks from it
207+
block_from = max(1, block_from - 100) # to download previous package as node may require some blocks from it
207208

208209
enable_ton_storage(local)
209210
url = 'https://archival-dump.ton.org/index/mainnet.json'
@@ -244,7 +245,7 @@ def download_archive_from_ts(local):
244245
update_init_block(local, state_bag['at_block'])
245246
estimated_size = len(block_bags) * 4 * 2**30 + len(master_block_bags) * 4 * 2**30 * 0.2 # 4 GB per bag, 20% for master blocks
246247

247-
local.add_log(f"Downloading archive blocks. Rough estimate total blocks size is {estimated_size / 2**30} GB", "info")
248+
local.add_log(f"Downloading archive blocks. Rough estimate total blocks size is {int(estimated_size / 2**30)} GB", "info")
248249
with ThreadPoolExecutor(max_workers=4) as executor:
249250
futures = [executor.submit(download_blocks, local, bag) for bag in block_bags]
250251
futures += [executor.submit(download_master_blocks, local, bag) for bag in master_block_bags]
@@ -255,7 +256,7 @@ def download_archive_from_ts(local):
255256
local.add_log(f"Error while downloading blocks: {e}", "error")
256257
return
257258

258-
local.add_log(f"Downloading blocks is completed", "info")
259+
local.add_log(f"Downloading blocks is completed, moving files", "info")
259260

260261
archive_dir = local.buffer.ton_db_dir + 'archive/'
261262
import_dir = local.buffer.ton_db_dir + 'import/'

scripts/install.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def get_archive_ttl_message(answers: dict):
1010
archive_blocks = answers.get('archive-blocks')
1111
if not archive_blocks and answers['archive-blocks'] != 0:
12-
return 'Send the number of seconds to keep the block data in the node database. Default is 2592000 (30 days)'
12+
return 'Send the number of seconds to keep the blocks data in the node database. Press Enter to use default: 2592000 (30 days)'
1313
block_from = archive_blocks.split()[0]
1414
# or sent -1 to store downloaded blocks always
1515
if block_from.isdigit():
@@ -18,12 +18,14 @@ def get_archive_ttl_message(answers: dict):
1818
if answers['network'] == 'Testnet':
1919
url = url.replace('toncenter.com', 'testnet.toncenter.com')
2020
data = requests.get(url).json()
21+
if not data['ok']:
22+
raise Exception(f'Failed to get block: {data}')
2123
utime = int(data['result']['gen_utime'])
2224
else:
2325
utime = int(datetime.datetime.strptime(block_from, '%Y-%m-%d').timestamp())
2426
default_archive_ttl = int(time.time() - (utime - datetime.timedelta(days=30).total_seconds()))
25-
answers['archive-ttl-default'] = default_archive_ttl
26-
return f'Send the number of seconds to keep the block data in the node database.\nSkip to use default: {default_archive_ttl} to keep blocks from provided date for 30 days ({datetime.datetime.fromtimestamp(utime - datetime.timedelta(days=30).total_seconds())})\nOr send -1 to keep downloaded blocks always (recommended).'
27+
answers['archive-ttl-default'] = '-1'
28+
return f'Send the number of seconds to keep blocks in the node database. Press Enter to keep downloaded blocks always (recommended).\nFor your reference you can use TTL `{default_archive_ttl}` to keep blocks from provided block for 30 days (since {datetime.datetime.fromtimestamp(utime - datetime.timedelta(days=30).total_seconds())}).\nNote: in case you want to keep blocks garbage collection the node will sync 8-10 times slower.'
2729

2830

2931
def is_valid_date_format(date_str):
@@ -63,13 +65,22 @@ def validate_digits_or_empty(value):
6365
return True
6466
try:
6567
int(value)
66-
if int(value) != -1 and int(value) <= 0:
68+
if int(value) <= 0:
6769
return "Input must be a positive number"
6870
return True
6971
except ValueError:
7072
return "Input must be a number"
7173

7274

75+
def validate_state_ttl(value, archive_ttl):
76+
v = validate_digits_or_empty(value)
77+
if v is not True: return v
78+
if archive_ttl and value and int(value) > int(archive_ttl):
79+
return "State TTL cannot be greater than blocks TTL"
80+
return True
81+
82+
83+
7384
def validate_shard_format(value):
7485
if not value:
7586
return True
@@ -108,12 +119,13 @@ def run_cli():
108119
if mode != "validator":
109120
archive_blocks = questionary.text(
110121
"Do you want to download archive blocks via TON Storage? Press Enter to skip.\n"
111-
"If yes, provide block seqno or date to start from and (optionally) block seqno or date to end with (send 0 to download all blocks and setup full archive node).\n"
122+
"If yes, provide block seqno or date to start from and (optionally) block seqno or date to end with (send `1` to download all blocks and setup full archive node).\n"
112123
"Examples: `30850000`, `10000000 10200000`, `2025-01-01`, `2025-01-01 2025-01-30`",
113124
validate=validate_archive_blocks
114125
).unsafe_ask()
115126

116127
archive_ttl = None
128+
state_ttl = None
117129
if mode == "liteserver":
118130
temp_answers = {
119131
'archive-blocks': archive_blocks,
@@ -125,6 +137,11 @@ def run_cli():
125137
).unsafe_ask()
126138
if not archive_ttl and 'archive-ttl-default' in temp_answers:
127139
archive_ttl = temp_answers['archive-ttl-default']
140+
if archive_ttl != '-1':
141+
state_ttl = questionary.text(
142+
'Send the number of seconds to keep blocks states in the node database. Press Enter to use default: 86400 (24 hours)',
143+
validate= lambda x: validate_state_ttl(x, archive_ttl)
144+
).unsafe_ask()
128145

129146
dump = None
130147
if not archive_blocks:
@@ -134,8 +151,8 @@ def run_cli():
134151
).unsafe_ask()
135152

136153
add_shard = questionary.text(
137-
"Set shards node will sync. Skip to sync all shards. "
138-
"Format: <workchain>:<shard>. Divide multiple shards with space. "
154+
"Set shards node will sync. Press Enter to sync all shards.\n"
155+
"Format: <workchain>:<shard>. Divide multiple shards with space.\n"
139156
"Example: `0:2000000000000000 0:6000000000000000`",
140157
validate=validate_shard_format
141158
).unsafe_ask()
@@ -151,6 +168,7 @@ def run_cli():
151168
"validator-mode": validator_mode,
152169
"archive-blocks": archive_blocks,
153170
"archive-ttl": archive_ttl,
171+
'state-ttl': state_ttl,
154172
"dump": dump,
155173
"add-shard": add_shard,
156174
"background": background
@@ -159,11 +177,12 @@ def run_cli():
159177
return answers
160178

161179

162-
def run_install(answers: dict) -> list:
180+
def run_install(answers: dict):
163181
mode = answers["mode"]
164182
network = answers["network"].lower()
165183
config = answers["config"]
166184
archive_ttl = answers["archive-ttl"]
185+
state_ttl = answers["state-ttl"]
167186
add_shard = answers["add-shard"]
168187
validator_mode = answers["validator-mode"]
169188
archive_blocks = answers["archive-blocks"]
@@ -181,6 +200,8 @@ def run_install(answers: dict) -> list:
181200

182201
if archive_ttl:
183202
os.environ['ARCHIVE_TTL'] = archive_ttl # set env variable
203+
if state_ttl:
204+
os.environ['STATE_TTL'] = state_ttl
184205
if add_shard:
185206
os.environ['ADD_SHARD'] = add_shard
186207
if archive_blocks:

0 commit comments

Comments
 (0)