Skip to content

Commit

Permalink
check main chain againt undelying blockchain
Browse files Browse the repository at this point in the history
  • Loading branch information
almogdepaz committed Jan 20, 2025
1 parent 4f144a6 commit 5a918d7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
9 changes: 6 additions & 3 deletions chia/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ async def short_sync_batch(self, peer: WSChiaConnection, start_height: uint32, t
fork_hash = self.constants.GENESIS_CHALLENGE
assert fork_hash
fork_info = ForkInfo(start_height - 1, start_height - 1, fork_hash)
blockchain = AugmentedBlockchain(self.blockchain)
for height in range(start_height, target_height, batch_size):
end_height = min(target_height, height + batch_size)
request = RequestBlocks(uint32(height), uint32(end_height), True)
Expand All @@ -638,7 +639,7 @@ async def short_sync_batch(self, peer: WSChiaConnection, start_height: uint32, t
)
vs = ValidationState(ssi, diff, None)
success, state_change_summary = await self.add_block_batch(
response.blocks, peer_info, fork_info, vs
response.blocks, peer_info, fork_info, vs, blockchain
)
if not success:
raise ValueError(f"Error short batch syncing, failed to validate blocks {height}-{end_height}")
Expand Down Expand Up @@ -1478,13 +1479,13 @@ async def add_block_batch(
peer_info: PeerInfo,
fork_info: ForkInfo,
vs: ValidationState, # in-out parameter
blockchain: AugmentedBlockchain,
wp_summaries: Optional[list[SubEpochSummary]] = None,
) -> tuple[bool, Optional[StateChangeSummary]]:
# Precondition: All blocks must be contiguous blocks, index i+1 must be the parent of index i
# Returns a bool for success, as well as a StateChangeSummary if the peak was advanced

pre_validate_start = time.monotonic()
blockchain = AugmentedBlockchain(self.blockchain)
blocks_to_validate = await self.skip_blocks(blockchain, all_blocks, fork_info, vs)

if len(blocks_to_validate) == 0:
Expand Down Expand Up @@ -1547,7 +1548,7 @@ async def skip_blocks(
# we have already validated this block once, no need to do it again.
# however, if this block is not part of the main chain, we need to
# update the fork context with its additions and removals
if blockchain.height_to_hash(block.height) == header_hash:
if self.blockchain.height_to_hash(block.height) == header_hash:
# we're on the main chain, just fast-forward the fork height
fork_info.reset(block.height, header_hash)
else:
Expand Down Expand Up @@ -1628,6 +1629,8 @@ async def add_prevalidated_blocks(
vs.ssi = cc_sub_slot.new_sub_slot_iters
assert cc_sub_slot.new_difficulty is not None
vs.difficulty = cc_sub_slot.new_difficulty
if expected_sub_slot_iters != vs.ssi:
self.log.info(f"block {block.height} fails")
assert expected_sub_slot_iters == vs.ssi
assert expected_difficulty == vs.difficulty
result, error, state_change_summary = await self.blockchain.add_block(
Expand Down
5 changes: 3 additions & 2 deletions chia/simulator/add_blocks_in_batches.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from chia.types.full_block import FullBlock
from chia.types.peer_info import PeerInfo
from chia.types.validation_state import ValidationState
from chia.util.augmented_chain import AugmentedBlockchain
from chia.util.batches import to_batches
from chia.util.ints import uint32

Expand All @@ -34,14 +35,14 @@ async def add_blocks_in_batches(
fork_info = ForkInfo(fork_height, blocks[0].height - 1, peak_hash)

vs = ValidationState(ssi, diff, None)

blockchain = AugmentedBlockchain(full_node.blockchain)
for block_batch in to_batches(blocks, 64):
b = block_batch.entries[0]
if (b.height % 128) == 0:
print(f"main chain: {b.height:4} weight: {b.weight}")
# vs is updated by the call to add_block_batch()
success, state_change_summary = await full_node.add_block_batch(
block_batch.entries, PeerInfo("0.0.0.0", 0), fork_info, vs
block_batch.entries, PeerInfo("0.0.0.0", 0), fork_info, vs, blockchain
)
assert success is True
if state_change_summary is not None:
Expand Down
2 changes: 1 addition & 1 deletion chia/util/augmented_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def get_block_record_from_db(self, header_hash: bytes32) -> Optional[Block

def add_block_record(self, block_record: BlockRecord) -> None:
self._underlying.add_block_record(block_record)

self._height_to_hash[block_record.height] = block_record.header_hash
# now that we're adding the block to the underlying blockchain, we don't
# need to keep the extra block around anymore
hh = block_record.header_hash
Expand Down

0 comments on commit 5a918d7

Please sign in to comment.