-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2713 from mkalinin/validate-merge-block-tests
Refine validate_merge_block unit tests
- Loading branch information
Showing
6 changed files
with
241 additions
and
162 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
from random import Random | ||
from eth2spec.utils.ssz.ssz_typing import uint256 | ||
|
||
|
||
class PowChain: | ||
blocks = [] | ||
|
||
def __init__(self, blocks): | ||
self.blocks = blocks | ||
|
||
def __iter__(self): | ||
return iter(self.blocks) | ||
|
||
def head(self, offset=0): | ||
assert offset <= 0 | ||
return self.blocks[offset - 1] | ||
|
||
|
||
def prepare_random_pow_block(spec, rng=Random(3131)): | ||
return spec.PowBlock( | ||
block_hash=spec.Hash32(spec.hash(bytearray(rng.getrandbits(8) for _ in range(32)))), | ||
parent_hash=spec.Hash32(spec.hash(bytearray(rng.getrandbits(8) for _ in range(32)))), | ||
total_difficulty=uint256(0), | ||
difficulty=uint256(0) | ||
) | ||
|
||
|
||
def prepare_random_pow_chain(spec, length, rng=Random(3131)) -> PowChain: | ||
assert length > 0 | ||
chain = [prepare_random_pow_block(spec, rng)] | ||
for i in range(1, length): | ||
chain.append(prepare_random_pow_block(spec, rng)) | ||
chain[i].parent_hash = chain[i - 1].block_hash | ||
return PowChain(chain) |
This file contains 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
44 changes: 44 additions & 0 deletions
44
tests/core/pyspec/eth2spec/test/merge/unittests/test_is_valid_terminal_pow_block.py
This file contains 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,44 @@ | ||
from eth2spec.utils.ssz.ssz_typing import uint256 | ||
from eth2spec.test.helpers.pow_block import ( | ||
prepare_random_pow_block, | ||
) | ||
from eth2spec.test.context import ( | ||
spec_state_test, | ||
with_merge_and_later, | ||
) | ||
|
||
|
||
@with_merge_and_later | ||
@spec_state_test | ||
def test_is_valid_terminal_pow_block_success_valid(spec, state): | ||
parent_block = prepare_random_pow_block(spec) | ||
parent_block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY - uint256(1) | ||
block = prepare_random_pow_block(spec) | ||
block.parent_hash = parent_block.block_hash | ||
block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY | ||
|
||
assert spec.is_valid_terminal_pow_block(block, parent_block) | ||
|
||
|
||
@with_merge_and_later | ||
@spec_state_test | ||
def test_is_valid_terminal_pow_block_fail_before_terminal(spec, state): | ||
parent_block = prepare_random_pow_block(spec) | ||
parent_block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY - uint256(2) | ||
block = prepare_random_pow_block(spec) | ||
block.parent_hash = parent_block.block_hash | ||
block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY - uint256(1) | ||
|
||
assert not spec.is_valid_terminal_pow_block(block, parent_block) | ||
|
||
|
||
@with_merge_and_later | ||
@spec_state_test | ||
def test_is_valid_terminal_pow_block_fail_just_after_terminal(spec, state): | ||
parent_block = prepare_random_pow_block(spec) | ||
parent_block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY | ||
block = prepare_random_pow_block(spec) | ||
block.parent_hash = parent_block.block_hash | ||
block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY + uint256(1) | ||
|
||
assert not spec.is_valid_terminal_pow_block(block, parent_block) |
143 changes: 0 additions & 143 deletions
143
tests/core/pyspec/eth2spec/test/merge/unittests/test_terminal_validity.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.