From ccc40e198232c7f1398179ddbdb4a03607128cfc Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 15 Aug 2022 07:49:53 -0600 Subject: [PATCH] a few more bellatrix tests (#2962) * Do not overload index with WithdrawalIndex and ValidatorIndex * a few more bellatrix tests * use function from other PR * fix tests * Update tests/core/pyspec/eth2spec/test/bellatrix/transition/test_transition.py Co-authored-by: Hsiao-Wei Wang * refactor to reuse bellatrix transitio ntests for all transitions Co-authored-by: Potuz Co-authored-by: Hsiao-Wei Wang --- .../test/altair/transition/test_transition.py | 56 +++++++++++++++++++ .../test_process_execution_payload.py | 29 +--------- .../test/bellatrix/sanity/test_blocks.py | 23 +++++++- .../bellatrix/transition/test_transition.py | 35 ------------ .../test/helpers/execution_payload.py | 28 ++++++++++ 5 files changed, 106 insertions(+), 65 deletions(-) delete mode 100644 tests/core/pyspec/eth2spec/test/bellatrix/transition/test_transition.py diff --git a/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py b/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py index 32861b866..dcbd8a38b 100644 --- a/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py +++ b/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py @@ -3,6 +3,9 @@ from eth2spec.test.context import ( ForkMeta, with_fork_metas, ) +from eth2spec.test.helpers.random import ( + randomize_state, +) from eth2spec.test.helpers.constants import ( ALL_PRE_POST_FORKS, ) @@ -17,9 +20,31 @@ from eth2spec.test.helpers.fork_transition import ( skip_slots, state_transition_across_slots, transition_to_next_epoch_and_append_blocks, + transition_until_fork, ) +@with_fork_metas([ForkMeta(pre_fork_name=pre, post_fork_name=post, fork_epoch=2) for pre, post in ALL_PRE_POST_FORKS]) +def test_simple_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag): + transition_until_fork(spec, state, fork_epoch) + + # check pre state + assert spec.get_current_epoch(state) < fork_epoch + + yield "pre", state + + # irregular state transition to handle fork: + blocks = [] + state, block = do_fork(state, spec, post_spec, fork_epoch) + blocks.append(post_tag(block)) + + # continue regular state transition with new spec into next epoch + transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks, only_last_block=True) + + yield "blocks", blocks + yield "post", state + + @with_fork_metas([ForkMeta(pre_fork_name=pre, post_fork_name=post, fork_epoch=2) for pre, post in ALL_PRE_POST_FORKS]) def test_normal_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag): """ @@ -56,6 +81,37 @@ def test_normal_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag yield "post", state +@with_fork_metas([ForkMeta(pre_fork_name=pre, post_fork_name=post, fork_epoch=8) for pre, post in ALL_PRE_POST_FORKS]) +def test_transition_randomized_state(state, fork_epoch, spec, post_spec, pre_tag, post_tag): + randomize_state(spec, state) + + transition_until_fork(spec, state, fork_epoch) + + # check pre state + assert spec.get_current_epoch(state) < fork_epoch + + yield "pre", state + + # irregular state transition to handle fork: + blocks = [] + # since there are slashed validators, set with_block=False here + state, _ = do_fork(state, spec, post_spec, fork_epoch, with_block=False) + slashed_indices = [index for index, validator in enumerate(state.validators) if validator.slashed] + + # continue regular state transition with new spec into next epoch + transition_to_next_epoch_and_append_blocks( + post_spec, + state, + post_tag, + blocks, + only_last_block=True, + ignoring_proposers=slashed_indices, + ) + + yield "blocks", blocks + yield "post", state + + @with_fork_metas([ForkMeta(pre_fork_name=pre, post_fork_name=post, fork_epoch=2) for pre, post in ALL_PRE_POST_FORKS]) def test_transition_missing_first_post_block(state, fork_epoch, spec, post_spec, pre_tag, post_tag): """ diff --git a/tests/core/pyspec/eth2spec/test/bellatrix/block_processing/test_process_execution_payload.py b/tests/core/pyspec/eth2spec/test/bellatrix/block_processing/test_process_execution_payload.py index 90125b69c..2819b2a24 100644 --- a/tests/core/pyspec/eth2spec/test/bellatrix/block_processing/test_process_execution_payload.py +++ b/tests/core/pyspec/eth2spec/test/bellatrix/block_processing/test_process_execution_payload.py @@ -1,8 +1,8 @@ from random import Random -from eth2spec.debug.random_value import get_random_bytes_list from eth2spec.test.helpers.execution_payload import ( build_empty_execution_payload, + build_randomized_execution_payload, get_execution_payload_header, build_state_with_incomplete_transition, build_state_with_complete_transition, @@ -307,33 +307,6 @@ def test_zero_length_transaction_regular_payload(spec, state): yield from run_zero_length_transaction_test(spec, state) -def build_randomized_execution_payload(spec, state, rng): - execution_payload = build_empty_execution_payload(spec, state) - execution_payload.fee_recipient = spec.ExecutionAddress(get_random_bytes_list(rng, 20)) - execution_payload.state_root = spec.Bytes32(get_random_bytes_list(rng, 32)) - execution_payload.receipts_root = spec.Bytes32(get_random_bytes_list(rng, 32)) - execution_payload.logs_bloom = spec.ByteVector[spec.BYTES_PER_LOGS_BLOOM]( - get_random_bytes_list(rng, spec.BYTES_PER_LOGS_BLOOM) - ) - execution_payload.block_number = rng.randint(0, 10e10) - execution_payload.gas_limit = rng.randint(0, 10e10) - execution_payload.gas_used = rng.randint(0, 10e10) - extra_data_length = rng.randint(0, spec.MAX_EXTRA_DATA_BYTES) - execution_payload.extra_data = spec.ByteList[spec.MAX_EXTRA_DATA_BYTES]( - get_random_bytes_list(rng, extra_data_length) - ) - execution_payload.base_fee_per_gas = rng.randint(0, 2**256 - 1) - execution_payload.block_hash = spec.Hash32(get_random_bytes_list(rng, 32)) - - num_transactions = rng.randint(0, 100) - execution_payload.transactions = [ - spec.Transaction(get_random_bytes_list(rng, rng.randint(0, 1000))) - for _ in range(num_transactions) - ] - - return execution_payload - - def run_randomized_non_validated_execution_fields_test(spec, state, execution_valid=True, rng=Random(5555)): next_slot(spec, state) execution_payload = build_randomized_execution_payload(spec, state, rng) diff --git a/tests/core/pyspec/eth2spec/test/bellatrix/sanity/test_blocks.py b/tests/core/pyspec/eth2spec/test/bellatrix/sanity/test_blocks.py index bad922d4f..ef6bb75a9 100644 --- a/tests/core/pyspec/eth2spec/test/bellatrix/sanity/test_blocks.py +++ b/tests/core/pyspec/eth2spec/test/bellatrix/sanity/test_blocks.py @@ -1,9 +1,14 @@ +from random import Random from eth2spec.test.helpers.state import ( - state_transition_and_sign_block + state_transition_and_sign_block, + next_slot, ) from eth2spec.test.helpers.block import ( build_empty_block_for_next_slot ) +from eth2spec.test.helpers.execution_payload import ( + build_randomized_execution_payload +) from eth2spec.test.context import ( with_bellatrix_and_later, spec_state_test ) @@ -22,7 +27,21 @@ def test_empty_block_transition_no_tx(spec, state): yield 'blocks', [signed_block] yield 'post', state -# TODO: tests with EVM, mock or replacement? + +@with_bellatrix_and_later +@spec_state_test +def test_empty_block_transition_randomized_payload(spec, state): + yield 'pre', state + + block = build_empty_block_for_next_slot(spec, state) + next_slot_state = state.copy() + next_slot(spec, next_slot_state) + block.body.execution_payload = build_randomized_execution_payload(spec, next_slot_state, rng=Random(34433)) + + signed_block = state_transition_and_sign_block(spec, state, block) + + yield 'blocks', [signed_block] + yield 'post', state @with_bellatrix_and_later diff --git a/tests/core/pyspec/eth2spec/test/bellatrix/transition/test_transition.py b/tests/core/pyspec/eth2spec/test/bellatrix/transition/test_transition.py deleted file mode 100644 index 2382a3758..000000000 --- a/tests/core/pyspec/eth2spec/test/bellatrix/transition/test_transition.py +++ /dev/null @@ -1,35 +0,0 @@ -from eth2spec.test.context import ( - ForkMeta, - with_fork_metas, -) -from eth2spec.test.helpers.constants import ( - AFTER_BELLATRIX_PRE_POST_FORKS, -) -from eth2spec.test.helpers.fork_transition import ( - do_fork, - transition_to_next_epoch_and_append_blocks, - transition_until_fork, -) - - -@with_fork_metas([ - ForkMeta(pre_fork_name=pre, post_fork_name=post, fork_epoch=2) for pre, post in AFTER_BELLATRIX_PRE_POST_FORKS -]) -def test_sample_transition(state, fork_epoch, spec, post_spec, pre_tag, post_tag): - transition_until_fork(spec, state, fork_epoch) - - # check pre state - assert spec.get_current_epoch(state) < fork_epoch - - yield "pre", state - - # irregular state transition to handle fork: - blocks = [] - state, block = do_fork(state, spec, post_spec, fork_epoch) - blocks.append(post_tag(block)) - - # continue regular state transition with new spec into next epoch - transition_to_next_epoch_and_append_blocks(post_spec, state, post_tag, blocks, only_last_block=True) - - yield "blocks", blocks - yield "post", state diff --git a/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py b/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py index cce4c89ed..83162e1c2 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py +++ b/tests/core/pyspec/eth2spec/test/helpers/execution_payload.py @@ -1,3 +1,4 @@ +from eth2spec.debug.random_value import get_random_bytes_list from eth2spec.test.context import is_post_capella @@ -38,6 +39,33 @@ def build_empty_execution_payload(spec, state, randao_mix=None): return payload +def build_randomized_execution_payload(spec, state, rng): + execution_payload = build_empty_execution_payload(spec, state) + execution_payload.fee_recipient = spec.ExecutionAddress(get_random_bytes_list(rng, 20)) + execution_payload.state_root = spec.Bytes32(get_random_bytes_list(rng, 32)) + execution_payload.receipts_root = spec.Bytes32(get_random_bytes_list(rng, 32)) + execution_payload.logs_bloom = spec.ByteVector[spec.BYTES_PER_LOGS_BLOOM]( + get_random_bytes_list(rng, spec.BYTES_PER_LOGS_BLOOM) + ) + execution_payload.block_number = rng.randint(0, 10e10) + execution_payload.gas_limit = rng.randint(0, 10e10) + execution_payload.gas_used = rng.randint(0, 10e10) + extra_data_length = rng.randint(0, spec.MAX_EXTRA_DATA_BYTES) + execution_payload.extra_data = spec.ByteList[spec.MAX_EXTRA_DATA_BYTES]( + get_random_bytes_list(rng, extra_data_length) + ) + execution_payload.base_fee_per_gas = rng.randint(0, 2**256 - 1) + execution_payload.block_hash = spec.Hash32(get_random_bytes_list(rng, 32)) + + num_transactions = rng.randint(0, 100) + execution_payload.transactions = [ + spec.Transaction(get_random_bytes_list(rng, rng.randint(0, 1000))) + for _ in range(num_transactions) + ] + + return execution_payload + + def get_execution_payload_header(spec, execution_payload): payload_header = spec.ExecutionPayloadHeader( parent_hash=execution_payload.parent_hash,