/
providers.go
306 lines (260 loc) · 12.6 KB
/
providers.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package keeper
import (
"fmt"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/rewards/types"
)
const DAY_SECONDS = 60 * 60 * 24
func (k Keeper) AggregateCU(ctx sdk.Context, subscription, provider string, chainID string, cu uint64) {
if !k.IsIprpcSubscription(ctx, subscription) {
return
}
index := types.BasePayIndex{Provider: provider, ChainID: chainID}
basepay, found := k.getBasePay(ctx, index)
if !found {
basepay = types.BasePay{IprpcCu: cu}
} else {
basepay.IprpcCu += cu
}
k.setBasePay(ctx, index, basepay)
}
func (k Keeper) AggregateRewards(ctx sdk.Context, provider, chainid string, adjustment sdk.Dec, rewards math.Int) {
index := types.BasePayIndex{Provider: provider, ChainID: chainid}
basepay, found := k.getBasePay(ctx, index)
adjustedPay := adjustment.MulInt(rewards)
adjustedPay = sdk.MinDec(adjustedPay, sdk.NewDecFromInt(rewards))
if !found {
basepay = types.BasePay{Total: rewards, TotalAdjusted: adjustedPay}
} else {
basepay.Total = basepay.Total.Add(rewards)
basepay.TotalAdjusted = basepay.TotalAdjusted.Add(adjustedPay)
}
k.setBasePay(ctx, index, basepay)
}
// Distribute bonus rewards to providers across all chains based on performance
func (k Keeper) distributeMonthlyBonusRewards(ctx sdk.Context) {
details := map[string]string{}
coins := k.TotalPoolTokens(ctx, types.ProviderRewardsDistributionPool)
total := coins.AmountOf(k.stakingKeeper.BondDenom(ctx))
totalRewarded := sdk.ZeroInt()
// specs emissions from the total reward pool base on stake
specs := k.specEmissionParts(ctx)
defer func() {
k.removeAllBasePay(ctx)
utils.LogLavaEvent(ctx, k.Logger(ctx), types.ProvidersBonusRewardsEventName, details, "provider bonus rewards distributed")
}()
// Get serviced CU for each provider + spec
specCuMap := map[string]types.SpecCuType{} // spec -> specCu
for _, spec := range specs {
// all providers basepays and the total basepay of the spec
basepays, totalbasepay := k.specProvidersBasePay(ctx, spec.ChainID, true)
if len(basepays) == 0 {
continue
}
// calculate the maximum rewards for the spec
specTotalPayout := math.LegacyZeroDec()
if !totalbasepay.IsZero() {
specTotalPayout = k.specTotalPayout(ctx, total, sdk.NewDecFromInt(totalbasepay), spec)
}
// distribute the rewards to all providers
for _, basepay := range basepays {
if !specTotalPayout.IsZero() {
// calculate the providers bonus base on adjusted base pay
reward := specTotalPayout.Mul(basepay.TotalAdjusted).QuoInt(totalbasepay).TruncateInt()
totalRewarded = totalRewarded.Add(reward)
if totalRewarded.GT(total) {
utils.LavaFormatError("provider rewards are larger than the distribution pool balance", nil,
utils.LogAttr("distribution_pool_balance", total.String()),
utils.LogAttr("provider_reward", totalRewarded.String()))
details["error"] = "provider rewards are larger than the distribution pool balance"
return
}
// now give the reward the provider contributor and delegators
_, _, err := k.dualstakingKeeper.RewardProvidersAndDelegators(ctx, basepay.Provider, basepay.ChainID, sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), reward)), string(types.ProviderRewardsDistributionPool), false, false, false)
if err != nil {
utils.LavaFormatError("failed to send bonus rewards to provider", err, utils.LogAttr("provider", basepay.Provider))
}
details[basepay.Provider+" "+spec.ChainID] = reward.String()
}
// count iprpc cu
k.countIprpcCu(specCuMap, basepay.IprpcCu, spec.ChainID, basepay.Provider)
}
}
// Get current month IprpcReward and use it to distribute rewards
iprpcReward, found := k.PopIprpcReward(ctx)
if !found {
utils.LavaFormatError("current month iprpc reward not found", fmt.Errorf("did not reward providers IPRPC bonus"))
return
}
// distribute IPRPC rewards
k.distributeIprpcRewards(ctx, iprpcReward, specCuMap)
}
// specTotalPayout calculates the total bonus for a specific spec
// specPayoutAllocation: maximum rewards that the spec can have
// rewardBoost: bonus based on the total rewards providers got factored by maxboost
// diminishingRewards: makes sure to diminish the bonuses in case there are enough consumers on the chain
func (k Keeper) specTotalPayout(ctx sdk.Context, totalMonthlyPayout math.Int, totalProvidersBaseRewards sdk.Dec, spec types.SpecEmissionPart) math.LegacyDec {
specPayoutAllocation := spec.Emission.MulInt(totalMonthlyPayout)
rewardBoost := totalProvidersBaseRewards.MulInt64(int64(k.MaxRewardBoost(ctx)))
diminishingRewards := sdk.MaxDec(sdk.ZeroDec(), (sdk.NewDecWithPrec(15, 1).Mul(specPayoutAllocation)).Sub(sdk.NewDecWithPrec(5, 1).Mul(totalProvidersBaseRewards)))
return sdk.MinDec(sdk.MinDec(specPayoutAllocation, rewardBoost), diminishingRewards)
}
func (k Keeper) specEmissionParts(ctx sdk.Context) (emissions []types.SpecEmissionPart) {
chainIDs := k.specKeeper.GetAllChainIDs(ctx)
totalStake := sdk.ZeroDec()
chainStake := map[string]sdk.Dec{}
for _, chainID := range chainIDs {
spec, found := k.specKeeper.GetSpec(ctx, chainID)
if !found {
continue
}
if !spec.Enabled || spec.Shares == 0 {
continue
}
stakeStorage, found := k.epochstorage.GetStakeStorageCurrent(ctx, chainID)
if !found {
continue
}
chainStake[chainID] = sdk.ZeroDec()
for _, entry := range stakeStorage.StakeEntries {
chainStake[chainID] = chainStake[chainID].Add(sdk.NewDecFromInt(entry.EffectiveStake()))
}
chainStake[chainID] = chainStake[chainID].MulInt64(int64(spec.Shares))
totalStake = totalStake.Add(chainStake[chainID])
}
if totalStake.IsZero() {
return emissions
}
for _, chainID := range chainIDs {
if stake, ok := chainStake[chainID]; ok {
if stake.IsZero() {
continue
}
emissions = append(emissions, types.SpecEmissionPart{ChainID: chainID, Emission: stake.Quo(totalStake)})
}
}
return emissions
}
func (k Keeper) specProvidersBasePay(ctx sdk.Context, chainID string, pop bool) ([]types.BasePayWithIndex, math.Int) {
var basepays []types.BasePayWithIndex
if pop {
basepays = k.popAllBasePayForChain(ctx, chainID)
} else {
basepays = k.getAllBasePayForChain(ctx, chainID, "") // getting all basepays with chainID (for all providers)
}
totalBasePay := math.ZeroInt()
for _, basepay := range basepays {
totalBasePay = totalBasePay.Add(basepay.Total)
}
return basepays, totalBasePay
}
// ContributeToValidatorsAndCommunityPool transfers some of the providers' rewards to the validators and community pool
// the function return the updated reward after the participation deduction
func (k Keeper) ContributeToValidatorsAndCommunityPool(ctx sdk.Context, reward sdk.Coin, senderModule string) (updatedReward sdk.Coin, err error) {
validatorsParticipationReward, communityParticipationReward, err := k.CalculateValidatorsAndCommunityParticipationRewards(ctx, reward)
if err != nil {
return reward, utils.LavaFormatError("calculating validators and community participation fees failed", err,
utils.LogAttr("reward", reward.String()),
)
}
// send validators participation
if !validatorsParticipationReward.AmountOf(reward.Denom).IsZero() {
pool := types.ValidatorsRewardsDistributionPoolName
if k.isEndOfMonth(ctx) {
pool = types.ValidatorsRewardsAllocationPoolName
}
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, senderModule, string(pool), validatorsParticipationReward)
if err != nil {
return reward, utils.LavaFormatError("sending validators participation failed", err,
utils.Attribute{Key: "validators_participation_reward", Value: validatorsParticipationReward.String()},
utils.Attribute{Key: "reward", Value: reward.String()},
)
}
}
// send community participation
if !communityParticipationReward.AmountOf(reward.Denom).IsZero() {
err = k.FundCommunityPoolFromModule(ctx, communityParticipationReward, senderModule)
if err != nil {
return reward, utils.LavaFormatError("sending community participation failed", err,
utils.Attribute{Key: "community_participation_reward", Value: communityParticipationReward.String()},
utils.Attribute{Key: "reward", Value: reward.String()},
)
}
}
// update reward amount
reward = reward.SubAmount(communityParticipationReward.AmountOf(reward.Denom)).SubAmount(validatorsParticipationReward.AmountOf(reward.Denom))
return reward, nil
}
func (k Keeper) CalculateValidatorsAndCommunityParticipationRewards(ctx sdk.Context, reward sdk.Coin) (validatorsCoins sdk.Coins, communityCoins sdk.Coins, err error) {
zeroCoins := sdk.NewCoins(sdk.NewCoin(reward.Denom, math.ZeroInt()))
validatorsCoins = zeroCoins
communityCoins = zeroCoins
// calculate validators and community participation fractions
validatorsParticipation, communityParticipation, err := k.CalculateContributionPercentages(ctx, reward.Amount)
if err != nil {
return zeroCoins, zeroCoins, err
}
if communityParticipation.Equal(sdk.OneDec()) {
return zeroCoins, sdk.NewCoins(reward), nil
}
validatorsPart := validatorsParticipation.MulInt(reward.Amount).TruncateInt()
communityPart := communityParticipation.MulInt(reward.Amount).TruncateInt()
if !validatorsPart.IsZero() {
validatorsCoins = sdk.NewCoins(sdk.NewCoin(reward.Denom, validatorsPart))
}
if !communityPart.IsZero() {
communityCoins = sdk.NewCoins(sdk.NewCoin(reward.Denom, communityPart))
}
return validatorsCoins, communityCoins, nil
}
// CalculateContributionPercentages calculates the providers' rewards participation to the validators and community pool
func (k Keeper) CalculateContributionPercentages(ctx sdk.Context, reward math.Int) (validatorsParticipation math.LegacyDec, communityParticipation math.LegacyDec, err error) {
communityTax := k.distributionKeeper.GetParams(ctx).CommunityTax
if communityTax.Equal(sdk.OneDec()) {
return sdk.ZeroDec(), sdk.OneDec(), nil
}
// validators_participation = validators_participation_param / (1-community_tax)
validatorsParticipationParam := k.GetParams(ctx).ValidatorsSubscriptionParticipation
validatorsParticipation = validatorsParticipationParam.Quo(sdk.OneDec().Sub(communityTax))
if validatorsParticipation.GT(sdk.OneDec()) {
return sdk.ZeroDec(), sdk.ZeroDec(), utils.LavaFormatError("validators participation bigger than 100%", fmt.Errorf("validators participation calc failed"),
utils.Attribute{Key: "validators_participation", Value: validatorsParticipation.String()},
utils.Attribute{Key: "validators_subscription_participation_param", Value: validatorsParticipationParam.String()},
utils.Attribute{Key: "community_tax", Value: communityTax.String()},
)
}
// community_participation = (community_tax + validators_participation_param) - validators_participation
communityParticipation = communityTax.Add(validatorsParticipationParam).Sub(validatorsParticipation)
if communityParticipation.IsNegative() || communityParticipation.GT(sdk.OneDec()) {
return sdk.ZeroDec(), sdk.ZeroDec(), utils.LavaFormatError("community participation is negative or bigger than 100%", fmt.Errorf("community participation calc failed"),
utils.Attribute{Key: "community_participation", Value: communityParticipation.String()},
utils.Attribute{Key: "validators_participation", Value: validatorsParticipation.String()},
utils.Attribute{Key: "validators_subscription_participation_param", Value: validatorsParticipationParam.String()},
utils.Attribute{Key: "community_tax", Value: communityTax.String()},
)
}
// check the participation rewards are not more than 100%
if validatorsParticipation.Add(communityParticipation).GT(sdk.OneDec()) {
return sdk.ZeroDec(), sdk.ZeroDec(), utils.LavaFormatError("validators and community participation parts are bigger than 100%", fmt.Errorf("validators and community participation aborted"),
utils.Attribute{Key: "community_participation", Value: communityParticipation.String()},
utils.Attribute{Key: "validators_participation", Value: validatorsParticipation.String()},
)
}
return validatorsParticipation, communityParticipation, nil
}
func (k Keeper) FundCommunityPoolFromModule(ctx sdk.Context, amount sdk.Coins, senderModule string) error {
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, senderModule, distributiontypes.ModuleName, amount); err != nil {
return err
}
feePool := k.distributionKeeper.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...)
k.distributionKeeper.SetFeePool(ctx, feePool)
return nil
}
// isEndOfMonth checks that we're close to next timer expiry by at least 24 hours
func (k Keeper) isEndOfMonth(ctx sdk.Context) bool {
return ctx.BlockTime().UTC().Unix()+DAY_SECONDS > k.TimeToNextTimerExpiry(ctx)
}