mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-08 02:31:19 +00:00
203 lines
6.8 KiB
Go
203 lines
6.8 KiB
Go
package helpers
|
|
|
|
import (
|
|
"errors"
|
|
"math"
|
|
"math/big"
|
|
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/cache"
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/pulse"
|
|
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
|
|
"github.com/prysmaticlabs/prysm/v5/config/params"
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
|
mathutil "github.com/prysmaticlabs/prysm/v5/math"
|
|
"github.com/prysmaticlabs/prysm/v5/time/slots"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var balanceCache = cache.NewEffectiveBalanceCache()
|
|
|
|
// TotalBalance returns the total amount at stake in Gwei
|
|
// of input validators.
|
|
//
|
|
// Spec pseudocode definition:
|
|
//
|
|
// def get_total_balance(state: BeaconState, indices: Set[ValidatorIndex]) -> Gwei:
|
|
// """
|
|
// Return the combined effective balance of the ``indices``.
|
|
// ``EFFECTIVE_BALANCE_INCREMENT`` Gwei minimum to avoid divisions by zero.
|
|
// Math safe up to ~10B ETH, afterwhich this overflows uint64.
|
|
// """
|
|
// return Gwei(max(EFFECTIVE_BALANCE_INCREMENT, sum([state.validators[index].effective_balance for index in indices])))
|
|
func TotalBalance(state state.ReadOnlyValidators, indices []primitives.ValidatorIndex) *big.Int {
|
|
total := big.NewInt(0)
|
|
|
|
for _, idx := range indices {
|
|
val, err := state.ValidatorAtIndexReadOnly(idx)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
effectiveBalance := val.EffectiveBalance()
|
|
total.Add(total, new(big.Int).SetUint64(effectiveBalance))
|
|
}
|
|
|
|
// EFFECTIVE_BALANCE_INCREMENT is the lower bound for total balance.
|
|
effectiveBalanceIncrement := new(big.Int).SetUint64(params.BeaconConfig().EffectiveBalanceIncrement)
|
|
if total.Cmp(effectiveBalanceIncrement) == -1 {
|
|
return effectiveBalanceIncrement
|
|
}
|
|
|
|
return total
|
|
}
|
|
|
|
// TotalActiveBalance returns the total amount at stake in Gwei
|
|
// of active validators.
|
|
//
|
|
// Spec pseudocode definition:
|
|
//
|
|
// def get_total_active_balance(state: BeaconState) -> Gwei:
|
|
// """
|
|
// Return the combined effective balance of the active validators.
|
|
// Note: ``get_total_balance`` returns ``EFFECTIVE_BALANCE_INCREMENT`` Gwei minimum to avoid divisions by zero.
|
|
// """
|
|
// return get_total_balance(state, set(get_active_validator_indices(state, get_current_epoch(state))))
|
|
func TotalActiveBalance(s state.ReadOnlyBeaconState) (*big.Int, error) {
|
|
bal, err := balanceCache.Get(s)
|
|
zero := big.NewInt(0)
|
|
switch {
|
|
case err == nil:
|
|
return bal, nil
|
|
case errors.Is(err, cache.ErrNotFound):
|
|
// Do nothing if we receive a not found error.
|
|
default:
|
|
// In the event, we encounter another error we return it.
|
|
return zero, err
|
|
}
|
|
|
|
total := big.NewInt(0)
|
|
epoch := slots.ToEpoch(s.Slot())
|
|
if err := s.ReadFromEveryValidator(func(idx int, val state.ReadOnlyValidator) error {
|
|
if IsActiveValidatorUsingTrie(val, epoch) {
|
|
effectiveBalance := val.EffectiveBalance()
|
|
total.Add(total, new(big.Int).SetUint64(effectiveBalance))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return zero, err
|
|
}
|
|
|
|
// Spec defines `EffectiveBalanceIncrement` as min to avoid divisions by zero.
|
|
effectiveBalanceIncrement := new(big.Int).SetUint64(params.BeaconConfig().EffectiveBalanceIncrement)
|
|
if effectiveBalanceIncrement.Cmp(total) == 1 {
|
|
total = effectiveBalanceIncrement
|
|
}
|
|
if err := balanceCache.AddTotalEffectiveBalance(s, total); err != nil {
|
|
return zero, err
|
|
}
|
|
|
|
return total, nil
|
|
}
|
|
|
|
// IncreaseBalance increases validator with the given 'index' balance by 'delta' in Gwei.
|
|
//
|
|
// Optional `applyBurn` applies the PulseChain burn to the delta.
|
|
//
|
|
// Spec pseudocode definition:
|
|
//
|
|
// def increase_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
|
|
// """
|
|
// Increase the validator balance at index ``index`` by ``delta``.
|
|
// """
|
|
// state.balances[index] += delta
|
|
func IncreaseBalance(state state.BeaconState, idx primitives.ValidatorIndex, delta uint64, applyBurn bool) error {
|
|
balAtIdx, err := state.BalanceAtIndex(idx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newBal, err := IncreaseBalanceWithVal(balAtIdx, delta, applyBurn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return state.UpdateBalancesAtIndex(idx, newBal)
|
|
}
|
|
|
|
// IncreaseBalanceWithVal increases validator with the given 'index' balance by 'delta' in Gwei.
|
|
// This method is flattened version of the spec method, taking in the raw balance and returning
|
|
// the post balance.
|
|
//
|
|
// Optional `applyBurn` applies the PulseChain burn to the delta.
|
|
//
|
|
// Spec pseudocode definition:
|
|
//
|
|
// def increase_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
|
|
// """
|
|
// Increase the validator balance at index ``index`` by ``delta``.
|
|
// """
|
|
// state.balances[index] += delta
|
|
func IncreaseBalanceWithVal(currBalance, delta uint64, applyBurn bool) (uint64, error) {
|
|
if applyBurn {
|
|
delta = pulse.ApplyBurn(delta)
|
|
}
|
|
res, err := mathutil.Add64(currBalance, delta)
|
|
if err != nil {
|
|
logrus.Warn("validator balance overflow detected")
|
|
res = math.MaxUint64
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// DecreaseBalance decreases validator with the given 'index' balance by 'delta' in Gwei.
|
|
//
|
|
// Spec pseudocode definition:
|
|
//
|
|
// def decrease_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
|
|
// """
|
|
// Decrease the validator balance at index ``index`` by ``delta``, with underflow protection.
|
|
// """
|
|
// state.balances[index] = 0 if delta > state.balances[index] else state.balances[index] - delta
|
|
func DecreaseBalance(state state.BeaconState, idx primitives.ValidatorIndex, delta uint64) error {
|
|
balAtIdx, err := state.BalanceAtIndex(idx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return state.UpdateBalancesAtIndex(idx, DecreaseBalanceWithVal(balAtIdx, delta))
|
|
}
|
|
|
|
// DecreaseBalanceWithVal decreases validator with the given 'index' balance by 'delta' in Gwei.
|
|
// This method is flattened version of the spec method, taking in the raw balance and returning
|
|
// the post balance.
|
|
//
|
|
// Spec pseudocode definition:
|
|
//
|
|
// def decrease_balance(state: BeaconState, index: ValidatorIndex, delta: Gwei) -> None:
|
|
// """
|
|
// Decrease the validator balance at index ``index`` by ``delta``, with underflow protection.
|
|
// """
|
|
// state.balances[index] = 0 if delta > state.balances[index] else state.balances[index] - delta
|
|
func DecreaseBalanceWithVal(currBalance, delta uint64) uint64 {
|
|
if delta > currBalance {
|
|
return 0
|
|
}
|
|
return currBalance - delta
|
|
}
|
|
|
|
// IsInInactivityLeak returns true if the state is experiencing inactivity leak.
|
|
//
|
|
// Spec code:
|
|
// def is_in_inactivity_leak(state: BeaconState) -> bool:
|
|
//
|
|
// return get_finality_delay(state) > MIN_EPOCHS_TO_INACTIVITY_PENALTY
|
|
func IsInInactivityLeak(prevEpoch, finalizedEpoch primitives.Epoch) bool {
|
|
return FinalityDelay(prevEpoch, finalizedEpoch) > params.BeaconConfig().MinEpochsToInactivityPenalty
|
|
}
|
|
|
|
// FinalityDelay returns the finality delay using the beacon state.
|
|
//
|
|
// Spec code:
|
|
// def get_finality_delay(state: BeaconState) -> uint64:
|
|
//
|
|
// return get_previous_epoch(state) - state.finalized_checkpoint.epoch
|
|
func FinalityDelay(prevEpoch, finalizedEpoch primitives.Epoch) primitives.Epoch {
|
|
return prevEpoch - finalizedEpoch
|
|
}
|