Skip to content

Commit

Permalink
transition find_fork_point_in_chain() to be async and pull blocks fro…
Browse files Browse the repository at this point in the history
…m the database
  • Loading branch information
arvidn committed Sep 30, 2023
1 parent da483e7 commit c03bced
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion chia/consensus/block_body_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ async def validate_block_body(
elif fork_point_with_peak is not None:
fork_h = fork_point_with_peak
else:
fork_h = find_fork_point_in_chain(blocks, peak, blocks.block_record(block.prev_header_hash))
fork_h = await find_fork_point_in_chain(blocks, peak, blocks.block_record(block.prev_header_hash))

# Get additions and removals since (after) fork_h but not including this block
# The values include: the coin that was added, the height of the block in which it was confirmed, and the
Expand Down
4 changes: 2 additions & 2 deletions chia/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ async def _reconsider_peak(
elif fork_point_with_peak is not None:
fork_height = fork_point_with_peak
else:
fork_height = find_fork_point_in_chain(self, block_record, peak)
fork_height = await find_fork_point_in_chain(self, block_record, peak)

if block_record.prev_hash != peak.header_hash:
for coin_record in await self.coin_store.rollback_to_block(fork_height):
Expand Down Expand Up @@ -938,7 +938,7 @@ async def get_block_generator(
prev_block = await self.block_store.get_full_block(previous_block_hash)
assert prev_block is not None
assert prev_block_record is not None
fork = find_fork_point_in_chain(self, peak, prev_block_record)
fork = await find_fork_point_in_chain(self, peak, prev_block_record)
curr_2: Optional[FullBlock] = prev_block
assert curr_2 is not None and isinstance(curr_2, FullBlock)
reorg_chain[curr_2.height] = curr_2
Expand Down
19 changes: 13 additions & 6 deletions chia/consensus/find_fork_point.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from __future__ import annotations

from typing import Union
from typing import Optional, Union

from chia.consensus.block_record import BlockRecord
from chia.consensus.blockchain_interface import BlockchainInterface
from chia.types.header_block import HeaderBlock


def find_fork_point_in_chain(
def unwrap(block: Optional[BlockRecord]) -> BlockRecord:
if block is None:
raise KeyError("missing block in chain")
return block


async def find_fork_point_in_chain(
blocks: BlockchainInterface,
block_1: Union[BlockRecord, HeaderBlock],
block_2: Union[BlockRecord, HeaderBlock],
Expand All @@ -19,14 +25,15 @@ def find_fork_point_in_chain(
"""
while block_2.height > 0 or block_1.height > 0:
if block_2.height > block_1.height:
block_2 = blocks.block_record(block_2.prev_hash)
block_2 = unwrap(await blocks.get_block_record_from_db(block_2.prev_hash))
elif block_1.height > block_2.height:
block_1 = blocks.block_record(block_1.prev_hash)
block_1 = unwrap(await blocks.get_block_record_from_db(block_1.prev_hash))
else:
if block_2.header_hash == block_1.header_hash:
return block_2.height
block_2 = blocks.block_record(block_2.prev_hash)
block_1 = blocks.block_record(block_1.prev_hash)
block_2 = unwrap(await blocks.get_block_record_from_db(block_2.prev_hash))
block_1 = unwrap(await blocks.get_block_record_from_db(block_1.prev_hash))

if block_2 != block_1:
# All blocks are different
return -1
Expand Down
2 changes: 1 addition & 1 deletion chia/wallet/wallet_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async def add_block(self, block: HeaderBlock) -> Tuple[AddBlockResult, Optional[
if block_record.prev_hash == self._peak.header_hash:
fork_height: int = self._peak.height
else:
fork_height = find_fork_point_in_chain(self, block_record, self._peak)
fork_height = await find_fork_point_in_chain(self, block_record, self._peak)
await self._rollback_to_height(fork_height)
curr_record: BlockRecord = block_record
latest_timestamp = self._latest_timestamp
Expand Down
4 changes: 2 additions & 2 deletions tests/core/full_node/stores/test_full_node_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async def test_basic_store(
assert peak_here is not None
if peak_here.header_hash == block.header_hash:
sb = blockchain.block_record(block.header_hash)
fork = find_fork_point_in_chain(blockchain, peak, blockchain.block_record(sb.header_hash))
fork = await find_fork_point_in_chain(blockchain, peak, blockchain.block_record(sb.header_hash))
if fork > 0:
fork_block = blockchain.height_to_block_record(uint32(fork))
else:
Expand Down Expand Up @@ -375,7 +375,7 @@ async def test_basic_store(
assert peak_here is not None
if peak_here.header_hash == blocks[-1].header_hash:
sb = blockchain.block_record(blocks[-1].header_hash)
fork = find_fork_point_in_chain(blockchain, peak, blockchain.block_record(sb.header_hash))
fork = await find_fork_point_in_chain(blockchain, peak, blockchain.block_record(sb.header_hash))
if fork > 0:
fork_block = blockchain.height_to_block_record(uint32(fork))
else:
Expand Down

0 comments on commit c03bced

Please sign in to comment.