Fix overflow bug

This commit is contained in:
Shane Bammel 2023-02-11 01:28:23 -06:00
parent 298143bca4
commit 3a6adf5c0d

View File

@ -306,39 +306,50 @@ func attestationDelta(
cfg := params.BeaconConfig() cfg := params.BeaconConfig()
increment := new(big.Int).SetUint64(cfg.EffectiveBalanceIncrement) increment := new(big.Int).SetUint64(cfg.EffectiveBalanceIncrement)
effectiveBalance := val.CurrentEpochEffectiveBalance effectiveBalance := val.CurrentEpochEffectiveBalance
baseReward := (effectiveBalance / cfg.EffectiveBalanceIncrement) * baseRewardMultiplier baseReward := new(big.Int).SetUint64((effectiveBalance / cfg.EffectiveBalanceIncrement) * baseRewardMultiplier)
activeIncrement := new(big.Int).Div(bal.ActiveCurrentEpoch, increment).Uint64()
weightDenominator := cfg.WeightDenominator weightDenominator := new(big.Int).SetUint64(cfg.WeightDenominator)
srcWeight := cfg.TimelySourceWeight srcWeight := new(big.Int).SetUint64(cfg.TimelySourceWeight)
tgtWeight := cfg.TimelyTargetWeight tgtWeight := new(big.Int).SetUint64(cfg.TimelyTargetWeight)
headWeight := cfg.TimelyHeadWeight headWeight := new(big.Int).SetUint64(cfg.TimelyHeadWeight)
attDelta := &AttDelta{} attDelta := &AttDelta{}
// rewardDenominator = activeIncrement * weightDenominator
rewardDenominator := new(big.Int).Div(bal.ActiveCurrentEpoch, increment)
rewardDenominator.Mul(rewardDenominator, weightDenominator)
// Process source reward / penalty // Process source reward / penalty
if val.IsPrevEpochSourceAttester && !val.IsSlashed { if val.IsPrevEpochSourceAttester && !val.IsSlashed {
if !inactivityLeak { if !inactivityLeak {
n := baseReward * srcWeight * (new(big.Int).Div(bal.PrevEpochAttested, increment)).Uint64() n := new(big.Int).Mul(baseReward, srcWeight)
attDelta.SourceReward += n / (activeIncrement * weightDenominator) n.Mul(n, new(big.Int).Div(bal.PrevEpochAttested, increment))
attDelta.SourceReward += n.Div(n, rewardDenominator).Uint64()
} }
} else { } else {
attDelta.SourcePenalty += baseReward * srcWeight / weightDenominator n := new(big.Int).Mul(baseReward, srcWeight)
n.Div(n, weightDenominator)
attDelta.SourcePenalty += n.Uint64()
} }
// Process target reward / penalty // Process target reward / penalty
if val.IsPrevEpochTargetAttester && !val.IsSlashed { if val.IsPrevEpochTargetAttester && !val.IsSlashed {
if !inactivityLeak { if !inactivityLeak {
n := baseReward * tgtWeight * (new(big.Int).Div(bal.PrevEpochTargetAttested, increment)).Uint64() n := new(big.Int).Mul(baseReward, tgtWeight)
attDelta.TargetReward += n / (activeIncrement * weightDenominator) n.Mul(n, new(big.Int).Div(bal.PrevEpochTargetAttested, increment))
attDelta.TargetReward += n.Div(n, rewardDenominator).Uint64()
} }
} else { } else {
attDelta.TargetPenalty += baseReward * tgtWeight / weightDenominator n := new(big.Int).Mul(baseReward, tgtWeight)
n.Div(n, weightDenominator)
attDelta.TargetPenalty += n.Uint64()
} }
// Process head reward / penalty // Process head reward / penalty
if val.IsPrevEpochHeadAttester && !val.IsSlashed { if val.IsPrevEpochHeadAttester && !val.IsSlashed {
if !inactivityLeak { if !inactivityLeak {
n := baseReward * headWeight * (new(big.Int).Div(bal.PrevEpochHeadAttested, increment)).Uint64() n := new(big.Int).Mul(baseReward, headWeight)
attDelta.HeadReward += n / (activeIncrement * weightDenominator) n.Mul(n, new(big.Int).Div(bal.PrevEpochHeadAttested, increment))
attDelta.HeadReward += n.Div(n, rewardDenominator).Uint64()
} }
} }