-
Notifications
You must be signed in to change notification settings - Fork 204
/
keeper.go
93 lines (80 loc) · 3.29 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
package keeper
import (
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ibckeeper "github.com/cosmos/ibc-go/v5/modules/core/keeper"
"github.com/tendermint/tendermint/libs/log"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/Stride-Labs/stride/v6/utils"
"github.com/Stride-Labs/stride/v6/x/interchainquery/types"
)
// Keeper of this module maintains collections of registered zones.
type Keeper struct {
cdc codec.Codec
storeKey storetypes.StoreKey
callbacks map[string]types.QueryCallbacks
IBCKeeper *ibckeeper.Keeper
}
// NewKeeper returns a new instance of zones Keeper
func NewKeeper(cdc codec.Codec, storeKey storetypes.StoreKey, ibckeeper *ibckeeper.Keeper) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
callbacks: make(map[string]types.QueryCallbacks),
IBCKeeper: ibckeeper,
}
}
func (k *Keeper) SetCallbackHandler(module string, handler types.QueryCallbacks) error {
_, found := k.callbacks[module]
if found {
return fmt.Errorf("callback handler already set for %s", module)
}
k.callbacks[module] = handler.RegisterICQCallbacks()
return nil
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k *Keeper) MakeRequest(ctx sdk.Context, module string, callbackId string, chainId string, connectionId string, queryType string, request []byte, ttl uint64) error {
k.Logger(ctx).Info(utils.LogWithHostZone(chainId,
"Submitting ICQ Request - module=%s, callbackId=%s, connectionId=%s, queryType=%s, ttl=%d", module, callbackId, connectionId, queryType, ttl))
// Confirm the connectionId and chainId are valid
if connectionId == "" {
errMsg := "[ICQ Validation Check] Failed! connection id cannot be empty"
k.Logger(ctx).Error(errMsg)
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, errMsg)
}
if !strings.HasPrefix(connectionId, "connection") {
errMsg := "[ICQ Validation Check] Failed! connection id must begin with 'connection'"
k.Logger(ctx).Error(errMsg)
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, errMsg)
}
if chainId == "" {
errMsg := "[ICQ Validation Check] Failed! chain_id cannot be empty"
k.Logger(ctx).Error(errMsg)
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, errMsg)
}
// Confirm the module and callbackId exist
if module != "" {
if _, exists := k.callbacks[module]; !exists {
err := fmt.Errorf("no callback handler registered for module %s", module)
k.Logger(ctx).Error(err.Error())
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "no callback handler registered for module")
}
if exists := k.callbacks[module].HasICQCallback(callbackId); !exists {
err := fmt.Errorf("no callback %s registered for module %s", callbackId, module)
k.Logger(ctx).Error(err.Error())
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "no callback handler registered for module")
}
}
// Save the query to the store
// If the same query is re-requested, it will get replace in the store with an updated TTL
// and the RequestSent bool reset to false
query := k.NewQuery(ctx, module, callbackId, chainId, connectionId, queryType, request, ttl)
k.SetQuery(ctx, *query)
return nil
}