-
Notifications
You must be signed in to change notification settings - Fork 933
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #807 from ethereum/JustinDrake-patch-20
Avoid underflow in voluntary exits
- Loading branch information
Showing
7 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
from copy import deepcopy | ||
import pytest | ||
|
||
import build.phase0.spec as spec | ||
|
||
from build.phase0.spec import ( | ||
get_active_validator_indices, | ||
get_current_epoch, | ||
process_voluntary_exit, | ||
) | ||
from tests.phase0.helpers import ( | ||
build_voluntary_exit, | ||
) | ||
|
||
|
||
def test_success(state, pub_to_priv): | ||
pre_state = deepcopy(state) | ||
# | ||
# setup pre_state | ||
# | ||
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow for exit | ||
pre_state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH | ||
|
||
# | ||
# build voluntary exit | ||
# | ||
current_epoch = get_current_epoch(pre_state) | ||
validator_index = get_active_validator_indices(pre_state.validator_registry, current_epoch)[0] | ||
privkey = pub_to_priv[pre_state.validator_registry[validator_index].pubkey] | ||
|
||
voluntary_exit = build_voluntary_exit( | ||
pre_state, | ||
current_epoch, | ||
validator_index, | ||
privkey, | ||
) | ||
|
||
post_state = deepcopy(pre_state) | ||
|
||
# | ||
# test valid exit | ||
# | ||
process_voluntary_exit(post_state, voluntary_exit) | ||
|
||
assert not pre_state.validator_registry[validator_index].initiated_exit | ||
assert post_state.validator_registry[validator_index].initiated_exit | ||
|
||
return pre_state, voluntary_exit, post_state | ||
|
||
|
||
def test_validator_not_active(state, pub_to_priv): | ||
pre_state = deepcopy(state) | ||
current_epoch = get_current_epoch(pre_state) | ||
validator_index = get_active_validator_indices(pre_state.validator_registry, current_epoch)[0] | ||
privkey = pub_to_priv[pre_state.validator_registry[validator_index].pubkey] | ||
|
||
# | ||
# setup pre_state | ||
# | ||
pre_state.validator_registry[validator_index].activation_epoch = spec.FAR_FUTURE_EPOCH | ||
|
||
# | ||
# build and test voluntary exit | ||
# | ||
voluntary_exit = build_voluntary_exit( | ||
pre_state, | ||
current_epoch, | ||
validator_index, | ||
privkey, | ||
) | ||
|
||
with pytest.raises(AssertionError): | ||
process_voluntary_exit(pre_state, voluntary_exit) | ||
|
||
return pre_state, voluntary_exit, None | ||
|
||
|
||
def test_validator_already_exited(state, pub_to_priv): | ||
pre_state = deepcopy(state) | ||
# | ||
# setup pre_state | ||
# | ||
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow validator able to exit | ||
pre_state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH | ||
|
||
current_epoch = get_current_epoch(pre_state) | ||
validator_index = get_active_validator_indices(pre_state.validator_registry, current_epoch)[0] | ||
privkey = pub_to_priv[pre_state.validator_registry[validator_index].pubkey] | ||
|
||
# but validator already has exited | ||
pre_state.validator_registry[validator_index].exit_epoch = current_epoch + 2 | ||
|
||
# | ||
# build voluntary exit | ||
# | ||
voluntary_exit = build_voluntary_exit( | ||
pre_state, | ||
current_epoch, | ||
validator_index, | ||
privkey, | ||
) | ||
|
||
with pytest.raises(AssertionError): | ||
process_voluntary_exit(pre_state, voluntary_exit) | ||
|
||
return pre_state, voluntary_exit, None | ||
|
||
|
||
def test_validator_already_initiated_exit(state, pub_to_priv): | ||
pre_state = deepcopy(state) | ||
# | ||
# setup pre_state | ||
# | ||
# move state forward PERSISTENT_COMMITTEE_PERIOD epochs to allow validator able to exit | ||
pre_state.slot += spec.PERSISTENT_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH | ||
|
||
current_epoch = get_current_epoch(pre_state) | ||
validator_index = get_active_validator_indices(pre_state.validator_registry, current_epoch)[0] | ||
privkey = pub_to_priv[pre_state.validator_registry[validator_index].pubkey] | ||
|
||
# but validator already has initiated exit | ||
pre_state.validator_registry[validator_index].initiated_exit = True | ||
|
||
# | ||
# build voluntary exit | ||
# | ||
voluntary_exit = build_voluntary_exit( | ||
pre_state, | ||
current_epoch, | ||
validator_index, | ||
privkey, | ||
) | ||
|
||
with pytest.raises(AssertionError): | ||
process_voluntary_exit(pre_state, voluntary_exit) | ||
|
||
return pre_state, voluntary_exit, None | ||
|
||
|
||
def test_validator_not_active_long_enough(state, pub_to_priv): | ||
pre_state = deepcopy(state) | ||
# | ||
# setup pre_state | ||
# | ||
current_epoch = get_current_epoch(pre_state) | ||
validator_index = get_active_validator_indices(pre_state.validator_registry, current_epoch)[0] | ||
privkey = pub_to_priv[pre_state.validator_registry[validator_index].pubkey] | ||
|
||
# but validator already has initiated exit | ||
pre_state.validator_registry[validator_index].initiated_exit = True | ||
|
||
# | ||
# build voluntary exit | ||
# | ||
voluntary_exit = build_voluntary_exit( | ||
pre_state, | ||
current_epoch, | ||
validator_index, | ||
privkey, | ||
) | ||
|
||
assert ( | ||
current_epoch - pre_state.validator_registry[validator_index].activation_epoch < | ||
spec.PERSISTENT_COMMITTEE_PERIOD | ||
) | ||
|
||
with pytest.raises(AssertionError): | ||
process_voluntary_exit(pre_state, voluntary_exit) | ||
|
||
return pre_state, voluntary_exit, None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters