-
Notifications
You must be signed in to change notification settings - Fork 197
/
keeper.go
155 lines (136 loc) · 5.78 KB
/
keeper.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
package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper"
"github.com/Stride-Labs/stride/v6/x/icacallbacks/types"
recordstypes "github.com/Stride-Labs/stride/v6/x/records/types"
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
icacontrollerkeeper "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/keeper"
)
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
scopedKeeper capabilitykeeper.ScopedKeeper
icacallbacks map[string]types.ICACallbackHandler
IBCKeeper ibckeeper.Keeper
ICAControllerKeeper icacontrollerkeeper.Keeper
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
scopedKeeper capabilitykeeper.ScopedKeeper,
ibcKeeper ibckeeper.Keeper,
icacontrollerkeeper icacontrollerkeeper.Keeper,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
scopedKeeper: scopedKeeper,
icacallbacks: make(map[string]types.ICACallbackHandler),
IBCKeeper: ibcKeeper,
ICAControllerKeeper: icacontrollerkeeper,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// Should we add a `AddICACallback`
func (k *Keeper) SetICACallbackHandler(module string, handler types.ICACallbackHandler) error {
_, found := k.icacallbacks[module]
if found {
return fmt.Errorf("callback handler already set for %s", module)
}
k.icacallbacks[module] = handler.RegisterICACallbacks()
return nil
}
func (k *Keeper) GetICACallbackHandler(module string) (types.ICACallbackHandler, error) {
callback, found := k.icacallbacks[module]
if !found {
return nil, fmt.Errorf("no callback handler found for %s", module)
}
return callback, nil
}
// ClaimCapability claims the channel capability passed via the OnOpenChanInit callback
func (k *Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
func (k Keeper) GetCallbackDataFromPacket(ctx sdk.Context, modulePacket channeltypes.Packet, callbackDataKey string) (cbd *types.CallbackData, found bool) {
// get the relevant module from the channel and port
portID := modulePacket.GetSourcePort()
channelID := modulePacket.GetSourceChannel()
// fetch the callback data
callbackData, found := k.GetCallbackData(ctx, callbackDataKey)
if !found {
k.Logger(ctx).Info(fmt.Sprintf("callback data not found for portID: %s, channelID: %s, sequence: %d", portID, channelID, modulePacket.Sequence))
return nil, false
} else {
k.Logger(ctx).Info(fmt.Sprintf("callback data found for portID: %s, channelID: %s, sequence: %d", portID, channelID, modulePacket.Sequence))
}
return &callbackData, true
}
func (k Keeper) GetICACallbackHandlerFromPacket(ctx sdk.Context, modulePacket channeltypes.Packet) (*types.ICACallbackHandler, error) {
module, _, err := k.IBCKeeper.ChannelKeeper.LookupModuleByChannel(ctx, modulePacket.GetSourcePort(), modulePacket.GetSourceChannel())
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("error LookupModuleByChannel for portID: %s, channelID: %s, sequence: %d", modulePacket.GetSourcePort(), modulePacket.GetSourceChannel(), modulePacket.Sequence))
return nil, err
}
// redirect transfer callbacks to the records module
// is there a better way to do this?
if module == "transfer" {
module = recordstypes.ModuleName
}
// fetch the callback function
callbackHandler, err := k.GetICACallbackHandler(module)
if err != nil {
return nil, sdkerrors.Wrapf(types.ErrCallbackHandlerNotFound, "Callback handler does not exist for module %s | err: %s", module, err.Error())
}
return &callbackHandler, nil
}
func (k Keeper) CallRegisteredICACallback(ctx sdk.Context, modulePacket channeltypes.Packet, ackResponse *types.AcknowledgementResponse) error {
callbackDataKey := types.PacketID(modulePacket.GetSourcePort(), modulePacket.GetSourceChannel(), modulePacket.Sequence)
callbackData, found := k.GetCallbackDataFromPacket(ctx, modulePacket, callbackDataKey)
if !found {
return nil
}
callbackHandler, err := k.GetICACallbackHandlerFromPacket(ctx, modulePacket)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("GetICACallbackHandlerFromPacket %s", err.Error()))
return err
}
// call the callback
if (*callbackHandler).HasICACallback(callbackData.CallbackId) {
k.Logger(ctx).Info(fmt.Sprintf("Calling callback for %s", callbackData.CallbackId))
// if acknowledgement is empty, then it is a timeout
err := (*callbackHandler).CallICACallback(ctx, callbackData.CallbackId, modulePacket, ackResponse, callbackData.CallbackArgs)
if err != nil {
errMsg := fmt.Sprintf("Error occured while calling ICACallback (%s) | err: %s", callbackData.CallbackId, err.Error())
k.Logger(ctx).Error(errMsg)
return sdkerrors.Wrapf(types.ErrCallbackFailed, errMsg)
}
} else {
k.Logger(ctx).Error(fmt.Sprintf("Callback %v has no associated callback", callbackData))
}
// remove the callback data
k.RemoveCallbackData(ctx, callbackDataKey)
return nil
}