From dc02bbc14223e225726ef77ac7fbde6192f08c70 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Fri, 26 Jun 2020 00:42:29 +0800 Subject: [PATCH] Add more shard block tests --- .../test/phase1/sanity/test_shard_blocks.py | 113 +++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) diff --git a/tests/core/pyspec/eth2spec/test/phase1/sanity/test_shard_blocks.py b/tests/core/pyspec/eth2spec/test/phase1/sanity/test_shard_blocks.py index 99696dd45..21f5cb18a 100644 --- a/tests/core/pyspec/eth2spec/test/phase1/sanity/test_shard_blocks.py +++ b/tests/core/pyspec/eth2spec/test/phase1/sanity/test_shard_blocks.py @@ -10,7 +10,7 @@ from eth2spec.test.helpers.shard_block import ( sign_shard_block, ) from eth2spec.test.helpers.shard_transitions import is_full_crosslink -from eth2spec.test.helpers.state import transition_to_valid_shard_slot +from eth2spec.test.helpers.state import next_slot, transition_to_valid_shard_slot, transition_to def run_shard_blocks(spec, shard_state, signed_shard_block, beacon_parent_state, valid=True): @@ -138,6 +138,66 @@ def test_invalid_proposer_index(spec, state): yield from run_shard_blocks(spec, shard_state, signed_shard_block, beacon_state, valid=False) +@with_all_phases_except([PHASE0]) +@spec_state_test +@always_bls +def test_out_of_bound_offset(spec, state): + # TODO: Handle this edge case properly + if not is_full_crosslink(spec, state): + # skip + return + + beacon_state = transition_to_valid_shard_slot(spec, state) + shard = 0 + slot = ( + beacon_state.shard_states[shard].slot + + spec.SHARD_BLOCK_OFFSETS[spec.MAX_SHARD_BLOCKS_PER_ATTESTATION - 1] + + 1 # out-of-bound + ) + transition_to(spec, beacon_state, slot) + + shard_state = beacon_state.shard_states[shard] + signed_shard_block = build_shard_block(spec, beacon_state, shard, slot=beacon_state.slot, signed=True) + + yield from run_shard_blocks(spec, shard_state, signed_shard_block, beacon_state, valid=False) + + +@with_all_phases_except([PHASE0]) +@spec_state_test +@always_bls +def test_invalid_offset(spec, state): + if not is_full_crosslink(spec, state): + # skip + return + + beacon_state = transition_to_valid_shard_slot(spec, state) + # 4 is not in `SHARD_BLOCK_OFFSETS` + shard = 0 + slot = beacon_state.shard_states[shard].slot + 4 + transition_to(spec, beacon_state, slot) + + shard_state = beacon_state.shard_states[shard] + signed_shard_block = build_shard_block(spec, beacon_state, shard, slot=beacon_state.slot, signed=True) + + yield from run_shard_blocks(spec, shard_state, signed_shard_block, beacon_state, valid=False) + + +@with_all_phases_except([PHASE0]) +@spec_state_test +@always_bls +def test_empty_block_body(spec, state): + if not is_full_crosslink(spec, state): + # skip + return + + beacon_state = transition_to_valid_shard_slot(spec, state) + shard = 0 + shard_state = beacon_state.shard_states[shard] + signed_shard_block = build_shard_block(spec, beacon_state, shard, slot=beacon_state.slot, body=b'', signed=True) + + yield from run_shard_blocks(spec, shard_state, signed_shard_block, beacon_state, valid=False) + + # # verify_shard_block_signature # @@ -157,3 +217,54 @@ def test_invalid_signature(spec, state): signed_shard_block = build_shard_block(spec, beacon_state, shard, slot=beacon_state.slot, signed=False) yield from run_shard_blocks(spec, shard_state, signed_shard_block, beacon_state, valid=False) + + +# +# Other cases +# + + +@with_all_phases_except([PHASE0]) +@spec_state_test +@always_bls +def test_max_offset(spec, state): + if not is_full_crosslink(spec, state): + # skip + return + + beacon_state = transition_to_valid_shard_slot(spec, state) + shard = 0 + slot = beacon_state.shard_states[shard].slot + spec.SHARD_BLOCK_OFFSETS[spec.MAX_SHARD_BLOCKS_PER_ATTESTATION - 1] + transition_to(spec, beacon_state, slot) + + shard_state = beacon_state.shard_states[shard] + signed_shard_block = build_shard_block(spec, beacon_state, shard, slot=beacon_state.slot, signed=True) + + yield from run_shard_blocks(spec, shard_state, signed_shard_block, beacon_state) + + +@with_all_phases_except([PHASE0]) +@spec_state_test +@always_bls +def test_pending_shard_parent_block(spec, state): + if not is_full_crosslink(spec, state): + # skip + return + + # Block N + beacon_state = transition_to_valid_shard_slot(spec, state) + shard = 0 + shard_state = beacon_state.shard_states[shard] + signed_shard_block_1 = build_shard_block(spec, beacon_state, shard, slot=beacon_state.slot, signed=True) + _, _, _, _ = run_shard_blocks(spec, shard_state, signed_shard_block_1, beacon_state) + + # Block N+1 + next_slot(spec, beacon_state) + signed_shard_block_2 = build_shard_block( + spec, beacon_state, shard, + slot=beacon_state.slot, shard_parent_state=shard_state, signed=True + ) + + assert signed_shard_block_2.message.shard_parent_root == shard_state.latest_block_root + assert signed_shard_block_2.message.slot == signed_shard_block_1.message.slot + 1 + yield from run_shard_blocks(spec, shard_state, signed_shard_block_2, beacon_state)