-
Notifications
You must be signed in to change notification settings - Fork 191
/
icacallbacks_delegate.go
89 lines (75 loc) · 3.02 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
package keeper
import (
"fmt"
"github.com/spf13/cast"
"github.com/Stride-Labs/stride/x/icacallbacks"
"github.com/Stride-Labs/stride/x/stakeibc/types"
icacallbackstypes "github.com/Stride-Labs/stride/x/icacallbacks/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
"github.com/golang/protobuf/proto"
)
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
}
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
}
func DelegateCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ack *channeltypes.Acknowledgement, args []byte) error {
k.Logger(ctx).Info("DelegateCallback executing", "packet", packet)
if ack == nil {
// timeout
k.Logger(ctx).Error(fmt.Sprintf("DelegateCallback timeout, ack is nil, packet %v", packet))
return nil
}
txMsgData, err := icacallbacks.GetTxMsgData(ctx, *ack, k.Logger(ctx))
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("failed to fetch txMsgData, packet %v", packet))
return sdkerrors.Wrap(icacallbackstypes.ErrTxMsgData, err.Error())
}
if len(txMsgData.Data) == 0 {
// failed transaction
k.Logger(ctx).Error(fmt.Sprintf("DelegateCallback tx failed, ack is empty (ack error), packet %v", packet))
return nil
}
// deserialize the args
delegateCallback, err := k.UnmarshalDelegateCallbackArgs(ctx, args)
if err != nil {
return err
}
k.Logger(ctx).Info(fmt.Sprintf("DelegateCallback %v", delegateCallback))
hostZone := delegateCallback.GetHostZoneId()
zone, found := k.GetHostZone(ctx, hostZone)
if !found {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "host zone not found %s", hostZone)
}
recordId := delegateCallback.GetDepositRecordId()
for _, splitDelegation := range delegateCallback.SplitDelegations {
amount, err := cast.ToInt64E(splitDelegation.Amount)
if err != nil {
return err
}
validator := splitDelegation.Validator
k.Logger(ctx).Info(fmt.Sprintf("incrementing stakedBal %d on %s", amount, validator))
zone.StakedBal += splitDelegation.Amount
success := k.AddDelegationToValidator(ctx, zone, validator, amount)
if !success {
return sdkerrors.Wrapf(types.ErrValidatorDelegationChg, "Failed to add delegation to validator")
}
k.SetHostZone(ctx, zone)
}
k.RecordsKeeper.RemoveDepositRecord(ctx, cast.ToUint64(recordId))
k.Logger(ctx).Info(fmt.Sprintf("[DELEGATION] success on %s", hostZone))
return nil
}