125 lines
4.7 KiB
Go
125 lines
4.7 KiB
Go
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"
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
"github.com/cosmos/cosmos-sdk/x/capability"
|
|
channel "github.com/cosmos/cosmos-sdk/x/ibc/04-channel"
|
|
channelexported "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/exported"
|
|
porttypes "github.com/cosmos/cosmos-sdk/x/ibc/05-port/types"
|
|
"github.com/cosmos/cosmos-sdk/x/ibc/20-transfer/types"
|
|
ibctypes "github.com/cosmos/cosmos-sdk/x/ibc/types"
|
|
)
|
|
|
|
const (
|
|
// DefaultPacketTimeoutHeight is the default packet timeout height relative
|
|
// to the current block height. The timeout is disabled when set to 0.
|
|
DefaultPacketTimeoutHeight = 1000 // NOTE: in blocks
|
|
|
|
// DefaultPacketTimeoutTimestamp is the default packet timeout timestamp relative
|
|
// to the current block timestamp. The timeout is disabled when set to 0.
|
|
DefaultPacketTimeoutTimestamp = 0 // NOTE: in nanoseconds
|
|
)
|
|
|
|
// Keeper defines the IBC transfer keeper
|
|
type Keeper struct {
|
|
storeKey sdk.StoreKey
|
|
cdc *codec.Codec
|
|
|
|
channelKeeper types.ChannelKeeper
|
|
portKeeper types.PortKeeper
|
|
authKeeper types.AccountKeeper
|
|
bankKeeper types.BankKeeper
|
|
scopedKeeper capability.ScopedKeeper
|
|
}
|
|
|
|
// NewKeeper creates a new IBC transfer Keeper instance
|
|
func NewKeeper(
|
|
cdc *codec.Codec, key sdk.StoreKey,
|
|
channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper,
|
|
authKeeper types.AccountKeeper, bankKeeper types.BankKeeper, scopedKeeper capability.ScopedKeeper,
|
|
) Keeper {
|
|
|
|
// ensure ibc transfer module account is set
|
|
if addr := authKeeper.GetModuleAddress(types.GetModuleAccountName()); addr == nil {
|
|
panic("the IBC transfer module account has not been set")
|
|
}
|
|
|
|
return Keeper{
|
|
storeKey: key,
|
|
cdc: cdc,
|
|
channelKeeper: channelKeeper,
|
|
portKeeper: portKeeper,
|
|
authKeeper: authKeeper,
|
|
bankKeeper: bankKeeper,
|
|
scopedKeeper: scopedKeeper,
|
|
}
|
|
}
|
|
|
|
// Logger returns a module-specific logger.
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
return ctx.Logger().With("module", fmt.Sprintf("x/%s/%s", ibctypes.ModuleName, types.ModuleName))
|
|
}
|
|
|
|
// GetTransferAccount returns the ICS20 - transfers ModuleAccount
|
|
func (k Keeper) GetTransferAccount(ctx sdk.Context) authexported.ModuleAccountI {
|
|
return k.authKeeper.GetModuleAccount(ctx, types.GetModuleAccountName())
|
|
}
|
|
|
|
// PacketExecuted defines a wrapper function for the channel Keeper's function
|
|
// in order to expose it to the ICS20 transfer handler.
|
|
// Keeper retreives channel capability and passes it into channel keeper for authentication
|
|
func (k Keeper) PacketExecuted(ctx sdk.Context, packet channelexported.PacketI, acknowledgement []byte) error {
|
|
chanCap, ok := k.scopedKeeper.GetCapability(ctx, ibctypes.ChannelCapabilityPath(packet.GetDestPort(), packet.GetDestChannel()))
|
|
if !ok {
|
|
return sdkerrors.Wrap(channel.ErrChannelCapabilityNotFound, "channel capability could not be retrieved for packet")
|
|
}
|
|
return k.channelKeeper.PacketExecuted(ctx, chanCap, packet, acknowledgement)
|
|
}
|
|
|
|
// ChanCloseInit defines a wrapper function for the channel Keeper's function
|
|
// in order to expose it to the ICS20 trasfer handler.
|
|
func (k Keeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error {
|
|
capName := ibctypes.ChannelCapabilityPath(portID, channelID)
|
|
chanCap, ok := k.scopedKeeper.GetCapability(ctx, capName)
|
|
if !ok {
|
|
return sdkerrors.Wrapf(channel.ErrChannelCapabilityNotFound, "could not retrieve channel capability at: %s", capName)
|
|
}
|
|
return k.channelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap)
|
|
}
|
|
|
|
// IsBound checks if the transfer module is already bound to the desired port
|
|
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
|
|
_, ok := k.scopedKeeper.GetCapability(ctx, porttypes.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 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, porttypes.PortPath(portID))
|
|
}
|
|
|
|
// GetPort returns the portID for the transfer 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 *capability.Capability, name string) error {
|
|
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
|
|
}
|