-
Notifications
You must be signed in to change notification settings - Fork 3
/
block_rewards.go
74 lines (61 loc) · 2.58 KB
/
block_rewards.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package agora
import (
"github.com/pkg/errors"
"math/big"
"github.com/prysmaticlabs/prysm/v3/config/params"
)
// Global constants (start with a capital letter)
const (
YearOfSecs uint64 = 365 * 24 * 60 * 60
)
// Package scope constants
const (
firstYearValRewards uint64 = 27 * (YearOfSecs / 5)
)
type RewardConfig struct {
SlotsPerEpoch uint64
SecondsPerSlot uint64
GweiPerBoa uint64
EffectiveBalanceIncrement uint64
}
// Agora rewards as defined in the white paper (in Gwei)
//
// Allocated Validator rewards are 27 coins per 5 seconds for first year
// then reduced by 6.31% every year
func AllocatedYearlyValidatorRewards(secondsSinceGenesis uint64, GweiPerBoa uint64) uint64 {
yearsSinceGenesis := secondsSinceGenesis / YearOfSecs
bigGweiPerBoa := new(big.Int).SetUint64(GweiPerBoa)
bigfirstYearValRewards := new(big.Int).SetUint64(firstYearValRewards)
yearlyReward := new(big.Int).Mul(bigGweiPerBoa, bigfirstYearValRewards)
for y := yearsSinceGenesis; y > 0; y-- {
yearlyReward.Mul(yearlyReward, big.NewInt(9_369))
yearlyReward.Div(yearlyReward, big.NewInt(10_000))
}
return yearlyReward.Uint64()
}
func AllocatedValidatorRewardsPerEpoch(secondsSinceGenesis uint64, cfg RewardConfig) uint64 {
allocatedRewardsPerSecond := AllocatedYearlyValidatorRewards(secondsSinceGenesis, cfg.GweiPerBoa) / YearOfSecs
return cfg.SecondsPerSlot * cfg.SlotsPerEpoch * allocatedRewardsPerSecond
}
func ValidatorRewardPerEpoch(secondsSinceGenesis uint64, totalBalance uint64, effectiveBalance uint64, cfg RewardConfig) (uint64, error) {
if totalBalance <= 0 {
return 0, errors.New("active balance can't be 0")
}
allocatedValidatorRewardsPerEpoch := new(big.Int).SetUint64(AllocatedValidatorRewardsPerEpoch(secondsSinceGenesis, cfg))
increments := new(big.Int).SetUint64(effectiveBalance / cfg.EffectiveBalanceIncrement)
bigTotalBalance := new(big.Int).SetUint64(totalBalance)
bigEffectiveBalance := new(big.Int).SetUint64(cfg.EffectiveBalanceIncrement)
bigEffectiveBalance.Mul(bigEffectiveBalance, increments)
bigEffectiveBalance.Mul(bigEffectiveBalance, allocatedValidatorRewardsPerEpoch)
bigEffectiveBalance.Div(bigEffectiveBalance, bigTotalBalance)
return bigEffectiveBalance.Uint64(), nil
}
func MakeAgoraRewardConfig(beaconCfg *params.BeaconChainConfig) RewardConfig {
agoraConfig := RewardConfig{
SlotsPerEpoch: uint64(beaconCfg.SlotsPerEpoch),
SecondsPerSlot: beaconCfg.SecondsPerSlot,
GweiPerBoa: beaconCfg.GweiPerEth,
EffectiveBalanceIncrement: beaconCfg.EffectiveBalanceIncrement,
}
return agoraConfig
}