@@ -118,15 +118,37 @@ def download_blocks(local, bag: dict):
118118 local .add_log ("Error downloading archive bag" , "error" )
119119 return
120120
121- def download_bag (local , bag_id : str ):
121+
122+ def download_master_blocks (local , bag : dict ):
123+ local .add_log (f"Downloading master blocks from { bag ['from' ]} to { bag ['to' ]} " , "info" )
124+ if not download_bag (local , bag ['bag' ], download_all = False ):
125+ local .add_log ("Error downloading master bag" , "error" )
126+ return
127+
128+
129+ def download_bag (local , bag_id : str , download_all : bool = True ):
130+ indexes = []
122131 local_ts_url = f"http://127.0.0.1:{ local .buffer .ton_storage .api_port } "
123132 downloads_path = '/tmp/ts-downloads/'
124133
125- resp = requests .post (local_ts_url + '/api/v1/add' , json = {'bag_id' : bag_id , 'download_all' : True , 'path' : downloads_path })
134+ resp = requests .post (local_ts_url + '/api/v1/add' , json = {'bag_id' : bag_id , 'download_all' : download_all , 'path' : downloads_path })
126135 if not resp .json ()['ok' ]:
127136 local .add_log ("Error adding bag: " + resp .json (), "error" )
128137 return False
129138 resp = requests .get (local_ts_url + f'/api/v1/details?bag_id={ bag_id } ' ).json ()
139+ if not download_all :
140+ while not resp ['header_loaded' ]:
141+ time .sleep (1 )
142+ resp = requests .get (local_ts_url + f'/api/v1/details?bag_id={ bag_id } ' ).json ()
143+ for f in resp ['files' ]:
144+ if ':' not in f ['name' ]: # do not download shardblock packs
145+ indexes .append (f ['index' ])
146+ resp = requests .post (local_ts_url + '/api/v1/add' , json = {'bag_id' : bag_id , 'download_all' : download_all , 'path' : downloads_path , 'files' : indexes })
147+ if not resp .json ()['ok' ]:
148+ local .add_log ("Error adding bag: " + resp .json (), "error" )
149+ return False
150+ time .sleep (5 )
151+ resp = requests .get (local_ts_url + f'/api/v1/details?bag_id={ bag_id } ' ).json ()
130152 while not resp ['completed' ]:
131153 if resp ['size' ] == 0 :
132154 local .add_log (f"STARTING DOWNLOADING { bag_id } " , "debug" )
@@ -174,6 +196,7 @@ def download_archive_from_ts(local):
174196 if len (archive_blocks .split ()) > 1 :
175197 block_from , block_to = archive_blocks .split ()
176198 block_from , block_to = parse_block_value (local , block_from ), parse_block_value (local , block_to )
199+ block_from = max (0 , block_from - 100 ) # to download previous package as node may require some blocks from it
177200
178201 enable_ton_storage (local )
179202 url = 'https://archival-dump.ton.org/index/mainnet.json'
@@ -182,18 +205,25 @@ def download_archive_from_ts(local):
182205
183206 state_bag = {}
184207 block_bags = []
208+ master_block_bags = []
185209
186210 blocks_config = requests .get (url ).json ()
187211 for state in blocks_config ['states' ]:
188212 if state ['at_block' ] > block_from :
189213 break
190214 state_bag = state
191215 block_from = state_bag ['at_block' ]
216+ completed = False
192217 for block in blocks_config ['blocks' ]:
218+ if completed :
219+ master_block_bags .append (block )
220+ continue
221+ if block_to is not None and block ['from' ] > block_to :
222+ completed = True
223+ master_block_bags .append (block )
224+ continue
193225 if block ['to' ] >= block_from :
194226 block_bags .append (block )
195- if block_to is not None and block ['from' ] > block_to :
196- break
197227
198228 if not state_bag or not block_bags :
199229 local .add_log ("Skip downloading archive blocks: No bags found for the specified block" , "error" )
@@ -209,6 +239,7 @@ def download_archive_from_ts(local):
209239 local .add_log ("Downloading archive blocks" , "info" )
210240 with ThreadPoolExecutor (max_workers = 4 ) as executor :
211241 futures = [executor .submit (download_blocks , local , bag ) for bag in block_bags ]
242+ futures += [executor .submit (download_master_blocks , local , bag ) for bag in master_block_bags ]
212243 for future in as_completed (futures ):
213244 try :
214245 future .result ()
@@ -224,6 +255,7 @@ def download_archive_from_ts(local):
224255 downloads_path = '/tmp/ts-downloads/'
225256
226257 os .makedirs (states_dir , exist_ok = True )
258+ os .makedirs (import_dir , exist_ok = True )
227259
228260 local .add_log ("Copying data to node db" , "info" )
229261 subprocess .run (f'cp -a { downloads_path } /{ state_bag ["bag" ]} /state-*/* { states_dir } ' , shell = True )
0 commit comments