/
icacallbacks_rebalance.go
115 lines (98 loc) · 4.8 KB
/
icacallbacks_rebalance.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
package keeper
import (
"fmt"
"github.com/Stride-Labs/stride/v18/utils"
icacallbackstypes "github.com/Stride-Labs/stride/v18/x/icacallbacks/types"
"github.com/Stride-Labs/stride/v18/x/stakeibc/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 rebalance callback arguments
func (k Keeper) MarshalRebalanceCallbackArgs(ctx sdk.Context, rebalanceCallback types.RebalanceCallback) ([]byte, error) {
out, err := proto.Marshal(&rebalanceCallback)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("MarshalRebalanceCallbackArgs %v", err.Error()))
return nil, err
}
return out, nil
}
// Unmarshalls rebalance callback arguments into a RebalanceCallback struct
func (k Keeper) UnmarshalRebalanceCallbackArgs(ctx sdk.Context, rebalanceCallback []byte) (*types.RebalanceCallback, error) {
unmarshalledRebalanceCallback := types.RebalanceCallback{}
if err := proto.Unmarshal(rebalanceCallback, &unmarshalledRebalanceCallback); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("UnmarshalRebalanceCallbackArgs %v", err.Error()))
return nil, err
}
return &unmarshalledRebalanceCallback, nil
}
// ICA Callback after rebalance validators on a host zone
// * If successful: Updates relevant validator delegations on the host zone struct
// * If timeout/failure: Does nothing
func (k Keeper) RebalanceCallback(ctx sdk.Context, packet channeltypes.Packet, ackResponse *icacallbackstypes.AcknowledgementResponse, args []byte) error {
// Fetch callback args
rebalanceCallback, err := k.UnmarshalRebalanceCallbackArgs(ctx, args)
if err != nil {
return errorsmod.Wrapf(types.ErrUnmarshalFailure, fmt.Sprintf("Unable to unmarshal rebalance callback args: %s", err.Error()))
}
chainId := rebalanceCallback.HostZoneId
k.Logger(ctx).Info(utils.LogICACallbackWithHostZone(chainId, ICACallbackID_Rebalance, "Starting rebalance callback"))
// Regardless of failure/success/timeout, indicate that this ICA has completed
hostZone, found := k.GetHostZone(ctx, rebalanceCallback.HostZoneId)
if !found {
return errorsmod.Wrapf(sdkerrors.ErrKeyNotFound, "Host zone not found: %s", rebalanceCallback.HostZoneId)
}
for _, rebalancing := range rebalanceCallback.Rebalancings {
if err := k.DecrementValidatorDelegationChangesInProgress(&hostZone, rebalancing.SrcValidator); err != nil {
return err
}
if err := k.DecrementValidatorDelegationChangesInProgress(&hostZone, rebalancing.DstValidator); err != nil {
return err
}
}
k.SetHostZone(ctx, hostZone)
// Check for timeout (ack nil)
// No action is necessary on a timeout
if ackResponse.Status == icacallbackstypes.AckResponseStatus_TIMEOUT {
k.Logger(ctx).Error(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Rebalance,
icacallbackstypes.AckResponseStatus_TIMEOUT, packet))
return nil
}
// Check for a failed transaction (ack error)
// No action is necessary on a failure
if ackResponse.Status == icacallbackstypes.AckResponseStatus_FAILURE {
k.Logger(ctx).Error(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Rebalance,
icacallbackstypes.AckResponseStatus_FAILURE, packet))
return nil
}
k.Logger(ctx).Info(utils.LogICACallbackStatusWithHostZone(chainId, ICACallbackID_Rebalance,
icacallbackstypes.AckResponseStatus_SUCCESS, packet))
// Assemble a map from validatorAddress -> validator
valAddrMap := make(map[string]*types.Validator)
for _, val := range hostZone.Validators {
valAddrMap[val.Address] = val
}
// For each re-delegation transaction, update the relevant validators on the host zone
for _, rebalancing := range rebalanceCallback.Rebalancings {
srcValidator := rebalancing.SrcValidator
dstValidator := rebalancing.DstValidator
if _, valFound := valAddrMap[srcValidator]; !valFound {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "source validator not found %s", srcValidator)
}
if _, valFound := valAddrMap[dstValidator]; !valFound {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "destination validator not found %s", dstValidator)
}
// Decrement the delegation from the source validator and increment the delegation
// for the destination validator
valAddrMap[srcValidator].Delegation = valAddrMap[srcValidator].Delegation.Sub(rebalancing.Amt)
valAddrMap[dstValidator].Delegation = valAddrMap[dstValidator].Delegation.Add(rebalancing.Amt)
k.Logger(ctx).Info(utils.LogICACallbackWithHostZone(chainId, ICACallbackID_Rebalance,
" Decrementing delegation on %s by %v", srcValidator, rebalancing.Amt))
k.Logger(ctx).Info(utils.LogICACallbackWithHostZone(chainId, ICACallbackID_Rebalance,
" Incrementing delegation on %s by %v", dstValidator, rebalancing.Amt))
}
k.SetHostZone(ctx, hostZone)
return nil
}