prysm-pulse/beacon-chain/core/altair/reward.go
Shane Bammel de93ae97b8 Fix overflow in reward calculations
Overflow occurs when using updated issuance parameters.
2024-04-05 15:40:48 -05:00

70 lines
2.7 KiB
Go

package altair
import (
"math/big"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
)
// BaseReward takes state and validator index and calculate
// individual validator's base reward.
//
// Spec code:
//
// def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei:
// """
// Return the base reward for the validator defined by ``index`` with respect to the current ``state``.
//
// Note: An optimally performing validator can earn one base reward per epoch over a long time horizon.
// This takes into account both per-epoch (e.g. attestation) and intermittent duties (e.g. block proposal
// and sync committees).
// """
// increments = state.validators[index].effective_balance // EFFECTIVE_BALANCE_INCREMENT
// return Gwei(increments * get_base_reward_per_increment(state))
func BaseReward(s state.ReadOnlyBeaconState, index primitives.ValidatorIndex) (uint64, error) {
totalBalance, err := helpers.TotalActiveBalance(s)
if err != nil {
return 0, errors.Wrap(err, "could not calculate active balance")
}
return BaseRewardWithTotalBalance(s, index, totalBalance)
}
// BaseRewardWithTotalBalance calculates the base reward with the provided total balance.
func BaseRewardWithTotalBalance(s state.ReadOnlyBeaconState, index primitives.ValidatorIndex, totalBalance *big.Int) (uint64, error) {
val, err := s.ValidatorAtIndexReadOnly(index)
if err != nil {
return 0, err
}
cfg := params.BeaconConfig()
increments := val.EffectiveBalance() / cfg.EffectiveBalanceIncrement
baseRewardPerInc, err := BaseRewardPerIncrement(totalBalance)
if err != nil {
return 0, err
}
return increments * baseRewardPerInc, nil
}
// BaseRewardPerIncrement of the beacon state
//
// Spec code:
// def get_base_reward_per_increment(state: BeaconState) -> Gwei:
//
// return Gwei(EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR // integer_squareroot(get_total_active_balance(state)))
func BaseRewardPerIncrement(activeBalance *big.Int) (uint64, error) {
if activeBalance.Cmp(big.NewInt(0)) == 0 {
return 0, errors.New("active balance can't be 0")
}
cfg := params.BeaconConfig()
// baseRewardsPerIncrement = EffectiveBalanceIncrement * BaseRewardFactor / sqrt(activeBalance)
baseRewardsPerIncrement := new(big.Int).SetUint64(cfg.EffectiveBalanceIncrement)
baseRewardsPerIncrement.Mul(baseRewardsPerIncrement, new(big.Int).SetUint64(cfg.BaseRewardFactor))
baseRewardsPerIncrement.Div(baseRewardsPerIncrement, new(big.Int).Sqrt(activeBalance))
return baseRewardsPerIncrement.Uint64(), nil
}