ensure balance differential as a sanity check

This commit is contained in:
Alex Stokes 2021-09-01 15:46:40 -07:00
parent 58c0da9059
commit df89763777
No known key found for this signature in database
GPG Key ID: 99B3D88FD6C55A69
2 changed files with 15 additions and 0 deletions

View File

@ -155,3 +155,15 @@ def ensure_state_has_validators_across_lifecycle(spec, state):
has_slashed = any(filter(lambda v: v.slashed, state.validators)) has_slashed = any(filter(lambda v: v.slashed, state.validators))
return has_pending and has_active and has_exited and has_slashed return has_pending and has_active and has_exited and has_slashed
def has_active_balance_differential(spec, state):
"""
Ensure there is a difference between the total balance of
all _active_ validators and _all_ validators.
"""
epoch = spec.get_current_epoch(state)
active_indices = spec.get_active_validator_indices(state, epoch)
active_balance = spec.get_total_balance(state, set(active_indices))
total_balance = spec.get_total_balance(state, set(range(len(state.validators))))
return active_balance // spec.EFFECTIVE_BALANCE_INCREMENT != total_balance // spec.EFFECTIVE_BALANCE_INCREMENT

View File

@ -10,6 +10,7 @@ from eth2spec.test.context import (
) )
import eth2spec.test.helpers.rewards as rewards_helpers import eth2spec.test.helpers.rewards as rewards_helpers
from eth2spec.test.helpers.random import randomize_state, patch_state_to_non_leaking from eth2spec.test.helpers.random import randomize_state, patch_state_to_non_leaking
from eth2spec.test.helpers.state import has_active_balance_differential
from eth2spec.test.helpers.voluntary_exits import get_unslashed_exited_validators from eth2spec.test.helpers.voluntary_exits import get_unslashed_exited_validators
@ -48,6 +49,7 @@ def test_full_random_4(spec, state):
assert spec.is_in_inactivity_leak(state) assert spec.is_in_inactivity_leak(state)
target_validators = get_unslashed_exited_validators(spec, state) target_validators = get_unslashed_exited_validators(spec, state)
assert len(target_validators) != 0 assert len(target_validators) != 0
assert has_active_balance_differential(spec, state)
yield from rewards_helpers.run_deltas(spec, state) yield from rewards_helpers.run_deltas(spec, state)
@ -85,4 +87,5 @@ def test_full_random_without_leak_0(spec, state):
assert not spec.is_in_inactivity_leak(state) assert not spec.is_in_inactivity_leak(state)
target_validators = get_unslashed_exited_validators(spec, state) target_validators = get_unslashed_exited_validators(spec, state)
assert len(target_validators) != 0 assert len(target_validators) != 0
assert has_active_balance_differential(spec, state)
yield from rewards_helpers.run_deltas(spec, state) yield from rewards_helpers.run_deltas(spec, state)