Test case for get_beacon_proposer_index(), loop with multiple iterations.

This commit is contained in:
Denis Bogdanas 2019-09-26 22:55:58 +03:00
parent f47e023bf0
commit 82d41db1b4
3 changed files with 32 additions and 5 deletions

View File

@ -10,7 +10,19 @@ from .utils import vector_test, with_meta_tags
def with_state(fn): def with_state(fn):
def entry(*args, **kw): def entry(*args, **kw):
try: 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: except KeyError:
raise TypeError('Spec decorator must come within state decorator to inject spec into state.') raise TypeError('Spec decorator must come within state decorator to inject spec into state.')
return fn(*args, **kw) return fn(*args, **kw)
@ -41,6 +53,10 @@ def spec_state_test(fn):
return spec_test(with_state(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): def expect_assertion_error(fn):
bad = False bad = False
try: try:

View File

@ -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 deposit_root = b'\x42' * 32
state = spec.BeaconState( state = spec.BeaconState(
@ -34,12 +34,12 @@ def create_genesis_state(spec, num_validators):
# We "hack" in the initial validators, # We "hack" in the initial validators,
# as it is much faster than creating and processing genesis deposits for every single test case. # 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)] state.validators = [build_mock_validator(spec, i, state.balances[i]) for i in range(num_validators)]
# Process genesis activations # Process genesis activations
for validator in state.validators: 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_eligibility_epoch = spec.GENESIS_EPOCH
validator.activation_epoch = spec.GENESIS_EPOCH validator.activation_epoch = spec.GENESIS_EPOCH

View File

@ -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 ( from eth2spec.test.helpers.attestations import (
get_valid_attestation, get_valid_attestation,
sign_aggregate_attestation, sign_aggregate_attestation,
@ -56,6 +57,16 @@ def test_success(spec, state):
yield from run_attestation_processing(spec, state, attestation) 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 @with_all_phases
@spec_state_test @spec_state_test
def test_success_previous_epoch(spec, state): def test_success_previous_epoch(spec, state):