Skip to content

Commit a09d455

Browse files
committed
update downloading blocks
1 parent 6517eb3 commit a09d455

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

mytoninstaller/settings.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import os
23
import os.path
34
import time
@@ -21,7 +22,7 @@
2122
Dict, int2ip
2223
)
2324
from mytoninstaller.utils import StartValidator, StartMytoncore, start_service, stop_service, get_ed25519_pubkey, \
24-
disable_service
25+
disable_service, is_testnet, get_block_from_toncenter
2526
from mytoninstaller.config import SetConfig, GetConfig, get_own_ip, backup_config
2627
from mytoncore.utils import hex2b64
2728

@@ -70,7 +71,15 @@ def FirstNodeSettings(local):
7071

7172
# Прописать автозагрузку
7273
cpus = psutil.cpu_count() - 1
74+
75+
ttl_cmd = ''
76+
if archive_ttl == -1 and local.buffer.mode == 'liteserver':
77+
archive_ttl = 10**9
78+
ttl_cmd = f' --state-ttl {10**9} --permanent-celldb'
79+
80+
7381
cmd = f"{validatorAppPath} --threads {cpus} --daemonize --global-config {globalConfigPath} --db {ton_db_dir} --logname {tonLogPath} --archive-ttl {archive_ttl} --verbosity 1"
82+
cmd += ttl_cmd
7483

7584
if os.getenv('ADD_SHARD'):
7685
add_shard = os.getenv('ADD_SHARD')
@@ -103,14 +112,6 @@ def FirstNodeSettings(local):
103112
StartValidator(local)
104113
#end define
105114

106-
def is_testnet(local):
107-
testnet_zero_state_root_hash = "gj+B8wb/AmlPk1z1AhVI484rhrUpgSr2oSFIh56VoSg="
108-
with open(local.buffer.global_config_path) as f:
109-
config = json.load(f)
110-
if config['validator']['zero_state']['root_hash'] == testnet_zero_state_root_hash:
111-
return True
112-
return False
113-
114115
def download_blocks(local, bag: dict):
115116
local.add_log(f"Downloading blocks from {bag['from']} to {bag['to']}", "info")
116117
if not download_bag(local, bag['bag']):
@@ -143,28 +144,36 @@ def download_bag(local, bag_id: str):
143144

144145
def update_init_block(local, seqno: int):
145146
local.add_log(f"Editing init block in {local.buffer.global_config_path}", "info")
146-
url = f'https://toncenter.com/api/v2/lookupBlock?workchain=-1&shard=-9223372036854775808&seqno={seqno}'
147-
if is_testnet(local):
148-
url = url.replace('toncenter.com', 'testnet.toncenter.com')
149-
resp = requests.get(url).json()
150-
if not resp['ok']:
151-
local.add_log("Error getting init block from toncenter", "error")
152-
return False
147+
data = get_block_from_toncenter(local, workchain=-1, seqno=seqno)
153148
with open(local.buffer.global_config_path, 'r') as f:
154149
config = json.load(f)
155150
config['validator']['init_block']['seqno'] = seqno
156-
config['validator']['init_block']['file_hash'] = resp['result']['file_hash']
157-
config['validator']['init_block']['root_hash'] = resp['result']['root_hash']
151+
config['validator']['init_block']['file_hash'] = data['file_hash']
152+
config['validator']['init_block']['root_hash'] = data['root_hash']
158153
with open(local.buffer.global_config_path, 'w') as f:
159154
f.write(json.dumps(config, indent=4))
160155
return True
161156

162157

158+
def parse_block_value(local, block: str):
159+
if block is None:
160+
return None
161+
if block.isdigit():
162+
return int(block)
163+
dt = datetime.datetime.strptime(block, "%Y-%m-%d")
164+
ts = int(dt.timestamp())
165+
data = get_block_from_toncenter(local, workchain=-1, utime=ts)
166+
return int(data['seqno'])
167+
168+
163169
def download_archive_from_ts(local):
164-
archive_block = os.getenv('ARCHIVE_BLOCKS')
165-
if archive_block is None:
170+
archive_blocks = os.getenv('ARCHIVE_BLOCKS')
171+
if archive_blocks is None:
166172
return
167-
archive_block = int(archive_block)
173+
block_from, block_to = archive_blocks, None
174+
if len(archive_blocks.split()) > 1:
175+
block_from, block_to = archive_blocks.split()
176+
block_from, block_to = parse_block_value(local, block_from), parse_block_value(local, block_to)
168177

169178
enable_ton_storage(local)
170179
url = 'https://archival-dump.ton.org/index/mainnet.json'
@@ -175,13 +184,17 @@ def download_archive_from_ts(local):
175184
block_bags = []
176185

177186
blocks_config = requests.get(url).json()
178-
for block in blocks_config['blocks']:
179-
if block['to'] >= archive_block:
180-
block_bags.append(block)
181187
for state in blocks_config['states']:
182-
if state['at_block'] > archive_block:
188+
if state['at_block'] > block_from:
183189
break
184190
state_bag = state
191+
block_from = state_bag['at_block']
192+
for block in blocks_config['blocks']:
193+
if block['to'] >= block_from:
194+
block_bags.append(block)
195+
if block_to is not None and block['from'] > block_to:
196+
break
197+
185198
if not state_bag or not block_bags:
186199
local.add_log("Skip downloading archive blocks: No bags found for the specified block", "error")
187200
return
@@ -226,6 +239,8 @@ def download_archive_from_ts(local):
226239
from mytoninstaller.mytoninstaller import set_node_argument
227240

228241
set_node_argument(local, ['--skip-key-sync'])
242+
if block_to is not None:
243+
set_node_argument(local, ['--sync-shards-upto', str(block_to)])
229244

230245

231246
def DownloadDump(local):

mytoninstaller/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import json
33
import time
44
import subprocess
5+
6+
import requests
57
from nacl.signing import SigningKey
68

79

@@ -55,3 +57,32 @@ def get_ed25519_pubkey(privkey):
5557
pubkey = privkey_obj.verify_key.encode()
5658
return pubkey
5759
#end define
60+
61+
62+
def is_testnet(local):
63+
testnet_zero_state_root_hash = "gj+B8wb/AmlPk1z1AhVI484rhrUpgSr2oSFIh56VoSg="
64+
with open(local.buffer.global_config_path) as f:
65+
config = json.load(f)
66+
if config['validator']['zero_state']['root_hash'] == testnet_zero_state_root_hash:
67+
return True
68+
return False
69+
70+
71+
def get_block_from_toncenter(local, workchain: int, shard: int = -9223372036854775808, seqno: int = None, utime: int = None):
72+
url = f'https://toncenter.com/api/v2/lookupBlock?workchain={workchain}&shard={shard}'
73+
if is_testnet(local):
74+
url = url.replace('toncenter.com', 'testnet.toncenter.com')
75+
if seqno:
76+
url += f'&seqno={seqno}'
77+
if utime:
78+
url += f'&unixtime={utime}'
79+
local.add_log(f"Requesting block information from {url}", "debug")
80+
resp = requests.get(url)
81+
if resp.status_code != 200:
82+
local.add_log(f"Toncenter API returned status code {resp.status_code}", "error")
83+
raise Exception(f"Toncenter API request failed: {resp.text}")
84+
data = resp.json()
85+
if not data['ok']:
86+
local.add_log(f"Toncenter API returned error: {data.get('error', 'Unknown error')}", "error")
87+
raise Exception(f"Toncenter API returned error: {data.get('error', 'Unknown error')}")
88+
return data['result']

0 commit comments

Comments
 (0)