From 01a69288b6dfa94f28f51c525e6869e41eb19164 Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 15 Jun 2020 15:33:05 -0600 Subject: [PATCH] custody 0.01 testing cleanup --- .../eth2spec/test/helpers/attestations.py | 18 +- .../pyspec/eth2spec/test/helpers/custody.py | 4 +- .../test_process_custody_slashing.py | 190 +++++------------- 3 files changed, 50 insertions(+), 162 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/helpers/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/attestations.py index b9c91eaa0..07e227022 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/attestations.py @@ -96,16 +96,7 @@ def build_attestation_data(spec, state, slot, index, shard_transition=None, on_t return attestation_data -def convert_to_valid_on_time_attestation(spec, state, attestation, shard_transition, - signed=False): - if signed: - sign_attestation(spec, state, attestation) - - return attestation - - -def get_valid_on_time_attestation(spec, state, slot=None, index=None, - shard_transition=None, signed=False): +def get_valid_on_time_attestation(spec, state, slot=None, index=None, shard_transition=None, signed=False): ''' Construct on-time attestation for next slot ''' @@ -172,13 +163,6 @@ def get_valid_attestation(spec, # fill the attestation with (optionally filtered) participants, and optionally sign it fill_aggregate_attestation(spec, state, attestation, signed=signed, filter_participant_set=filter_participant_set) - if spec.fork == PHASE1 and on_time: - attestation = convert_to_valid_on_time_attestation( - spec, state, attestation, - shard_transition, - signed=signed, - ) - return attestation diff --git a/tests/core/pyspec/eth2spec/test/helpers/custody.py b/tests/core/pyspec/eth2spec/test/helpers/custody.py index edd7ac19f..4f91e6dc5 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/custody.py +++ b/tests/core/pyspec/eth2spec/test/helpers/custody.py @@ -194,6 +194,6 @@ def get_custody_slashable_shard_transition(spec, start_slot, block_lengths, cust shard_transition = get_shard_transition(spec, start_slot, block_lengths) slashable_test_vector = get_custody_slashable_test_vector(spec, custody_secret, block_lengths[0], slashable=slashable) - shard_transition.shard_data_roots[0] = ByteList[spec.MAX_SHARD_BLOCK_SIZE](slashable_test_vector) \ - .get_backing().get_left().merkle_root() + block_data = ByteList[spec.MAX_SHARD_BLOCK_SIZE](slashable_test_vector) + shard_transition.shard_data_roots[0] = block_data.get_backing().get_left().merkle_root() return shard_transition, slashable_test_vector diff --git a/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_custody_slashing.py b/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_custody_slashing.py index d1649484e..ec0bac82d 100644 --- a/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_custody_slashing.py +++ b/tests/core/pyspec/eth2spec/test/phase_1/block_processing/test_process_custody_slashing.py @@ -53,21 +53,36 @@ def run_custody_slashing_processing(spec, state, custody_slashing, valid=True, c yield 'post', state -@with_all_phases_except(['phase0']) -@spec_state_test -def test_custody_slashing(spec, state): - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH) - shard = 0 - offset_slots = spec.get_offset_slots(state, shard) +def run_standard_custody_slashing_test(spec, + state, + shard_lateness=None, + shard=None, + validator_index=None, + block_lengths=None, + slashing_message_data=None, + correct=True, + valid=True): + if shard_lateness is None: + shard_lateness = spec.SLOTS_PER_EPOCH + transition_to(spec, state, state.slot + shard_lateness) + + if shard is None: + shard = 0 + if validator_index is None: + validator_index = spec.get_beacon_committee(state, state.slot, shard)[0] + + offset_slots = spec.get_offset_slots(state, shard) + if block_lengths is None: + block_lengths = [2**15 // 3] * len(offset_slots) - validator_index = spec.get_beacon_committee( - state, - state.slot, - shard, - )[0] custody_secret = get_custody_secret(spec, state, validator_index) - shard_transition, slashable_test_vector = get_custody_slashable_shard_transition(spec, state.slot, - [2**15 // 3] * len(offset_slots), custody_secret) + shard_transition, slashable_test_vector = get_custody_slashable_shard_transition( + spec, + state.slot, + block_lengths, + custody_secret, + slashable=correct, + ) attestation = get_valid_on_time_attestation(spec, state, index=shard, signed=True, shard_transition=shard_transition) @@ -80,154 +95,43 @@ def test_custody_slashing(spec, state): slashing = get_valid_custody_slashing(spec, state, attestation, shard_transition, custody_secret, slashable_test_vector) - yield from run_custody_slashing_processing(spec, state, slashing, correct=True) + + if slashing_message_data is not None: + slashing.message.data = slashing_message_data + + yield from run_custody_slashing_processing(spec, state, slashing, valid=valid, correct=correct) + + +@with_all_phases_except(['phase0']) +@spec_state_test +def test_custody_slashing(spec, state): + yield from run_standard_custody_slashing_test(spec, state) @with_all_phases_except(['phase0']) @spec_state_test def test_incorrect_custody_slashing(spec, state): - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH) - shard = 0 - offset_slots = spec.get_offset_slots(state, shard) - - validator_index = spec.get_beacon_committee( - state, - state.slot, - shard, - )[0] - custody_secret = get_custody_secret(spec, state, validator_index) - shard_transition, slashable_test_vector = get_custody_slashable_shard_transition(spec, state.slot, - [2**15 // 3] * len(offset_slots), custody_secret, slashable=False) - - attestation = get_valid_on_time_attestation(spec, state, index=shard, signed=True, - shard_transition=shard_transition) - - transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) - - _, _, _ = run_attestation_processing(spec, state, attestation) - - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * (spec.EPOCHS_PER_CUSTODY_PERIOD - 1)) - - slashing = get_valid_custody_slashing(spec, state, attestation, - shard_transition, custody_secret, slashable_test_vector) - - yield from run_custody_slashing_processing(spec, state, slashing, correct=False) + yield from run_standard_custody_slashing_test(spec, state, correct=False) @with_all_phases_except(['phase0']) @spec_state_test def test_multiple_epochs_custody(spec, state): - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 3) - shard = 0 - offset_slots = spec.get_offset_slots(state, shard) - - validator_index = spec.get_beacon_committee( - state, - state.slot, - shard, - )[0] - custody_secret = get_custody_secret(spec, state, validator_index) - shard_transition, slashable_test_vector = get_custody_slashable_shard_transition(spec, state.slot, - [2**15 // 3] * len(offset_slots), custody_secret) - - attestation = get_valid_on_time_attestation(spec, state, index=shard, signed=True, - shard_transition=shard_transition) - - transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) - - _, _, _ = run_attestation_processing(spec, state, attestation) - - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * (spec.EPOCHS_PER_CUSTODY_PERIOD - 1)) - - slashing = get_valid_custody_slashing(spec, state, attestation, shard_transition, - custody_secret, slashable_test_vector) - yield from run_custody_slashing_processing(spec, state, slashing, correct=True) + yield from run_standard_custody_slashing_test(spec, state, shard_lateness=spec.SLOTS_PER_EPOCH * 3) @with_all_phases_except(['phase0']) @spec_state_test def test_many_epochs_custody(spec, state): - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * 10) - shard = 0 - offset_slots = spec.get_offset_slots(state, shard) - - validator_index = spec.get_beacon_committee( - state, - state.slot, - shard, - )[0] - custody_secret = get_custody_secret(spec, state, validator_index) - shard_transition, slashable_test_vector = get_custody_slashable_shard_transition(spec, state.slot, - [2**15 // 3] * len(offset_slots), custody_secret) - - attestation = get_valid_on_time_attestation(spec, state, index=shard, signed=True, - shard_transition=shard_transition) - - transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) - - _, _, _ = run_attestation_processing(spec, state, attestation) - - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * (spec.EPOCHS_PER_CUSTODY_PERIOD - 1)) - - slashing = get_valid_custody_slashing(spec, state, attestation, shard_transition, - custody_secret, slashable_test_vector) - yield from run_custody_slashing_processing(spec, state, slashing, correct=True) - - -@with_all_phases_except(['phase0']) -@spec_state_test -def test_off_chain_attestation(spec, state): - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH) - shard = 0 - offset_slots = spec.get_offset_slots(state, shard) - - validator_index = spec.get_beacon_committee( - state, - state.slot, - shard, - )[0] - custody_secret = get_custody_secret(spec, state, validator_index) - shard_transition, slashable_test_vector = get_custody_slashable_shard_transition(spec, state.slot, - [2**15 // 3] * len(offset_slots), custody_secret) - - attestation = get_valid_on_time_attestation(spec, state, index=shard, signed=True, - shard_transition=shard_transition) - - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * (spec.EPOCHS_PER_CUSTODY_PERIOD - 1)) - - slashing = get_valid_custody_slashing(spec, state, attestation, shard_transition, - custody_secret, slashable_test_vector) - yield from run_custody_slashing_processing(spec, state, slashing, correct=True) + yield from run_standard_custody_slashing_test(spec, state, shard_lateness=spec.SLOTS_PER_EPOCH * 10) @with_all_phases_except(['phase0']) @spec_state_test def test_invalid_custody_slashing(spec, state): - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH) - shard = 0 - offset_slots = spec.get_offset_slots(state, shard) - - validator_index = spec.get_beacon_committee( + yield from run_standard_custody_slashing_test( + spec, state, - state.slot, - shard, - )[0] - custody_secret = get_custody_secret(spec, state, validator_index) - shard_transition, slashable_test_vector = get_custody_slashable_shard_transition(spec, state.slot, - [2**15 // 3] * len(offset_slots), custody_secret) - - attestation = get_valid_on_time_attestation(spec, state, index=shard, signed=True, - shard_transition=shard_transition) - - transition_to(spec, state, state.slot + spec.MIN_ATTESTATION_INCLUSION_DELAY) - - _, _, _ = run_attestation_processing(spec, state, attestation) - - transition_to(spec, state, state.slot + spec.SLOTS_PER_EPOCH * (spec.EPOCHS_PER_CUSTODY_PERIOD - 1)) - - slashing = get_valid_custody_slashing(spec, state, attestation, shard_transition, - custody_secret, slashable_test_vector) - - slashing.message.data = ByteList[spec.MAX_SHARD_BLOCK_SIZE]() - - yield from run_custody_slashing_processing(spec, state, slashing, valid=False) + slashing_message_data=ByteList[spec.MAX_SHARD_BLOCK_SIZE](), + valid=False, + )