/
icacallbacks_delegate.go
116 lines (100 loc) · 4.75 KB
/
icacallbacks_delegate.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
package keeper
import (
"errors"
"fmt"
"github.com/spf13/cast"
"github.com/Stride-Labs/stride/v16/utils"
recordstypes "github.com/Stride-Labs/stride/v16/x/records/types"
"github.com/Stride-Labs/stride/v16/x/stakeibc/types"
icacallbackstypes "github.com/Stride-Labs/stride/v16/x/icacallbacks/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/gogoproto/proto"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
)
// Marshalls delegate callback arguments
func (k Keeper) MarshalDelegateCallbackArgs(ctx sdk.Context, delegateCallback types.DelegateCallback) ([]byte, error) {
out, err := proto.Marshal(&delegateCallback)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("MarshalDelegateCallbackArgs %v", err.Error()))
return nil, err
}
return out, nil
}
// Unmarshalls delegate callback arguments into a DelegateCallback struct
func (k Keeper) UnmarshalDelegateCallbackArgs(ctx sdk.Context, delegateCallback []byte) (*types.DelegateCallback, error) {
unmarshalledDelegateCallback := types.DelegateCallback{}
if err := proto.Unmarshal(delegateCallback, &unmarshalledDelegateCallback); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("UnmarshalDelegateCallbackArgs %v", err.Error()))
return nil, err
}
return &unmarshalledDelegateCallback, nil
}
// ICA Callback after delegating deposit records
// * If successful: Updates deposit record status and records delegation changes on the host zone and validators
// * If timeout: Does nothing
// * If failure: Reverts deposit record status
func (k Keeper) DelegateCallback(ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error {
// Deserialize the callback args
delegateCallback, err := k.UnmarshalDelegateCallbackArgs(ctx, args)
if err != nil {
return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal delegate callback args: %s", err.Error()))
}
chainId := delegateCallback.HostZoneId
k.Logger(ctx).Info(utils.LogICACallbackWithHostZone(chainId, ICACallbackID_Delegate,
"Starting delegate callback for Deposit Record: %d", delegateCallback.DepositRecordId))
// Confirm chainId and deposit record Id exist
hostZone, found := k.GetHostZone(ctx, chainId)
if !found {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "host zone not found %s", chainId)
}
recordId := delegateCallback.DepositRecordId
depositRecord, found := k.RecordsKeeper.GetDepositRecord(ctx, recordId)
if !found {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "deposit record not found %d", recordId)
}
// Regardless of failure/success/timeout, indicate that this ICA has completed
for _, splitDelegation := range delegateCallback.SplitDelegations {
if err := k.DecrementValidatorDelegationChangesInProgress(&hostZone, splitDelegation.Validator); err != nil {
// TODO: Revert after v14 upgrade
if errors.Is(err, types.ErrInvalidValidatorDelegationUpdates) {
k.Logger(ctx).Error(utils.LogICACallbackWithHostZone(chainId, ICACallbackID_Delegate,
"Invariant failed - delegation changes in progress fell below 0 for %s", splitDelegation.Validator))
continue
}
return err
}
}
k.SetHostZone(ctx, hostZone)
// Check for timeout (ack nil)
// No need to reset the deposit record status since it will get reverted when the channel is restored
if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT {
k.Logger(ctx).Error(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Delegate,
icacallbackstypes.AckResponseStatus_TIMEOUT, packet))
return nil
}
// Check for a failed transaction (ack error)
// Reset the deposit record status upon failure
if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE {
k.Logger(ctx).Error(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Delegate,
icacallbackstypes.AckResponseStatus_FAILURE, packet))
// Reset deposit record status
depositRecord.Status = recordstypes.DepositRecord_DELEGATION_QUEUE
k.RecordsKeeper.SetDepositRecord(ctx, depositRecord)
return nil
}
k.Logger(ctx).Info(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Delegate,
icacallbackstypes.AckResponseStatus_SUCCESS, packet))
// Update delegations on the host zone
for _, splitDelegation := range delegateCallback.SplitDelegations {
err := k.AddDelegationToValidator(ctx, &hostZone, splitDelegation.Validator, splitDelegation.Amount, ICACallbackID_Delegate)
if err != nil {
return errorsmod.Wrapf(err, "Failed to add delegation to validator")
}
}
k.SetHostZone(ctx, hostZone)
k.RecordsKeeper.RemoveDepositRecord(ctx, cast.ToUint64(recordId))
k.Logger(ctx).Info(fmt.Sprintf("[DELEGATION] success on %s", chainId))
return nil
}