Merge pull request #1821 from ethereum/rewards-with-inactive-vals

Rewards with not yet activated validators
This commit is contained in:
Danny Ryan 2020-05-18 14:13:27 -06:00 committed by GitHub
commit 5da4fe37f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 9 deletions

View File

@ -5,6 +5,17 @@ from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_typing import List from eth2spec.utils.ssz.ssz_typing import List
def mock_deposit(spec, state, index):
"""
Mock validator at ``index`` as having just made a deposit
"""
assert spec.is_active_validator(state.validators[index], spec.get_current_epoch(state))
state.validators[index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH
state.validators[index].activation_epoch = spec.FAR_FUTURE_EPOCH
state.validators[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE
assert not spec.is_active_validator(state.validators[index], spec.get_current_epoch(state))
def build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, signed=False): def build_deposit_data(spec, pubkey, privkey, amount, withdrawal_credentials, signed=False):
deposit_data = spec.DepositData( deposit_data = spec.DepositData(
pubkey=pubkey, pubkey=pubkey,

View File

@ -2,6 +2,7 @@ from random import Random
from eth2spec.phase0 import spec as spec_phase0 from eth2spec.phase0 import spec as spec_phase0
from eth2spec.test.helpers.attestations import prepare_state_with_attestations from eth2spec.test.helpers.attestations import prepare_state_with_attestations
from eth2spec.test.helpers.deposits import mock_deposit
from eth2spec.test.helpers.state import next_epoch from eth2spec.test.helpers.state import next_epoch
from eth2spec.utils.ssz.ssz_typing import Container, uint64, List from eth2spec.utils.ssz.ssz_typing import Container, uint64, List
@ -61,6 +62,17 @@ def run_attestation_component_deltas(spec, state, component_delta_fn, matching_a
assert penalties[index] == 0 assert penalties[index] == 0
def set_some_new_deposits(spec, state, rng):
num_validators = len(state.validators)
# Set ~1/10 to just recently deposited
for index in range(num_validators):
if rng.randrange(num_validators) < num_validators // 10:
mock_deposit(spec, state, index)
# Set ~half of selected to eligible for activation
if rng.choice([True, False]):
state.validators[index].activation_eligibility_epoch = spec.get_current_epoch(state)
def exit_random_validators(spec, state, rng): def exit_random_validators(spec, state, rng):
if spec.get_current_epoch(state) < 5: if spec.get_current_epoch(state) < 5:
# Move epochs forward to allow for some validators already exited/withdrawable # Move epochs forward to allow for some validators already exited/withdrawable
@ -69,10 +81,11 @@ def exit_random_validators(spec, state, rng):
current_epoch = spec.get_current_epoch(state) current_epoch = spec.get_current_epoch(state)
# Exit ~1/2 of validators # Exit ~1/2 of validators
for validator in state.validators: for index in spec.get_active_validator_indices(state, current_epoch):
if rng.choice([True, False]): if rng.choice([True, False]):
continue continue
validator = state.validators[index]
validator.exit_epoch = rng.choice([current_epoch - 1, current_epoch - 2, current_epoch - 3]) validator.exit_epoch = rng.choice([current_epoch - 1, current_epoch - 2, current_epoch - 3])
# ~1/2 are withdrawable # ~1/2 are withdrawable
if rng.choice([True, False]): if rng.choice([True, False]):
@ -133,6 +146,13 @@ def run_test_one_attestation_one_correct(spec, state, runner):
yield from runner(spec, state) yield from runner(spec, state)
def run_test_with_not_yet_activated_validators(spec, state, runner, rng=Random(5555)):
set_some_new_deposits(spec, state, rng)
prepare_state_with_attestations(spec, state)
yield from runner(spec, state)
def run_test_with_exited_validators(spec, state, runner, rng=Random(1337)): def run_test_with_exited_validators(spec, state, runner, rng=Random(1337)):
exit_random_validators(spec, state, rng) exit_random_validators(spec, state, rng)
prepare_state_with_attestations(spec, state) prepare_state_with_attestations(spec, state)
@ -190,6 +210,7 @@ def run_test_full_fraction_incorrect(spec, state, correct_target, correct_head,
def run_test_full_random(spec, state, runner, rng=Random(8020)): def run_test_full_random(spec, state, runner, rng=Random(8020)):
set_some_new_deposits(spec, state, rng)
exit_random_validators(spec, state, rng) exit_random_validators(spec, state, rng)
slash_random_validators(spec, state, rng) slash_random_validators(spec, state, rng)

View File

@ -1,3 +1,4 @@
from eth2spec.test.helpers.deposits import mock_deposit
from eth2spec.test.helpers.state import next_epoch, next_slots from eth2spec.test.helpers.state import next_epoch, next_slots
from eth2spec.test.context import spec_state_test, with_all_phases from eth2spec.test.context import spec_state_test, with_all_phases
from eth2spec.test.phase_0.epoch_processing.run_epoch_process_base import run_epoch_processing_with from eth2spec.test.phase_0.epoch_processing.run_epoch_process_base import run_epoch_processing_with
@ -7,14 +8,6 @@ def run_process_registry_updates(spec, state):
yield from run_epoch_processing_with(spec, state, 'process_registry_updates') yield from run_epoch_processing_with(spec, state, 'process_registry_updates')
def mock_deposit(spec, state, index):
assert spec.is_active_validator(state.validators[index], spec.get_current_epoch(state))
state.validators[index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH
state.validators[index].activation_epoch = spec.FAR_FUTURE_EPOCH
state.validators[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE
assert not spec.is_active_validator(state.validators[index], spec.get_current_epoch(state))
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_add_to_activation_queue(spec, state): def test_add_to_activation_queue(spec, state):

View File

@ -48,6 +48,12 @@ def test_one_attestation_one_correct(spec, state):
yield from rewards_helpers.run_test_one_attestation_one_correct(spec, state, run_get_head_deltas) yield from rewards_helpers.run_test_one_attestation_one_correct(spec, state, run_get_head_deltas)
@with_all_phases
@spec_state_test
def test_with_not_yet_activated_validators(spec, state):
yield from rewards_helpers.run_test_with_not_yet_activated_validators(spec, state, run_get_head_deltas)
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_with_exited_validators(spec, state): def test_with_exited_validators(spec, state):

View File

@ -115,6 +115,27 @@ def test_full_but_partial_participation_leak(spec, state):
yield from rewards_helpers.run_test_full_but_partial_participation(spec, state, run_get_inactivity_penalty_deltas) yield from rewards_helpers.run_test_full_but_partial_participation(spec, state, run_get_inactivity_penalty_deltas)
@with_all_phases
@spec_state_test
def test_with_not_yet_activated_validators_no_leak(spec, state):
yield from rewards_helpers.run_test_with_not_yet_activated_validators(
spec,
state,
run_get_inactivity_penalty_deltas,
)
@with_all_phases
@spec_state_test
def test_with_not_yet_activated_validators_leak(spec, state):
transition_state_to_leak(spec, state)
yield from rewards_helpers.run_test_with_not_yet_activated_validators(
spec,
state,
run_get_inactivity_penalty_deltas,
)
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_with_exited_validators_no_leak(spec, state): def test_with_exited_validators_no_leak(spec, state):

View File

@ -83,6 +83,12 @@ def test_full_but_partial_participation(spec, state):
yield from rewards_helpers.run_test_full_but_partial_participation(spec, state, run_get_inclusion_delay_deltas) yield from rewards_helpers.run_test_full_but_partial_participation(spec, state, run_get_inclusion_delay_deltas)
@with_all_phases
@spec_state_test
def test_with_not_yet_activated_validators(spec, state):
yield from rewards_helpers.run_test_with_not_yet_activated_validators(spec, state, run_get_inclusion_delay_deltas)
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_with_exited_validators(spec, state): def test_with_exited_validators(spec, state):

View File

@ -48,6 +48,12 @@ def test_one_attestation_one_correct(spec, state):
yield from rewards_helpers.run_test_one_attestation_one_correct(spec, state, run_get_source_deltas) yield from rewards_helpers.run_test_one_attestation_one_correct(spec, state, run_get_source_deltas)
@with_all_phases
@spec_state_test
def test_with_not_yet_activated_validators(spec, state):
yield from rewards_helpers.run_test_with_not_yet_activated_validators(spec, state, run_get_source_deltas)
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_with_exited_validators(spec, state): def test_with_exited_validators(spec, state):

View File

@ -48,6 +48,18 @@ def test_one_attestation_one_correct(spec, state):
yield from rewards_helpers.run_test_one_attestation_one_correct(spec, state, run_get_target_deltas) yield from rewards_helpers.run_test_one_attestation_one_correct(spec, state, run_get_target_deltas)
@with_all_phases
@spec_state_test
def test_with_not_yet_activated_validators(spec, state):
yield from rewards_helpers.run_test_with_not_yet_activated_validators(spec, state, run_get_target_deltas)
@with_all_phases
@spec_state_test
def test_with_exited_validators(spec, state):
yield from rewards_helpers.run_test_with_exited_validators(spec, state, run_get_target_deltas)
@with_all_phases @with_all_phases
@spec_state_test @spec_state_test
def test_with_slashed_validators(spec, state): def test_with_slashed_validators(spec, state):