|
9 | 9 | import random |
10 | 10 | import json |
11 | 11 | import pkg_resources |
| 12 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 13 | + |
12 | 14 |
|
13 | 15 | from mypylib.mypylib import ( |
14 | 16 | add2systemd, |
@@ -109,6 +111,12 @@ def is_testnet(local): |
109 | 111 | return True |
110 | 112 | return False |
111 | 113 |
|
| 114 | +def download_blocks(local, bag: dict): |
| 115 | + local.add_log(f"Downloading blocks from {bag['from']} to {bag['to']}", "info") |
| 116 | + if not download_bag(local, bag['bag']): |
| 117 | + local.add_log("Error downloading archive bag", "error") |
| 118 | + return |
| 119 | + |
112 | 120 | def download_bag(local, bag_id: str): |
113 | 121 | local_ts_url = f"http://127.0.0.1:{local.buffer.ton_storage.api_port}" |
114 | 122 | downloads_path = '/tmp/ts-downloads/' |
@@ -137,7 +145,7 @@ def update_init_block(local, seqno: int): |
137 | 145 | local.add_log(f"Editing init block in {local.buffer.global_config_path}", "info") |
138 | 146 | url = f'https://toncenter.com/api/v2/lookupBlock?workchain=-1&shard=-9223372036854775808&seqno={seqno}' |
139 | 147 | if is_testnet(local): |
140 | | - url.replace('toncenter.com', 'testnet.toncenter.com') |
| 148 | + url = url.replace('toncenter.com', 'testnet.toncenter.com') |
141 | 149 | resp = requests.get(url).json() |
142 | 150 | if not resp['ok']: |
143 | 151 | local.add_log("Error getting init block from toncenter", "error") |
@@ -186,11 +194,15 @@ def download_archive_from_ts(local): |
186 | 194 | update_init_block(local, state_bag['at_block']) |
187 | 195 |
|
188 | 196 | local.add_log("Downloading archive blocks", "info") |
189 | | - for bag in block_bags: |
190 | | - local.add_log(f"Downloading blocks from {bag['from']} to {bag['to']}", "info") |
191 | | - if not download_bag(local, bag['bag']): |
192 | | - local.add_log("Error downloading archive bag", "error") |
193 | | - return |
| 197 | + with ThreadPoolExecutor(max_workers=4) as executor: |
| 198 | + futures = [executor.submit(download_blocks, local, bag) for bag in block_bags] |
| 199 | + for future in as_completed(futures): |
| 200 | + try: |
| 201 | + future.result() |
| 202 | + except Exception as e: |
| 203 | + local.add_log(f"Error while downloading blocks: {e}", "error") |
| 204 | + return |
| 205 | + |
194 | 206 | local.add_log(f"Downloading blocks is completed", "info") |
195 | 207 |
|
196 | 208 | archive_dir = local.buffer.ton_db_dir + 'archive/' |
|
0 commit comments