-
Notifications
You must be signed in to change notification settings - Fork 22
/
mature.go
99 lines (77 loc) · 3.16 KB
/
mature.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
package keeper
import (
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stratosnet/stratos-chain/x/pot/types"
)
const (
MatureCountPerBlock = 100
)
// RewardMatureAndSubSlashing mature rewards and deduct slashing for all nodes
func (k Keeper) RewardMatureAndSubSlashing(ctx sdk.Context) error {
lastDistributedEpoch := k.GetLastDistributedEpoch(ctx)
maturedEpoch := k.GetMaturedEpoch(ctx)
// The first batch of reward is matured from the value of mature_epoch(param) + 1
if maturedEpoch.IsZero() {
maturedEpoch = sdkmath.NewInt(k.MatureEpoch(ctx))
k.SetMaturedEpoch(ctx, maturedEpoch)
}
if lastDistributedEpoch.LTE(maturedEpoch) {
return nil
}
maturedIndividualKeys := make([][]byte, 0)
matureStartEpochOffset := int64(1)
matureEndEpochOffset := lastDistributedEpoch.Sub(maturedEpoch).Int64()
processCount := 1
for i := matureStartEpochOffset; i <= matureEndEpochOffset; i++ {
processingEpoch := sdkmath.NewInt(i).Add(maturedEpoch)
totalSlashed := sdk.Coins{}
isBreak := true
k.IteratorIndividualReward(ctx, processingEpoch, func(walletAddress sdk.AccAddress, individualReward types.Reward) (stop bool) {
// Stop iteration when executed wallet reaches MatureCountPerBlock && no new volume report is received
if processCount > MatureCountPerBlock &&
lastDistributedEpoch.Equal(processingEpoch) {
isBreak = true
return true
}
// Mature reward
oldMatureTotal := k.GetMatureTotalReward(ctx, walletAddress)
oldImmatureTotal := k.GetImmatureTotalReward(ctx, walletAddress)
immatureToMature := individualReward.RewardFromMiningPool.Add(individualReward.RewardFromTrafficPool...)
// Deduct slashing amount from upcoming mature reward, don't need to deduct slashing from immatureTotal & individual
remaining, deducted := k.registerKeeper.DeductSlashing(ctx, walletAddress, immatureToMature, k.RewardDenom(ctx))
totalSlashed = totalSlashed.Add(deducted...)
matureTotal := oldMatureTotal.Add(remaining...)
immatureTotal := oldImmatureTotal.Sub(immatureToMature...)
processCount++
k.SetMatureTotalReward(ctx, walletAddress, matureTotal)
k.SetImmatureTotalReward(ctx, walletAddress, immatureTotal)
maturedIndividualKeys = append(maturedIndividualKeys, types.GetIndividualRewardKey(walletAddress, processingEpoch))
isBreak = false
return false
})
// transfer deducted slashing amount
err := k.transferTokensForMatureReward(ctx, totalSlashed)
if err != nil {
return err
}
// when isBreak == false, means reward mature for processingEpoch is completed, update MaturedEpoch
if !isBreak {
k.SetMaturedEpoch(ctx, processingEpoch)
}
}
// remove matured individual reward records
for _, key := range maturedIndividualKeys {
k.RemoveIndividualReward(ctx, key)
}
return nil
}
func (k Keeper) transferTokensForMatureReward(ctx sdk.Context, totalSlashed sdk.Coins) error {
// [TLC] [TotalRewardPool -> Distribution] Transfer slashed reward to FeePool.CommunityPool
totalRewardPoolAccAddr := k.accountKeeper.GetModuleAddress(types.TotalRewardPool)
err := k.distrKeeper.FundCommunityPool(ctx, totalSlashed, totalRewardPoolAccAddr)
if err != nil {
return err
}
return nil
}