-
Notifications
You must be signed in to change notification settings - Fork 197
/
msg_server_submit_tx.go
552 lines (489 loc) · 21.2 KB
/
msg_server_submit_tx.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package keeper
import (
"fmt"
"strings"
"time"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/gogoproto/proto"
"github.com/spf13/cast"
"github.com/Stride-Labs/stride/v16/utils"
icacallbackstypes "github.com/Stride-Labs/stride/v16/x/icacallbacks/types"
recordstypes "github.com/Stride-Labs/stride/v16/x/records/types"
"github.com/Stride-Labs/stride/v16/x/stakeibc/types"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
epochstypes "github.com/Stride-Labs/stride/v16/x/epochs/types"
icqtypes "github.com/Stride-Labs/stride/v16/x/interchainquery/types"
sdk "github.com/cosmos/cosmos-sdk/types"
icacontrollerkeeper "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/keeper"
icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
)
func (k Keeper) DelegateOnHost(ctx sdk.Context, hostZone types.HostZone, amt sdk.Coin, depositRecord recordstypes.DepositRecord) error {
// the relevant ICA is the delegate account
owner := types.FormatICAAccountOwner(hostZone.ChainId, types.ICAAccountType_DELEGATION)
portID, err := icatypes.NewControllerPortID(owner)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "%s has no associated portId", owner)
}
connectionId, err := k.GetConnectionId(ctx, portID)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidChainID, "%s has no associated connection", portID)
}
// Fetch the relevant ICA
if hostZone.DelegationIcaAddress == "" {
return errorsmod.Wrapf(types.ErrICAAccountNotFound, "no delegation account found for %s", hostZone.ChainId)
}
// Construct the transaction
targetDelegatedAmts, err := k.GetTargetValAmtsForHostZone(ctx, hostZone, amt.Amount)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Error getting target delegation amounts for host zone %s", hostZone.ChainId))
return err
}
var splitDelegations []*types.SplitDelegation
var msgs []proto.Message
for _, validator := range hostZone.Validators {
relativeAmount, ok := targetDelegatedAmts[validator.Address]
if !ok || !relativeAmount.IsPositive() {
continue
}
msgs = append(msgs, &stakingtypes.MsgDelegate{
DelegatorAddress: hostZone.DelegationIcaAddress,
ValidatorAddress: validator.Address,
Amount: sdk.NewCoin(amt.Denom, relativeAmount),
})
splitDelegations = append(splitDelegations, &types.SplitDelegation{
Validator: validator.Address,
Amount: relativeAmount,
})
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Preparing MsgDelegates from the delegation account to each validator"))
if len(msgs) == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Target delegation amount was 0 for each validator")
}
// add callback data
delegateCallback := types.DelegateCallback{
HostZoneId: hostZone.ChainId,
DepositRecordId: depositRecord.Id,
SplitDelegations: splitDelegations,
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Marshalling DelegateCallback args: %+v", delegateCallback))
marshalledCallbackArgs, err := k.MarshalDelegateCallbackArgs(ctx, delegateCallback)
if err != nil {
return err
}
// Send the transaction through SubmitTx
_, err = k.SubmitTxsStrideEpoch(ctx, connectionId, msgs, types.ICAAccountType_DELEGATION, ICACallbackID_Delegate, marshalledCallbackArgs)
if err != nil {
return errorsmod.Wrapf(err, "Failed to SubmitTxs for connectionId %s on %s. Messages: %s", connectionId, hostZone.ChainId, msgs)
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "ICA MsgDelegates Successfully Sent"))
// flag the delegation change in progress on each validator
for _, splitDelegation := range splitDelegations {
if err := k.IncrementValidatorDelegationChangesInProgress(&hostZone, splitDelegation.Validator); err != nil {
return err
}
}
k.SetHostZone(ctx, hostZone)
// update the record state to DELEGATION_IN_PROGRESS
depositRecord.Status = recordstypes.DepositRecord_DELEGATION_IN_PROGRESS
k.RecordsKeeper.SetDepositRecord(ctx, depositRecord)
return nil
}
func (k Keeper) SetWithdrawalAddressOnHost(ctx sdk.Context, hostZone types.HostZone) error {
// The relevant ICA is the delegate account
owner := types.FormatICAAccountOwner(hostZone.ChainId, types.ICAAccountType_DELEGATION)
portID, err := icatypes.NewControllerPortID(owner)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "%s has no associated portId", owner)
}
connectionId, err := k.GetConnectionId(ctx, portID)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidChainID, "%s has no associated connection", portID)
}
// Fetch the relevant ICA
if hostZone.DelegationIcaAddress == "" {
k.Logger(ctx).Error(fmt.Sprintf("Zone %s is missing a delegation address!", hostZone.ChainId))
return nil
}
if hostZone.WithdrawalIcaAddress == "" {
k.Logger(ctx).Error(fmt.Sprintf("Zone %s is missing a withdrawal address!", hostZone.ChainId))
return nil
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Withdrawal Address: %s, Delegator Address: %s",
hostZone.WithdrawalIcaAddress, hostZone.DelegationIcaAddress))
// Construct the ICA message
msgs := []proto.Message{
&distributiontypes.MsgSetWithdrawAddress{
DelegatorAddress: hostZone.DelegationIcaAddress,
WithdrawAddress: hostZone.WithdrawalIcaAddress,
},
}
_, err = k.SubmitTxsStrideEpoch(ctx, connectionId, msgs, types.ICAAccountType_DELEGATION, "", nil)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Failed to SubmitTxs for %s, %s, %s", connectionId, hostZone.ChainId, msgs)
}
return nil
}
// Submits an ICQ for the withdrawal account balance
func (k Keeper) UpdateWithdrawalBalance(ctx sdk.Context, hostZone types.HostZone) error {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Submitting ICQ for withdrawal account balance"))
// Get the withdrawal account address from the host zone
if hostZone.WithdrawalIcaAddress == "" {
return errorsmod.Wrapf(types.ErrICAAccountNotFound, "no withdrawal account found for %s", hostZone.ChainId)
}
// Encode the withdrawal account address for the query request
// The query request consists of the withdrawal account address and denom
_, withdrawalAddressBz, err := bech32.DecodeAndConvert(hostZone.WithdrawalIcaAddress)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid withdrawal account address, could not decode (%s)", err.Error())
}
queryData := append(bankTypes.CreateAccountBalancesPrefix(withdrawalAddressBz), []byte(hostZone.HostDenom)...)
// Timeout query at end of epoch
strideEpochTracker, found := k.GetEpochTracker(ctx, epochstypes.STRIDE_EPOCH)
if !found {
return errorsmod.Wrapf(types.ErrEpochNotFound, epochstypes.STRIDE_EPOCH)
}
timeout := time.Unix(0, int64(strideEpochTracker.NextEpochStartTime))
timeoutDuration := timeout.Sub(ctx.BlockTime())
// Submit the ICQ for the withdrawal account balance
query := icqtypes.Query{
ChainId: hostZone.ChainId,
ConnectionId: hostZone.ConnectionId,
QueryType: icqtypes.BANK_STORE_QUERY_WITH_PROOF,
RequestData: queryData,
CallbackModule: types.ModuleName,
CallbackId: ICQCallbackID_WithdrawalBalance,
TimeoutDuration: timeoutDuration,
TimeoutPolicy: icqtypes.TimeoutPolicy_REJECT_QUERY_RESPONSE,
}
if err := k.InterchainQueryKeeper.SubmitICQRequest(ctx, query, false); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Error querying for withdrawal balance, error: %s", err.Error()))
return err
}
return nil
}
// helper to get time at which next epoch begins, in unix nano units
func (k Keeper) GetStartTimeNextEpoch(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, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Failed to get epoch tracker for %s", epochType)
}
return epochTracker.NextEpochStartTime, nil
}
func (k Keeper) SubmitTxsDayEpoch(
ctx sdk.Context,
connectionId string,
msgs []proto.Message,
icaAccountType types.ICAAccountType,
callbackId string,
callbackArgs []byte,
) (uint64, error) {
sequence, err := k.SubmitTxsEpoch(ctx, connectionId, msgs, icaAccountType, epochstypes.DAY_EPOCH, callbackId, callbackArgs)
if err != nil {
return 0, err
}
return sequence, nil
}
func (k Keeper) SubmitTxsStrideEpoch(
ctx sdk.Context,
connectionId string,
msgs []proto.Message,
icaAccountType types.ICAAccountType,
callbackId string,
callbackArgs []byte,
) (uint64, error) {
sequence, err := k.SubmitTxsEpoch(ctx, connectionId, msgs, icaAccountType, epochstypes.STRIDE_EPOCH, callbackId, callbackArgs)
if err != nil {
return 0, err
}
return sequence, nil
}
func (k Keeper) SubmitTxsEpoch(
ctx sdk.Context,
connectionId string,
msgs []proto.Message,
icaAccountType types.ICAAccountType,
epochType string,
callbackId string,
callbackArgs []byte,
) (uint64, error) {
timeoutNanosUint64, err := k.GetICATimeoutNanos(ctx, epochType)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Failed to get ICA timeout nanos for epochType %s using param, error: %s", epochType, err.Error()))
return 0, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Failed to convert timeoutNanos to uint64, error: %s", err.Error())
}
sequence, err := k.SubmitTxs(ctx, connectionId, msgs, icaAccountType, timeoutNanosUint64, callbackId, callbackArgs)
if err != nil {
return 0, err
}
return sequence, nil
}
// SubmitTxs submits an ICA transaction containing multiple messages
func (k Keeper) SubmitTxs(
ctx sdk.Context,
connectionId string,
msgs []proto.Message,
icaAccountType types.ICAAccountType,
timeoutTimestamp uint64,
callbackId string,
callbackArgs []byte,
) (uint64, error) {
chainId, err := k.GetChainID(ctx, connectionId)
if err != nil {
return 0, err
}
owner := types.FormatICAAccountOwner(chainId, icaAccountType)
portID, err := icatypes.NewControllerPortID(owner)
if err != nil {
return 0, err
}
k.Logger(ctx).Info(utils.LogWithHostZone(chainId, " Submitting ICA Tx on %s, %s with TTL: %d", portID, connectionId, timeoutTimestamp))
protoMsgs := []proto.Message{}
for _, msg := range msgs {
k.Logger(ctx).Info(utils.LogWithHostZone(chainId, " Msg: %+v", msg))
protoMsgs = append(protoMsgs, msg)
}
channelID, found := k.ICAControllerKeeper.GetActiveChannelID(ctx, connectionId, portID)
if !found {
return 0, errorsmod.Wrapf(icatypes.ErrActiveChannelNotFound, "failed to retrieve active channel for port %s", portID)
}
data, err := icatypes.SerializeCosmosTx(k.cdc, protoMsgs)
if err != nil {
return 0, err
}
packetData := icatypes.InterchainAccountPacketData{
Type: icatypes.EXECUTE_TX,
Data: data,
}
// Submit ICA tx
msgServer := icacontrollerkeeper.NewMsgServerImpl(&k.ICAControllerKeeper)
relativeTimeoutOffset := timeoutTimestamp - uint64(ctx.BlockTime().UnixNano())
msgSendTx := icacontrollertypes.NewMsgSendTx(owner, connectionId, relativeTimeoutOffset, packetData)
res, err := msgServer.SendTx(ctx, msgSendTx)
if err != nil {
return 0, err
}
sequence := res.Sequence
// Store the callback data
if callbackId != "" && callbackArgs != nil {
callback := icacallbackstypes.CallbackData{
CallbackKey: icacallbackstypes.PacketID(portID, channelID, sequence),
PortId: portID,
ChannelId: channelID,
Sequence: sequence,
CallbackId: callbackId,
CallbackArgs: callbackArgs,
}
k.Logger(ctx).Info(utils.LogWithHostZone(chainId, "Storing callback data: %+v", callback))
k.ICACallbacksKeeper.SetCallbackData(ctx, callback)
}
return sequence, nil
}
func (k Keeper) GetLightClientHeightSafely(ctx sdk.Context, connectionID string) (uint64, error) {
// get light client's latest height
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 0, 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 0, fmt.Errorf(errMsg)
} else {
latestHeightHostZone, err := cast.ToUint64E(clientState.GetLatestHeight().GetRevisionHeight())
if err != nil {
errMsg := fmt.Sprintf("error casting latest height to int64: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return 0, fmt.Errorf(errMsg)
}
return latestHeightHostZone, nil
}
}
func (k Keeper) GetLightClientTimeSafely(ctx sdk.Context, connectionID string) (uint64, error) {
// get light client's latest height
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 0, fmt.Errorf(errMsg)
}
// TODO(TEST-112) make sure to update host LCs here!
latestConsensusClientState, found := k.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(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 0, fmt.Errorf(errMsg)
} else {
latestTime := latestConsensusClientState.GetTimestamp()
return latestTime, nil
}
}
// Submit a validator sharesToTokens rate ICQ as triggered either manually or epochly with a conservative timeout
func (k Keeper) QueryValidatorSharesToTokensRate(ctx sdk.Context, chainId string, validatorAddress string) error {
timeoutDuration := time.Hour * 24
timeoutPolicy := icqtypes.TimeoutPolicy_REJECT_QUERY_RESPONSE
callbackData := []byte{}
return k.SubmitValidatorSharesToTokensRateICQ(ctx, chainId, validatorAddress, callbackData, timeoutDuration, timeoutPolicy)
}
// Submits an ICQ to get a validator's shares to tokens rate
func (k Keeper) SubmitValidatorSharesToTokensRateICQ(
ctx sdk.Context,
chainId string,
validatorAddress string,
callbackDataBz []byte,
timeoutDuration time.Duration,
timeoutPolicy icqtypes.TimeoutPolicy,
) error {
k.Logger(ctx).Info(utils.LogWithHostZone(chainId, "Submitting ICQ for validator sharesToTokens rate to %s", validatorAddress))
// Confirm the host zone exists
hostZone, found := k.GetHostZone(ctx, chainId)
if !found {
return errorsmod.Wrapf(types.ErrInvalidHostZone, "Host zone not found (%s)", chainId)
}
// check that the validator address matches the bech32 prefix of the hz
if !strings.Contains(validatorAddress, hostZone.Bech32Prefix) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "validator operator address must match the host zone bech32 prefix")
}
// Encode the validator address to form the query request
_, validatorAddressBz, err := bech32.DecodeAndConvert(validatorAddress)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid validator operator address, could not decode (%s)", err.Error())
}
queryData := stakingtypes.GetValidatorKey(validatorAddressBz)
// Submit validator sharesToTokens rate ICQ
// Considering this query is executed manually, we can be conservative with the timeout
query := icqtypes.Query{
ChainId: hostZone.ChainId,
ConnectionId: hostZone.ConnectionId,
QueryType: icqtypes.STAKING_STORE_QUERY_WITH_PROOF,
RequestData: queryData,
CallbackModule: types.ModuleName,
CallbackId: ICQCallbackID_Validator,
CallbackData: callbackDataBz,
TimeoutDuration: timeoutDuration,
TimeoutPolicy: timeoutPolicy,
}
if err := k.InterchainQueryKeeper.SubmitICQRequest(ctx, query, true); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Error submitting ICQ for validator sharesToTokens rate, error %s", err.Error()))
return err
}
return nil
}
// Submits an ICQ to get a validator's delegations
// This is called after the validator's sharesToTokens rate is determined
// The timeoutDuration parameter represents the length of the timeout (not to be confused with an actual timestamp)
func (k Keeper) SubmitDelegationICQ(ctx sdk.Context, hostZone types.HostZone, validatorAddress string) error {
if hostZone.DelegationIcaAddress == "" {
return errorsmod.Wrapf(types.ErrICAAccountNotFound, "no delegation address found for %s", hostZone.ChainId)
}
validator, valIndex, found := GetValidatorFromAddress(hostZone.Validators, validatorAddress)
if !found {
return errorsmod.Wrapf(types.ErrValidatorNotFound, "no registered validator for address (%s)", validatorAddress)
}
// Only submit the query if there's not already one in progress
if validator.SlashQueryInProgress {
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Delegations ICQ already in progress"))
return nil
}
k.Logger(ctx).Info(utils.LogWithHostZone(hostZone.ChainId, "Submitting ICQ for delegations to %s", validatorAddress))
// Get the validator and delegator encoded addresses to form the query request
_, validatorAddressBz, err := bech32.DecodeAndConvert(validatorAddress)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid validator address, could not decode (%s)", err.Error())
}
_, delegatorAddressBz, err := bech32.DecodeAndConvert(hostZone.DelegationIcaAddress)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid delegator address, could not decode (%s)", err.Error())
}
queryData := stakingtypes.GetDelegationKey(delegatorAddressBz, validatorAddressBz)
// Store the current validator's delegation in the callback data so we can determine if it changed
// while the query was in flight
callbackData := types.DelegatorSharesQueryCallback{
InitialValidatorDelegation: validator.Delegation,
}
callbackDataBz, err := proto.Marshal(&callbackData)
if err != nil {
return errorsmod.Wrapf(err, "unable to marshal delegator shares callback data")
}
// Update the validator to indicate that the slash query is in progress
validator.SlashQueryInProgress = true
hostZone.Validators[valIndex] = &validator
k.SetHostZone(ctx, hostZone)
// Submit delegator shares ICQ
query := icqtypes.Query{
ChainId: hostZone.ChainId,
ConnectionId: hostZone.ConnectionId,
QueryType: icqtypes.STAKING_STORE_QUERY_WITH_PROOF,
RequestData: queryData,
CallbackModule: types.ModuleName,
CallbackId: ICQCallbackID_Delegation,
CallbackData: callbackDataBz,
TimeoutDuration: time.Hour,
TimeoutPolicy: icqtypes.TimeoutPolicy_RETRY_QUERY_REQUEST,
}
if err := k.InterchainQueryKeeper.SubmitICQRequest(ctx, query, false); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Error submitting ICQ for delegation, error : %s", err.Error()))
return err
}
return nil
}
// Submits an ICQ to get a validator's delegations
// This is called after the validator's sharesToTokens rate is determined
// The timeoutDuration parameter represents the length of the timeout (not to be confused with an actual timestamp)
func (k Keeper) SubmitCalibrationICQ(ctx sdk.Context, hostZone types.HostZone, validatorAddress string) error {
if hostZone.DelegationIcaAddress == "" {
return errorsmod.Wrapf(types.ErrICAAccountNotFound, "no delegation address found for %s", hostZone.ChainId)
}
validator, valIndex, found := GetValidatorFromAddress(hostZone.Validators, validatorAddress)
if !found {
return errorsmod.Wrapf(types.ErrValidatorNotFound, "no registered validator for address (%s)", validatorAddress)
}
// Get the validator and delegator encoded addresses to form the query request
_, validatorAddressBz, err := bech32.DecodeAndConvert(validatorAddress)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid validator address, could not decode (%s)", err.Error())
}
_, delegatorAddressBz, err := bech32.DecodeAndConvert(hostZone.DelegationIcaAddress)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid delegator address, could not decode (%s)", err.Error())
}
queryData := stakingtypes.GetDelegationKey(delegatorAddressBz, validatorAddressBz)
// Store the current validator's delegation in the callback data so we can determine if it changed
// while the query was in flight
callbackData := types.DelegatorSharesQueryCallback{
InitialValidatorDelegation: validator.Delegation,
}
callbackDataBz, err := proto.Marshal(&callbackData)
if err != nil {
return errorsmod.Wrapf(err, "unable to marshal delegator shares callback data")
}
// Update the validator to indicate that the slash query is in progress
validator.SlashQueryInProgress = true
hostZone.Validators[valIndex] = &validator
k.SetHostZone(ctx, hostZone)
// Submit delegator shares ICQ
query := icqtypes.Query{
ChainId: hostZone.ChainId,
ConnectionId: hostZone.ConnectionId,
QueryType: icqtypes.STAKING_STORE_QUERY_WITH_PROOF,
RequestData: queryData,
CallbackModule: types.ModuleName,
CallbackId: ICQCallbackID_Calibrate,
CallbackData: callbackDataBz,
TimeoutDuration: time.Hour,
TimeoutPolicy: icqtypes.TimeoutPolicy_RETRY_QUERY_REQUEST,
}
if err := k.InterchainQueryKeeper.SubmitICQRequest(ctx, query, false); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("Error submitting ICQ for delegation, error : %s", err.Error()))
return err
}
return nil
}