From 49a291909936cec4653db407f8e53ca09631f136 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 30 Sep 2019 12:58:05 +0900 Subject: [PATCH] add more shard block sanity tests --- .../test/helpers/phase1/shard_block.py | 6 +- .../test/helpers/phase1/shard_state.py | 18 ++ .../test/phase_1/sanity/test_shard_blocks.py | 170 ++++++++++++++++++ .../shard_data_chain/test_shard_block.py | 62 ------- 4 files changed, 191 insertions(+), 65 deletions(-) create mode 100644 test_libs/pyspec/eth2spec/test/helpers/phase1/shard_state.py create mode 100644 test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py delete mode 100644 test_libs/pyspec/eth2spec/test/phase_1/shard_data_chain/test_shard_block.py diff --git a/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py b/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py index 3ceb3e0aa..ea5783655 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py +++ b/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_block.py @@ -15,9 +15,9 @@ from .attestations import ( @only_with_bls() -def sign_shard_block(spec, beacon_state, block, shard, proposer_index=None): +def sign_shard_block(spec, beacon_state, shard_state, block, proposer_index=None): if proposer_index is None: - proposer_index = spec.get_shard_proposer_index(beacon_state, shard, block.slot) + proposer_index = spec.get_shard_proposer_index(beacon_state, shard_state.shard, block.slot) privkey = privkeys[proposer_index] @@ -75,6 +75,6 @@ def build_empty_shard_block(spec, ) if signed: - sign_shard_block(spec, beacon_state, block, shard_state.shard) + sign_shard_block(spec, beacon_state, shard_state, block) return block diff --git a/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_state.py b/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_state.py new file mode 100644 index 000000000..24240b5fa --- /dev/null +++ b/test_libs/pyspec/eth2spec/test/helpers/phase1/shard_state.py @@ -0,0 +1,18 @@ +from eth2spec.test.helpers.phase1.shard_block import sign_shard_block + + +def configure_shard_state(spec, beacon_state, shard=0): + beacon_state.slot = spec.Slot(spec.SHARD_GENESIS_EPOCH * spec.SLOTS_PER_EPOCH) + shard_state = spec.get_genesis_shard_state(spec.Shard(shard)) + shard_state.slot = spec.ShardSlot(spec.SHARD_GENESIS_EPOCH * spec.SHARD_SLOTS_PER_EPOCH) + return beacon_state, shard_state + + +def shard_state_transition_and_sign_block(spec, beacon_state, shard_state, block): + """ + Shard state transition via the provided ``block`` + then package the block with the state root and signature. + """ + spec.shard_state_transition(beacon_state, shard_state, block) + block.state_root = shard_state.hash_tree_root() + sign_shard_block(spec, beacon_state, shard_state, block) diff --git a/test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py b/test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py new file mode 100644 index 000000000..2669ffb53 --- /dev/null +++ b/test_libs/pyspec/eth2spec/test/phase_1/sanity/test_shard_blocks.py @@ -0,0 +1,170 @@ +from copy import deepcopy + +from eth2spec.test.helpers.phase1.shard_block import ( + build_empty_shard_block, + sign_shard_block, +) +from eth2spec.test.helpers.phase1.shard_state import ( + configure_shard_state, + shard_state_transition_and_sign_block, +) +from eth2spec.test.context import ( + always_bls, + expect_assertion_error, + spec_state_test, + with_all_phases_except, +) + + +@with_all_phases_except(['phase0']) +@spec_state_test +@always_bls +def test_process_empty_shard_block(spec, state): + beacon_state, shard_state = configure_shard_state(spec, state) + + block = build_empty_shard_block( + spec, + beacon_state, + shard_state, + slot=shard_state.slot + 1, + signed=True, + full_attestation=False, + ) + + yield 'pre', shard_state + yield 'beacon_state', beacon_state + + shard_state_transition_and_sign_block(spec, beacon_state, shard_state, block) + + yield 'blocks', [block] + yield 'post', shard_state + + +@with_all_phases_except(['phase0']) +@spec_state_test +@always_bls +def test_process_full_attestation_shard_block(spec, state): + beacon_state, shard_state = configure_shard_state(spec, state) + + block = build_empty_shard_block( + spec, + beacon_state, + shard_state, + slot=shard_state.slot + 1, + signed=True, + full_attestation=True, + ) + + yield 'pre', shard_state + yield 'beacon_state', beacon_state + + shard_state_transition_and_sign_block(spec, beacon_state, shard_state, block) + + yield 'blocks', [block] + yield 'post', shard_state + + +@with_all_phases_except(['phase0']) +@spec_state_test +def test_prev_slot_block_transition(spec, state): + beacon_state, shard_state = configure_shard_state(spec, state) + + # Go to clean slot + spec.process_shard_slots(shard_state, shard_state.slot + 1) + # Make a block for it + block = build_empty_shard_block(spec, beacon_state, shard_state, slot=shard_state.slot, signed=True) + # Transition to next slot, above block will not be invalid on top of new state. + spec.process_shard_slots(shard_state, shard_state.slot + 1) + + yield 'pre', shard_state + yield 'beacon_state', beacon_state + expect_assertion_error( + lambda: spec.shard_state_transition(beacon_state, shard_state, block) + ) + yield 'blocks', [block] + yield 'post', None + + +@with_all_phases_except(['phase0']) +@spec_state_test +def test_same_slot_block_transition(spec, state): + beacon_state, shard_state = configure_shard_state(spec, state) + + # Same slot on top of pre-state, but move out of slot 0 first. + spec.process_shard_slots(shard_state, shard_state.slot + 1) + block = build_empty_shard_block(spec, beacon_state, shard_state, slot=shard_state.slot, signed=True) + + yield 'pre', shard_state + yield 'beacon_state', beacon_state + + shard_state_transition_and_sign_block(spec, beacon_state, shard_state, block) + + yield 'blocks', [block] + yield 'post', shard_state + + +@with_all_phases_except(['phase0']) +@spec_state_test +def test_invalid_state_root(spec, state): + beacon_state, shard_state = configure_shard_state(spec, state) + + spec.process_shard_slots(shard_state, shard_state.slot + 1) + block = build_empty_shard_block(spec, beacon_state, shard_state, slot=shard_state.slot) + block.state_root = b'\x36' * 32 + sign_shard_block(spec, beacon_state, shard_state, block) + + yield 'pre', shard_state + yield 'beacon_state', beacon_state + expect_assertion_error( + lambda: spec.shard_state_transition(beacon_state, shard_state, block, validate_state_root=True) + ) + yield 'blocks', [block] + yield 'post', None + + +@with_all_phases_except(['phase0']) +@spec_state_test +def test_skipped_slots(spec, state): + beacon_state, shard_state = configure_shard_state(spec, state) + + block = build_empty_shard_block(spec, beacon_state, shard_state, slot=shard_state.slot + 3, signed=True) + + yield 'pre', shard_state + yield 'beacon_state', beacon_state + + shard_state_transition_and_sign_block(spec, beacon_state, shard_state, block) + + yield 'blocks', [block] + yield 'post', shard_state + + assert shard_state.slot == block.slot + latest_block_header = deepcopy(shard_state.latest_block_header) + latest_block_header.state_root = shard_state.hash_tree_root() + assert latest_block_header.signing_root() == block.signing_root() + + +@with_all_phases_except(['phase0']) +@spec_state_test +def test_empty_shard_period_transition(spec, state): + beacon_state, shard_state = configure_shard_state(spec, state) + + # modify some of the deltas to ensure the period transition works properly + stub_delta = 10 + shard_state.newer_committee_positive_deltas[0] = stub_delta + shard_state.newer_committee_negative_deltas[0] = stub_delta + + slot = shard_state.slot + spec.SHARD_SLOTS_PER_EPOCH * spec.EPOCHS_PER_SHARD_PERIOD + block = build_empty_shard_block(spec, beacon_state, shard_state, slot=slot, signed=True) + + yield 'pre', shard_state + yield 'beacon_state', beacon_state + + shard_state_transition_and_sign_block(spec, beacon_state, shard_state, block) + + yield 'blocks', [block] + yield 'post', shard_state + + shard_state.older_committee_positive_deltas[0] == stub_delta + shard_state.older_committee_negative_deltas[0] == stub_delta + shard_state.newer_committee_positive_deltas[0] == 0 + shard_state.newer_committee_negative_deltas[0] == 0 diff --git a/test_libs/pyspec/eth2spec/test/phase_1/shard_data_chain/test_shard_block.py b/test_libs/pyspec/eth2spec/test/phase_1/shard_data_chain/test_shard_block.py deleted file mode 100644 index b0d8ad5e6..000000000 --- a/test_libs/pyspec/eth2spec/test/phase_1/shard_data_chain/test_shard_block.py +++ /dev/null @@ -1,62 +0,0 @@ -from eth2spec.test.helpers.phase1.shard_block import ( - build_empty_shard_block, -) -from eth2spec.test.context import ( - with_all_phases_except, - spec_state_test, - always_bls, -) - - -@with_all_phases_except(['phase0']) -@spec_state_test -@always_bls -def test_process_empty_shard_block(spec, state): - beacon_state = state - beacon_state.slot = spec.Slot(spec.SHARD_GENESIS_EPOCH * spec.SLOTS_PER_EPOCH) - shard_state = spec.get_genesis_shard_state(spec.Shard(0)) - shard_state.slot = spec.ShardSlot(spec.SHARD_GENESIS_EPOCH * spec.SHARD_SLOTS_PER_EPOCH) - - block = build_empty_shard_block( - spec, - beacon_state, - shard_state, - slot=shard_state.slot + 1, - signed=True, - full_attestation=False, - ) - - yield 'pre', shard_state - yield 'beacon_state', beacon_state - yield 'block', block - - spec.shard_state_transition(beacon_state, shard_state, block) - - yield 'post', shard_state - - -@with_all_phases_except(['phase0']) -@spec_state_test -@always_bls -def test_process_full_attestation_shard_block(spec, state): - beacon_state = state - beacon_state.slot = spec.Slot(spec.SHARD_GENESIS_EPOCH * spec.SLOTS_PER_EPOCH) - shard_state = spec.get_genesis_shard_state(spec.Shard(0)) - shard_state.slot = spec.SHARD_GENESIS_EPOCH * spec.SHARD_SLOTS_PER_EPOCH - - block = build_empty_shard_block( - spec, - beacon_state, - shard_state, - slot=shard_state.slot + 1, - signed=True, - full_attestation=True, - ) - - yield 'pre', shard_state - yield 'beacon_state', beacon_state - yield 'block', block - - spec.shard_state_transition(beacon_state, shard_state, block) - - yield 'post', shard_state