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

add assertions to contains block #19001

Closed
wants to merge 8 commits into from

Conversation

almogdepaz
Copy link
Contributor

Purpose:

make sure height_to_hash is used when checking contains_block

Current Behavior:

New Behavior:

in some places we use the block_records cache for this and in some we use the height_to_hash

Testing Notes:

@almogdepaz almogdepaz added full_node sync Fixed Required label for PR that categorizes merge commit message as "Fixed" for changelog labels Dec 9, 2024
@almogdepaz almogdepaz changed the title add assertions add assertions to contains block Dec 9, 2024
Copy link
Contributor

@arvidn arvidn left a comment

Choose a reason for hiding this comment

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

I'm not sure this 100% right, nor that it adds clarity to the expectations and behavior to this function. I get the impression that we may need two separate functions:

  1. is the block in the cache and in the main chain
  2. is the block part of the main chain

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.

we also return false for blocks that are part of the main chain, but no longer in the cache.

@@ -769,12 +769,20 @@ async def validate_unfinished_block(

return PreValidationResult(None, required_iters, conds, uint32(0))

def contains_block(self, header_hash: bytes32) -> bool:
def contains_block(self, header_hash: bytes32, height: Optional[uint32] = None) -> bool:
"""
True if we have already added this block to the chain. This may return false for orphan blocks
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment should be updated to say that it will return false for orphan blocks. I take the current "may" to mean that orphan blocks are less likely to be in the cache.

This function also always returned false for blocks that aren't in the cache, regardless of whether they're in the main chain or not. The comment should say this too.

The new height parameter isn't really explained either. I assume it adds an extra check; not only that header_hash is in the main chain and in the cache, but also that it has the specified height.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the hight is an optimization for most cases where we now the hight of the block (the only case we dont is unfinished blocks)

i agree with all your points, im still trying to figure out exactly what the correct Behavior is supposed to be but im pretty sure the current state is incorect for both short_sync_backtrack and short_sync_batch

Comment on lines +943 to 944
if header_hash in self.__block_records:
return self.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.

it seems a bit backwards to make try_block_record() be implemented in terms of block_record(). It (probably) would be simpler to do it the other way around.

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 dont have a strong opinion on that, if we do it the other way around we need to throw an explicit exception for when we dont have the block

Comment on lines +783 to +784
if block_hash_from_hh is None or block_hash_from_hh != header_hash:
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.

I'm not convinced this was the intention of this function, or rather, that it is the expectation of callers. I think the block being in the cache is one of the expectations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this doesn't really make sense to me in the case of the short_sync methods or even for add block

@@ -595,7 +595,7 @@ async def short_sync_batch(self, peer: WSChiaConnection, start_height: uint32, t
self.sync_store.batch_syncing.remove(peer.peer_node_id)
self.log.error(f"Error short batch syncing, could not fetch block at height {start_height}")
return False
if not self.blockchain.contains_block(first.block.prev_header_hash):
if not self.blockchain.contains_block(first.block.prev_header_hash, first.block.height - 1):
Copy link
Contributor

Choose a reason for hiding this comment

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

here, we're not really asking whether the blocks i part of the main chain, we're asking if its in the block cache. if it isn't, we declare this a deep reorg or catch-up (and presumably transition to long-sync).

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 disagree, for short_sync_batch we assume we are syncing from start_height where start_height - 1 is in our chain so checking if first -1 is in the main chain is correct

this is only done for the first_block.prev_header_hash which is expected to be the fork point i.e in out chain

Copy link
Contributor

Choose a reason for hiding this comment

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

right, I guess we expect both in this case.

if (
self.blockchain.contains_block(curr.block.prev_header_hash, curr.block.height - 1)
or curr_height == 0
):
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that this call site is not concerned with whether the block is in the cache or not

@almogdepaz almogdepaz closed this Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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