Skip to content

Commit

Permalink
Merge pull request #1821 from ethereum/rewards-with-inactive-vals
Browse files Browse the repository at this point in the history
Rewards with not yet activated validators
  • Loading branch information
djrtwo committed May 18, 2020
2 parents c536729 + 96b5733 commit 5da4fe3
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 9 deletions.
11 changes: 11 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/deposits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
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):
deposit_data = spec.DepositData(
pubkey=pubkey,
Expand Down
23 changes: 22 additions & 1 deletion tests/core/pyspec/eth2spec/test/helpers/rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from eth2spec.phase0 import spec as spec_phase0
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.utils.ssz.ssz_typing import Container, uint64, List

Expand Down Expand Up @@ -61,6 +62,17 @@ def run_attestation_component_deltas(spec, state, component_delta_fn, matching_a
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):
if spec.get_current_epoch(state) < 5:
# Move epochs forward to allow for some validators already exited/withdrawable
Expand All @@ -69,10 +81,11 @@ def exit_random_validators(spec, state, rng):

current_epoch = spec.get_current_epoch(state)
# 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]):
continue

validator = state.validators[index]
validator.exit_epoch = rng.choice([current_epoch - 1, current_epoch - 2, current_epoch - 3])
# ~1/2 are withdrawable
if rng.choice([True, False]):
Expand Down Expand Up @@ -133,6 +146,13 @@ def run_test_one_attestation_one_correct(spec, state, runner):
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)):
exit_random_validators(spec, state, rng)
prepare_state_with_attestations(spec, state)
Expand Down Expand Up @@ -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)):
set_some_new_deposits(spec, state, rng)
exit_random_validators(spec, state, rng)
slash_random_validators(spec, state, rng)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.context import spec_state_test, with_all_phases
from eth2spec.test.phase_0.epoch_processing.run_epoch_process_base import run_epoch_processing_with
Expand All @@ -7,14 +8,6 @@ def run_process_registry_updates(spec, state):
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
@spec_state_test
def test_add_to_activation_queue(spec, state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)


@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
@spec_state_test
def test_with_exited_validators(spec, state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)


@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
@spec_state_test
def test_with_exited_validators_no_leak(spec, state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)


@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
@spec_state_test
def test_with_exited_validators(spec, state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)


@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
@spec_state_test
def test_with_exited_validators(spec, state):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)


@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
@spec_state_test
def test_with_slashed_validators(spec, state):
Expand Down

0 comments on commit 5da4fe3

Please sign in to comment.