173 lines
5.3 KiB
Python
Raw Normal View History

2019-03-19 15:49:01 -06:00
import pytest
2019-04-03 14:18:17 +11:00
import eth2spec.phase0.spec as spec
2019-03-19 15:49:01 -06:00
2019-04-03 14:18:17 +11:00
from eth2spec.phase0.spec import (
2019-03-19 15:49:01 -06:00
get_active_validator_indices,
2019-04-14 17:52:50 +10:00
get_churn_limit,
2019-03-19 15:49:01 -06:00
get_current_epoch,
process_voluntary_exit,
)
2019-04-17 17:47:56 +10:00
from tests.helpers import (
2019-03-19 15:49:01 -06:00
build_voluntary_exit,
2019-03-25 11:25:33 -06:00
pubkey_to_privkey,
2019-03-19 15:49:01 -06:00
)
2019-05-06 17:10:43 +02:00
from tests.context import spec_state_test
2019-04-14 09:13:53 +10:00
def run_voluntary_exit_processing(state, voluntary_exit, valid=True):
"""
2019-05-06 16:51:46 +02:00
Run ``process_voluntary_exit``, yielding:
- pre-state ('pre')
- voluntary_exit ('voluntary_exit')
- post-state ('post').
2019-04-14 09:13:53 +10:00
If ``valid == False``, run expecting ``AssertionError``
"""
2019-05-06 16:51:46 +02:00
validator_index = voluntary_exit.validator_index
pre_exit_epoch = state.validator_registry[validator_index].exit_epoch
yield 'pre', state
yield 'voluntary_exit', voluntary_exit
2019-04-14 09:13:53 +10:00
if not valid:
with pytest.raises(AssertionError):
2019-05-06 16:51:46 +02:00
process_voluntary_exit(state, voluntary_exit)
yield 'post', None
return
2019-04-14 09:13:53 +10:00
2019-05-06 16:51:46 +02:00
process_voluntary_exit(state, voluntary_exit)
2019-04-14 09:13:53 +10:00
2019-05-06 16:51:46 +02:00
yield 'post', state
2019-04-14 09:13:53 +10:00
2019-05-06 16:51:46 +02:00
assert pre_exit_epoch == spec.FAR_FUTURE_EPOCH
assert state.validator_registry[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH
2019-04-14 09:13:53 +10:00
2019-05-06 16:51:46 +02:00
@spec_state_test
2019-03-25 11:25:33 -06:00
def test_success(state):
2019-03-19 15:49:01 -06:00
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit
2019-04-14 09:13:53 +10:00
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
2019-03-19 15:49:01 -06:00
2019-04-14 09:13:53 +10:00
current_epoch = get_current_epoch(state)
2019-04-14 17:52:50 +10:00
validator_index = get_active_validator_indices(state, current_epoch)[0]
2019-04-14 09:13:53 +10:00
privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
2019-03-19 15:49:01 -06:00
voluntary_exit = build_voluntary_exit(
2019-04-14 09:13:53 +10:00
state,
2019-03-19 15:49:01 -06:00
current_epoch,
validator_index,
privkey,
)
2019-05-06 16:51:46 +02:00
yield from run_voluntary_exit_processing(state, voluntary_exit)
2019-03-19 15:49:01 -06:00
2019-05-06 16:51:46 +02:00
@spec_state_test
2019-04-14 09:13:53 +10:00
def test_success_exit_queue(state):
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
current_epoch = get_current_epoch(state)
# exit `MAX_EXITS_PER_EPOCH`
2019-04-14 17:52:50 +10:00
initial_indices = get_active_validator_indices(state, current_epoch)[:get_churn_limit(state)]
2019-05-06 16:51:46 +02:00
# Prepare a bunch of exits, based on the current state
exit_queue = []
2019-04-14 09:13:53 +10:00
for index in initial_indices:
privkey = pubkey_to_privkey[state.validator_registry[index].pubkey]
2019-05-06 16:51:46 +02:00
exit_queue.append(build_voluntary_exit(
2019-04-14 09:13:53 +10:00
state,
current_epoch,
index,
privkey,
2019-05-06 16:51:46 +02:00
))
2019-04-14 09:13:53 +10:00
2019-05-06 16:51:46 +02:00
# Now run all the exits
for voluntary_exit in exit_queue:
# the function yields data, but we are just interested in running it here, ignore yields.
for _ in run_voluntary_exit_processing(state, voluntary_exit):
continue
2019-04-14 09:13:53 +10:00
# exit an additional validator
2019-04-14 17:52:50 +10:00
validator_index = get_active_validator_indices(state, current_epoch)[-1]
2019-04-14 09:13:53 +10:00
privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
2019-03-19 15:49:01 -06:00
voluntary_exit = build_voluntary_exit(
2019-04-14 09:13:53 +10:00
state,
2019-03-19 15:49:01 -06:00
current_epoch,
validator_index,
privkey,
)
2019-05-06 16:51:46 +02:00
# This is the interesting part of the test: on a pre-state with a full exit queue,
# when processing an additional exit, it results in an exit in a later epoch
yield from run_voluntary_exit_processing(state, voluntary_exit)
2019-03-19 15:49:01 -06:00
2019-04-14 09:13:53 +10:00
assert (
2019-05-06 16:51:46 +02:00
state.validator_registry[validator_index].exit_epoch ==
state.validator_registry[initial_indices[0]].exit_epoch + 1
2019-04-14 09:13:53 +10:00
)
2019-03-19 15:49:01 -06:00
2019-05-06 16:51:46 +02:00
@spec_state_test
2019-03-25 11:25:33 -06:00
def test_validator_not_active(state):
2019-04-14 09:13:53 +10:00
current_epoch = get_current_epoch(state)
2019-04-14 17:52:50 +10:00
validator_index = get_active_validator_indices(state, current_epoch)[0]
2019-04-14 09:13:53 +10:00
privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
2019-03-19 15:49:01 -06:00
2019-04-14 09:13:53 +10:00
state.validator_registry[validator_index].activation_epoch = spec.FAR_FUTURE_EPOCH
2019-03-19 15:49:01 -06:00
# build and test voluntary exit
voluntary_exit = build_voluntary_exit(
2019-04-14 09:13:53 +10:00
state,
2019-03-19 15:49:01 -06:00
current_epoch,
validator_index,
privkey,
)
2019-05-06 16:51:46 +02:00
yield from run_voluntary_exit_processing(state, voluntary_exit, False)
2019-03-19 15:49:01 -06:00
2019-05-06 16:51:46 +02:00
@spec_state_test
2019-03-25 11:25:33 -06:00
def test_validator_already_exited(state):
2019-03-19 15:49:01 -06:00
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow validator able to exit
2019-04-14 09:13:53 +10:00
state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
2019-03-19 15:49:01 -06:00
2019-04-14 09:13:53 +10:00
current_epoch = get_current_epoch(state)
2019-04-14 17:52:50 +10:00
validator_index = get_active_validator_indices(state, current_epoch)[0]
2019-04-14 09:13:53 +10:00
privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
2019-03-19 15:49:01 -06:00
# but validator already has exited
2019-04-14 09:13:53 +10:00
state.validator_registry[validator_index].exit_epoch = current_epoch + 2
2019-03-19 15:49:01 -06:00
voluntary_exit = build_voluntary_exit(
2019-04-14 09:13:53 +10:00
state,
2019-03-19 15:49:01 -06:00
current_epoch,
validator_index,
privkey,
)
2019-05-06 16:51:46 +02:00
yield from run_voluntary_exit_processing(state, voluntary_exit, False)
2019-03-19 15:49:01 -06:00
2019-05-06 16:51:46 +02:00
@spec_state_test
2019-03-25 11:25:33 -06:00
def test_validator_not_active_long_enough(state):
2019-04-14 09:13:53 +10:00
current_epoch = get_current_epoch(state)
2019-04-14 17:52:50 +10:00
validator_index = get_active_validator_indices(state, current_epoch)[0]
2019-04-14 09:13:53 +10:00
privkey = pubkey_to_privkey[state.validator_registry[validator_index].pubkey]
2019-03-19 15:49:01 -06:00
voluntary_exit = build_voluntary_exit(
2019-04-14 09:13:53 +10:00
state,
2019-03-19 15:49:01 -06:00
current_epoch,
validator_index,
privkey,
)
assert (
2019-04-14 09:13:53 +10:00
current_epoch - state.validator_registry[validator_index].activation_epoch <
2019-03-19 15:49:01 -06:00
spec.PERSISTENT_COMMITTEE_PERIOD
)
2019-05-06 16:51:46 +02:00
yield from run_voluntary_exit_processing(state, voluntary_exit, False)