-
Notifications
You must be signed in to change notification settings - Fork 199
/
unstaking.go
165 lines (142 loc) · 6.26 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
package keeper
import (
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/pairing/types"
spectypes "github.com/lavanet/lava/x/spec/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},
)
}
senderAddr, err := sdk.AccAddressFromBech32(creator)
if err != nil {
return utils.LavaFormatWarning("invalid address", err,
utils.Attribute{Key: "provider", Value: creator},
)
}
existingEntry, entryExists, _ := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, senderAddr)
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
existingEntry, _, indexInStakeStorage := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, senderAddr)
err = k.epochStorageKeeper.RemoveStakeEntryCurrent(ctx, chainID, indexInStakeStorage)
if err != nil {
return utils.LavaFormatWarning("can't remove stake Entry, stake entry not found in index", err,
utils.Attribute{Key: "index", Value: indexInStakeStorage},
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.getUnstakeHoldBlocks(ctx, existingEntry.Chain)
return k.epochStorageKeeper.AppendUnstakeEntry(ctx, existingEntry, unstakeHoldBlocks)
}
func (k Keeper) CheckUnstakingForCommit(ctx sdk.Context) {
// this pops all the entries that had their deadline pass
k.epochStorageKeeper.PopUnstakeEntries(ctx, uint64(ctx.BlockHeight()))
}
// NOTE: duplicated in x/dualstaking/keeper/delegate.go; any changes should be applied there too.
func (k Keeper) getUnstakeHoldBlocks(ctx sdk.Context, chainID string) uint64 {
_, found, providerType := k.specKeeper.IsSpecFoundAndActive(ctx, chainID)
if !found {
utils.LavaFormatError("critical: failed to get spec for chainID",
fmt.Errorf("unknown chainID"),
utils.Attribute{Key: "chainID", Value: chainID},
)
}
// note: if spec was not found, the default choice is Spec_dynamic == 0
block := uint64(ctx.BlockHeight())
if providerType == spectypes.Spec_static {
return k.epochStorageKeeper.UnstakeHoldBlocksStatic(ctx, block)
} else {
return k.epochStorageKeeper.UnstakeHoldBlocks(ctx, block)
}
// NOT REACHED
}
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, providerAddr)
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, _, indexInStakeStorage := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, providerAddr)
err = k.epochStorageKeeper.RemoveStakeEntryCurrent(ctx, chainID, indexInStakeStorage)
if err != nil {
return utils.LavaFormatWarning("can't remove stake Entry, stake entry not found in index", err,
utils.Attribute{Key: "index", Value: indexInStakeStorage},
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.getUnstakeHoldBlocks(ctx, existingEntry.Chain)
return k.epochStorageKeeper.AppendUnstakeEntry(ctx, existingEntry, unstakeHoldBlocks)
}
}
return nil
}