mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-01-27 19:05:00 +00:00
update attester slashing testing
This commit is contained in:
parent
7bbf9ed3fc
commit
61c0ddbcbb
@ -1,4 +1,3 @@
|
|||||||
from copy import deepcopy
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import eth2spec.phase0.spec as spec
|
import eth2spec.phase0.spec as spec
|
||||||
@ -12,52 +11,69 @@ from tests.helpers import (
|
|||||||
next_epoch,
|
next_epoch,
|
||||||
)
|
)
|
||||||
|
|
||||||
# mark entire file as 'attester_slashing'
|
from tests.utils import spectest
|
||||||
pytestmark = pytest.mark.attester_slashings
|
from tests.context import with_state
|
||||||
|
|
||||||
|
from .block_test_helpers import spec_state_test
|
||||||
|
|
||||||
|
|
||||||
def run_attester_slashing_processing(state, attester_slashing, valid=True):
|
def run_attester_slashing_processing(state, attester_slashing, valid=True):
|
||||||
"""
|
"""
|
||||||
Run ``process_attester_slashing`` returning the pre and post state.
|
Run ``process_attester_slashing``, yielding:
|
||||||
|
- pre-state ('pre')
|
||||||
|
- attester_slashing ('attester_slashing')
|
||||||
|
- post-state ('post').
|
||||||
If ``valid == False``, run expecting ``AssertionError``
|
If ``valid == False``, run expecting ``AssertionError``
|
||||||
"""
|
"""
|
||||||
post_state = deepcopy(state)
|
|
||||||
|
yield 'pre', state
|
||||||
|
yield 'attester_slashing', attester_slashing
|
||||||
|
|
||||||
if not valid:
|
if not valid:
|
||||||
with pytest.raises(AssertionError):
|
with pytest.raises(AssertionError):
|
||||||
process_attester_slashing(post_state, attester_slashing)
|
process_attester_slashing(state, attester_slashing)
|
||||||
return state, None
|
yield 'post', None
|
||||||
|
return
|
||||||
process_attester_slashing(post_state, attester_slashing)
|
|
||||||
|
|
||||||
slashed_index = attester_slashing.attestation_1.custody_bit_0_indices[0]
|
slashed_index = attester_slashing.attestation_1.custody_bit_0_indices[0]
|
||||||
slashed_validator = post_state.validator_registry[slashed_index]
|
pre_slashed_balance = get_balance(state, slashed_index)
|
||||||
|
|
||||||
|
proposer_index = get_beacon_proposer_index(state)
|
||||||
|
pre_proposer_balance = get_balance(state, proposer_index)
|
||||||
|
|
||||||
|
# Process slashing
|
||||||
|
process_attester_slashing(state, attester_slashing)
|
||||||
|
|
||||||
|
slashed_validator = state.validator_registry[slashed_index]
|
||||||
|
|
||||||
|
# Check slashing
|
||||||
assert slashed_validator.slashed
|
assert slashed_validator.slashed
|
||||||
assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH
|
assert slashed_validator.exit_epoch < spec.FAR_FUTURE_EPOCH
|
||||||
assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH
|
assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH
|
||||||
|
|
||||||
# lost whistleblower reward
|
# lost whistleblower reward
|
||||||
assert (
|
assert (
|
||||||
get_balance(post_state, slashed_index) <
|
get_balance(state, slashed_index) <
|
||||||
get_balance(state, slashed_index)
|
pre_slashed_balance
|
||||||
)
|
)
|
||||||
proposer_index = get_beacon_proposer_index(state)
|
|
||||||
# gained whistleblower reward
|
# gained whistleblower reward
|
||||||
assert (
|
assert (
|
||||||
get_balance(post_state, proposer_index) >
|
get_balance(state, proposer_index) >
|
||||||
get_balance(state, proposer_index)
|
pre_proposer_balance
|
||||||
)
|
)
|
||||||
|
|
||||||
return state, post_state
|
yield 'post', state
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_success_double(state):
|
def test_success_double(state):
|
||||||
attester_slashing = get_valid_attester_slashing(state)
|
attester_slashing = get_valid_attester_slashing(state)
|
||||||
|
|
||||||
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing)
|
yield from run_attester_slashing_processing(state, attester_slashing)
|
||||||
|
|
||||||
return pre_state, attester_slashing, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_success_surround(state):
|
def test_success_surround(state):
|
||||||
next_epoch(state)
|
next_epoch(state)
|
||||||
state.current_justified_epoch += 1
|
state.current_justified_epoch += 1
|
||||||
@ -67,31 +83,28 @@ def test_success_surround(state):
|
|||||||
attester_slashing.attestation_1.data.source_epoch = attester_slashing.attestation_2.data.source_epoch - 1
|
attester_slashing.attestation_1.data.source_epoch = attester_slashing.attestation_2.data.source_epoch - 1
|
||||||
attester_slashing.attestation_1.data.target_epoch = attester_slashing.attestation_2.data.target_epoch + 1
|
attester_slashing.attestation_1.data.target_epoch = attester_slashing.attestation_2.data.target_epoch + 1
|
||||||
|
|
||||||
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing)
|
yield from run_attester_slashing_processing(state, attester_slashing)
|
||||||
|
|
||||||
return pre_state, attester_slashing, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_same_data(state):
|
def test_same_data(state):
|
||||||
attester_slashing = get_valid_attester_slashing(state)
|
attester_slashing = get_valid_attester_slashing(state)
|
||||||
|
|
||||||
attester_slashing.attestation_1.data = attester_slashing.attestation_2.data
|
attester_slashing.attestation_1.data = attester_slashing.attestation_2.data
|
||||||
|
|
||||||
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
|
yield from run_attester_slashing_processing(state, attester_slashing, False)
|
||||||
|
|
||||||
return pre_state, attester_slashing, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_no_double_or_surround(state):
|
def test_no_double_or_surround(state):
|
||||||
attester_slashing = get_valid_attester_slashing(state)
|
attester_slashing = get_valid_attester_slashing(state)
|
||||||
|
|
||||||
attester_slashing.attestation_1.data.target_epoch += 1
|
attester_slashing.attestation_1.data.target_epoch += 1
|
||||||
|
|
||||||
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
|
yield from run_attester_slashing_processing(state, attester_slashing, False)
|
||||||
|
|
||||||
return pre_state, attester_slashing, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_participants_already_slashed(state):
|
def test_participants_already_slashed(state):
|
||||||
attester_slashing = get_valid_attester_slashing(state)
|
attester_slashing = get_valid_attester_slashing(state)
|
||||||
|
|
||||||
@ -101,17 +114,15 @@ def test_participants_already_slashed(state):
|
|||||||
for index in validator_indices:
|
for index in validator_indices:
|
||||||
state.validator_registry[index].slashed = True
|
state.validator_registry[index].slashed = True
|
||||||
|
|
||||||
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
|
yield from run_attester_slashing_processing(state, attester_slashing, False)
|
||||||
|
|
||||||
return pre_state, attester_slashing, post_state
|
|
||||||
|
|
||||||
|
|
||||||
|
@spec_state_test
|
||||||
def test_custody_bit_0_and_1(state):
|
def test_custody_bit_0_and_1(state):
|
||||||
attester_slashing = get_valid_attester_slashing(state)
|
attester_slashing = get_valid_attester_slashing(state)
|
||||||
|
|
||||||
attester_slashing.attestation_1.custody_bit_1_indices = (
|
attester_slashing.attestation_1.custody_bit_1_indices = (
|
||||||
attester_slashing.attestation_1.custody_bit_0_indices
|
attester_slashing.attestation_1.custody_bit_0_indices
|
||||||
)
|
)
|
||||||
pre_state, post_state = run_attester_slashing_processing(state, attester_slashing, False)
|
|
||||||
|
|
||||||
return pre_state, attester_slashing, post_state
|
yield from run_attester_slashing_processing(state, attester_slashing, False)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user