Skip to content

Commit 9fbeb5e

Browse files
committed
Move batching and simplify protocol.
1 parent 315bc4e commit 9fbeb5e

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

chia/consensus/block_store_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ async def count_uncompactified_blocks(self) -> int: ...
4545
async def get_block_records_close_to_peak(
4646
self, blocks_n: int
4747
) -> tuple[dict[bytes32, BlockRecord], Optional[bytes32]]: ...
48-
def get_host_parameter_limit(self) -> int: ...
48+
4949
async def get_prev_hash(self, header_hash: bytes32) -> bytes32: ...
5050
def transaction(self) -> AbstractAsyncContextManager[Any]: ...

chia/consensus/blockchain.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -926,27 +926,18 @@ async def get_header_block_by_height(
926926
return None
927927
return header_dict[header_hash]
928928

929-
async def get_block_records_at(self, heights: list[uint32], batch_size: int = 900) -> list[BlockRecord]:
929+
async def get_block_records_at(self, heights: list[uint32]) -> list[BlockRecord]:
930930
"""
931931
gets block records by height (only blocks that are part of the chain)
932932
"""
933-
records: list[BlockRecord] = []
934933
hashes: list[bytes32] = []
935-
assert batch_size < self.block_store.get_host_parameter_limit()
936934
for height in heights:
937935
header_hash: Optional[bytes32] = self.height_to_hash(height)
938936
if header_hash is None:
939937
raise ValueError(f"Do not have block at height {height}")
940938
hashes.append(header_hash)
941-
if len(hashes) > batch_size:
942-
res = await self.block_store.get_block_records_by_hash(hashes)
943-
records.extend(res)
944-
hashes = []
945-
946-
if len(hashes) > 0:
947-
res = await self.block_store.get_block_records_by_hash(hashes)
948-
records.extend(res)
949-
return records
939+
940+
return await self.block_store.get_block_records_by_hash(hashes)
950941

951942
def try_block_record(self, header_hash: bytes32) -> Optional[BlockRecord]:
952943
if header_hash in self.__block_records:

chia/full_node/block_store.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from chia_rs.sized_ints import uint32
1414

1515
from chia.full_node.full_block_utils import GeneratorBlockInfo, block_info_from_block, generator_from_block
16+
from chia.util.batches import to_batches
1617
from chia.util.db_wrapper import DBWrapper2, execute_fetchone
1718
from chia.util.errors import Err
1819
from chia.util.lru_cache import LRUCache
@@ -190,9 +191,6 @@ async def get_sub_epoch_challenge_segments(
190191
return challenge_segments
191192
return None
192193

193-
def get_host_parameter_limit(self) -> int:
194-
return self.db_wrapper.host_parameter_limit
195-
196194
def transaction(self) -> AbstractAsyncContextManager[Any]:
197195
return self.db_wrapper.writer()
198196

@@ -332,20 +330,21 @@ async def get_block_records_by_hash(self, header_hashes: list[bytes32]) -> list[
332330
Returns a list of Block Records, ordered by the same order in which header_hashes are passed in.
333331
Throws an exception if the blocks are not present
334332
"""
333+
335334
if len(header_hashes) == 0:
336335
return []
337336

338337
all_blocks: dict[bytes32, BlockRecord] = {}
339-
async with self.db_wrapper.reader_no_transaction() as conn:
340-
async with conn.execute(
341-
"SELECT header_hash,block_record "
342-
"FROM full_blocks "
343-
f"WHERE header_hash in ({'?,' * (len(header_hashes) - 1)}?)",
344-
header_hashes,
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
338+
for batch in to_batches(header_hashes, self.db_wrapper.host_parameter_limit):
339+
async with self.db_wrapper.reader_no_transaction() as conn:
340+
async with conn.execute(
341+
"SELECT header_hash,block_record FROM full_blocks "
342+
f"WHERE header_hash in ({'?,' * (len(batch.entries) - 1)}?)",
343+
batch.entries,
344+
) as cursor:
345+
for row in await cursor.fetchall():
346+
block_rec = BlockRecord.from_bytes(row[1])
347+
all_blocks[block_rec.header_hash] = block_rec
349348

350349
ret: list[BlockRecord] = []
351350
for hh in header_hashes:

0 commit comments

Comments
 (0)