/
expected_keepers.go
133 lines (117 loc) · 5.05 KB
/
expected_keepers.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
package types
import (
sdkerrors "cosmossdk.io/errors"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
connTypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
)
// AccountKeeper defines the expected account keeper used for simulations (noalias)
type AccountKeeper interface {
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
// Methods imported from account should be defined here
}
// BankKeeper defines the expected interface needed to retrieve account balances.
type BankKeeper interface {
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
SendCoins(ctx sdk.Context, from, to sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error
SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error
// Methods imported from bank should be defined here
}
// ConnectionKeeper defines the expected interfaces needed to retrieve connection info
type ConnectionKeeper interface {
GetConnection(ctx sdk.Context, connectionID string) (connTypes.ConnectionEnd, bool)
}
// ChannelKeeper defines the expected IBC channel keeper
type ChannelKeeper interface {
GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool)
GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool)
SendPacket(
ctx sdk.Context,
channelCap *capabilitytypes.Capability,
sourcePort string,
sourceChannel string,
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (uint64, error)
ChanCloseInit(ctx sdk.Context, portID, channelID string, chanCap *capabilitytypes.Capability) error
}
// PortKeeper defines the expected IBC port keeper
type PortKeeper interface {
BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability
}
// ScopedKeeper defines the expected IBC scoped keeper
type ScopedKeeper interface {
GetCapability(ctx sdk.Context, name string) (*capabilitytypes.Capability, bool)
AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool
ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error
}
// IBCKeeper defines the IBC Keeper
type IBCKeeper struct {
portKey []byte
storeKey storetypes.StoreKey
ChannelKeeper ChannelKeeper
PortKeeper PortKeeper
ScopedKeeper ScopedKeeper
}
// NewKeeper create an IBC Keeper
func NewIBCKeeper(
portKey []byte,
storeKey storetypes.StoreKey,
channelKeeper ChannelKeeper,
portKeeper PortKeeper,
scopedKeeper ScopedKeeper,
) *IBCKeeper {
return &IBCKeeper{
portKey: portKey,
storeKey: storeKey,
ChannelKeeper: channelKeeper,
PortKeeper: portKeeper,
ScopedKeeper: scopedKeeper,
}
}
// ChanCloseInit defines a wrapper function for the channel Keeper's function
func (k IBCKeeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error {
capName := host.ChannelCapabilityPath(portID, channelID)
chanCap, ok := k.ScopedKeeper.GetCapability(ctx, capName)
if !ok {
return sdkerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
}
return k.ChannelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap)
}
// IsBound checks if the module is already bound to the desired port
func (k IBCKeeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.ScopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}
// BindPort defines a wrapper function for the ort Keeper's function in
// order to expose it to module's InitGenesis function
func (k IBCKeeper) BindPort(ctx sdk.Context, portID string) error {
cap := k.PortKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
}
// GetPort returns the portID for the module. Used in ExportGenesis
func (k IBCKeeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get(k.portKey))
}
// SetPort sets the portID for the module. Used in InitGenesis
func (k IBCKeeper) SetPort(ctx sdk.Context, portID string) {
store := ctx.KVStore(k.storeKey)
store.Set(k.portKey, []byte(portID))
}
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
func (k IBCKeeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.ScopedKeeper.AuthenticateCapability(ctx, cap, name)
}
// ClaimCapability allows the module that can claim a capability that IBC module passes to it
func (k IBCKeeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.ScopedKeeper.ClaimCapability(ctx, cap, name)
}