From 82d41db1b4d5ef8fa7f30e22246cc5647e0d9e24 Mon Sep 17 00:00:00 2001 From: Denis Bogdanas Date: Thu, 26 Sep 2019 22:55:58 +0300 Subject: [PATCH] Test case for get_beacon_proposer_index(), loop with multiple iterations. --- test_libs/pyspec/eth2spec/test/context.py | 18 +++++++++++++++++- .../pyspec/eth2spec/test/helpers/genesis.py | 6 +++--- .../test_process_attestation.py | 13 ++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/test_libs/pyspec/eth2spec/test/context.py b/test_libs/pyspec/eth2spec/test/context.py index 5a0ddb59d..05e3706e6 100644 --- a/test_libs/pyspec/eth2spec/test/context.py +++ b/test_libs/pyspec/eth2spec/test/context.py @@ -10,7 +10,19 @@ from .utils import vector_test, with_meta_tags def with_state(fn): def entry(*args, **kw): try: - kw['state'] = create_genesis_state(spec=kw['spec'], num_validators=spec_phase0.SLOTS_PER_EPOCH * 8) + kw['state'] = create_genesis_state(spec=kw['spec'], num_validators=spec_phase0.SLOTS_PER_EPOCH * 8, + validator_balance=spec_phase0.MAX_EFFECTIVE_BALANCE) + except KeyError: + raise TypeError('Spec decorator must come within state decorator to inject spec into state.') + return fn(*args, **kw) + return entry + + +def with_state_low_balance(fn): + def entry(*args, **kw): + try: + kw['state'] = create_genesis_state(spec=kw['spec'], num_validators=spec_phase0.SLOTS_PER_EPOCH * 8, + validator_balance=18 * 10**9) except KeyError: raise TypeError('Spec decorator must come within state decorator to inject spec into state.') return fn(*args, **kw) @@ -41,6 +53,10 @@ def spec_state_test(fn): return spec_test(with_state(fn)) +def spec_state_low_balance_test(fn): + return spec_test(with_state_low_balance(fn)) + + def expect_assertion_error(fn): bad = False try: diff --git a/test_libs/pyspec/eth2spec/test/helpers/genesis.py b/test_libs/pyspec/eth2spec/test/helpers/genesis.py index 11ab76b79..19fd65829 100644 --- a/test_libs/pyspec/eth2spec/test/helpers/genesis.py +++ b/test_libs/pyspec/eth2spec/test/helpers/genesis.py @@ -18,7 +18,7 @@ def build_mock_validator(spec, i: int, balance: int): ) -def create_genesis_state(spec, num_validators): +def create_genesis_state(spec, num_validators, validator_balance): deposit_root = b'\x42' * 32 state = spec.BeaconState( @@ -34,12 +34,12 @@ def create_genesis_state(spec, num_validators): # We "hack" in the initial validators, # as it is much faster than creating and processing genesis deposits for every single test case. - state.balances = [spec.MAX_EFFECTIVE_BALANCE] * num_validators + state.balances = [validator_balance] * num_validators state.validators = [build_mock_validator(spec, i, state.balances[i]) for i in range(num_validators)] # Process genesis activations for validator in state.validators: - if validator.effective_balance >= spec.MAX_EFFECTIVE_BALANCE: + if validator.effective_balance >= validator_balance: validator.activation_eligibility_epoch = spec.GENESIS_EPOCH validator.activation_epoch = spec.GENESIS_EPOCH diff --git a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py index 24ffd940b..a13db1815 100644 --- a/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py +++ b/test_libs/pyspec/eth2spec/test/phase_0/block_processing/test_process_attestation.py @@ -1,4 +1,5 @@ -from eth2spec.test.context import spec_state_test, expect_assertion_error, always_bls, with_all_phases, with_phases +from eth2spec.test.context import spec_state_test, spec_state_low_balance_test, expect_assertion_error, always_bls, \ + with_all_phases, with_phases from eth2spec.test.helpers.attestations import ( get_valid_attestation, sign_aggregate_attestation, @@ -56,6 +57,16 @@ def test_success(spec, state): yield from run_attestation_processing(spec, state, attestation) +@with_all_phases +@spec_state_low_balance_test +def test_success_multi_proposer_index_iterations(spec, state): + state.slot += spec.SLOTS_PER_EPOCH * 2 + attestation = get_valid_attestation(spec, state, signed=True) + state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY + + yield from run_attestation_processing(spec, state, attestation) + + @with_all_phases @spec_state_test def test_success_previous_epoch(spec, state):