Skip to content
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

Add more tests for shard_state_transition #1938

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 112 additions & 1 deletion tests/core/pyspec/eth2spec/test/phase1/sanity/test_shard_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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):
Expand Down Expand Up @@ -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)
hwwhww marked this conversation as resolved.
Show resolved Hide resolved

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
#
Expand All @@ -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)