update attester slashings processing tests

This commit is contained in:
protolambda 2019-06-29 03:19:57 +02:00
parent 518db42de7
commit efd9d173d7
No known key found for this signature in database
GPG Key ID: EC89FDBB2B4C7623
1 changed files with 31 additions and 27 deletions

View File

@ -17,8 +17,8 @@ def slash_validators(spec, state, indices, out_epochs):
v.withdrawable_epoch = out_epoch
total_slashed_balance += v.effective_balance
state.slashed_balances[
spec.get_current_epoch(state) % spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR
state.slashings[
spec.get_current_epoch(state) % spec.EPOCHS_PER_SLASHINGS_VECTOR
] = total_slashed_balance
@ -26,13 +26,13 @@ def slash_validators(spec, state, indices, out_epochs):
@spec_state_test
def test_max_penalties(spec, state):
slashed_count = (len(state.validators) // 3) + 1
out_epoch = spec.get_current_epoch(state) + (spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR // 2)
out_epoch = spec.get_current_epoch(state) + (spec.EPOCHS_PER_SLASHINGS_VECTOR // 2)
slashed_indices = list(range(slashed_count))
slash_validators(spec, state, slashed_indices, [out_epoch] * slashed_count)
total_balance = spec.get_total_active_balance(state)
total_penalties = state.slashed_balances[spec.get_current_epoch(state) % spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR]
total_penalties = sum(state.slashings)
assert total_balance // 3 <= total_penalties
@ -44,28 +44,30 @@ def test_max_penalties(spec, state):
@with_all_phases
@spec_state_test
def test_min_penalties(spec, state):
# run_epoch_processing_to(spec, state, 'process_slashings', exclusive=True)
def test_small_penalty(spec, state):
# Just the bare minimum for this one validator
pre_balance = state.balances[0] = state.validators[0].effective_balance = spec.EJECTION_BALANCE
state.balances[0] = state.validators[0].effective_balance = spec.EJECTION_BALANCE
# All the other validators get the maximum.
for i in range(1, len(state.validators)):
state.validators[i].effective_balance = state.balances[i] = spec.MAX_EFFECTIVE_BALANCE
out_epoch = spec.get_current_epoch(state) + (spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR // 2)
out_epoch = spec.get_current_epoch(state) + (spec.EPOCHS_PER_SLASHINGS_VECTOR // 2)
slash_validators(spec, state, [0], [out_epoch])
total_balance = spec.get_total_active_balance(state)
total_penalties = state.slashed_balances[spec.get_current_epoch(state) % spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR]
total_penalties = sum(state.slashings)
# we are testing the minimum here, i.e. get slashed (effective_balance / MIN_SLASHING_PENALTY_QUOTIENT)
assert total_penalties * 3 / total_balance < 1 / spec.MIN_SLASHING_PENALTY_QUOTIENT
assert total_balance // 3 > total_penalties
yield from run_process_slashings(spec, state)
run_epoch_processing_to(spec, state, 'process_slashings')
pre_slash_balances = list(state.balances)
yield 'pre', state
spec.process_slashings(state)
yield 'post', state
assert state.balances[0] == pre_balance - (pre_balance // spec.MIN_SLASHING_PENALTY_QUOTIENT)
assert state.balances[0] == pre_slash_balances[0] - (state.validators[0].effective_balance
* 3 * total_penalties // total_balance)
@with_all_phases
@ -75,14 +77,13 @@ def test_scaled_penalties(spec, state):
state.slot = spec.SLOTS_PER_EPOCH
# Also mock some previous slashings, so that we test to have the delta in the penalties computation.
for i in range(spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR):
state.slashed_balances[i] = spec.MAX_EFFECTIVE_BALANCE * 3
# Mock the very last one (which is to be used for the delta balance computation) to be different.
# To enforce the client test runner to correctly get this one from the array, not the others.
prev_penalties = state.slashed_balances[
(spec.get_current_epoch(state) + 1) % spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR
] = spec.MAX_EFFECTIVE_BALANCE * 2
base = spec.EJECTION_BALANCE
incr = spec.EFFECTIVE_BALANCE_INCREMENT
# Just add some random slashings. non-zero slashings are at least the minimal effective balance.
state.slashings[0] = base + (incr * 12)
state.slashings[4] = base + (incr * 3)
state.slashings[5] = base + (incr * 6)
state.slashings[spec.EPOCHS_PER_SLASHINGS_VECTOR - 1] = base + (incr * 7)
slashed_count = len(state.validators) // 4
@ -90,13 +91,17 @@ def test_scaled_penalties(spec, state):
# make the balances non-uniform.
# Otherwise it would just be a simple 3/4 balance slashing. Test the per-validator scaled penalties.
diff = spec.MAX_EFFECTIVE_BALANCE - base
increments = diff // incr
for i in range(10):
state.validators[i].effective_balance += spec.EFFECTIVE_BALANCE_INCREMENT * 4
state.balances[i] += spec.EFFECTIVE_BALANCE_INCREMENT * 4
state.validators[i].effective_balance = base + (incr * (i % increments))
assert state.validators[i].effective_balance <= spec.MAX_EFFECTIVE_BALANCE
# add/remove some, see if balances different than the effective balances are picked up
state.balances[i] = state.validators[i].effective_balance + i - 5
total_balance = spec.get_total_active_balance(state)
out_epoch = spec.get_current_epoch(state) + (spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR // 2)
out_epoch = spec.get_current_epoch(state) + (spec.EPOCHS_PER_SLASHINGS_VECTOR // 2)
slashed_indices = list(range(slashed_count))
@ -112,8 +117,7 @@ def test_scaled_penalties(spec, state):
spec.process_slashings(state)
yield 'post', state
total_penalties = state.slashed_balances[spec.get_current_epoch(state) % spec.EPOCHS_PER_SLASHED_BALANCES_VECTOR]
total_penalties -= prev_penalties
total_penalties = sum(state.slashings)
for i in slashed_indices:
v = state.validators[i]