-
Notifications
You must be signed in to change notification settings - Fork 202
/
keeper.go
304 lines (268 loc) · 11.7 KB
/
keeper.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
package keeper
import (
"fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cast"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
icqkeeper "github.com/Stride-Labs/stride/v4/x/interchainquery/keeper"
"github.com/Stride-Labs/stride/v4/x/stakeibc/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
icacontrollerkeeper "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/controller/keeper"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types"
epochstypes "github.com/Stride-Labs/stride/v4/x/epochs/types"
icacallbackskeeper "github.com/Stride-Labs/stride/v4/x/icacallbacks/keeper"
recordsmodulekeeper "github.com/Stride-Labs/stride/v4/x/records/keeper"
)
type (
Keeper struct {
// *cosmosibckeeper.Keeper
cdc codec.BinaryCodec
storeKey sdk.StoreKey
memKey sdk.StoreKey
paramstore paramtypes.Subspace
ICAControllerKeeper icacontrollerkeeper.Keeper
IBCKeeper ibckeeper.Keeper
scopedKeeper capabilitykeeper.ScopedKeeper
bankKeeper bankkeeper.Keeper
InterchainQueryKeeper icqkeeper.Keeper
RecordsKeeper recordsmodulekeeper.Keeper
StakingKeeper stakingkeeper.Keeper
ICACallbacksKeeper icacallbackskeeper.Keeper
hooks types.StakeIBCHooks
accountKeeper types.AccountKeeper
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey sdk.StoreKey,
ps paramtypes.Subspace,
// channelKeeper cosmosibckeeper.ChannelKeeper,
// portKeeper cosmosibckeeper.PortKeeper,
// scopedKeeper cosmosibckeeper.ScopedKeeper,
accountKeeper types.AccountKeeper,
bankKeeper bankkeeper.Keeper,
icacontrollerkeeper icacontrollerkeeper.Keeper,
ibcKeeper ibckeeper.Keeper,
scopedKeeper capabilitykeeper.ScopedKeeper,
interchainQueryKeeper icqkeeper.Keeper,
RecordsKeeper recordsmodulekeeper.Keeper,
StakingKeeper stakingkeeper.Keeper,
ICACallbacksKeeper icacallbackskeeper.Keeper,
) Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
ICAControllerKeeper: icacontrollerkeeper,
IBCKeeper: ibcKeeper,
scopedKeeper: scopedKeeper,
InterchainQueryKeeper: interchainQueryKeeper,
RecordsKeeper: RecordsKeeper,
StakingKeeper: StakingKeeper,
ICACallbacksKeeper: ICACallbacksKeeper,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// SetHooks sets the hooks for ibc staking
func (k *Keeper) SetHooks(gh types.StakeIBCHooks) *Keeper {
if k.hooks != nil {
panic("cannot set ibc staking hooks twice")
}
k.hooks = gh
return k
}
// ClaimCapability claims the channel capability passed via the OnOpenChanInit callback
func (k *Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
func (k Keeper) GetChainID(ctx sdk.Context, connectionID string) (string, error) {
conn, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, connectionID)
if !found {
errMsg := fmt.Sprintf("invalid connection id, %s not found", connectionID)
k.Logger(ctx).Error(errMsg)
return "", fmt.Errorf(errMsg)
}
clientState, found := k.IBCKeeper.ClientKeeper.GetClientState(ctx, conn.ClientId)
if !found {
errMsg := fmt.Sprintf("client id %s not found for connection %s", conn.ClientId, connectionID)
k.Logger(ctx).Error(errMsg)
return "", fmt.Errorf(errMsg)
}
client, ok := clientState.(*ibctmtypes.ClientState)
if !ok {
errMsg := fmt.Sprintf("invalid client state for client %s on connection %s", conn.ClientId, connectionID)
k.Logger(ctx).Error(errMsg)
return "", fmt.Errorf(errMsg)
}
return client.ChainId, nil
}
func (k Keeper) GetCounterpartyChainId(ctx sdk.Context, connectionID string) (string, error) {
conn, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, connectionID)
if !found {
errMsg := fmt.Sprintf("invalid connection id, %s not found", connectionID)
k.Logger(ctx).Error(errMsg)
return "", fmt.Errorf(errMsg)
}
counterPartyClientState, found := k.IBCKeeper.ClientKeeper.GetClientState(ctx, conn.Counterparty.ClientId)
if !found {
errMsg := fmt.Sprintf("counterparty client id %s not found for connection %s", conn.Counterparty.ClientId, connectionID)
k.Logger(ctx).Error(errMsg)
return "", fmt.Errorf(errMsg)
}
counterpartyClient, ok := counterPartyClientState.(*ibctmtypes.ClientState)
if !ok {
errMsg := fmt.Sprintf("invalid client state for client %s on connection %s", conn.Counterparty.ClientId, connectionID)
k.Logger(ctx).Error(errMsg)
return "", fmt.Errorf(errMsg)
}
return counterpartyClient.ChainId, nil
}
func (k Keeper) GetConnectionId(ctx sdk.Context, portId string) (string, error) {
icas := k.ICAControllerKeeper.GetAllInterchainAccounts(ctx)
for _, ica := range icas {
if ica.PortId == portId {
return ica.ConnectionId, nil
}
}
errMsg := fmt.Sprintf("portId %s has no associated connectionId", portId)
k.Logger(ctx).Error(errMsg)
return "", fmt.Errorf(errMsg)
}
// helper to get what share of the curr epoch we're through
func (k Keeper) GetStrideEpochElapsedShare(ctx sdk.Context) (sdk.Dec, error) {
// Get the current stride epoch
epochTracker, found := k.GetEpochTracker(ctx, epochstypes.STRIDE_EPOCH)
if !found {
errMsg := fmt.Sprintf("Failed to get epoch tracker for %s", epochstypes.STRIDE_EPOCH)
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), sdkerrors.Wrapf(sdkerrors.ErrNotFound, errMsg)
}
// Get epoch start time, end time, and duration
epochDuration, err := cast.ToInt64E(epochTracker.Duration)
if err != nil {
errMsg := fmt.Sprintf("unable to convert epoch duration to int64, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrIntCast, errMsg)
}
epochEndTime, err := cast.ToInt64E(epochTracker.NextEpochStartTime)
if err != nil {
errMsg := fmt.Sprintf("unable to convert next epoch start time to int64, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrIntCast, errMsg)
}
epochStartTime := epochEndTime - epochDuration
// Confirm the current block time is inside the current epoch's start and end times
currBlockTime := ctx.BlockTime().UnixNano()
if currBlockTime < epochStartTime || currBlockTime > epochEndTime {
errMsg := fmt.Sprintf("current block time %d is not within current epoch (ending at %d)", currBlockTime, epochTracker.NextEpochStartTime)
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrInvalidEpoch, errMsg)
}
// Get elapsed share
elapsedTime := currBlockTime - epochStartTime
elapsedShare := sdk.NewDec(elapsedTime).Quo(sdk.NewDec(epochDuration))
if elapsedShare.LT(sdk.ZeroDec()) || elapsedShare.GT(sdk.OneDec()) {
errMsg := fmt.Sprintf("elapsed share (%s) for epoch is not between 0 and 1", elapsedShare)
k.Logger(ctx).Error(errMsg)
return sdk.ZeroDec(), sdkerrors.Wrapf(types.ErrInvalidEpoch, errMsg)
}
k.Logger(ctx).Info(fmt.Sprintf("Epoch elapsed share: %v (Block Time: %d, Epoch End Time: %d)", elapsedShare, currBlockTime, epochEndTime))
return elapsedShare, nil
}
// helper to check whether ICQs are valid in this portion of the epoch
func (k Keeper) IsWithinBufferWindow(ctx sdk.Context) (bool, error) {
elapsedShareOfEpoch, err := k.GetStrideEpochElapsedShare(ctx)
if err != nil {
return false, err
}
bufferSize, err := cast.ToInt64E(k.GetParam(ctx, types.KeyBufferSize))
if err != nil {
return false, err
}
epochShareThresh := sdk.NewDec(1).Sub(sdk.NewDec(1).Quo(sdk.NewDec(bufferSize)))
inWindow := elapsedShareOfEpoch.GT(epochShareThresh)
if !inWindow {
k.Logger(ctx).Error(fmt.Sprintf("ICQCB: We're %d pct through the epoch, ICQ cutoff is %d", elapsedShareOfEpoch, epochShareThresh))
}
return inWindow, nil
}
func (k Keeper) GetICATimeoutNanos(ctx sdk.Context, epochType string) (uint64, error) {
epochTracker, found := k.GetEpochTracker(ctx, epochType)
if !found {
k.Logger(ctx).Error(fmt.Sprintf("Failed to get epoch tracker for %s", epochType))
return 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Failed to get epoch tracker for %s", epochType)
}
// BUFFER by 5% of the epoch length
bufferSizeParam := k.GetParam(ctx, types.KeyBufferSize)
bufferSize := epochTracker.Duration / bufferSizeParam
// buffer size should not be negative or longer than the epoch duration
if bufferSize > epochTracker.Duration {
k.Logger(ctx).Error(fmt.Sprintf("Invalid buffer size %d", bufferSize))
return 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Invalid buffer size %d", bufferSize)
}
timeoutNanos := epochTracker.NextEpochStartTime - bufferSize
timeoutNanosUint64, err := cast.ToUint64E(timeoutNanos)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Failed to convert timeoutNanos to uint64, error: %s", err.Error()))
return 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "Failed to convert timeoutNanos to uint64, error: %s", err.Error())
}
return timeoutNanosUint64, nil
}
// safety check: ensure the redemption rate is NOT below our min safety threshold && NOT above our max safety threshold on host zone
func (k Keeper) IsRedemptionRateWithinSafetyBounds(ctx sdk.Context, zone types.HostZone) (bool, error) {
minSafetyThresholdInt := k.GetParam(ctx, types.KeySafetyMinRedemptionRateThreshold)
minSafetyThreshold := sdk.NewDec(int64(minSafetyThresholdInt)).Quo(sdk.NewDec(100))
maxSafetyThresholdInt := k.GetParam(ctx, types.KeySafetyMaxRedemptionRateThreshold)
maxSafetyThreshold := sdk.NewDec(int64(maxSafetyThresholdInt)).Quo(sdk.NewDec(100))
redemptionRate := zone.RedemptionRate
if redemptionRate.LT(minSafetyThreshold) || redemptionRate.GT(maxSafetyThreshold) {
errMsg := fmt.Sprintf("IsRedemptionRateWithinSafetyBounds check failed %s is outside safety bounds [%s, %s]", redemptionRate.String(), minSafetyThreshold.String(), maxSafetyThreshold.String())
k.Logger(ctx).Error(errMsg)
return false, sdkerrors.Wrapf(types.ErrRedemptionRateOutsideSafetyBounds, errMsg)
}
return true, nil
}
// Check the max number of validators to confirm we won't exceed it when adding a new validator
// Types of additions:
// - change a weight from zero to non-zero
// - add a new validator with non-zero weight
func (k Keeper) ConfirmValSetHasSpace(ctx sdk.Context, validators []*types.Validator) error {
// get max val parameter
maxNumVals, err := cast.ToIntE(k.GetParam(ctx, types.KeySafetyNumValidators))
if err != nil {
errMsg := fmt.Sprintf("Error getting safety max num validators | err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return sdkerrors.Wrap(types.ErrMaxNumValidators, errMsg)
}
// count up the number of validators with non-zero weights
numNonzeroWgtValidators := 0
for _, validator := range validators {
if validator.Weight > 0 {
numNonzeroWgtValidators++
}
}
// check if the number of validators with non-zero weights is below than the max
if numNonzeroWgtValidators >= maxNumVals {
errMsg := fmt.Sprintf("Attempting to add new validator but already reached max number of validators (%d)", maxNumVals)
k.Logger(ctx).Error(errMsg)
return sdkerrors.Wrap(types.ErrMaxNumValidators, errMsg)
}
return nil
}