From 3bd55077367fa2d7813588f1ee98f8adbfe4738e Mon Sep 17 00:00:00 2001 From: antstalepresh Date: Wed, 26 Jun 2024 20:46:04 +0800 Subject: [PATCH] Remove unused wasm hooks and channel keeper --- app/app.go | 27 +- proto/kujira/onion/params.proto | 2 - proto/kujira/onion/tx.proto | 15 - x/onion/client/cli/query.go | 39 -- x/onion/hooks.go | 145 ------- x/onion/ibc_module.go | 208 ++-------- x/onion/ibcutils.go | 78 ---- x/onion/ics4_middleware.go | 85 ---- x/onion/keeper/ibcutils.go | 33 -- x/onion/keeper/keeper.go | 217 +--------- x/onion/keeper/msg_server.go | 24 -- x/onion/types/codec.go | 2 - x/onion/types/expected_keepers.go | 11 - x/onion/types/msgs.go | 31 -- x/onion/types/params.go | 39 +- x/onion/types/params.pb.go | 75 +--- x/onion/types/tx.pb.go | 668 +----------------------------- x/onion/types/types.go | 84 ---- x/onion/wasm_hook.go | 381 ----------------- 19 files changed, 60 insertions(+), 2104 deletions(-) delete mode 100644 x/onion/hooks.go delete mode 100644 x/onion/ibcutils.go delete mode 100644 x/onion/ics4_middleware.go delete mode 100644 x/onion/keeper/ibcutils.go delete mode 100644 x/onion/wasm_hook.go diff --git a/app/app.go b/app/app.go index 04e0c779..5c98f4aa 100644 --- a/app/app.go +++ b/app/app.go @@ -300,8 +300,6 @@ type App struct { ParamsKeeper paramskeeper.Keeper IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly OnionKeeper *onionkeeper.Keeper - Ics20WasmHooks *onion.WasmHooks - HooksICS4Wrapper onion.ICS4Middleware IBCFeeKeeper ibcfeekeeper.Keeper ICAControllerKeeper icacontrollerkeeper.Keeper ICAHostKeeper icahostkeeper.Keeper @@ -558,15 +556,12 @@ func New( app.OnionKeeper = onionkeeper.NewKeeper( keys[oniontypes.StoreKey], app.GetSubspace(oniontypes.ModuleName), - app.IBCKeeper.ChannelKeeper, nil, app.MsgServiceRouter(), app.AccountKeeper, txConfig.SignModeHandler(), ) - app.WireICS20PreWasmKeeper(appCodec, bApp, app.OnionKeeper) - // IBC Fee Module keeper app.IBCFeeKeeper = ibcfeekeeper.NewKeeper( @@ -725,10 +720,6 @@ func New( wasmOpts..., ) - // Pass the contract keeper to all the structs (generally ICS4Wrappers for ibc middlewares) that need it - app.Ics20WasmHooks.ContractKeeper = &app.WasmKeeper - app.OnionKeeper.ContractKeeper = wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper) - // Register the proposal types // Deprecated: Avoid adding new handlers, instead use the new proposal flow // by granting the governance module the right to execute the message. @@ -783,7 +774,7 @@ func New( var transferStack ibcporttypes.IBCModule transferStack = transfer.NewIBCModule(app.TransferKeeper) transferStack = ibcfee.NewIBCMiddleware(transferStack, app.IBCFeeKeeper) - transferStack = onion.NewIBCMiddleware(transferStack, &app.HooksICS4Wrapper, app.OnionKeeper, encodingConfig.TxConfig) + transferStack = onion.NewIBCModule(transferStack, app.OnionKeeper, encodingConfig.TxConfig) // Create Interchain Accounts Stack // SendPacket, since it is originating from the application to core IBC: @@ -1207,22 +1198,6 @@ func New( return app } -// WireICS20PreWasmKeeper Create the IBC Transfer Stack from bottom to top: -func (app *App) WireICS20PreWasmKeeper( - appCodec codec.Codec, - bApp *baseapp.BaseApp, - hooksKeeper *onionkeeper.Keeper, -) { - // Setup the ICS4Wrapper used by the hooks middleware - addrPrefix := sdk.GetConfig().GetBech32AccountAddrPrefix() - wasmHooks := onion.NewWasmHooks(hooksKeeper, nil, addrPrefix) // The contract keeper needs to be set later - app.Ics20WasmHooks = &wasmHooks - app.HooksICS4Wrapper = onion.NewICS4Middleware( - app.IBCKeeper.ChannelKeeper, - app.Ics20WasmHooks, - ) -} - func (app *App) setPostHandler() { postHandler, err := posthandler.NewPostHandler( posthandler.HandlerOptions{}, diff --git a/proto/kujira/onion/params.proto b/proto/kujira/onion/params.proto index 9bbd27d5..9faa6c9b 100644 --- a/proto/kujira/onion/params.proto +++ b/proto/kujira/onion/params.proto @@ -8,6 +8,4 @@ import "google/protobuf/duration.proto"; option go_package = "github.com/Team-Kujira/core/x/onion/types"; message Params { - repeated string allowed_async_ack_contracts = 1 - [ (gogoproto.moretags) = "yaml:\"allowed_async_ack_contracts\"" ]; } diff --git a/proto/kujira/onion/tx.proto b/proto/kujira/onion/tx.proto index 5f8b85e0..145d3b7f 100644 --- a/proto/kujira/onion/tx.proto +++ b/proto/kujira/onion/tx.proto @@ -7,19 +7,4 @@ option go_package = "github.com/Team-Kujira/core/x/onion/types"; // Msg defines the Msg service. service Msg { - // EmitIBCAck checks the sender can emit the ack and writes the IBC - // acknowledgement - rpc EmitIBCAck(MsgEmitIBCAck) returns (MsgEmitIBCAckResponse); -} - -message MsgEmitIBCAck { - string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ]; - uint64 packet_sequence = 2 - [ (gogoproto.moretags) = "yaml:\"packet_sequence\"" ]; - string channel = 3 [ (gogoproto.moretags) = "yaml:\"channel\"" ]; -} -message MsgEmitIBCAckResponse { - string contract_result = 1 - [ (gogoproto.moretags) = "yaml:\"contract_result\"" ]; - string ibc_ack = 2 [ (gogoproto.moretags) = "yaml:\"ibc_ack\"" ]; } diff --git a/x/onion/client/cli/query.go b/x/onion/client/cli/query.go index 3ca04fb8..bc1991bf 100644 --- a/x/onion/client/cli/query.go +++ b/x/onion/client/cli/query.go @@ -3,16 +3,11 @@ package cli import ( "context" "fmt" - "strings" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/version" "github.com/spf13/cobra" - "github.com/Team-Kujira/core/x/onion/keeper" - "github.com/Team-Kujira/core/x/onion/types" ) @@ -40,45 +35,11 @@ func GetQueryCmd() *cobra.Command { } cmd.AddCommand( - GetCmdWasmSender(), GetCmdQuerySequence(), ) return cmd } -// GetCmdPoolParams return pool params. -func GetCmdWasmSender() *cobra.Command { - cmd := &cobra.Command{ - Use: "wasm-sender ", - Short: "Generate the local address for a wasm hooks sender", - Long: strings.TrimSpace( - fmt.Sprintf(`Generate the local address for a wasm hooks sender. -Example: -$ %s query ibc-hooks wasm-hooks-sender channel-42 juno12smx2wdlyttvyzvzg54y2vnqwq2qjatezqwqxu -`, - version.AppName, - ), - ), - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - channelID := args[0] - originalSender := args[1] - // ToDo: Make this flexible as an arg - prefix := sdk.GetConfig().GetBech32AccountAddrPrefix() - senderBech32, err := keeper.DeriveIntermediateSender(channelID, originalSender, prefix) - if err != nil { - return err - } - fmt.Println(senderBech32) - return nil - }, - } - - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} - // GetCmdQuerySequence implements the query sequence command. func GetCmdQuerySequence() *cobra.Command { cmd := &cobra.Command{ diff --git a/x/onion/hooks.go b/x/onion/hooks.go deleted file mode 100644 index aacadf95..00000000 --- a/x/onion/hooks.go +++ /dev/null @@ -1,145 +0,0 @@ -package onion - -import ( - // external libraries - sdk "github.com/cosmos/cosmos-sdk/types" - capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" - - // ibc-go - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" -) - -type Hooks interface{} - -type OnChanOpenInitOverrideHooks interface { - OnChanOpenInitOverride(im IBCMiddleware, ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID string, channelID string, channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version string) (string, error) -} -type OnChanOpenInitBeforeHooks interface { - OnChanOpenInitBeforeHook(ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID string, channelID string, channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version string) -} -type OnChanOpenInitAfterHooks interface { - OnChanOpenInitAfterHook(ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID string, channelID string, channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, version string, finalVersion string, err error) -} - -// OnChanOpenTry Hooks -type OnChanOpenTryOverrideHooks interface { - OnChanOpenTryOverride(im IBCMiddleware, ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID, channelID string, channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, counterpartyVersion string) (string, error) -} -type OnChanOpenTryBeforeHooks interface { - OnChanOpenTryBeforeHook(ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID, channelID string, channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, counterpartyVersion string) -} -type OnChanOpenTryAfterHooks interface { - OnChanOpenTryAfterHook(ctx sdk.Context, order channeltypes.Order, connectionHops []string, portID, channelID string, channelCap *capabilitytypes.Capability, counterparty channeltypes.Counterparty, counterpartyVersion string, version string, err error) -} - -// OnChanOpenAck Hooks -type OnChanOpenAckOverrideHooks interface { - OnChanOpenAckOverride(im IBCMiddleware, ctx sdk.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string) error -} -type OnChanOpenAckBeforeHooks interface { - OnChanOpenAckBeforeHook(ctx sdk.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string) -} -type OnChanOpenAckAfterHooks interface { - OnChanOpenAckAfterHook(ctx sdk.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string, err error) -} - -// OnChanOpenConfirm Hooks -type OnChanOpenConfirmOverrideHooks interface { - OnChanOpenConfirmOverride(im IBCMiddleware, ctx sdk.Context, portID, channelID string) error -} -type OnChanOpenConfirmBeforeHooks interface { - OnChanOpenConfirmBeforeHook(ctx sdk.Context, portID, channelID string) -} -type OnChanOpenConfirmAfterHooks interface { - OnChanOpenConfirmAfterHook(ctx sdk.Context, portID, channelID string, err error) -} - -// OnChanCloseInit Hooks -type OnChanCloseInitOverrideHooks interface { - OnChanCloseInitOverride(im IBCMiddleware, ctx sdk.Context, portID, channelID string) error -} -type OnChanCloseInitBeforeHooks interface { - OnChanCloseInitBeforeHook(ctx sdk.Context, portID, channelID string) -} -type OnChanCloseInitAfterHooks interface { - OnChanCloseInitAfterHook(ctx sdk.Context, portID, channelID string, err error) -} - -// OnChanCloseConfirm Hooks -type OnChanCloseConfirmOverrideHooks interface { - OnChanCloseConfirmOverride(im IBCMiddleware, ctx sdk.Context, portID, channelID string) error -} -type OnChanCloseConfirmBeforeHooks interface { - OnChanCloseConfirmBeforeHook(ctx sdk.Context, portID, channelID string) -} -type OnChanCloseConfirmAfterHooks interface { - OnChanCloseConfirmAfterHook(ctx sdk.Context, portID, channelID string, err error) -} - -// OnRecvPacket Hooks -type OnRecvPacketOverrideHooks interface { - OnRecvPacketOverride(im IBCMiddleware, ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) ibcexported.Acknowledgement -} -type OnRecvPacketBeforeHooks interface { - OnRecvPacketBeforeHook(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) -} -type OnRecvPacketAfterHooks interface { - OnRecvPacketAfterHook(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress, ack ibcexported.Acknowledgement) -} - -// OnAcknowledgementPacket Hooks -type OnAcknowledgementPacketOverrideHooks interface { - OnAcknowledgementPacketOverride(im IBCMiddleware, ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress) error -} -type OnAcknowledgementPacketBeforeHooks interface { - OnAcknowledgementPacketBeforeHook(ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress) -} -type OnAcknowledgementPacketAfterHooks interface { - OnAcknowledgementPacketAfterHook(ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress, err error) -} - -// OnTimeoutPacket Hooks -type OnTimeoutPacketOverrideHooks interface { - OnTimeoutPacketOverride(im IBCMiddleware, ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error -} -type OnTimeoutPacketBeforeHooks interface { - OnTimeoutPacketBeforeHook(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) -} -type OnTimeoutPacketAfterHooks interface { - OnTimeoutPacketAfterHook(ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress, err error) -} - -// SendPacket Hooks -type SendPacketOverrideHooks interface { - SendPacketOverride(i ICS4Middleware, ctx sdk.Context, chanCap *capabilitytypes.Capability, sourcePort string, sourceChannel string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) (uint64, error) -} -type SendPacketBeforeHooks interface { - SendPacketBeforeHook(ctx sdk.Context, chanCap *capabilitytypes.Capability, sourcePort string, sourceChannel string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) -} -type SendPacketAfterHooks interface { - SendPacketAfterHook(cctx sdk.Context, chanCap *capabilitytypes.Capability, sourcePort string, sourceChannel string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte, err error) -} - -// WriteAcknowledgement Hooks -type WriteAcknowledgementOverrideHooks interface { - WriteAcknowledgementOverride(i ICS4Middleware, ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, ack ibcexported.Acknowledgement) error -} -type WriteAcknowledgementBeforeHooks interface { - WriteAcknowledgementBeforeHook(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, ack ibcexported.Acknowledgement) -} -type WriteAcknowledgementAfterHooks interface { - WriteAcknowledgementAfterHook(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, ack ibcexported.Acknowledgement, err error) -} - -// GetAppVersion Hooks -type GetAppVersionOverrideHooks interface { - GetAppVersionOverride(i ICS4Middleware, ctx sdk.Context, portID, channelID string) (string, bool) -} -type GetAppVersionBeforeHooks interface { - GetAppVersionBeforeHook(ctx sdk.Context, portID, channelID string) -} -type GetAppVersionAfterHooks interface { - GetAppVersionAfterHook(ctx sdk.Context, portID, channelID string, result string, success bool) -} diff --git a/x/onion/ibc_module.go b/x/onion/ibc_module.go index 8c836e8b..f3b626c6 100644 --- a/x/onion/ibc_module.go +++ b/x/onion/ibc_module.go @@ -11,37 +11,33 @@ import ( // ibc-go "github.com/Team-Kujira/core/x/onion/keeper" "github.com/cosmos/cosmos-sdk/client" - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" ) -var _ porttypes.Middleware = &IBCMiddleware{} +var _ porttypes.IBCModule = IBCModule{} -type IBCMiddleware struct { +type IBCModule struct { App porttypes.IBCModule - ICS4Middleware *ICS4Middleware Keeper *keeper.Keeper txEncodingConfig client.TxEncodingConfig } -func NewIBCMiddleware( +func NewIBCModule( app porttypes.IBCModule, - ics4 *ICS4Middleware, Keeper *keeper.Keeper, txEncodingConfig client.TxEncodingConfig, -) IBCMiddleware { - return IBCMiddleware{ +) IBCModule { + return IBCModule{ App: app, - ICS4Middleware: ics4, Keeper: Keeper, txEncodingConfig: txEncodingConfig, } } -// OnChanOpenInit implements the IBCMiddleware interface -func (im IBCMiddleware) OnChanOpenInit( +// OnChanOpenInit implements the IBCModule interface +func (im IBCModule) OnChanOpenInit( ctx sdk.Context, order channeltypes.Order, connectionHops []string, @@ -51,24 +47,11 @@ func (im IBCMiddleware) OnChanOpenInit( counterparty channeltypes.Counterparty, version string, ) (string, error) { - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenInitOverrideHooks); ok { - return hook.OnChanOpenInitOverride(im, ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenInitBeforeHooks); ok { - hook.OnChanOpenInitBeforeHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version) - } - - finalVersion, err := im.App.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version) - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenInitAfterHooks); ok { - hook.OnChanOpenInitAfterHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version, finalVersion, err) - } - return version, err + return im.App.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version) } -// OnChanOpenTry implements the IBCMiddleware interface -func (im IBCMiddleware) OnChanOpenTry( +// OnChanOpenTry implements the IBCModule interface +func (im IBCModule) OnChanOpenTry( ctx sdk.Context, order channeltypes.Order, connectionHops []string, @@ -78,111 +61,49 @@ func (im IBCMiddleware) OnChanOpenTry( counterparty channeltypes.Counterparty, counterpartyVersion string, ) (string, error) { - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenTryOverrideHooks); ok { - return hook.OnChanOpenTryOverride(im, ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenTryBeforeHooks); ok { - hook.OnChanOpenTryBeforeHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion) - } - - version, err := im.App.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion) - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenTryAfterHooks); ok { - hook.OnChanOpenTryAfterHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion, version, err) - } - return version, err + return im.App.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion) } -// OnChanOpenAck implements the IBCMiddleware interface -func (im IBCMiddleware) OnChanOpenAck( +// OnChanOpenAck implements the IBCModule interface +func (im IBCModule) OnChanOpenAck( ctx sdk.Context, portID, channelID string, counterpartyChannelID string, counterpartyVersion string, ) error { - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenAckOverrideHooks); ok { - return hook.OnChanOpenAckOverride(im, ctx, portID, channelID, counterpartyChannelID, counterpartyVersion) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenAckBeforeHooks); ok { - hook.OnChanOpenAckBeforeHook(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion) - } - err := im.App.OnChanOpenAck(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion) - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenAckAfterHooks); ok { - hook.OnChanOpenAckAfterHook(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion, err) - } - - return err + return im.App.OnChanOpenAck(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion) } -// OnChanOpenConfirm implements the IBCMiddleware interface -func (im IBCMiddleware) OnChanOpenConfirm( +// OnChanOpenConfirm implements the IBCModule interface +func (im IBCModule) OnChanOpenConfirm( ctx sdk.Context, portID, channelID string, ) error { - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenConfirmOverrideHooks); ok { - return hook.OnChanOpenConfirmOverride(im, ctx, portID, channelID) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenConfirmBeforeHooks); ok { - hook.OnChanOpenConfirmBeforeHook(ctx, portID, channelID) - } - err := im.App.OnChanOpenConfirm(ctx, portID, channelID) - if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenConfirmAfterHooks); ok { - hook.OnChanOpenConfirmAfterHook(ctx, portID, channelID, err) - } - return err + return im.App.OnChanOpenConfirm(ctx, portID, channelID) } -// OnChanCloseInit implements the IBCMiddleware interface -func (im IBCMiddleware) OnChanCloseInit( +// OnChanCloseInit implements the IBCModule interface +func (im IBCModule) OnChanCloseInit( ctx sdk.Context, portID, channelID string, ) error { - // Here we can remove the limits when a new channel is closed. For now, they can remove them manually on the contract - if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseInitOverrideHooks); ok { - return hook.OnChanCloseInitOverride(im, ctx, portID, channelID) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseInitBeforeHooks); ok { - hook.OnChanCloseInitBeforeHook(ctx, portID, channelID) - } - err := im.App.OnChanCloseInit(ctx, portID, channelID) - if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseInitAfterHooks); ok { - hook.OnChanCloseInitAfterHook(ctx, portID, channelID, err) - } - - return err + return im.App.OnChanCloseInit(ctx, portID, channelID) } -// OnChanCloseConfirm implements the IBCMiddleware interface -func (im IBCMiddleware) OnChanCloseConfirm( +// OnChanCloseConfirm implements the IBCModule interface +func (im IBCModule) OnChanCloseConfirm( ctx sdk.Context, portID, channelID string, ) error { - // Here we can remove the limits when a new channel is closed. For now, they can remove them manually on the contract - if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseConfirmOverrideHooks); ok { - return hook.OnChanCloseConfirmOverride(im, ctx, portID, channelID) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseConfirmBeforeHooks); ok { - hook.OnChanCloseConfirmBeforeHook(ctx, portID, channelID) - } - err := im.App.OnChanCloseConfirm(ctx, portID, channelID) - if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseConfirmAfterHooks); ok { - hook.OnChanCloseConfirmAfterHook(ctx, portID, channelID, err) - } - - return err + return im.App.OnChanCloseConfirm(ctx, portID, channelID) } -// OnRecvPacket implements the IBCMiddleware interface -func (im IBCMiddleware) OnRecvPacket( +// OnRecvPacket implements the IBCModule interface +func (im IBCModule) OnRecvPacket( ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress, @@ -207,89 +128,24 @@ func (im IBCMiddleware) OnRecvPacket( } } - if hook, ok := im.ICS4Middleware.Hooks.(OnRecvPacketOverrideHooks); ok { - return hook.OnRecvPacketOverride(im, ctx, packet, relayer) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnRecvPacketBeforeHooks); ok { - hook.OnRecvPacketBeforeHook(ctx, packet, relayer) - } - - ack := im.App.OnRecvPacket(ctx, packet, relayer) - - if hook, ok := im.ICS4Middleware.Hooks.(OnRecvPacketAfterHooks); ok { - hook.OnRecvPacketAfterHook(ctx, packet, relayer, ack) - } - - return ack + return im.App.OnRecvPacket(ctx, packet, relayer) } -// OnAcknowledgementPacket implements the IBCMiddleware interface -func (im IBCMiddleware) OnAcknowledgementPacket( +// OnAcknowledgementPacket implements the IBCModule interface +func (im IBCModule) OnAcknowledgementPacket( ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress, ) error { - if hook, ok := im.ICS4Middleware.Hooks.(OnAcknowledgementPacketOverrideHooks); ok { - return hook.OnAcknowledgementPacketOverride(im, ctx, packet, acknowledgement, relayer) - } - if hook, ok := im.ICS4Middleware.Hooks.(OnAcknowledgementPacketBeforeHooks); ok { - hook.OnAcknowledgementPacketBeforeHook(ctx, packet, acknowledgement, relayer) - } - - err := im.App.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) - - if hook, ok := im.ICS4Middleware.Hooks.(OnAcknowledgementPacketAfterHooks); ok { - hook.OnAcknowledgementPacketAfterHook(ctx, packet, acknowledgement, relayer, err) - } - - return err + return im.App.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) } -// OnTimeoutPacket implements the IBCMiddleware interface -func (im IBCMiddleware) OnTimeoutPacket( +// OnTimeoutPacket implements the IBCModule interface +func (im IBCModule) OnTimeoutPacket( ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress, ) error { - if hook, ok := im.ICS4Middleware.Hooks.(OnTimeoutPacketOverrideHooks); ok { - return hook.OnTimeoutPacketOverride(im, ctx, packet, relayer) - } - - if hook, ok := im.ICS4Middleware.Hooks.(OnTimeoutPacketBeforeHooks); ok { - hook.OnTimeoutPacketBeforeHook(ctx, packet, relayer) - } - err := im.App.OnTimeoutPacket(ctx, packet, relayer) - if hook, ok := im.ICS4Middleware.Hooks.(OnTimeoutPacketAfterHooks); ok { - hook.OnTimeoutPacketAfterHook(ctx, packet, relayer, err) - } - - return err -} - -// SendPacket implements the ICS4 Wrapper interface -func (im IBCMiddleware) SendPacket( - ctx sdk.Context, - chanCap *capabilitytypes.Capability, - sourcePort string, sourceChannel string, - timeoutHeight clienttypes.Height, - timeoutTimestamp uint64, - data []byte, -) (sequence uint64, err error) { - return im.ICS4Middleware.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) -} - -// WriteAcknowledgement implements the ICS4 Wrapper interface -func (im IBCMiddleware) WriteAcknowledgement( - ctx sdk.Context, - chanCap *capabilitytypes.Capability, - packet ibcexported.PacketI, - ack ibcexported.Acknowledgement, -) error { - return im.ICS4Middleware.WriteAcknowledgement(ctx, chanCap, packet, ack) -} - -func (im IBCMiddleware) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) { - return im.ICS4Middleware.GetAppVersion(ctx, portID, channelID) + return im.App.OnTimeoutPacket(ctx, packet, relayer) } diff --git a/x/onion/ibcutils.go b/x/onion/ibcutils.go deleted file mode 100644 index a94871de..00000000 --- a/x/onion/ibcutils.go +++ /dev/null @@ -1,78 +0,0 @@ -package onion - -import ( - "encoding/json" - - sdk "github.com/cosmos/cosmos-sdk/types" - transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" -) - -const IbcAcknowledgementErrorType = "ibc-acknowledgement-error" - -// NewEmitErrorAcknowledgement creates a new error acknowledgement after having emitted an event with the -// details of the error. -func NewEmitErrorAcknowledgement(ctx sdk.Context, err error, errorContexts ...string) channeltypes.Acknowledgement { - EmitIBCErrorEvents(ctx, err, errorContexts) - - return channeltypes.NewErrorAcknowledgement(err) -} - -// EmitIBCErrorEvents Emit and Log errors -func EmitIBCErrorEvents(ctx sdk.Context, err error, errorContexts []string) { - attributes := make([]sdk.Attribute, len(errorContexts)+1) - attributes[0] = sdk.NewAttribute("error", err.Error()) - for i, s := range errorContexts { - attributes[i+1] = sdk.NewAttribute("error-context", s) - } - - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - IbcAcknowledgementErrorType, - attributes..., - ), - }) -} - -// MustExtractDenomFromPacketOnRecv takes a packet with a valid ICS20 token data in the Data field and returns the -// denom as represented in the local chain. -// If the data cannot be unmarshalled this function will panic -func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string { - var data transfertypes.FungibleTokenPacketData - if err := json.Unmarshal(packet.GetData(), &data); err != nil { - panic("unable to unmarshal ICS20 packet data") - } - - var denom string - if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom) { - // remove prefix added by sender chain - voucherPrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) - - unprefixedDenom := data.Denom[len(voucherPrefix):] - - // coin denomination used in sending from the escrow address - denom = unprefixedDenom - - // The denomination used to send the coins is either the native denom or the hash of the path - // if the denomination is not native. - denomTrace := transfertypes.ParseDenomTrace(unprefixedDenom) - if denomTrace.Path != "" { - denom = denomTrace.IBCDenom() - } - } else { - prefixedDenom := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) + data.Denom - denom = transfertypes.ParseDenomTrace(prefixedDenom).IBCDenom() - } - return denom -} - -// IsAckError checks an IBC acknowledgement to see if it's an error. -// This is a replacement for ack.Success() which is currently not working on some circumstances -func IsAckError(acknowledgement []byte) bool { - var ackErr channeltypes.Acknowledgement_Error - if err := json.Unmarshal(acknowledgement, &ackErr); err == nil && len(ackErr.Error) > 0 { - return true - } - return false -} diff --git a/x/onion/ics4_middleware.go b/x/onion/ics4_middleware.go deleted file mode 100644 index 06897f41..00000000 --- a/x/onion/ics4_middleware.go +++ /dev/null @@ -1,85 +0,0 @@ -package onion - -import ( - // external libraries - sdk "github.com/cosmos/cosmos-sdk/types" - capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" - - // ibc-go - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" - ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" -) - -var _ porttypes.ICS4Wrapper = &ICS4Middleware{} - -type ICS4Middleware struct { - channel porttypes.ICS4Wrapper - - // Hooks - Hooks Hooks -} - -func NewICS4Middleware(channel porttypes.ICS4Wrapper, hooks Hooks) ICS4Middleware { - return ICS4Middleware{ - channel: channel, - Hooks: hooks, - } -} - -func (i ICS4Middleware) SendPacket( - ctx sdk.Context, - chanCap *capabilitytypes.Capability, - sourcePort string, sourceChannel string, - timeoutHeight clienttypes.Height, - timeoutTimestamp uint64, - data []byte, -) (sequence uint64, err error) { - if hook, ok := i.Hooks.(SendPacketOverrideHooks); ok { - return hook.SendPacketOverride(i, ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) - } - - if hook, ok := i.Hooks.(SendPacketBeforeHooks); ok { - hook.SendPacketBeforeHook(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) - } - - seq, err := i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) - - if hook, ok := i.Hooks.(SendPacketAfterHooks); ok { - hook.SendPacketAfterHook(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data, err) - } - - return seq, err -} - -func (i ICS4Middleware) WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet ibcexported.PacketI, ack ibcexported.Acknowledgement) error { - if hook, ok := i.Hooks.(WriteAcknowledgementOverrideHooks); ok { - return hook.WriteAcknowledgementOverride(i, ctx, chanCap, packet, ack) - } - - if hook, ok := i.Hooks.(WriteAcknowledgementBeforeHooks); ok { - hook.WriteAcknowledgementBeforeHook(ctx, chanCap, packet, ack) - } - err := i.channel.WriteAcknowledgement(ctx, chanCap, packet, ack) - if hook, ok := i.Hooks.(WriteAcknowledgementAfterHooks); ok { - hook.WriteAcknowledgementAfterHook(ctx, chanCap, packet, ack, err) - } - - return err -} - -func (i ICS4Middleware) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) { - if hook, ok := i.Hooks.(GetAppVersionOverrideHooks); ok { - return hook.GetAppVersionOverride(i, ctx, portID, channelID) - } - - if hook, ok := i.Hooks.(GetAppVersionBeforeHooks); ok { - hook.GetAppVersionBeforeHook(ctx, portID, channelID) - } - version, err := i.channel.GetAppVersion(ctx, portID, channelID) - if hook, ok := i.Hooks.(GetAppVersionAfterHooks); ok { - hook.GetAppVersionAfterHook(ctx, portID, channelID, version, err) - } - - return version, err -} diff --git a/x/onion/keeper/ibcutils.go b/x/onion/keeper/ibcutils.go deleted file mode 100644 index 60fd0f6c..00000000 --- a/x/onion/keeper/ibcutils.go +++ /dev/null @@ -1,33 +0,0 @@ -package keeper - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" -) - -const IbcAcknowledgementErrorType = "ibc-acknowledgement-error" - -// NewSuccessAckRepresentingAnError creates a new success acknowledgement that represents an error. -// This is useful for notifying the sender that an error has occurred in a way that does not allow -// the received tokens to be reverted (which means they shouldn't be released by the sender's ics20 escrow) -func NewSuccessAckRepresentingAnError(ctx sdk.Context, err error, errorContent []byte, errorContexts ...string) channeltypes.Acknowledgement { - EmitIBCErrorEvents(ctx, err, errorContexts) - - return channeltypes.NewResultAcknowledgement(errorContent) -} - -// EmitIBCErrorEvents Emit and Log errors -func EmitIBCErrorEvents(ctx sdk.Context, err error, errorContexts []string) { - attributes := make([]sdk.Attribute, len(errorContexts)+1) - attributes[0] = sdk.NewAttribute("error", err.Error()) - for i, s := range errorContexts { - attributes[i+1] = sdk.NewAttribute("error-context", s) - } - - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - IbcAcknowledgementErrorType, - attributes..., - ), - }) -} diff --git a/x/onion/keeper/keeper.go b/x/onion/keeper/keeper.go index c9ccfbef..3d9d664b 100644 --- a/x/onion/keeper/keeper.go +++ b/x/onion/keeper/keeper.go @@ -1,23 +1,16 @@ package keeper import ( - "encoding/hex" - "encoding/json" "fmt" - "strings" wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" "github.com/Team-Kujira/core/x/onion/types" - "github.com/cometbft/cometbft/crypto/tmhash" "github.com/cometbft/cometbft/libs/log" "github.com/cosmos/cosmos-sdk/baseapp" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/address" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ) type ( @@ -25,9 +18,7 @@ type ( storeKey storetypes.StoreKey paramSpace paramtypes.Subspace - channelKeeper types.ChannelKeeper - ContractKeeper *wasmkeeper.PermissionedKeeper - accountKeeper types.AccountKeeper + accountKeeper types.AccountKeeper router *baseapp.MsgServiceRouter signModeHandler authsigning.SignModeHandler @@ -38,7 +29,6 @@ type ( func NewKeeper( storeKey storetypes.StoreKey, paramSpace paramtypes.Subspace, - channelKeeper types.ChannelKeeper, contractKeeper *wasmkeeper.PermissionedKeeper, router *baseapp.MsgServiceRouter, accountKeeper types.AccountKeeper, @@ -50,8 +40,6 @@ func NewKeeper( return &Keeper{ storeKey: storeKey, paramSpace: paramSpace, - channelKeeper: channelKeeper, - ContractKeeper: contractKeeper, router: router, accountKeeper: accountKeeper, signModeHandler: signModeHandler, @@ -74,11 +62,6 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { k.paramSpace.SetParamSet(ctx, ¶ms) } -// SetParam sets a specific ibc-hooks module's parameter with the provided parameter. -func (k Keeper) SetParam(ctx sdk.Context, key []byte, value interface{}) { - k.paramSpace.Set(ctx, key, value) -} - func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) { k.SetParams(ctx, genState.Params) for _, seq := range genState.Sequences { @@ -92,201 +75,3 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { Sequences: k.GetAllSequences(ctx), } } - -func GetPacketCallbackKey(channel string, packetSequence uint64) []byte { - return []byte(fmt.Sprintf("%s::%d", channel, packetSequence)) -} - -func GetPacketAckKey(channel string, packetSequence uint64) []byte { - return []byte(fmt.Sprintf("%s::%d::ack", channel, packetSequence)) -} - -func GeneratePacketAckValue(packet channeltypes.Packet, contract string) ([]byte, error) { - if _, err := sdk.AccAddressFromBech32(contract); err != nil { - return nil, sdkerrors.Wrap(types.ErrInvalidContractAddr, contract) - } - - packetHash, err := hashPacket(packet) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not hash packet") - } - - return []byte(fmt.Sprintf("%s::%s", contract, packetHash)), nil -} - -// StorePacketCallback stores which contract will be listening for the ack or timeout of a packet -func (k Keeper) StorePacketCallback(ctx sdk.Context, channel string, packetSequence uint64, contract string) { - store := ctx.KVStore(k.storeKey) - store.Set(GetPacketCallbackKey(channel, packetSequence), []byte(contract)) -} - -// GetPacketCallback returns the bech32 addr of the contract that is expecting a callback from a packet -func (k Keeper) GetPacketCallback(ctx sdk.Context, channel string, packetSequence uint64) string { - store := ctx.KVStore(k.storeKey) - return string(store.Get(GetPacketCallbackKey(channel, packetSequence))) -} - -// IsInAllowList checks the params to see if the contract is in the KeyAsyncAckAllowList param -func (k Keeper) IsInAllowList(ctx sdk.Context, contract string) bool { - var allowList []string - k.paramSpace.GetIfExists(ctx, types.KeyAsyncAckAllowList, &allowList) - for _, addr := range allowList { - if addr == contract { - return true - } - } - return false -} - -// DeletePacketCallback deletes the callback from storage once it has been processed -func (k Keeper) DeletePacketCallback(ctx sdk.Context, channel string, packetSequence uint64) { - store := ctx.KVStore(k.storeKey) - store.Delete(GetPacketCallbackKey(channel, packetSequence)) -} - -// StorePacketAckActor stores which contract is allowed to send an ack for the packet -func (k Keeper) StorePacketAckActor(ctx sdk.Context, packet channeltypes.Packet, contract string) { - store := ctx.KVStore(k.storeKey) - channel := packet.GetSourceChannel() - packetSequence := packet.GetSequence() - - val, err := GeneratePacketAckValue(packet, contract) - if err != nil { - panic(err) - } - store.Set(GetPacketAckKey(channel, packetSequence), val) -} - -// GetPacketAckActor returns the bech32 addr of the contract that is allowed to send an ack for the packet and the packet hash -func (k Keeper) GetPacketAckActor(ctx sdk.Context, channel string, packetSequence uint64) (string, string) { - store := ctx.KVStore(k.storeKey) - rawData := store.Get(GetPacketAckKey(channel, packetSequence)) - if rawData == nil { - return "", "" - } - data := strings.Split(string(rawData), "::") - if len(data) != 2 { - return "", "" - } - // validate that the contract is a valid bech32 addr - if _, err := sdk.AccAddressFromBech32(data[0]); err != nil { - return "", "" - } - // validate that the hash is a valid sha256sum hash - if _, err := hex.DecodeString(data[1]); err != nil { - return "", "" - } - - return data[0], data[1] -} - -// DeletePacketAckActor deletes the ack actor from storage once it has been used -func (k Keeper) DeletePacketAckActor(ctx sdk.Context, channel string, packetSequence uint64) { - store := ctx.KVStore(k.storeKey) - store.Delete(GetPacketAckKey(channel, packetSequence)) -} - -// DeriveIntermediateSender derives the sender address to be used when calling wasm hooks -func DeriveIntermediateSender(channel, originalSender, bech32Prefix string) (string, error) { - senderStr := fmt.Sprintf("%s/%s", channel, originalSender) - senderHash32 := address.Hash(types.SenderPrefix, []byte(senderStr)) - sender := sdk.AccAddress(senderHash32[:]) - return sdk.Bech32ifyAddressBytes(bech32Prefix, sender) -} - -// EmitIBCAck emits an event that the IBC packet has been acknowledged -func (k Keeper) EmitIBCAck(ctx sdk.Context, sender, channel string, packetSequence uint64) ([]byte, error) { - contract, packetHash := k.GetPacketAckActor(ctx, channel, packetSequence) - if contract == "" { - return nil, fmt.Errorf("no ack actor set for channel %s packet %d", channel, packetSequence) - } - // Only the contract itself can request for the ack to be emitted. This will generally happen as a callback - // when the result of other IBC actions has finished, but it could be exposed directly by the contract if the - // proper checks are made - if sender != contract { - return nil, fmt.Errorf("sender %s is not allowed to send an ack for channel %s packet %d", sender, channel, packetSequence) - } - - // Write the acknowledgement - _, cap, err := k.channelKeeper.LookupModuleByChannel(ctx, "transfer", channel) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not retrieve module from port-id") - } - - // Calling the contract. This could be made generic by using an interface if we want - // to support other types of AckActors, but keeping it here for now for simplicity. - contractAddr, err := sdk.AccAddressFromBech32(contract) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not parse contract address") - } - - msg := types.IBCAsync{ - RequestAck: types.RequestAck{RequestAckI: types.RequestAckI{ - PacketSequence: packetSequence, - SourceChannel: channel, - }}, - } - msgBytes, err := json.Marshal(msg) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not marshal message") - } - bz, err := k.ContractKeeper.Sudo(ctx, contractAddr, msgBytes) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not execute contract") - } - - ack, err := types.UnmarshalIBCAck(bz) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not unmarshal into IBCAckResponse or IBCAckError") - - } - var newAck channeltypes.Acknowledgement - var packet channeltypes.Packet - - switch ack.Type { - case "ack_response": - jsonAck, err := json.Marshal(ack.AckResponse.ContractAck) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not marshal acknowledgement") - } - packet = ack.AckResponse.Packet - newAck = channeltypes.NewResultAcknowledgement(jsonAck) - case "ack_error": - packet = ack.AckError.Packet - newAck = NewSuccessAckRepresentingAnError(ctx, types.ErrAckFromContract, []byte(ack.AckError.ErrorResponse), ack.AckError.ErrorDescription) - default: - return nil, sdkerrors.Wrap(err, "could not unmarshal into IBCAckResponse or IBCAckError") - } - - // Validate that the packet returned by the contract matches the one we stored when sending - receivedPacketHash, err := hashPacket(packet) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not hash packet") - } - if receivedPacketHash != packetHash { - return nil, sdkerrors.Wrap(types.ErrAckPacketMismatch, fmt.Sprintf("packet hash mismatch. Expected %s, got %s", packetHash, receivedPacketHash)) - } - - // Now we can write the acknowledgement - err = k.channelKeeper.WriteAcknowledgement(ctx, cap, packet, newAck) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not write acknowledgement") - } - - response, err := json.Marshal(newAck) - if err != nil { - return nil, sdkerrors.Wrap(err, "could not marshal acknowledgement") - } - return response, nil -} - -func hashPacket(packet channeltypes.Packet) (string, error) { - // ignore the data here. We only care about the channel information - packet.Data = nil - bz, err := json.Marshal(packet) - if err != nil { - return "", sdkerrors.Wrap(err, "could not marshal packet") - } - packetHash := tmhash.Sum(bz) - return hex.EncodeToString(packetHash), nil -} diff --git a/x/onion/keeper/msg_server.go b/x/onion/keeper/msg_server.go index 18c2fa48..2749b4f2 100644 --- a/x/onion/keeper/msg_server.go +++ b/x/onion/keeper/msg_server.go @@ -1,11 +1,7 @@ package keeper import ( - "context" - "strconv" - "github.com/Team-Kujira/core/x/onion/types" - sdk "github.com/cosmos/cosmos-sdk/types" ) type msgServer struct { @@ -19,23 +15,3 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer { } var _ types.MsgServer = msgServer{} - -func (m msgServer) EmitIBCAck(goCtx context.Context, msg *types.MsgEmitIBCAck) (*types.MsgEmitIBCAckResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - - ctx.EventManager().EmitEvent( - sdk.NewEvent( - types.MsgEmitAckKey, - sdk.NewAttribute(types.AttributeSender, msg.Sender), - sdk.NewAttribute(types.AttributeChannel, msg.Channel), - sdk.NewAttribute(types.AttributePacketSequence, strconv.FormatUint(msg.PacketSequence, 10)), - ), - ) - - ack, err := m.Keeper.EmitIBCAck(ctx, msg.Sender, msg.Channel, msg.PacketSequence) - if err != nil { - return nil, err - } - - return &types.MsgEmitIBCAckResponse{ContractResult: string(ack), IbcAck: string(ack)}, nil -} diff --git a/x/onion/types/codec.go b/x/onion/types/codec.go index 5cd725f9..e98cf34b 100644 --- a/x/onion/types/codec.go +++ b/x/onion/types/codec.go @@ -11,13 +11,11 @@ import ( ) func RegisterCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgEmitIBCAck{}, "kujira/ibc-hooks/emit-ibc-ack", nil) } func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { registry.RegisterImplementations( (*sdk.Msg)(nil), - &MsgEmitIBCAck{}, ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } diff --git a/x/onion/types/expected_keepers.go b/x/onion/types/expected_keepers.go index bb12dc77..81b72b12 100644 --- a/x/onion/types/expected_keepers.go +++ b/x/onion/types/expected_keepers.go @@ -3,19 +3,8 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - "github.com/cosmos/ibc-go/v7/modules/core/exported" ) -type ChannelKeeper interface { - GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) - GetPacketCommitment(ctx sdk.Context, portID, channelID string, sequence uint64) []byte - GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) - LookupModuleByChannel(ctx sdk.Context, portID, channelID string) (string, *capabilitytypes.Capability, error) - WriteAcknowledgement(ctx sdk.Context, chanCap *capabilitytypes.Capability, packet exported.PacketI, acknowledgement exported.Acknowledgement) error -} - type AccountKeeper interface { GetParams(ctx sdk.Context) (params authtypes.Params) GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI diff --git a/x/onion/types/msgs.go b/x/onion/types/msgs.go index 85ccb3b8..ab1254f4 100644 --- a/x/onion/types/msgs.go +++ b/x/onion/types/msgs.go @@ -1,32 +1 @@ package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" -) - -// constants. -const ( - TypeMsgEmitIBCAck = "emit-ibc-ack" -) - -var _ sdk.Msg = &MsgEmitIBCAck{} - -func (m MsgEmitIBCAck) Route() string { return RouterKey } -func (m MsgEmitIBCAck) Type() string { return TypeMsgEmitIBCAck } -func (m MsgEmitIBCAck) ValidateBasic() error { - _, err := sdk.AccAddressFromBech32(m.Sender) - if err != nil { - return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid sender address (%s)", err) - } - return nil -} - -func (m MsgEmitIBCAck) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) -} - -func (m MsgEmitIBCAck) GetSigners() []sdk.AccAddress { - sender, _ := sdk.AccAddressFromBech32(m.Sender) - return []sdk.AccAddress{sender} -} diff --git a/x/onion/types/params.go b/x/onion/types/params.go index 091fbb14..a7db94df 100644 --- a/x/onion/types/params.go +++ b/x/onion/types/params.go @@ -1,62 +1,33 @@ package types import ( - "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" ) // Parameter store keys. var ( - KeyAsyncAckAllowList = []byte("AsyncAckAllowList") - _ paramtypes.ParamSet = &Params{} ) func ParamKeyTable() paramtypes.KeyTable { - return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) + return paramtypes.NewKeyTable() } -func NewParams(allowedAsyncAckContracts []string) Params { - return Params{ - AllowedAsyncAckContracts: allowedAsyncAckContracts, - } +func NewParams() Params { + return Params{} } // DefaultParams returns default concentrated-liquidity module parameters. func DefaultParams() Params { - return Params{ - AllowedAsyncAckContracts: []string{}, - } + return Params{} } // ParamSetPairs implements params.ParamSet. func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { - return paramtypes.ParamSetPairs{ - paramtypes.NewParamSetPair(KeyAsyncAckAllowList, &p.AllowedAsyncAckContracts, validateAsyncAckAllowList), - } + return paramtypes.ParamSetPairs{} } // Validate params. func (p Params) Validate() error { - if err := validateAsyncAckAllowList(p.AllowedAsyncAckContracts); err != nil { - return err - } - return nil -} - -func validateAsyncAckAllowList(i interface{}) error { - allowedContracts, ok := i.([]string) - - if !ok { - return fmt.Errorf("invalid parameter type: %T", i) - } - - for _, contract := range allowedContracts { - if _, err := sdk.AccAddressFromBech32(contract); err != nil { - return err - } - } - return nil } diff --git a/x/onion/types/params.pb.go b/x/onion/types/params.pb.go index 3146401e..5dd3232d 100644 --- a/x/onion/types/params.pb.go +++ b/x/onion/types/params.pb.go @@ -26,7 +26,6 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type Params struct { - AllowedAsyncAckContracts []string `protobuf:"bytes,1,rep,name=allowed_async_ack_contracts,json=allowedAsyncAckContracts,proto3" json:"allowed_async_ack_contracts,omitempty" yaml:"allowed_async_ack_contracts"` } func (m *Params) Reset() { *m = Params{} } @@ -62,13 +61,6 @@ func (m *Params) XXX_DiscardUnknown() { var xxx_messageInfo_Params proto.InternalMessageInfo -func (m *Params) GetAllowedAsyncAckContracts() []string { - if m != nil { - return m.AllowedAsyncAckContracts - } - return nil -} - func init() { proto.RegisterType((*Params)(nil), "kujira.onion.Params") } @@ -76,23 +68,19 @@ func init() { func init() { proto.RegisterFile("kujira/onion/params.proto", fileDescriptor_c9158e41ac96e8e0) } var fileDescriptor_c9158e41ac96e8e0 = []byte{ - // 248 bytes of a gzipped FileDescriptorProto + // 183 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0xcc, 0x2e, 0xcd, 0xca, 0x2c, 0x4a, 0xd4, 0xcf, 0xcf, 0xcb, 0xcc, 0xcf, 0xd3, 0x2f, 0x48, 0x2c, 0x4a, 0xcc, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x81, 0x48, 0xe9, 0x81, 0xa5, 0xa4, 0x44, 0xd2, 0xf3, 0xd3, 0xf3, 0xc1, 0x12, 0xfa, 0x20, 0x16, 0x44, 0x8d, 0x94, 0x64, 0x72, 0x7e, 0x71, 0x6e, 0x7e, 0x71, 0x3c, 0x44, 0x02, 0xc2, 0x81, 0x4a, 0xc9, 0xa5, 0xe7, 0xe7, 0xa7, 0xe7, 0xa4, 0xea, 0x83, - 0x79, 0x49, 0xa5, 0x69, 0xfa, 0x29, 0xa5, 0x45, 0x89, 0x25, 0x99, 0xf9, 0x79, 0x10, 0x79, 0xa5, - 0x7c, 0x2e, 0xb6, 0x00, 0xb0, 0x75, 0x42, 0xa9, 0x5c, 0xd2, 0x89, 0x39, 0x39, 0xf9, 0xe5, 0xa9, - 0x29, 0xf1, 0x89, 0xc5, 0x95, 0x79, 0xc9, 0xf1, 0x89, 0xc9, 0xd9, 0xf1, 0xc9, 0xf9, 0x79, 0x25, - 0x45, 0x89, 0xc9, 0x25, 0xc5, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0x9c, 0x4e, 0x6a, 0x9f, 0xee, 0xc9, - 0x2b, 0x55, 0x26, 0xe6, 0xe6, 0x58, 0x29, 0xe1, 0x51, 0xac, 0x14, 0x24, 0x01, 0x95, 0x75, 0x04, - 0x49, 0x3a, 0x26, 0x67, 0x3b, 0xc3, 0xa4, 0x9c, 0x9c, 0x4f, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, - 0x8e, 0xf1, 0xc1, 0x23, 0x39, 0xc6, 0x09, 0x8f, 0xe5, 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, 0xf1, - 0x58, 0x8e, 0x21, 0x4a, 0x33, 0x3d, 0xb3, 0x24, 0xa3, 0x34, 0x49, 0x2f, 0x39, 0x3f, 0x57, 0x3f, - 0x24, 0x35, 0x31, 0x57, 0xd7, 0x1b, 0x12, 0x28, 0xc9, 0xf9, 0x45, 0xa9, 0xfa, 0x15, 0xd0, 0xb0, - 0x29, 0xa9, 0x2c, 0x48, 0x2d, 0x4e, 0x62, 0x03, 0x3b, 0xde, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, - 0x5b, 0x3f, 0x21, 0xa2, 0x38, 0x01, 0x00, 0x00, + 0x79, 0x49, 0xa5, 0x69, 0xfa, 0x29, 0xa5, 0x45, 0x89, 0x25, 0x99, 0xf9, 0x79, 0x10, 0x79, 0x25, + 0x0e, 0x2e, 0xb6, 0x00, 0xb0, 0x75, 0x4e, 0xce, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, + 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, + 0xc7, 0x10, 0xa5, 0x99, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x1f, 0x92, + 0x9a, 0x98, 0xab, 0xeb, 0x0d, 0x71, 0x6d, 0x72, 0x7e, 0x51, 0xaa, 0x7e, 0x05, 0xd4, 0xd1, 0x25, + 0x95, 0x05, 0xa9, 0xc5, 0x49, 0x6c, 0x60, 0x53, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x42, + 0xa0, 0x06, 0x6b, 0xd1, 0x00, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -115,15 +103,6 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.AllowedAsyncAckContracts) > 0 { - for iNdEx := len(m.AllowedAsyncAckContracts) - 1; iNdEx >= 0; iNdEx-- { - i -= len(m.AllowedAsyncAckContracts[iNdEx]) - copy(dAtA[i:], m.AllowedAsyncAckContracts[iNdEx]) - i = encodeVarintParams(dAtA, i, uint64(len(m.AllowedAsyncAckContracts[iNdEx]))) - i-- - dAtA[i] = 0xa - } - } return len(dAtA) - i, nil } @@ -144,12 +123,6 @@ func (m *Params) Size() (n int) { } var l int _ = l - if len(m.AllowedAsyncAckContracts) > 0 { - for _, s := range m.AllowedAsyncAckContracts { - l = len(s) - n += 1 + l + sovParams(uint64(l)) - } - } return n } @@ -188,38 +161,6 @@ func (m *Params) Unmarshal(dAtA []byte) error { return fmt.Errorf("proto: Params: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field AllowedAsyncAckContracts", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowParams - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthParams - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthParams - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.AllowedAsyncAckContracts = append(m.AllowedAsyncAckContracts, string(dAtA[iNdEx:postIndex])) - iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:]) diff --git a/x/onion/types/tx.pb.go b/x/onion/types/tx.pb.go index f4eda7a8..cdd9e7a3 100644 --- a/x/onion/types/tx.pb.go +++ b/x/onion/types/tx.pb.go @@ -10,11 +10,7 @@ import ( grpc1 "github.com/cosmos/gogoproto/grpc" proto "github.com/cosmos/gogoproto/proto" grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" - io "io" math "math" - math_bits "math/bits" ) // Reference imports to suppress errors if they are not otherwise used. @@ -28,150 +24,19 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -type MsgEmitIBCAck struct { - Sender string `protobuf:"bytes,1,opt,name=sender,proto3" json:"sender,omitempty" yaml:"sender"` - PacketSequence uint64 `protobuf:"varint,2,opt,name=packet_sequence,json=packetSequence,proto3" json:"packet_sequence,omitempty" yaml:"packet_sequence"` - Channel string `protobuf:"bytes,3,opt,name=channel,proto3" json:"channel,omitempty" yaml:"channel"` -} - -func (m *MsgEmitIBCAck) Reset() { *m = MsgEmitIBCAck{} } -func (m *MsgEmitIBCAck) String() string { return proto.CompactTextString(m) } -func (*MsgEmitIBCAck) ProtoMessage() {} -func (*MsgEmitIBCAck) Descriptor() ([]byte, []int) { - return fileDescriptor_788539ffbe617633, []int{0} -} -func (m *MsgEmitIBCAck) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *MsgEmitIBCAck) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_MsgEmitIBCAck.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *MsgEmitIBCAck) XXX_Merge(src proto.Message) { - xxx_messageInfo_MsgEmitIBCAck.Merge(m, src) -} -func (m *MsgEmitIBCAck) XXX_Size() int { - return m.Size() -} -func (m *MsgEmitIBCAck) XXX_DiscardUnknown() { - xxx_messageInfo_MsgEmitIBCAck.DiscardUnknown(m) -} - -var xxx_messageInfo_MsgEmitIBCAck proto.InternalMessageInfo - -func (m *MsgEmitIBCAck) GetSender() string { - if m != nil { - return m.Sender - } - return "" -} - -func (m *MsgEmitIBCAck) GetPacketSequence() uint64 { - if m != nil { - return m.PacketSequence - } - return 0 -} - -func (m *MsgEmitIBCAck) GetChannel() string { - if m != nil { - return m.Channel - } - return "" -} - -type MsgEmitIBCAckResponse struct { - ContractResult string `protobuf:"bytes,1,opt,name=contract_result,json=contractResult,proto3" json:"contract_result,omitempty" yaml:"contract_result"` - IbcAck string `protobuf:"bytes,2,opt,name=ibc_ack,json=ibcAck,proto3" json:"ibc_ack,omitempty" yaml:"ibc_ack"` -} - -func (m *MsgEmitIBCAckResponse) Reset() { *m = MsgEmitIBCAckResponse{} } -func (m *MsgEmitIBCAckResponse) String() string { return proto.CompactTextString(m) } -func (*MsgEmitIBCAckResponse) ProtoMessage() {} -func (*MsgEmitIBCAckResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_788539ffbe617633, []int{1} -} -func (m *MsgEmitIBCAckResponse) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *MsgEmitIBCAckResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_MsgEmitIBCAckResponse.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *MsgEmitIBCAckResponse) XXX_Merge(src proto.Message) { - xxx_messageInfo_MsgEmitIBCAckResponse.Merge(m, src) -} -func (m *MsgEmitIBCAckResponse) XXX_Size() int { - return m.Size() -} -func (m *MsgEmitIBCAckResponse) XXX_DiscardUnknown() { - xxx_messageInfo_MsgEmitIBCAckResponse.DiscardUnknown(m) -} - -var xxx_messageInfo_MsgEmitIBCAckResponse proto.InternalMessageInfo - -func (m *MsgEmitIBCAckResponse) GetContractResult() string { - if m != nil { - return m.ContractResult - } - return "" -} - -func (m *MsgEmitIBCAckResponse) GetIbcAck() string { - if m != nil { - return m.IbcAck - } - return "" -} - -func init() { - proto.RegisterType((*MsgEmitIBCAck)(nil), "kujira.onion.MsgEmitIBCAck") - proto.RegisterType((*MsgEmitIBCAckResponse)(nil), "kujira.onion.MsgEmitIBCAckResponse") -} - func init() { proto.RegisterFile("kujira/onion/tx.proto", fileDescriptor_788539ffbe617633) } var fileDescriptor_788539ffbe617633 = []byte{ - // 357 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x91, 0xc1, 0x4e, 0xea, 0x40, - 0x18, 0x85, 0x99, 0xcb, 0x0d, 0xe4, 0x4e, 0x2e, 0xdc, 0xdc, 0x89, 0x18, 0x82, 0x49, 0x21, 0xe3, - 0x06, 0xa2, 0xb6, 0x89, 0xee, 0xdc, 0x51, 0xe2, 0xc2, 0x18, 0x5c, 0x8c, 0xba, 0x71, 0x43, 0xa6, - 0xe3, 0xa4, 0xd4, 0xd2, 0x99, 0xda, 0x99, 0x26, 0xf0, 0x08, 0xee, 0x7c, 0x11, 0xdf, 0xc3, 0x25, - 0x4b, 0x57, 0xc4, 0xc0, 0x1b, 0xf0, 0x04, 0x86, 0x4e, 0x9b, 0x00, 0x0b, 0x77, 0x93, 0xef, 0x9c, - 0x3f, 0x73, 0xfe, 0xff, 0xc0, 0x46, 0x98, 0x3e, 0x07, 0x09, 0x75, 0xa4, 0x08, 0xa4, 0x70, 0xf4, - 0xd4, 0x8e, 0x13, 0xa9, 0x25, 0xfa, 0x6b, 0xb0, 0x9d, 0xe1, 0xd6, 0x81, 0x2f, 0x7d, 0x99, 0x09, - 0xce, 0xe6, 0x65, 0x3c, 0xf8, 0x1d, 0xc0, 0xda, 0x50, 0xf9, 0x57, 0x51, 0xa0, 0xaf, 0xdd, 0x41, - 0x9f, 0x85, 0xa8, 0x07, 0x2b, 0x8a, 0x8b, 0x27, 0x9e, 0x34, 0x41, 0x07, 0x74, 0xff, 0xb8, 0xff, - 0xd7, 0x8b, 0x76, 0x6d, 0x46, 0xa3, 0xc9, 0x25, 0x36, 0x1c, 0x93, 0xdc, 0x80, 0x06, 0xf0, 0x5f, - 0x4c, 0x59, 0xc8, 0xf5, 0x48, 0xf1, 0x97, 0x94, 0x0b, 0xc6, 0x9b, 0xbf, 0x3a, 0xa0, 0xfb, 0xdb, - 0x6d, 0xad, 0x17, 0xed, 0x43, 0x33, 0xb3, 0x67, 0xc0, 0xa4, 0x6e, 0xc8, 0x5d, 0x0e, 0xd0, 0x29, - 0xac, 0xb2, 0x31, 0x15, 0x82, 0x4f, 0x9a, 0xe5, 0xec, 0x43, 0xb4, 0x5e, 0xb4, 0xeb, 0x66, 0x38, - 0x17, 0x30, 0x29, 0x2c, 0xf8, 0x15, 0xc0, 0xc6, 0x4e, 0x5e, 0xc2, 0x55, 0x2c, 0x85, 0xe2, 0x9b, - 0x30, 0x4c, 0x0a, 0x9d, 0x50, 0xa6, 0x47, 0x09, 0x57, 0xe9, 0x44, 0xe7, 0x0b, 0x6c, 0x85, 0xd9, - 0x33, 0x60, 0x52, 0x2f, 0x08, 0xc9, 0x00, 0x3a, 0x81, 0xd5, 0xc0, 0x63, 0x23, 0xca, 0xc2, 0x6c, - 0x93, 0x9d, 0x30, 0xb9, 0x80, 0x49, 0x25, 0xf0, 0x58, 0x9f, 0x85, 0xe7, 0x0f, 0xb0, 0x3c, 0x54, - 0x3e, 0xba, 0x85, 0x70, 0xeb, 0x7c, 0x47, 0xf6, 0xf6, 0xd5, 0xed, 0x9d, 0xac, 0xad, 0xe3, 0x1f, - 0xc4, 0x62, 0x11, 0x77, 0xf0, 0xb1, 0xb4, 0xc0, 0x7c, 0x69, 0x81, 0xaf, 0xa5, 0x05, 0xde, 0x56, - 0x56, 0x69, 0xbe, 0xb2, 0x4a, 0x9f, 0x2b, 0xab, 0xf4, 0xd8, 0xf3, 0x03, 0x3d, 0x4e, 0x3d, 0x9b, - 0xc9, 0xc8, 0xb9, 0xe7, 0x34, 0x3a, 0xbb, 0x31, 0xbd, 0x33, 0x99, 0x70, 0x67, 0x5a, 0xd4, 0x3f, - 0x8b, 0xb9, 0xf2, 0x2a, 0x59, 0xbd, 0x17, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb4, 0xc3, 0xf3, - 0x4c, 0x1b, 0x02, 0x00, 0x00, + // 139 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xcd, 0x2e, 0xcd, 0xca, + 0x2c, 0x4a, 0xd4, 0xcf, 0xcf, 0xcb, 0xcc, 0xcf, 0xd3, 0x2f, 0xa9, 0xd0, 0x2b, 0x28, 0xca, 0x2f, + 0xc9, 0x17, 0xe2, 0x81, 0x08, 0xeb, 0x81, 0x85, 0xa5, 0x44, 0xd2, 0xf3, 0xd3, 0xf3, 0xc1, 0x12, + 0xfa, 0x20, 0x16, 0x44, 0x8d, 0x11, 0x2b, 0x17, 0xb3, 0x6f, 0x71, 0xba, 0x93, 0xf3, 0x89, 0x47, + 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0xc3, 0x85, + 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0x69, 0xa6, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, + 0x25, 0xe7, 0xe7, 0xea, 0x87, 0xa4, 0x26, 0xe6, 0xea, 0x7a, 0x43, 0xec, 0x4a, 0xce, 0x2f, 0x4a, + 0xd5, 0xaf, 0x80, 0x59, 0x59, 0x59, 0x90, 0x5a, 0x9c, 0xc4, 0x06, 0x36, 0xd2, 0x18, 0x10, 0x00, + 0x00, 0xff, 0xff, 0x22, 0x53, 0x0d, 0xef, 0x8f, 0x00, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -186,9 +51,6 @@ const _ = grpc.SupportPackageIsVersion4 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type MsgClient interface { - // EmitIBCAck checks the sender can emit the ack and writes the IBC - // acknowledgement - EmitIBCAck(ctx context.Context, in *MsgEmitIBCAck, opts ...grpc.CallOption) (*MsgEmitIBCAckResponse, error) } type msgClient struct { @@ -199,526 +61,22 @@ func NewMsgClient(cc grpc1.ClientConn) MsgClient { return &msgClient{cc} } -func (c *msgClient) EmitIBCAck(ctx context.Context, in *MsgEmitIBCAck, opts ...grpc.CallOption) (*MsgEmitIBCAckResponse, error) { - out := new(MsgEmitIBCAckResponse) - err := c.cc.Invoke(ctx, "/kujira.onion.Msg/EmitIBCAck", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - // MsgServer is the server API for Msg service. type MsgServer interface { - // EmitIBCAck checks the sender can emit the ack and writes the IBC - // acknowledgement - EmitIBCAck(context.Context, *MsgEmitIBCAck) (*MsgEmitIBCAckResponse, error) } // UnimplementedMsgServer can be embedded to have forward compatible implementations. type UnimplementedMsgServer struct { } -func (*UnimplementedMsgServer) EmitIBCAck(ctx context.Context, req *MsgEmitIBCAck) (*MsgEmitIBCAckResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method EmitIBCAck not implemented") -} - func RegisterMsgServer(s grpc1.Server, srv MsgServer) { s.RegisterService(&_Msg_serviceDesc, srv) } -func _Msg_EmitIBCAck_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(MsgEmitIBCAck) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(MsgServer).EmitIBCAck(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/kujira.onion.Msg/EmitIBCAck", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(MsgServer).EmitIBCAck(ctx, req.(*MsgEmitIBCAck)) - } - return interceptor(ctx, in, info, handler) -} - var _Msg_serviceDesc = grpc.ServiceDesc{ ServiceName: "kujira.onion.Msg", HandlerType: (*MsgServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "EmitIBCAck", - Handler: _Msg_EmitIBCAck_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "kujira/onion/tx.proto", -} - -func (m *MsgEmitIBCAck) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *MsgEmitIBCAck) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *MsgEmitIBCAck) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.Channel) > 0 { - i -= len(m.Channel) - copy(dAtA[i:], m.Channel) - i = encodeVarintTx(dAtA, i, uint64(len(m.Channel))) - i-- - dAtA[i] = 0x1a - } - if m.PacketSequence != 0 { - i = encodeVarintTx(dAtA, i, uint64(m.PacketSequence)) - i-- - dAtA[i] = 0x10 - } - if len(m.Sender) > 0 { - i -= len(m.Sender) - copy(dAtA[i:], m.Sender) - i = encodeVarintTx(dAtA, i, uint64(len(m.Sender))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func (m *MsgEmitIBCAckResponse) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *MsgEmitIBCAckResponse) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *MsgEmitIBCAckResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - if len(m.IbcAck) > 0 { - i -= len(m.IbcAck) - copy(dAtA[i:], m.IbcAck) - i = encodeVarintTx(dAtA, i, uint64(len(m.IbcAck))) - i-- - dAtA[i] = 0x12 - } - if len(m.ContractResult) > 0 { - i -= len(m.ContractResult) - copy(dAtA[i:], m.ContractResult) - i = encodeVarintTx(dAtA, i, uint64(len(m.ContractResult))) - i-- - dAtA[i] = 0xa - } - return len(dAtA) - i, nil -} - -func encodeVarintTx(dAtA []byte, offset int, v uint64) int { - offset -= sovTx(v) - base := offset - for v >= 1<<7 { - dAtA[offset] = uint8(v&0x7f | 0x80) - v >>= 7 - offset++ - } - dAtA[offset] = uint8(v) - return base -} -func (m *MsgEmitIBCAck) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.Sender) - if l > 0 { - n += 1 + l + sovTx(uint64(l)) - } - if m.PacketSequence != 0 { - n += 1 + sovTx(uint64(m.PacketSequence)) - } - l = len(m.Channel) - if l > 0 { - n += 1 + l + sovTx(uint64(l)) - } - return n -} - -func (m *MsgEmitIBCAckResponse) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - l = len(m.ContractResult) - if l > 0 { - n += 1 + l + sovTx(uint64(l)) - } - l = len(m.IbcAck) - if l > 0 { - n += 1 + l + sovTx(uint64(l)) - } - return n -} - -func sovTx(x uint64) (n int) { - return (math_bits.Len64(x|1) + 6) / 7 + Methods: []grpc.MethodDesc{}, + Streams: []grpc.StreamDesc{}, + Metadata: "kujira/onion/tx.proto", } -func sozTx(x uint64) (n int) { - return sovTx(uint64((x << 1) ^ uint64((int64(x) >> 63)))) -} -func (m *MsgEmitIBCAck) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: MsgEmitIBCAck: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: MsgEmitIBCAck: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Sender", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTx - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTx - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Sender = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 2: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field PacketSequence", wireType) - } - m.PacketSequence = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.PacketSequence |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Channel", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTx - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTx - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Channel = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTx(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthTx - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func (m *MsgEmitIBCAckResponse) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: MsgEmitIBCAckResponse: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: MsgEmitIBCAckResponse: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - case 1: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field ContractResult", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTx - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTx - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.ContractResult = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field IbcAck", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTx - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - stringLen |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthTx - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthTx - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.IbcAck = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex - default: - iNdEx = preIndex - skippy, err := skipTx(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthTx - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} -func skipTx(dAtA []byte) (n int, err error) { - l := len(dAtA) - iNdEx := 0 - depth := 0 - for iNdEx < l { - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowTx - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= (uint64(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - wireType := int(wire & 0x7) - switch wireType { - case 0: - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowTx - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - iNdEx++ - if dAtA[iNdEx-1] < 0x80 { - break - } - } - case 1: - iNdEx += 8 - case 2: - var length int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return 0, ErrIntOverflowTx - } - if iNdEx >= l { - return 0, io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - length |= (int(b) & 0x7F) << shift - if b < 0x80 { - break - } - } - if length < 0 { - return 0, ErrInvalidLengthTx - } - iNdEx += length - case 3: - depth++ - case 4: - if depth == 0 { - return 0, ErrUnexpectedEndOfGroupTx - } - depth-- - case 5: - iNdEx += 4 - default: - return 0, fmt.Errorf("proto: illegal wireType %d", wireType) - } - if iNdEx < 0 { - return 0, ErrInvalidLengthTx - } - if depth == 0 { - return iNdEx, nil - } - } - return 0, io.ErrUnexpectedEOF -} - -var ( - ErrInvalidLengthTx = fmt.Errorf("proto: negative length found during unmarshaling") - ErrIntOverflowTx = fmt.Errorf("proto: integer overflow") - ErrUnexpectedEndOfGroupTx = fmt.Errorf("proto: unexpected end of group") -) diff --git a/x/onion/types/types.go b/x/onion/types/types.go index d3caffa6..ab1254f4 100644 --- a/x/onion/types/types.go +++ b/x/onion/types/types.go @@ -1,85 +1 @@ package types - -import ( - "encoding/json" - - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" -) - -// Async: The following types represent the response sent by a contract on OnRecvPacket when it wants the ack to be async - -// OnRecvPacketAsyncAckResponse the response a contract sends to instruct the module to make the ack async -type OnRecvPacketAsyncAckResponse struct { - IsAsyncAck bool `json:"is_async_ack"` -} - -// Async The following types are used to ask a contract that has sent a packet to generate an ack for it - -// RequestAckI internals of IBCAsync -type RequestAckI struct { - PacketSequence uint64 `json:"packet_sequence"` - SourceChannel string `json:"source_channel"` -} - -// RequestAck internals of IBCAsync -type RequestAck struct { - RequestAckI `json:"request_ack"` -} - -// IBCAsync is the sudo message to be sent to the contract for it to generate an ack for a sent packet -type IBCAsync struct { - RequestAck `json:"ibc_async"` -} - -// General - -// ContractAck is the response to be stored when a wasm hook is executed -type ContractAck struct { - ContractResult []byte `json:"contract_result"` - IbcAck []byte `json:"ibc_ack"` -} - -// IBCAckResponse is the response that a contract returns from the sudo() call on OnRecvPacket or RequestAck -type IBCAckResponse struct { - Packet channeltypes.Packet `json:"packet"` - ContractAck ContractAck `json:"contract_ack"` -} - -// IBCAckError is the error that a contract returns from the sudo() call on RequestAck -type IBCAckError struct { - Packet channeltypes.Packet `json:"packet"` - ErrorDescription string `json:"error_description"` - ErrorResponse string `json:"error_response"` -} - -type IBCAck struct { - Type string `json:"type"` - Content json.RawMessage `json:"content"` - // Note: These two fields have to be pointers so that they can be null - // If they are not pointers, they will be empty structs when null, - // which will cause issues with json.Unmarshal. - AckResponse *IBCAckResponse `json:"response,omitempty"` - AckError *IBCAckError `json:"error,omitempty"` -} - -func UnmarshalIBCAck(bz []byte) (*IBCAck, error) { - var ack IBCAck - if err := json.Unmarshal(bz, &ack); err != nil { - return nil, err - } - - switch ack.Type { - case "ack_response": - ack.AckResponse = &IBCAckResponse{} - if err := json.Unmarshal(ack.Content, ack.AckResponse); err != nil { - return nil, err - } - case "ack_error": - ack.AckError = &IBCAckError{} - if err := json.Unmarshal(ack.Content, ack.AckError); err != nil { - return nil, err - } - } - - return &ack, nil -} diff --git a/x/onion/wasm_hook.go b/x/onion/wasm_hook.go deleted file mode 100644 index cca116ae..00000000 --- a/x/onion/wasm_hook.go +++ /dev/null @@ -1,381 +0,0 @@ -package onion - -import ( - "encoding/json" - "fmt" - - sdk "github.com/cosmos/cosmos-sdk/types" - capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" - - errorsmod "cosmossdk.io/errors" - "cosmossdk.io/math" - wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" - wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" - - "github.com/Team-Kujira/core/x/onion/keeper" - transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" - - "github.com/Team-Kujira/core/x/onion/types" -) - -type WasmHooks struct { - ContractKeeper *wasmkeeper.Keeper - ibcHooksKeeper *keeper.Keeper - bech32PrefixAccAddr string -} - -func NewWasmHooks(ibcHooksKeeper *keeper.Keeper, contractKeeper *wasmkeeper.Keeper, bech32PrefixAccAddr string) WasmHooks { - return WasmHooks{ - ContractKeeper: contractKeeper, - ibcHooksKeeper: ibcHooksKeeper, - bech32PrefixAccAddr: bech32PrefixAccAddr, - } -} - -func (h WasmHooks) ProperlyConfigured() bool { - return h.ContractKeeper != nil && h.ibcHooksKeeper != nil -} - -func (h WasmHooks) OnRecvPacketOverride(im IBCMiddleware, ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) ibcexported.Acknowledgement { - if !h.ProperlyConfigured() { - // Not configured - return im.App.OnRecvPacket(ctx, packet, relayer) - } - isIcs20, data := isIcs20Packet(packet.GetData()) - if !isIcs20 { - return im.App.OnRecvPacket(ctx, packet, relayer) - } - - // Validate the memo - isWasmRouted, contractAddr, msgBytes, err := ValidateAndParseMemo(data.GetMemo(), data.Receiver) - if !isWasmRouted { - return im.App.OnRecvPacket(ctx, packet, relayer) - } - if err != nil { - return NewEmitErrorAcknowledgement(ctx, types.ErrMsgValidation, err.Error()) - } - if msgBytes == nil || contractAddr == nil { // This should never happen - return NewEmitErrorAcknowledgement(ctx, types.ErrMsgValidation) - } - - // Calculate the receiver / contract caller based on the packet's channel and sender - channel := packet.GetDestChannel() - sender := data.GetSender() - senderBech32, err := keeper.DeriveIntermediateSender(channel, sender, h.bech32PrefixAccAddr) - if err != nil { - return NewEmitErrorAcknowledgement(ctx, types.ErrBadSender, fmt.Sprintf("cannot convert sender address %s/%s to bech32: %s", channel, sender, err.Error())) - } - - // The funds sent on this packet need to be transferred to the intermediary account for the sender. - // For this, we override the ICS20 packet's Receiver (essentially hijacking the funds to this new address) - // and execute the underlying OnRecvPacket() call (which should eventually land on the transfer app's - // relay.go and send the sunds to the intermediary account. - // - // If that succeeds, we make the contract call - data.Receiver = senderBech32 - bz, err := json.Marshal(data) - if err != nil { - return NewEmitErrorAcknowledgement(ctx, types.ErrMarshaling, err.Error()) - } - packet.Data = bz - - // Execute the receive - ack := im.App.OnRecvPacket(ctx, packet, relayer) - if !ack.Success() { - return ack - } - - amount, ok := math.NewIntFromString(data.GetAmount()) - if !ok { - // This should never happen, as it should've been caught in the underlying call to OnRecvPacket, - // but returning here for completeness - return NewEmitErrorAcknowledgement(ctx, types.ErrInvalidPacket, "Amount is not an int") - } - - // The packet's denom is the denom in the sender chain. This needs to be converted to the local denom. - denom := MustExtractDenomFromPacketOnRecv(packet) - funds := sdk.NewCoins(sdk.NewCoin(denom, amount)) - - // Execute the contract - execMsg := wasmtypes.MsgExecuteContract{ - Sender: senderBech32, - Contract: contractAddr.String(), - Msg: msgBytes, - Funds: funds, - } - response, err := h.execWasmMsg(ctx, &execMsg) - if err != nil { - return NewEmitErrorAcknowledgement(ctx, types.ErrWasmError, err.Error()) - } - - // Check if the contract is requesting for the ack to be async. - var asyncAckRequest types.OnRecvPacketAsyncAckResponse - err = json.Unmarshal(response.Data, &asyncAckRequest) - if err == nil { - // If unmarshalling succeeds, the contract is requesting for the ack to be async. - if asyncAckRequest.IsAsyncAck { // in which case IsAsyncAck is expected to be set to true - if !h.ibcHooksKeeper.IsInAllowList(ctx, contractAddr.String()) { - // Only allowed contracts can send async acks - return NewEmitErrorAcknowledgement(ctx, types.ErrAsyncAckNotAllowed) - } - // Store the contract as the packet's ack actor and return nil - h.ibcHooksKeeper.StorePacketAckActor(ctx, packet, contractAddr.String()) - return nil - } - } - - // If the ack is not async, we continue generating the ack and return it - fullAck := types.ContractAck{ContractResult: response.Data, IbcAck: ack.Acknowledgement()} - bz, err = json.Marshal(fullAck) - if err != nil { - return NewEmitErrorAcknowledgement(ctx, types.ErrBadResponse, err.Error()) - } - - return channeltypes.NewResultAcknowledgement(bz) -} - -func (h WasmHooks) execWasmMsg(ctx sdk.Context, execMsg *wasmtypes.MsgExecuteContract) (*wasmtypes.MsgExecuteContractResponse, error) { - if err := execMsg.ValidateBasic(); err != nil { - return nil, fmt.Errorf(types.ErrBadExecutionMsg, err.Error()) - } - wasmMsgServer := wasmkeeper.NewMsgServerImpl(h.ContractKeeper) - return wasmMsgServer.ExecuteContract(sdk.WrapSDKContext(ctx), execMsg) -} - -func isIcs20Packet(data []byte) (isIcs20 bool, ics20data transfertypes.FungibleTokenPacketData) { - var packetdata transfertypes.FungibleTokenPacketData - if err := json.Unmarshal(data, &packetdata); err != nil { - return false, packetdata - } - return true, packetdata -} - -// jsonStringHasKey parses the memo as a json object and checks if it contains the key. -func jsonStringHasKey(memo, key string) (found bool, jsonObject map[string]interface{}) { - jsonObject = make(map[string]interface{}) - - // If there is no memo, the packet was either sent with an earlier version of IBC, or the memo was - // intentionally left blank. Nothing to do here. Ignore the packet and pass it down the stack. - if len(memo) == 0 { - return false, jsonObject - } - - // the jsonObject must be a valid JSON object - err := json.Unmarshal([]byte(memo), &jsonObject) - if err != nil { - return false, jsonObject - } - - // If the key doesn't exist, there's nothing to do on this hook. Continue by passing the packet - // down the stack - _, ok := jsonObject[key] - if !ok { - return false, jsonObject - } - - return true, jsonObject -} - -func ValidateAndParseMemo(memo string, receiver string) (isWasmRouted bool, contractAddr sdk.AccAddress, msgBytes []byte, err error) { - isWasmRouted, metadata := jsonStringHasKey(memo, "wasm") - if !isWasmRouted { - return isWasmRouted, sdk.AccAddress{}, nil, nil - } - - wasmRaw := metadata["wasm"] - - // Make sure the wasm key is a map. If it isn't, ignore this packet - wasm, ok := wasmRaw.(map[string]interface{}) - if !ok { - return isWasmRouted, sdk.AccAddress{}, nil, - fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, "wasm metadata is not a valid JSON map object") - } - - // Get the contract - contract, ok := wasm["contract"].(string) - if !ok { - // The tokens will be returned - return isWasmRouted, sdk.AccAddress{}, nil, - fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `Could not find key wasm["contract"]`) - } - - contractAddr, err = sdk.AccAddressFromBech32(contract) - if err != nil { - return isWasmRouted, sdk.AccAddress{}, nil, - fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `wasm["contract"] is not a valid bech32 address`) - } - - // The contract and the receiver should be the same for the packet to be valid - if contract != receiver { - return isWasmRouted, sdk.AccAddress{}, nil, - fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `wasm["contract"] should be the same as the receiver of the packet`) - } - - // Ensure the message key is provided - if wasm["msg"] == nil { - return isWasmRouted, sdk.AccAddress{}, nil, - fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `Could not find key wasm["msg"]`) - } - - // Make sure the msg key is a map. If it isn't, return an error - _, ok = wasm["msg"].(map[string]interface{}) - if !ok { - return isWasmRouted, sdk.AccAddress{}, nil, - fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `wasm["msg"] is not a map object`) - } - - // Get the message string by serializing the map - msgBytes, err = json.Marshal(wasm["msg"]) - if err != nil { - // The tokens will be returned - return isWasmRouted, sdk.AccAddress{}, nil, - fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, err.Error()) - } - - return isWasmRouted, contractAddr, msgBytes, nil -} - -func (h WasmHooks) SendPacketOverride(i ICS4Middleware, ctx sdk.Context, chanCap *capabilitytypes.Capability, sourcePort string, sourceChannel string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) (uint64, error) { - isIcs20, ics20data := isIcs20Packet(data) - if !isIcs20 { - return i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) // continue - } - - isCallbackRouted, metadata := jsonStringHasKey(ics20data.GetMemo(), types.IBCCallbackKey) - if !isCallbackRouted { - return i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) // continue - } - - // We remove the callback metadata from the memo as it has already been processed. - - // If the only available key in the memo is the callback, we should remove the memo - // from the data completely so the packet is sent without it. - // This way receiver chains that are on old versions of IBC will be able to process the packet - callbackRaw := metadata[types.IBCCallbackKey] // This will be used later. - delete(metadata, types.IBCCallbackKey) - bzMetadata, err := json.Marshal(metadata) - if err != nil { - return 0, errorsmod.Wrap(err, "Send packet with callback error") - } - stringMetadata := string(bzMetadata) - if stringMetadata == "{}" { - ics20data.Memo = "" - } else { - ics20data.Memo = stringMetadata - } - dataBytes, err := json.Marshal(ics20data) - if err != nil { - return 0, errorsmod.Wrap(err, "Send packet with callback error") - } - - seq, err := i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, dataBytes) - if err != nil { - return 0, err - } - - // Make sure the callback contract is a string and a valid bech32 addr. If it isn't, ignore this packet - contract, ok := callbackRaw.(string) - if !ok { - return 0, nil - } - _, err = sdk.AccAddressFromBech32(contract) - if err != nil { - return 0, nil - } - - h.ibcHooksKeeper.StorePacketCallback(ctx, sourceChannel, seq, contract) - return seq, nil -} - -func (h WasmHooks) OnAcknowledgementPacketOverride(im IBCMiddleware, ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress) error { - err := im.App.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) - if err != nil { - return err - } - - if !h.ProperlyConfigured() { - // Not configured. Return from the underlying implementation - return nil - } - - contract := h.ibcHooksKeeper.GetPacketCallback(ctx, packet.GetSourceChannel(), packet.GetSequence()) - if contract == "" { - // No callback configured - return nil - } - - contractAddr, err := sdk.AccAddressFromBech32(contract) - if err != nil { - return errorsmod.Wrap(err, "Ack callback error") // The callback configured is not a bech32. Error out - } - - success := "false" - if !IsAckError(acknowledgement) { - success = "true" - } - - // Notify the sender that the ack has been received - ackAsJson, err := json.Marshal(acknowledgement) - if err != nil { - // If the ack is not a json object, error - return err - } - - sudoMsg := []byte(fmt.Sprintf( - `{"ibc_lifecycle_complete": {"ibc_ack": {"channel": "%s", "sequence": %d, "ack": %s, "success": %s}}}`, - packet.SourceChannel, packet.Sequence, ackAsJson, success)) - _, err = h.ContractKeeper.Sudo(ctx, contractAddr, sudoMsg) - if err != nil { - // error processing the callback - // ToDo: Open Question: Should we also delete the callback here? - return errorsmod.Wrap(err, "Ack callback error") - } - h.ibcHooksKeeper.DeletePacketCallback(ctx, packet.GetSourceChannel(), packet.GetSequence()) - return nil -} - -func (h WasmHooks) OnTimeoutPacketOverride(im IBCMiddleware, ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress) error { - err := im.App.OnTimeoutPacket(ctx, packet, relayer) - if err != nil { - return err - } - - if !h.ProperlyConfigured() { - // Not configured. Return from the underlying implementation - return nil - } - - contract := h.ibcHooksKeeper.GetPacketCallback(ctx, packet.GetSourceChannel(), packet.GetSequence()) - if contract == "" { - // No callback configured - return nil - } - - contractAddr, err := sdk.AccAddressFromBech32(contract) - if err != nil { - return errorsmod.Wrap(err, "Timeout callback error") // The callback configured is not a bech32. Error out - } - - sudoMsg := []byte(fmt.Sprintf( - `{"ibc_lifecycle_complete": {"ibc_timeout": {"channel": "%s", "sequence": %d}}}`, - packet.SourceChannel, packet.Sequence)) - _, err = h.ContractKeeper.Sudo(ctx, contractAddr, sudoMsg) - if err != nil { - // error processing the callback. This could be because the contract doesn't implement the message type to - // process the callback. Retrying this will not help, so we can delete the callback from storage. - // Since the packet has timed out, we don't expect any other responses that may trigger the callback. - ctx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - "ibc-timeout-callback-error", - sdk.NewAttribute("contract", contractAddr.String()), - sdk.NewAttribute("message", string(sudoMsg)), - sdk.NewAttribute("error", err.Error()), - ), - }) - } - h.ibcHooksKeeper.DeletePacketCallback(ctx, packet.GetSourceChannel(), packet.GetSequence()) - return nil -}