Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert height to hash in contains block #19136

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

almogdepaz
Copy link
Contributor

@almogdepaz almogdepaz commented Jan 14, 2025

Purpose:

use the height to hash when checking for "contains_block" block that way we always get the relevant block for the chain being validated
and create better separation between Augmentedblockchain and the underlining blockchain.

Current Behavior:

we call contains block that checks the block_record cache, this will return true if the block is in the cache regardless if is relevant to the chain

New Behavior:

contains_block will check the height_to_hash, the AugmentedBlockchain will return true for blocks that are in the fork and the underlining will return true only for blocks that are in the current chain

Testing Notes:

@almogdepaz almogdepaz added full_node sync Fixed Required label for PR that categorizes merge commit message as "Fixed" for changelog labels Jan 14, 2025
@almogdepaz almogdepaz marked this pull request as ready for review January 27, 2025 17:27
@almogdepaz almogdepaz requested a review from a team as a code owner January 27, 2025 17:27
Copy link

coveralls-official bot commented Jan 27, 2025

Pull Request Test Coverage Report for Build 13111506377

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 43 of 56 (76.79%) changed or added relevant lines in 10 files are covered.
  • 25 unchanged lines in 7 files lost coverage.
  • Overall coverage increased (+0.009%) to 91.517%

Changes Missing Coverage Covered Lines Changed/Added Lines %
chia/util/augmented_chain.py 13 15 86.67%
chia/util/block_cache.py 8 10 80.0%
chia/_tests/util/blockchain_mock.py 1 10 10.0%
Files with Coverage Reduction New Missed Lines %
chia/_tests/simulation/test_simulation.py 1 96.45%
chia/full_node/full_node_api.py 1 84.38%
chia/rpc/rpc_server.py 1 89.16%
chia/full_node/full_node.py 1 86.09%
chia/consensus/difficulty_adjustment.py 3 97.69%
chia/daemon/server.py 7 83.54%
chia/timelord/timelord.py 11 78.52%
Totals Coverage Status
Change from base Build 13019247618: 0.009%
Covered Lines: 105295
Relevant Lines: 114876

💛 - Coveralls

@almogdepaz almogdepaz requested a review from arvidn February 3, 2025 14:00
Copy link
Contributor

github-actions bot commented Feb 3, 2025

File Coverage Missing Lines
chia/_tests/util/blockchain_mock.py 10.0% lines 70-78
chia/util/augmented_chain.py 86.7% lines 126, 129
chia/util/block_cache.py 80.0% lines 50, 54
Total Missing Coverage
56 lines 13 lines 76%

"""
True if we have already added this block to the chain. This may return false for orphan blocks
that we have added but no longer keep in memory.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's important to document this on the full node side as well. Perhaps this wallet counterpart will be removed or disconnected from the full node eventually

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this is no longer true for the full node, after this pr contains block will return true/false according to the height to hash and not the cache

if height is None:
block_rec = self.try_block_record(header_hash)
if block_rec is None:
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a bit risky, don't you think?
The caller think they're asking if the block is part of the main chain, but gets false if it's not in the cache, even if it is part of the main chain.

This should be clearly documented.

@@ -619,6 +619,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on line 640, we create another blockchain = AugmentedBlockchain(). It looks like a mistake

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right this is from the merge with my other pr that went into main, thanks

@@ -47,8 +47,7 @@ def add_extra_block(self, block: FullBlock, block_record: BlockRecord) -> None:

def remove_extra_block(self, hh: bytes32) -> None:
if hh in self._extra_blocks:
block_record = self._extra_blocks.pop(hh)[1]
del self._height_to_hash[block_record.height]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not obvious why you removed this line. Don't we need to clear the _height_to_hash entry?

Copy link
Contributor Author

@almogdepaz almogdepaz Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, the point of this pr is for the augmented chain to contain the alternate height_to_hash, remove extra block is called once we validated the block but not necessarily when we added it to the height to height to hash, so if i remove from the height to hash there will be blocks that are not in the augmented chain height to hash and not in the underlying chain height to hash

@@ -109,6 +107,7 @@ def height_to_block_record(self, height: uint32) -> BlockRecord:
ret = self._get_block_record(header_hash)
if ret is not None:
return ret
return self._underlying.block_record(header_hash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you can do this now that _height_to_hash grows indefinitely. But what's the upside of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have to do this, since we remove from extra_blocks once we validate and move to the underlying cache there might be a block in the augmented height_to_hash that was moved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
coverage-diff Fixed Required label for PR that categorizes merge commit message as "Fixed" for changelog full_node sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants