From ec9f8f15ed7c11c637f4ea7addd8f764093a9457 Mon Sep 17 00:00:00 2001 From: Carl Beekhuizen Date: Fri, 31 May 2019 11:32:53 +0200 Subject: [PATCH] Adds Custody Tests --- ...est_process_early_derived_secret_reveal.py | 122 ++++++++++++++++++ .../pyspec/eth2spec/test/helpers/custody.py | 38 ++++++ 2 files changed, 160 insertions(+) create mode 100644 test_libs/pyspec/eth2spec/test/block_processing/test_process_early_derived_secret_reveal.py create mode 100644 test_libs/pyspec/eth2spec/test/helpers/custody.py diff --git a/test_libs/pyspec/eth2spec/test/block_processing/test_process_early_derived_secret_reveal.py b/test_libs/pyspec/eth2spec/test/block_processing/test_process_early_derived_secret_reveal.py new file mode 100644 index 000000000..0f9057bb6 --- /dev/null +++ b/test_libs/pyspec/eth2spec/test/block_processing/test_process_early_derived_secret_reveal.py @@ -0,0 +1,122 @@ +from eth2spec.test.helpers.custody import get_valid_early_derived_secret_reveal +from eth2spec.test.context import with_phase1, spec_state_test, expect_assertion_error + + +def run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, valid=True): + """ + Run ``process_randao_key_reveal``, yielding: + - pre-state ('pre') + - randao_key_reveal ('randao_key_reveal') + - post-state ('post'). + If ``valid == False``, run expecting ``AssertionError`` + """ + yield 'pre', state + yield 'randao_key_reveal', randao_key_reveal + + if not valid: + expect_assertion_error(lambda: spec.process_early_derived_secret_reveal(state, randao_key_reveal)) + yield 'post', None + return + + spec.process_early_derived_secret_reveal(state, randao_key_reveal) + + slashed_validator = state.validator_registry[randao_key_reveal.revealed_index] + + if randao_key_reveal.epoch >= spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING: + assert slashed_validator.slashed + assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH + assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH + # lost whistleblower reward + # FIXME: Currently broken because get_base_reward in genesis epoch is 0 + assert ( + state.balances[randao_key_reveal.revealed_index] < + state.balances[randao_key_reveal.revealed_index] + ) + yield 'post', state + + +@with_phase1 +@spec_state_test +def test_success(spec, state): + randao_key_reveal = get_valid_early_derived_secret_reveal(spec, state) + + yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal) + + +@with_phase1 +@spec_state_test +def test_reveal_from_current_epoch(spec, state): + randao_key_reveal = get_valid_early_derived_secret_reveal(spec, state, spec.get_current_epoch(state)) + + yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, False) + + +# @with_phase1 +# @spec_state_test +# def test_reveal_from_past_epoch(state): +# randao_key_reveal = get_valid_early_derived_secret_reveal(spec, state, spec.get_current_epoch(state) - 1) +# +# yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, False) + + +@with_phase1 +@spec_state_test +def test_reveal_with_custody_padding(spec, state): + randao_key_reveal = get_valid_early_derived_secret_reveal( + spec, + state, + spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING, + ) + yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, True) + + +@with_phase1 +@spec_state_test +def test_reveal_with_custody_padding_minus_one(spec, state): + randao_key_reveal = get_valid_early_derived_secret_reveal( + spec, + state, + spec.get_current_epoch(state) + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING - 1, + ) + yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, True) + + +# @with_phase1 +# @spec_state_test +# def test_double_reveal(spec, state): +# randao_key_reveal1 = get_valid_early_derived_secret_reveal( +# spec, +# state, +# spec.get_current_epoch(state) + spec.RANDAO_PENALTY_EPOCHS + 1, +# ) +# pre_state, intermediate_state = run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal1) + +# randao_key_reveal2 = get_valid_early_derived_secret_reveal( +# spec, +# intermediate_state, +# spec.get_current_epoch(pre_state) + spec.RANDAO_PENALTY_EPOCHS + 1, +# ) +# _, post_state = run_early_derived_secret_reveal_processing(spec, intermediate_state, randao_key_reveal2, False) + +# return pre_state, [randao_key_reveal1, randao_key_reveal2], post_state + + +@with_phase1 +@spec_state_test +def test_revealer_is_slashed(spec, state): + randao_key_reveal = get_valid_early_derived_secret_reveal(spec, state, spec.get_current_epoch(state)) + state.validator_registry[randao_key_reveal.revealed_index].slashed = True + + yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, False) + + +@with_phase1 +@spec_state_test +def test_far_future_epoch(spec, state): + randao_key_reveal = get_valid_early_derived_secret_reveal( + spec, + state, + spec.get_current_epoch(state) + spec.EARLY_DERIVED_SECRET_PENALTY_MAX_FUTURE_EPOCHS, + ) + + yield from run_early_derived_secret_reveal_processing(spec, state, randao_key_reveal, False) diff --git a/test_libs/pyspec/eth2spec/test/helpers/custody.py b/test_libs/pyspec/eth2spec/test/helpers/custody.py new file mode 100644 index 000000000..67df12fcd --- /dev/null +++ b/test_libs/pyspec/eth2spec/test/helpers/custody.py @@ -0,0 +1,38 @@ +from eth2spec.test.helpers.keys import privkeys +from eth2spec.utils.bls import bls_sign + + +def get_valid_early_derived_secret_reveal(spec, state, epoch=None): + current_epoch = spec.get_current_epoch(state) + revealed_index = spec.get_active_validator_indices(state, current_epoch)[-1] + masker_index = spec.get_active_validator_indices(state, current_epoch)[0] + + if epoch is None: + epoch = current_epoch + spec.CUSTODY_PERIOD_TO_RANDAO_PADDING + + reveal = bls_sign( + message_hash=spec.hash_tree_root(epoch), + privkey=privkeys[revealed_index], + domain=spec.get_domain( + state=state, + domain_type=spec.DOMAIN_RANDAO, + message_epoch=epoch, + ), + ) + mask = bls_sign( + message_hash=spec.hash_tree_root(epoch), + privkey=privkeys[masker_index], + domain=spec.get_domain( + state=state, + domain_type=spec.DOMAIN_RANDAO, + message_epoch=epoch, + ), + ) + + return spec.EarlyDerivedSecretReveal( + revealed_index=revealed_index, + epoch=epoch, + reveal=reveal, + masker_index=masker_index, + mask=mask, + )