/
unstaking.go
221 lines (191 loc) · 7.63 KB
/
unstaking.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
package keeper
import (
"fmt"
"math"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/pairing/types"
)
func (k Keeper) UnstakeEntry(ctx sdk.Context, validator, chainID, creator, unstakeDescription string) error {
logger := k.Logger(ctx)
// TODO: validate chainID basic validation
// we can unstake disabled specs, but not missing ones
_, found := k.specKeeper.GetSpec(ctx, chainID)
if !found {
return utils.LavaFormatWarning("trying to unstake an entry on missing spec", fmt.Errorf("spec not found"),
utils.Attribute{Key: "spec", Value: chainID},
)
}
if _, err := sdk.ValAddressFromBech32(validator); err != nil {
return utils.LavaFormatWarning("invalid address", err,
utils.Attribute{Key: "validator", Value: validator},
)
}
existingEntry, entryExists := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, creator)
if !entryExists {
return utils.LavaFormatWarning("can't unstake Entry, stake entry not found for address", fmt.Errorf("stake entry not found"),
utils.Attribute{Key: "provider", Value: creator},
utils.Attribute{Key: "spec", Value: chainID},
)
}
err := k.dualstakingKeeper.UnbondFull(ctx, existingEntry.GetAddress(), validator, existingEntry.GetAddress(), existingEntry.GetChain(), existingEntry.Stake, true)
if err != nil {
return utils.LavaFormatWarning("can't unbond seld delegation", err,
utils.Attribute{Key: "address", Value: existingEntry.Address},
utils.Attribute{Key: "spec", Value: chainID},
)
}
// index might have changed in the unbond
_, found = k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, creator)
if found {
err = k.epochStorageKeeper.RemoveStakeEntryCurrent(ctx, chainID, creator)
if err != nil {
return utils.LavaFormatWarning("can't remove stake Entry, stake entry not found", err,
utils.Attribute{Key: "provider", Value: creator},
utils.Attribute{Key: "spec", Value: chainID},
)
}
}
details := map[string]string{
"address": existingEntry.GetAddress(),
"chainID": existingEntry.GetChain(),
"geolocation": strconv.FormatInt(int64(existingEntry.GetGeolocation()), 10),
"moniker": existingEntry.GetMoniker(),
"stake": existingEntry.GetStake().Amount.String(),
}
utils.LogLavaEvent(ctx, logger, types.ProviderUnstakeEventName, details, unstakeDescription)
unstakeHoldBlocks := k.epochStorageKeeper.GetUnstakeHoldBlocks(ctx, existingEntry.Chain)
k.epochStorageKeeper.AppendUnstakeEntry(ctx, existingEntry, unstakeHoldBlocks)
return nil
}
func (k Keeper) CheckUnstakingForCommit(ctx sdk.Context) {
// this pops all the entries that had their deadline pass
k.epochStorageKeeper.PopUnstakeEntries(ctx, uint64(ctx.BlockHeight()))
}
func (k Keeper) UnstakeEntryForce(ctx sdk.Context, chainID, provider, unstakeDescription string) error {
providerAddr, err := sdk.AccAddressFromBech32(provider)
if err != nil {
return utils.LavaFormatWarning("invalid address", err,
utils.Attribute{Key: "provider", Value: provider},
)
}
existingEntry, entryExists := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, provider)
if !entryExists {
return utils.LavaFormatWarning("can't unstake Entry, stake entry not found for address", fmt.Errorf("stake entry not found"),
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "spec", Value: chainID},
)
}
totalAmount := existingEntry.Stake.Amount
delegations := k.stakingKeeper.GetAllDelegatorDelegations(ctx, providerAddr)
for _, delegation := range delegations {
validator, found := k.stakingKeeper.GetValidator(ctx, delegation.GetValidatorAddr())
if !found {
continue
}
amount := validator.TokensFromShares(delegation.Shares).TruncateInt()
if totalAmount.LT(amount) {
amount = totalAmount
}
totalAmount = totalAmount.Sub(amount)
err = k.dualstakingKeeper.UnbondFull(ctx, existingEntry.GetAddress(), validator.OperatorAddress, existingEntry.GetAddress(), existingEntry.GetChain(), sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), amount), true)
if err != nil {
return utils.LavaFormatWarning("can't unbond seld delegation", err,
utils.Attribute{Key: "address", Value: existingEntry.Address},
utils.Attribute{Key: "spec", Value: chainID},
)
}
if totalAmount.IsZero() {
existingEntry, _ := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, provider)
err = k.epochStorageKeeper.RemoveStakeEntryCurrent(ctx, chainID, provider)
if err != nil {
return utils.LavaFormatWarning("can't remove stake Entry, stake entry not found", err,
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "spec", Value: chainID},
)
}
details := map[string]string{
"address": existingEntry.GetAddress(),
"chainID": existingEntry.GetChain(),
"geolocation": strconv.FormatInt(int64(existingEntry.GetGeolocation()), 10),
"moniker": existingEntry.GetMoniker(),
"stake": existingEntry.GetStake().Amount.String(),
}
utils.LogLavaEvent(ctx, k.Logger(ctx), types.ProviderUnstakeEventName, details, unstakeDescription)
unstakeHoldBlocks := k.epochStorageKeeper.GetUnstakeHoldBlocks(ctx, existingEntry.Chain)
k.epochStorageKeeper.AppendUnstakeEntry(ctx, existingEntry, unstakeHoldBlocks)
return nil
}
}
return nil
}
func (k Keeper) SlashDelegator(ctx sdk.Context, slashingInfo types.DelegatorSlashing) error {
delAddr, err := sdk.AccAddressFromBech32(slashingInfo.Delegator)
if err != nil {
return err
}
total := slashingInfo.SlashingAmount.Amount
// this method goes over all unbondings and tries to slash them
slashUnbonding := func() {
unbondings := k.stakingKeeper.GetUnbondingDelegations(ctx, delAddr, math.MaxUint16)
for _, unbonding := range unbondings {
totalBalance := sdk.ZeroInt()
for _, entry := range unbonding.Entries {
totalBalance = totalBalance.Add(entry.Balance)
}
if totalBalance.IsZero() {
continue
}
slashingFactor := total.ToLegacyDec().QuoInt(totalBalance)
slashingFactor = sdk.MinDec(sdk.OneDec(), slashingFactor)
slashedAmount := k.stakingKeeper.SlashUnbondingDelegation(ctx, unbonding, 1, slashingFactor)
slashedAmount = sdk.MinInt(total, slashedAmount)
total = total.Sub(slashedAmount)
if !total.IsPositive() {
return
}
}
}
// try slashing existing unbondings
slashUnbonding()
if !total.IsPositive() {
return nil
}
// we need to unbond from the delegator by force the amount left
delegations := k.stakingKeeper.GetAllDelegatorDelegations(ctx, delAddr)
tokensToUnbond := total
for _, delegation := range delegations {
validator, found := k.stakingKeeper.GetValidator(ctx, delegation.GetValidatorAddr())
if !found {
continue
}
amount := validator.TokensFromShares(delegation.Shares).TruncateInt()
amount = sdk.MinInt(tokensToUnbond, amount)
shares, err := validator.SharesFromTokensTruncated(amount)
if err != nil {
return utils.LavaFormatWarning("failed to get delegators shares", err,
utils.Attribute{Key: "address", Value: delAddr},
utils.Attribute{Key: "validator", Value: validator.OperatorAddress},
)
}
_, err = k.stakingKeeper.Undelegate(ctx, delAddr, validator.GetOperator(), shares)
if err != nil {
return utils.LavaFormatWarning("can't unbond self delegation for slashing", err,
utils.Attribute{Key: "address", Value: delAddr},
)
}
tokensToUnbond = tokensToUnbond.Sub(amount)
if !tokensToUnbond.IsPositive() {
break
}
}
if tokensToUnbond.IsPositive() {
utils.LavaFormatWarning("delegator does not have enough delegations to slash", err,
utils.Attribute{Key: "address", Value: delAddr},
)
}
// try slashing existing unbondings
slashUnbonding()
return nil
}