11import os
22import os .path
3+ import time
4+
35import psutil
46import base64
57import subprocess
1618 ip2int ,
1719 Dict , int2ip
1820)
19- from mytoninstaller .utils import StartValidator , StartMytoncore , start_service , stop_service , get_ed25519_pubkey
21+ from mytoninstaller .utils import StartValidator , StartMytoncore , start_service , stop_service , get_ed25519_pubkey , \
22+ disable_service
2023from mytoninstaller .config import SetConfig , GetConfig , get_own_ip , backup_config
2124from mytoncore .utils import hex2b64
2225
@@ -86,8 +89,9 @@ def FirstNodeSettings(local):
8689 args = [validatorAppPath , "--global-config" , globalConfigPath , "--db" , ton_db_dir , "--ip" , addr , "--logname" , tonLogPath ]
8790 subprocess .run (args )
8891
89- # Скачать дамп
90- DownloadDump (local )
92+ # Download dumps from TON Storage
93+ download_archive_from_ts (local )
94+ # DownloadDump(local)
9195
9296 # chown 1
9397 local .add_log ("Chown ton-work dir" , "debug" )
@@ -106,6 +110,89 @@ def is_testnet(local):
106110 return True
107111 return False
108112
113+ def download_bag (local , bag_id : str ):
114+ local_ts_url = f"http://127.0.0.1:{ local .buffer .ton_storage .api_port } "
115+ downloads_path = '/tmp/ts-downloads/'
116+
117+ resp = requests .post (local_ts_url + '/api/v1/add' , json = {'bag_id' : bag_id , 'download_all' : True , 'path' : downloads_path })
118+ if not resp .json ()['ok' ]:
119+ local .add_log ("Error adding bag: " + resp .json (), "error" )
120+ return False
121+ resp = requests .get (local_ts_url + f'/api/v1/details?bag_id={ bag_id } ' ).json ()
122+ while not resp ['completed' ]:
123+ if resp ['size' ] == 0 :
124+ local .add_log (f"STARTING DOWNLOADING { bag_id } " , "debug" )
125+ time .sleep (20 )
126+ resp = requests .get (local_ts_url + f'/api/v1/details?bag_id={ bag_id } ' ).json ()
127+ continue
128+ text = f'DOWNLOADING { bag_id } { round ((resp ["downloaded" ] / resp ["size" ]) * 100 )} % ({ resp ["downloaded" ] / 10 ** 6 } / { resp ["size" ] / 10 ** 6 } MB), speed: { resp ["download_speed" ] / 10 ** 6 } MB/s'
129+ local .add_log (text , "debug" )
130+ time .sleep (20 )
131+ resp = requests .get (local_ts_url + f'/api/v1/details?bag_id={ bag_id } ' ).json ()
132+ local .add_log (f"DOWNLOADED { bag_id } " , "debug" )
133+ return True
134+
135+
136+ def download_archive_from_ts (local ):
137+ archive_block = os .getenv ('ARCHIVE_BLOCKS' )
138+ dump = local .buffer .dump
139+ if not archive_block and not dump :
140+ return
141+
142+ enable_ton_storage (local )
143+ url = 'https://archival-dump.ton.org/index/mainnet.json'
144+ if is_testnet (local ):
145+ url = 'https://archival-dump.ton.org/index/testnet.json'
146+
147+ state_bag = {}
148+ block_bags = []
149+
150+ blocks_config = requests .get (url ).json ()
151+ if dump :
152+ state_bag = blocks_config ['states' ][- 1 ]
153+ else :
154+ for block in blocks_config ['blocks' ]:
155+ if block ['to' ] >= archive_block :
156+ block_bags .append (block )
157+ for state in blocks_config ['states' ]:
158+ state_bag = state
159+ if state ['at_block' ] > archive_block :
160+ break
161+ if not state_bag or not block_bags :
162+ local .add_log ("Skip downloading archive blocks: No bags found for the specified block" , "error" )
163+ return
164+
165+ local .add_log (f"Downloading blockchain state for block { state_bag ['at_block' ]} " , "info" )
166+ if not download_bag (local , state_bag ['bag' ]):
167+ local .add_log ("Error downloading state bag" , "error" )
168+ return
169+ local .add_log ("Downloading archive blocks" , "info" )
170+ for bag in block_bags :
171+ local .add_log (f"Downloading blocks from { bag ['from' ]} to { bag ['to' ]} " , "info" )
172+ if not download_bag (local , bag ['bag' ]):
173+ local .add_log ("Error downloading archive bag" , "error" )
174+ return
175+ local .add_log (f"Downloading blocks is completed" , "info" )
176+
177+ states_dir = local .buffer .ton_db_dir + 'archive/states'
178+ blocks_dir = local .buffer .ton_db_dir + 'archive/packages'
179+ downloads_path = '/tmp/ts-downloads/'
180+
181+ os .makedirs (states_dir , exist_ok = True )
182+ os .makedirs (blocks_dir , exist_ok = True )
183+
184+ subprocess .run (f'mv { downloads_path } /{ state_bag ["bag" ]} /state-*/* { states_dir } ' , shell = True )
185+ # subprocess.run(['rm', '-rf', f"{downloads_path}/{state_bag['bag']}"])
186+
187+ for bag in block_bags :
188+ subprocess .run (f'mv { downloads_path } /{ bag ["bag" ]} /packages/* { blocks_dir } ' , shell = True )
189+ # subprocess.run(['rm', '-rf', f"{downloads_path}/{bag['bag']}"])
190+ subprocess .run (['rm' , '-rf' , downloads_path ])
191+
192+ stop_service (local , "ton_storage" ) # stop TS
193+ disable_service (local , "ton_storage" )
194+
195+
109196def DownloadDump (local ):
110197 dump = local .buffer .dump
111198 if dump is False :
@@ -636,6 +723,8 @@ def enable_ton_storage(local):
636723 local .add_log ("write mconfig" , "debug" )
637724 SetConfig (path = mconfig_path , data = mconfig )
638725
726+ local .buffer .ton_storage = ton_storage
727+
639728 # start ton_storage
640729 start_service (local , bin_name )
641730 color_print ("enable_ton_storage - {green}OK{endc}" )
0 commit comments