-
Notifications
You must be signed in to change notification settings - Fork 586
/
keeper.go
157 lines (129 loc) · 4.46 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
156
157
package keeper
import (
"fmt"
"strings"
baseapp "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
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"
host "github.com/cosmos/ibc-go/modules/core/24-host"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
)
// Keeper defines the IBC transfer keeper
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
hook types.IBCAccountHooks
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
accountKeeper types.AccountKeeper
scopedKeeper capabilitykeeper.ScopedKeeper
msgRouter *baseapp.MsgServiceRouter
memKey sdk.StoreKey
}
// NewKeeper creates a new interchain account Keeper instance
func NewKeeper(
memKey sdk.StoreKey,
cdc codec.BinaryCodec, key sdk.StoreKey,
channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper,
accountKeeper types.AccountKeeper, scopedKeeper capabilitykeeper.ScopedKeeper, msgRouter *baseapp.MsgServiceRouter, hook types.IBCAccountHooks,
) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
accountKeeper: accountKeeper,
scopedKeeper: scopedKeeper,
msgRouter: msgRouter,
memKey: memKey,
hook: hook,
}
}
func (k Keeper) SerializeCosmosTx(cdc codec.BinaryCodec, data interface{}) ([]byte, error) {
msgs := make([]sdk.Msg, 0)
switch data := data.(type) {
case sdk.Msg:
msgs = append(msgs, data)
case []sdk.Msg:
msgs = append(msgs, data...)
default:
return nil, types.ErrInvalidOutgoingData
}
msgAnys := make([]*codectypes.Any, len(msgs))
for i, msg := range msgs {
var err error
msgAnys[i], err = codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
}
txBody := &types.IBCTxBody{
Messages: msgAnys,
}
txRaw := &types.IBCTxRaw{
BodyBytes: cdc.MustMarshal(txBody),
}
bz, err := cdc.Marshal(txRaw)
if err != nil {
return nil, err
}
return bz, nil
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, types.ModuleName))
}
// IsBound checks if the interchain account module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}
// BindPort defines a wrapper function for the port Keeper's BindPort function in
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
// Set the portID into our store so we can retrieve it later
store := ctx.KVStore(k.storeKey)
store.Set([]byte(types.PortKey), []byte(portID))
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
}
// GetPort returns the portID for the interchain accounts module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get([]byte(types.PortKey)))
}
// ClaimCapability allows the transfer module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}
// Utility function for parsing the connection number from the connection-id
func getConnectionNumber(connectionId string) string {
ss := strings.Split(connectionId, "-")
return ss[len(ss)-1]
}
func (k Keeper) GeneratePortId(owner, connectionId string) string {
ownerId := strings.TrimSpace(owner)
connectionNumber := getConnectionNumber(connectionId)
portId := types.IcaPrefix + connectionNumber + "-" + ownerId
return portId
}
func (k Keeper) SetActiveChannel(ctx sdk.Context, portId, channelId string) error {
store := ctx.KVStore(k.storeKey)
key := types.KeyActiveChannel(portId)
store.Set(key, []byte(channelId))
return nil
}
func (k Keeper) GetActiveChannel(ctx sdk.Context, portId string) (string, error) {
store := ctx.KVStore(k.storeKey)
key := types.KeyActiveChannel(portId)
if !store.Has(key) {
return "", sdkerrors.Wrap(types.ErrActiveChannelNotFound, portId)
}
activeChannel := string(store.Get(key))
return activeChannel, nil
}