avoid overflow in slashing penalty calculation (#1286)
Change presentation to avoid uint64 overflow in slashing penalty calculation. (Factor out `EFFECTIVE_BALANCE_INCREMENT` from `validator.effective_balance`.)
This commit is contained in:
parent
57c0eee197
commit
6dc306700b
|
@ -1482,7 +1482,9 @@ def process_slashings(state: BeaconState) -> None:
|
|||
total_balance = get_total_active_balance(state)
|
||||
for index, validator in enumerate(state.validators):
|
||||
if validator.slashed and epoch + EPOCHS_PER_SLASHINGS_VECTOR // 2 == validator.withdrawable_epoch:
|
||||
penalty = validator.effective_balance * min(sum(state.slashings) * 3, total_balance) // total_balance
|
||||
increment = EFFECTIVE_BALANCE_INCREMENT # Factored out from penalty numerator to avoid uint64 overflow
|
||||
penalty_numerator = validator.effective_balance // increment * min(sum(state.slashings) * 3, total_balance)
|
||||
penalty = penalty_numerator // total_balance * increment
|
||||
decrease_balance(state, ValidatorIndex(index), penalty)
|
||||
```
|
||||
|
||||
|
|
|
@ -66,8 +66,13 @@ def test_small_penalty(spec, state):
|
|||
spec.process_slashings(state)
|
||||
yield 'post', state
|
||||
|
||||
assert state.balances[0] == pre_slash_balances[0] - (state.validators[0].effective_balance
|
||||
* 3 * total_penalties // total_balance)
|
||||
expected_penalty = (
|
||||
state.validators[0].effective_balance // spec.EFFECTIVE_BALANCE_INCREMENT
|
||||
* (3 * total_penalties)
|
||||
// total_balance
|
||||
* spec.EFFECTIVE_BALANCE_INCREMENT
|
||||
)
|
||||
assert state.balances[0] == pre_slash_balances[0] - expected_penalty
|
||||
|
||||
|
||||
@with_all_phases
|
||||
|
@ -121,5 +126,10 @@ def test_scaled_penalties(spec, state):
|
|||
|
||||
for i in slashed_indices:
|
||||
v = state.validators[i]
|
||||
penalty = v.effective_balance * total_penalties * 3 // total_balance
|
||||
assert state.balances[i] == pre_slash_balances[i] - penalty
|
||||
expected_penalty = (
|
||||
v.effective_balance // spec.EFFECTIVE_BALANCE_INCREMENT
|
||||
* (3 * total_penalties)
|
||||
// (total_balance)
|
||||
* spec.EFFECTIVE_BALANCE_INCREMENT
|
||||
)
|
||||
assert state.balances[i] == pre_slash_balances[i] - expected_penalty
|
||||
|
|
Loading…
Reference in New Issue