mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2024-11-29 05:18:11 +03:00
parent
07282a8c8b
commit
8946029bbc
@ -685,14 +685,24 @@ class Blockchain(BlockchainInterface):
|
||||
return None
|
||||
return header_dict[header_hash]
|
||||
|
||||
async def get_block_records_at(self, heights: List[uint32]) -> List[BlockRecord]:
|
||||
async def get_block_records_at(self, heights: List[uint32], batch_size=900) -> List[BlockRecord]:
|
||||
"""
|
||||
gets block records by height (only blocks that are part of the chain)
|
||||
"""
|
||||
records: List[BlockRecord] = []
|
||||
hashes = []
|
||||
assert batch_size < 999 # sqlite in python 3.7 has a limit on 999 variables in queries
|
||||
for height in heights:
|
||||
hashes.append(self.height_to_hash(height))
|
||||
return await self.block_store.get_block_records_by_hash(hashes)
|
||||
if len(hashes) > batch_size:
|
||||
res = await self.block_store.get_block_records_by_hash(hashes)
|
||||
records.extend(res)
|
||||
hashes = []
|
||||
|
||||
if len(hashes) > 0:
|
||||
res = await self.block_store.get_block_records_by_hash(hashes)
|
||||
records.extend(res)
|
||||
return records
|
||||
|
||||
async def get_block_record_from_db(self, header_hash: bytes32) -> Optional[BlockRecord]:
|
||||
if header_hash in self.__block_records:
|
||||
|
@ -2589,3 +2589,17 @@ class TestReorgs:
|
||||
!= blocks_without_filter[header_hash].transactions_filter
|
||||
)
|
||||
assert blocks_with_filter[header_hash].header_hash == blocks_without_filter[header_hash].header_hash
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_blocks_at(self, empty_blockchain, default_1000_blocks):
|
||||
b = empty_blockchain
|
||||
heights = []
|
||||
for block in default_1000_blocks[:200]:
|
||||
heights.append(block.height)
|
||||
result, error_code, _ = await b.receive_block(block)
|
||||
assert error_code is None and result == ReceiveBlockResult.NEW_PEAK
|
||||
|
||||
blocks = await b.get_block_records_at(heights, batch_size=2)
|
||||
assert blocks
|
||||
assert len(blocks) == 200
|
||||
assert blocks[-1].height == 199
|
||||
|
Loading…
Reference in New Issue
Block a user