From 5ab2824427f631d99e297f5590b279752128d3eb Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Tue, 28 Sep 2021 00:59:03 +0800 Subject: [PATCH] Clean up, refactor test_transition.py --- .../merge/fork_choice/test_on_merge_block.py | 11 ++- .../merge/unittests/test_terminal_validity.py | 25 ++--- .../test/merge/unittests/test_transition.py | 91 ++++++------------- 3 files changed, 44 insertions(+), 83 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/merge/fork_choice/test_on_merge_block.py b/tests/core/pyspec/eth2spec/test/merge/fork_choice/test_on_merge_block.py index 38af539b1..fd141ce8d 100644 --- a/tests/core/pyspec/eth2spec/test/merge/fork_choice/test_on_merge_block.py +++ b/tests/core/pyspec/eth2spec/test/merge/fork_choice/test_on_merge_block.py @@ -16,6 +16,9 @@ from eth2spec.test.helpers.fork_choice import ( prepare_empty_pow_block, add_pow_block, ) +from eth2spec.test.helpers.execution_payload import ( + build_state_with_incomplete_transition, +) def with_pow_block_patch(spec, blocks, func): @@ -48,7 +51,7 @@ def with_pow_block_patch(spec, blocks, func): def test_all_valid(spec, state): test_steps = [] # Initialization - state.latest_execution_payload_header = spec.ExecutionPayloadHeader() + state = build_state_with_incomplete_transition(spec, state) store, anchor_block = get_genesis_forkchoice_store_and_block(spec, state) yield 'anchor_state', state yield 'anchor_block', anchor_block @@ -83,7 +86,7 @@ def test_all_valid(spec, state): def test_block_lookup_failed(spec, state): test_steps = [] # Initialization - state.latest_execution_payload_header = spec.ExecutionPayloadHeader() + state = build_state_with_incomplete_transition(spec, state) store, anchor_block = get_genesis_forkchoice_store_and_block(spec, state) yield 'anchor_state', state yield 'anchor_block', anchor_block @@ -114,7 +117,7 @@ def test_block_lookup_failed(spec, state): def test_too_early_for_merge(spec, state): test_steps = [] # Initialization - state.latest_execution_payload_header = spec.ExecutionPayloadHeader() + state = build_state_with_incomplete_transition(spec, state) store, anchor_block = get_genesis_forkchoice_store_and_block(spec, state) yield 'anchor_state', state yield 'anchor_block', anchor_block @@ -147,7 +150,7 @@ def test_too_early_for_merge(spec, state): def test_too_late_for_merge(spec, state): test_steps = [] # Initialization - state.latest_execution_payload_header = spec.ExecutionPayloadHeader() + state = build_state_with_incomplete_transition(spec, state) store, anchor_block = get_genesis_forkchoice_store_and_block(spec, state) yield 'anchor_state', state yield 'anchor_block', anchor_block diff --git a/tests/core/pyspec/eth2spec/test/merge/unittests/test_terminal_validity.py b/tests/core/pyspec/eth2spec/test/merge/unittests/test_terminal_validity.py index 2cf58f06a..cfd5ea091 100644 --- a/tests/core/pyspec/eth2spec/test/merge/unittests/test_terminal_validity.py +++ b/tests/core/pyspec/eth2spec/test/merge/unittests/test_terminal_validity.py @@ -14,20 +14,13 @@ def validate_transition_execution_payload(spec, execution_payload): def run_validate_transition_execution_payload(spec, block, parent_block, payload, - valid=True, block_lookup_success=True): + valid=True, block_lookup_success=True): """ - Run ``validate_transition_execution_payload``, yielding: - - current block ('block') - - parent block ('parent_block') - - execution payload ('payload') + Run ``validate_transition_execution_payload`` If ``valid == False``, run expecting ``AssertionError`` If ``block_lookup_success == False``, run expecting ``BlockNotFoundException`` """ - yield 'block', block - yield 'parent_block', parent_block - yield 'payload', payload - def get_pow_block(hash: spec.Bytes32) -> spec.PowBlock: if hash == block.block_hash: return block @@ -108,20 +101,20 @@ def test_validate_transition_execution_payload_success(spec, state): block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY payload = spec.ExecutionPayload() payload.parent_hash = block.block_hash - yield from run_process_merge_execution_payload(spec, block, parent_block, payload) + run_validate_transition_execution_payload(spec, block, parent_block, payload) @with_merge_and_later @spec_state_test -def test_process_merge_execution_payload_fail_block_lookup(spec, state): +def test_validate_transition_execution_payload_fail_block_lookup(spec, state): parent_block = prepare_empty_pow_block(spec) parent_block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY - uint256(1) block = prepare_empty_pow_block(spec) block.parent_hash = parent_block.block_hash block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY payload = spec.ExecutionPayload() - yield from run_validate_transition_execution_payload(spec, block, parent_block, payload, - block_lookup_success=False) + run_validate_transition_execution_payload(spec, block, parent_block, payload, + block_lookup_success=False) @with_merge_and_later @@ -133,8 +126,8 @@ def test_validate_transition_execution_payload_fail_parent_block_lookup(spec, st block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY payload = spec.ExecutionPayload() payload.parent_hash = block.block_hash - yield from run_validate_transition_execution_payload(spec, block, parent_block, payload, - block_lookup_success=False) + run_validate_transition_execution_payload(spec, block, parent_block, payload, + block_lookup_success=False) @with_merge_and_later @@ -147,4 +140,4 @@ def test_validate_transition_execution_payload_fail_after_terminal(spec, state): block.total_difficulty = spec.config.TERMINAL_TOTAL_DIFFICULTY + 1 payload = spec.ExecutionPayload() payload.parent_hash = block.block_hash - yield from run_validate_transition_execution_payload(spec, block, parent_block, payload, valid=False) + run_validate_transition_execution_payload(spec, block, parent_block, payload, valid=False) diff --git a/tests/core/pyspec/eth2spec/test/merge/unittests/test_transition.py b/tests/core/pyspec/eth2spec/test/merge/unittests/test_transition.py index d0dbfae40..05d3888da 100644 --- a/tests/core/pyspec/eth2spec/test/merge/unittests/test_transition.py +++ b/tests/core/pyspec/eth2spec/test/merge/unittests/test_transition.py @@ -3,7 +3,10 @@ from eth2spec.test.helpers.execution_payload import ( build_state_with_incomplete_transition, build_state_with_complete_transition, ) -from eth2spec.test.context import spec_state_test, with_merge_and_later +from eth2spec.test.context import ( + spec_state_test, + with_merge_and_later +) @with_merge_and_later @@ -20,71 +23,33 @@ def test_success_merge_complete(spec, state): assert spec.is_merge_complete(state) -@with_merge_and_later -@spec_state_test -def test_fail_merge_block(spec, state): - state = build_state_with_complete_transition(spec, state) - execution_payload = spec.ExecutionPayload() - body = spec.BeaconBlockBody() - body.execution_payload = execution_payload - assert not spec.is_merge_block(state, body) +# with_complete_transition', 'with_execution_payload', 'is_merge_block', 'is_execution_enabled' +expected_results = [ + (True, True, False, True), + (True, False, False, True), + (False, True, True, True), + (False, False, False, False) +] @with_merge_and_later @spec_state_test -def test_fail_merge_block_complete_transition(spec, state): - state = build_state_with_complete_transition(spec, state) - body = spec.BeaconBlockBody() - body.execution_payload = build_empty_execution_payload(spec, state) - assert not spec.is_merge_block(state, body) +def test_is_merge_block_and_is_execution_enabled(spec, state): + for result in expected_results: + ( + with_complete_transition, + with_execution_payload, + is_merge_block, + is_execution_enabled + ) = result + if with_complete_transition: + state = build_state_with_complete_transition(spec, state) + else: + state = build_state_with_incomplete_transition(spec, state) + body = spec.BeaconBlockBody() + if with_execution_payload: + body.execution_payload = build_empty_execution_payload(spec, state) -@with_merge_and_later -@spec_state_test -def test_fail_merge_block_no_execution_payload(spec, state): - state = build_state_with_incomplete_transition(spec, state) - body = spec.BeaconBlockBody() - assert not spec.is_merge_block(state, body) - - -@with_merge_and_later -@spec_state_test -def test_success_merge_block(spec, state): - state = build_state_with_incomplete_transition(spec, state) - body = spec.BeaconBlockBody() - body.execution_payload = build_empty_execution_payload(spec, state) - assert spec.is_merge_block(state, body) - - -@with_merge_and_later -@spec_state_test -def test_failed_execution_enabled(spec, state): - state = build_state_with_incomplete_transition(spec, state) - body = spec.BeaconBlockBody() - assert not spec.is_execution_enabled(state, body) - - -@with_merge_and_later -@spec_state_test -def test_success_execution_enabled_before_terminal(spec, state): - state = build_state_with_incomplete_transition(spec, state) - body = spec.BeaconBlockBody() - body.execution_payload = build_empty_execution_payload(spec, state) - assert spec.is_execution_enabled(state, body) - - -@with_merge_and_later -@spec_state_test -def test_success_execution_enabled_no_execution_payload(spec, state): - state = build_state_with_complete_transition(spec, state) - body = spec.BeaconBlockBody() - assert spec.is_execution_enabled(state, body) - - -@with_merge_and_later -@spec_state_test -def test_success_execution_enabled(spec, state): - state = build_state_with_complete_transition(spec, state) - body = spec.BeaconBlockBody() - body.execution_payload = build_empty_execution_payload(spec, state) - assert spec.is_execution_enabled(state, body) + assert spec.is_merge_block(state, body) == is_merge_block + assert spec.is_execution_enabled(state, body) == is_execution_enabled