|
3 | 3 | import dataclasses
|
4 | 4 | import logging
|
5 | 5 | import sqlite3
|
| 6 | +from contextlib import AbstractAsyncContextManager |
6 | 7 | from typing import Optional
|
7 | 8 |
|
| 9 | +import aiosqlite |
8 | 10 | import typing_extensions
|
9 | 11 | import zstd
|
10 | 12 | from chia_rs import BlockRecord, FullBlock, SubEpochChallengeSegment, SubEpochSegments
|
11 | 13 | from chia_rs.sized_bytes import bytes32
|
12 | 14 | from chia_rs.sized_ints import uint32
|
13 | 15 |
|
14 | 16 | from chia.full_node.full_block_utils import GeneratorBlockInfo, block_info_from_block, generator_from_block
|
| 17 | +from chia.util.batches import to_batches |
15 | 18 | from chia.util.db_wrapper import DBWrapper2, execute_fetchone
|
16 | 19 | from chia.util.errors import Err
|
17 | 20 | from chia.util.lru_cache import LRUCache
|
@@ -189,6 +192,12 @@ async def get_sub_epoch_challenge_segments(
|
189 | 192 | return challenge_segments
|
190 | 193 | return None
|
191 | 194 |
|
| 195 | + def transaction(self) -> AbstractAsyncContextManager[aiosqlite.Connection]: |
| 196 | + return self.db_wrapper.writer() |
| 197 | + |
| 198 | + def get_block_from_cache(self, header_hash: bytes32) -> Optional[FullBlock]: |
| 199 | + return self.block_cache.get(header_hash) |
| 200 | + |
192 | 201 | def rollback_cache_block(self, header_hash: bytes32) -> None:
|
193 | 202 | try:
|
194 | 203 | self.block_cache.remove(header_hash)
|
@@ -322,20 +331,21 @@ async def get_block_records_by_hash(self, header_hashes: list[bytes32]) -> list[
|
322 | 331 | Returns a list of Block Records, ordered by the same order in which header_hashes are passed in.
|
323 | 332 | Throws an exception if the blocks are not present
|
324 | 333 | """
|
| 334 | + |
325 | 335 | if len(header_hashes) == 0:
|
326 | 336 | return []
|
327 | 337 |
|
328 | 338 | all_blocks: dict[bytes32, BlockRecord] = {}
|
329 |
| - async with self.db_wrapper.reader_no_transaction() as conn: |
330 |
| - async with conn.execute( |
331 |
| - "SELECT header_hash,block_record " |
332 |
| - "FROM full_blocks " |
333 |
| - f"WHERE header_hash in ({'?,' * (len(header_hashes) - 1)}?)", |
334 |
| - header_hashes, |
335 |
| - ) as cursor: |
336 |
| - for row in await cursor.fetchall(): |
337 |
| - block_rec = BlockRecord.from_bytes(row[1]) |
338 |
| - all_blocks[block_rec.header_hash] = block_rec |
| 339 | + for batch in to_batches(header_hashes, self.db_wrapper.host_parameter_limit): |
| 340 | + async with self.db_wrapper.reader_no_transaction() as conn: |
| 341 | + async with conn.execute( |
| 342 | + "SELECT header_hash,block_record FROM full_blocks " |
| 343 | + f"WHERE header_hash in ({'?,' * (len(batch.entries) - 1)}?)", |
| 344 | + batch.entries, |
| 345 | + ) as cursor: |
| 346 | + for row in await cursor.fetchall(): |
| 347 | + block_rec = BlockRecord.from_bytes(row[1]) |
| 348 | + all_blocks[block_rec.header_hash] = block_rec |
339 | 349 |
|
340 | 350 | ret: list[BlockRecord] = []
|
341 | 351 | for hh in header_hashes:
|
|
0 commit comments