/
icacallbacks_redemption.go
93 lines (80 loc) · 3.77 KB
/
icacallbacks_redemption.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
package keeper
import (
"fmt"
"github.com/Stride-Labs/stride/v6/utils"
icacallbackstypes "github.com/Stride-Labs/stride/v6/x/icacallbacks/types"
recordstypes "github.com/Stride-Labs/stride/v6/x/records/types"
"github.com/Stride-Labs/stride/v6/x/stakeibc/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
"github.com/golang/protobuf/proto" //nolint:staticcheck
)
// Marshalls redemption callback arguments
func (k Keeper) MarshalRedemptionCallbackArgs(ctx sdk.Context, redemptionCallback types.RedemptionCallback) ([]byte, error) {
out, err := proto.Marshal(&redemptionCallback)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("MarshalRedemptionCallbackArgs | %s", err.Error()))
return nil, err
}
return out, nil
}
// Unmarshalls redemption callback arguments into a RedemptionCallback struct
func (k Keeper) UnmarshalRedemptionCallbackArgs(ctx sdk.Context, redemptionCallback []byte) (types.RedemptionCallback, error) {
unmarshalledRedemptionCallback := types.RedemptionCallback{}
if err := proto.Unmarshal(redemptionCallback, &unmarshalledRedemptionCallback); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("UnmarshalRedemptionCallbackArgs | %s", err.Error()))
return unmarshalledRedemptionCallback, err
}
return unmarshalledRedemptionCallback, nil
}
// ICA Callback after undelegating
// If successful:
// * Updates epoch unbonding record status
// If timeout:
// * Does nothing
// If failure:
// * Reverts epoch unbonding record status
func RedemptionCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error {
// Fetch callback args
redemptionCallback, err := k.UnmarshalRedemptionCallbackArgs(ctx, args)
if err != nil {
return sdkerrors.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal redemption callback args: %s", err.Error()))
}
chainId := redemptionCallback.HostZoneId
k.Logger(ctx).Info(utils.LogICACallbackWithHostZone(chainId, ICACallbackID_Redemption,
"Starting redemption callback for Epoch Unbonding Records: %+v", redemptionCallback.EpochUnbondingRecordIds))
// Check for timeout (ack nil)
// No need to reset the unbonding 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_Redemption,
icacallbackstypes.AckResponseStatus_TIMEOUT, packet))
return nil
}
// Check for a failed transaction (ack error)
// Reset the unbonding record status upon failure
if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE {
k.Logger(ctx).Error(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Redemption,
icacallbackstypes.AckResponseStatus_FAILURE, packet))
// Reset unbondings record status
err = k.RecordsKeeper.SetHostZoneUnbondings(ctx, chainId, redemptionCallback.EpochUnbondingRecordIds, recordstypes.HostZoneUnbonding_EXIT_TRANSFER_QUEUE)
if err != nil {
return err
}
return nil
}
k.Logger(ctx).Info(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Redemption,
icacallbackstypes.AckResponseStatus_SUCCESS, packet))
// Confirm host zone exists
_, found := k.GetHostZone(ctx, chainId)
if !found {
return sdkerrors.Wrapf(sdkerrors.ErrKeyNotFound, "Host zone not found: %s", chainId)
}
// Upon success, update the unbonding record status to CLAIMABLE
err = k.RecordsKeeper.SetHostZoneUnbondings(ctx, chainId, redemptionCallback.EpochUnbondingRecordIds, recordstypes.HostZoneUnbonding_CLAIMABLE)
if err != nil {
return err
}
k.Logger(ctx).Info(fmt.Sprintf("[REDEMPTION] completed on %s", chainId))
return nil
}