-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix get_pow_block_at_terminal_total_difficulty and add unit tests
#2726
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tests/core/pyspec/eth2spec/test/merge/unittests/validator/test_validator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from eth2spec.test.helpers.pow_block import ( | ||
| prepare_random_pow_chain, | ||
| ) | ||
| from eth2spec.test.context import ( | ||
| spec_state_test, | ||
| with_merge_and_later, | ||
| ) | ||
|
|
||
|
|
||
| # For test_get_pow_block_at_terminal_total_difficulty | ||
| IS_HEAD_BLOCK = 'is_head_block' | ||
| IS_HEAD_PARENT_BLOCK = 'is_head_parent_block' | ||
|
|
||
| # NOTE: The following parameter names are in the view of the head block (the second block) | ||
| # 'block_reached_ttd', 'block_parent_hash_is_empty', 'parent_reached_ttd', 'return_block' | ||
| expected_results = [ | ||
| (False, False, False, None), | ||
| (False, False, True, IS_HEAD_PARENT_BLOCK), | ||
| (False, True, False, None), | ||
| (False, True, True, IS_HEAD_PARENT_BLOCK), | ||
| (True, False, False, IS_HEAD_BLOCK), | ||
| (True, False, True, IS_HEAD_PARENT_BLOCK), | ||
| (True, True, False, IS_HEAD_BLOCK), | ||
| (True, True, True, IS_HEAD_PARENT_BLOCK), | ||
| ] | ||
| # NOTE: since the first block's `parent_hash` is set to `Hash32()` in test, if `parent_reached_ttd is True`, | ||
| # it would return the first block (IS_HEAD_PARENT_BLOCK). | ||
|
|
||
|
|
||
| @with_merge_and_later | ||
| @spec_state_test | ||
| def test_get_pow_block_at_terminal_total_difficulty(spec, state): | ||
| for result in expected_results: | ||
| ( | ||
| block_reached_ttd, | ||
| block_parent_hash_is_empty, | ||
| parent_reached_ttd, | ||
| return_block | ||
| ) = result | ||
| pow_chain = prepare_random_pow_chain(spec, 2) | ||
| pow_chain.head(-1).parent_hash = spec.Hash32() | ||
|
|
||
| if block_reached_ttd: | ||
| pow_chain.head().total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY | ||
| else: | ||
| pow_chain.head().total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY - 1 | ||
|
|
||
| if parent_reached_ttd: | ||
| pow_chain.head(-1).total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY | ||
| else: | ||
| pow_chain.head(-1).total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY - 1 | ||
|
|
||
| if block_parent_hash_is_empty: | ||
| pow_chain.head().parent_hash = spec.Hash32() | ||
|
|
||
| pow_block = spec.get_pow_block_at_terminal_total_difficulty(pow_chain.to_dict()) | ||
| if return_block == IS_HEAD_BLOCK: | ||
| assert pow_block == pow_chain.head() | ||
| elif return_block == IS_HEAD_PARENT_BLOCK: | ||
| assert pow_block == pow_chain.head(-1) | ||
| elif return_block is None: | ||
| assert pow_block is None | ||
| else: | ||
| raise Exception('Something is wrong') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we do it as follows: