-
Notifications
You must be signed in to change notification settings - Fork 201
/
keeper.go
136 lines (117 loc) · 4.71 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
package keeper
import (
"fmt"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
errorsmod "cosmossdk.io/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
"github.com/Stride-Labs/stride/v12/x/icacallbacks/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
)
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
icacallbacks map[string]types.ICACallbackHandler
IBCKeeper ibckeeper.Keeper
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
ibcKeeper ibckeeper.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,
icacallbacks: make(map[string]types.ICACallbackHandler),
IBCKeeper: ibcKeeper,
}
}
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
}
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
}
// fetch the callback function
callbackHandler, err := k.GetICACallbackHandler(module)
if err != nil {
return nil, errorsmod.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 errorsmod.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
}