batch get_blocks_at (#6475)

* batch get blocks at

* test

* comment
This commit is contained in:
Almog De Paz 2021-06-07 23:05:14 +03:00 committed by GitHub
parent 07282a8c8b
commit 8946029bbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -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:

View File

@ -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