Skip to content

Commit

Permalink
remove redundant block record conversion CHIA-1286 (#18930)
Browse files Browse the repository at this point in the history
* propegate block record

* revert changes to pytest.ini

* comments
  • Loading branch information
almogdepaz authored Jan 6, 2025
1 parent 2b1aef8 commit b6d5afc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
26 changes: 15 additions & 11 deletions chia/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ async def add_block(
sub_slot_iters: uint64,
fork_info: ForkInfo,
prev_ses_block: Optional[BlockRecord] = None,
block_record: Optional[BlockRecord] = None,
) -> tuple[AddBlockResult, Optional[Err], Optional[StateChangeSummary]]:
"""
This method must be called under the blockchain lock
Expand Down Expand Up @@ -355,14 +356,16 @@ async def add_block(
if extending_main_chain:
fork_info.reset(block.height - 1, block.prev_header_hash)

block_rec = await self.get_block_record_from_db(header_hash)
if block_rec is not None:
# we dont consider block_record passed in here since it might be from
# a current sync process and not yet fully validated and commited to the DB
block_rec_from_db = await self.get_block_record_from_db(header_hash)
if block_rec_from_db is not None:
# We have already validated the block, but if it's not part of the
# main chain, we still need to re-run it to update the additions and
# removals in fork_info.
await self.advance_fork_info(block, fork_info)
fork_info.include_spends(pre_validation_result.conds, block, header_hash)
self.add_block_record(block_rec)
self.add_block_record(block_rec_from_db)
return AddBlockResult.ALREADY_HAVE_BLOCK, None, None

if fork_info.peak_hash != block.prev_header_hash:
Expand Down Expand Up @@ -398,14 +401,15 @@ async def add_block(
if not genesis and prev_block is not None:
self.add_block_record(prev_block)

block_record = block_to_block_record(
self.constants,
self,
required_iters,
block,
sub_slot_iters=sub_slot_iters,
prev_ses_block=prev_ses_block,
)
if block_record is None:
block_record = block_to_block_record(
self.constants,
self,
required_iters,
block,
sub_slot_iters=sub_slot_iters,
prev_ses_block=prev_ses_block,
)

# in case we fail and need to restore the blockchain state, remember the
# peak height
Expand Down
12 changes: 7 additions & 5 deletions chia/full_node/full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,6 @@ async def validate_blocks(
await self.prevalidate_blocks(
blockchain,
[block],
peer.peer_info,
vs,
summaries,
)
Expand Down Expand Up @@ -1489,7 +1488,6 @@ async def add_block_batch(
futures = await self.prevalidate_blocks(
blockchain,
blocks_to_validate,
peer_info,
copy.copy(vs),
wp_summaries,
)
Expand Down Expand Up @@ -1558,7 +1556,6 @@ async def prevalidate_blocks(
self,
blockchain: AugmentedBlockchain,
blocks_to_validate: list[FullBlock],
peer_info: PeerInfo,
vs: ValidationState,
wp_summaries: Optional[list[SubEpochSummary]] = None,
) -> Sequence[Awaitable[PreValidationResult]]:
Expand All @@ -1568,7 +1565,6 @@ async def prevalidate_blocks(
Args:
blockchain:
blocks_to_validate:
peer_info:
vs: The ValidationState for the first block in the batch. This is an in-out
parameter. It will be updated to be the validation state for the next
batch of blocks.
Expand Down Expand Up @@ -1626,8 +1622,14 @@ async def add_prevalidated_blocks(
vs.difficulty = cc_sub_slot.new_difficulty
assert expected_sub_slot_iters == vs.ssi
assert expected_difficulty == vs.difficulty
block_rec = blockchain.block_record(block.header_hash)
result, error, state_change_summary = await self.blockchain.add_block(
block, pre_validation_results[i], vs.ssi, fork_info, prev_ses_block=vs.prev_ses_block
block,
pre_validation_results[i],
vs.ssi,
fork_info,
prev_ses_block=vs.prev_ses_block,
block_record=block_rec,
)
if error is None:
blockchain.remove_extra_block(header_hash)
Expand Down

0 comments on commit b6d5afc

Please sign in to comment.