From 6dfa5cb41c52180d30c2d0e1aed3fa2685a68c33 Mon Sep 17 00:00:00 2001 From: Assaf Morami Date: Wed, 3 May 2023 13:52:13 +0300 Subject: [PATCH] Use ICS4Wrapper to send raw IBC packets & fix Fee in wasm stack --- app/app.go | 1 + x/wasm/keeper/genesis_test.go | 1 + x/wasm/keeper/handler_plugin.go | 15 +++++++++++---- x/wasm/keeper/handler_plugin_test.go | 2 +- x/wasm/keeper/keeper_cgo.go | 5 ++++- x/wasm/keeper/keeper_test.go | 2 +- x/wasm/keeper/options_test.go | 2 +- x/wasm/keeper/test_common.go | 1 + 8 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/app.go b/app/app.go index d8e240a947..086ddfd090 100644 --- a/app/app.go +++ b/app/app.go @@ -522,6 +522,7 @@ func NewWasmApp( app.BankKeeper, app.StakingKeeper, app.DistrKeeper, + app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper, scopedWasmKeeper, diff --git a/x/wasm/keeper/genesis_test.go b/x/wasm/keeper/genesis_test.go index 063b2adc50..ddf89f8a29 100644 --- a/x/wasm/keeper/genesis_test.go +++ b/x/wasm/keeper/genesis_test.go @@ -663,6 +663,7 @@ func setupKeeper(t *testing.T) (*Keeper, sdk.Context, []sdk.StoreKey) { nil, nil, nil, + nil, tempDir, wasmConfig, AvailableCapabilities, diff --git a/x/wasm/keeper/handler_plugin.go b/x/wasm/keeper/handler_plugin.go index bff3550eaa..0e76d40b9a 100644 --- a/x/wasm/keeper/handler_plugin.go +++ b/x/wasm/keeper/handler_plugin.go @@ -9,6 +9,7 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + ibctransfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types" channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types" host "github.com/cosmos/ibc-go/v4/modules/core/24-host" @@ -34,6 +35,7 @@ type SDKMessageHandler struct { func NewDefaultMessageHandler( router MessageRouter, + ics4Wrapper ibctransfertypes.ICS4Wrapper, channelKeeper types.ChannelKeeper, capabilityKeeper types.CapabilityKeeper, bankKeeper types.Burner, @@ -47,7 +49,7 @@ func NewDefaultMessageHandler( } return NewMessageHandlerChain( NewSDKMessageHandler(router, encoders), - NewIBCRawPacketHandler(channelKeeper, capabilityKeeper), + NewIBCRawPacketHandler(ics4Wrapper, channelKeeper, capabilityKeeper), NewBurnCoinMessageHandler(bankKeeper), ) } @@ -142,12 +144,17 @@ func (m MessageHandlerChain) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAd // IBCRawPacketHandler handels IBC.SendPacket messages which are published to an IBC channel. type IBCRawPacketHandler struct { + ics4Wrapper ibctransfertypes.ICS4Wrapper channelKeeper types.ChannelKeeper capabilityKeeper types.CapabilityKeeper } -func NewIBCRawPacketHandler(chk types.ChannelKeeper, cak types.CapabilityKeeper) IBCRawPacketHandler { - return IBCRawPacketHandler{channelKeeper: chk, capabilityKeeper: cak} +func NewIBCRawPacketHandler(ics4Wrapper ibctransfertypes.ICS4Wrapper, channelKeeper types.ChannelKeeper, capabilityKeeper types.CapabilityKeeper) IBCRawPacketHandler { + return IBCRawPacketHandler{ + ics4Wrapper: ics4Wrapper, + channelKeeper: channelKeeper, + capabilityKeeper: capabilityKeeper, + } } // DispatchMsg publishes a raw IBC packet onto the channel. @@ -189,7 +196,7 @@ func (h IBCRawPacketHandler) DispatchMsg(ctx sdk.Context, _ sdk.AccAddress, cont msg.IBC.SendPacket.Timeout.Timestamp, ) - if err := h.channelKeeper.SendPacket(ctx, channelCap, packet); err != nil { + if err := h.ics4Wrapper.SendPacket(ctx, channelCap, packet); err != nil { return nil, nil, sdkerrors.Wrap(err, "failed to send packet") } diff --git a/x/wasm/keeper/handler_plugin_test.go b/x/wasm/keeper/handler_plugin_test.go index ffa63b692d..e572cf6e41 100644 --- a/x/wasm/keeper/handler_plugin_test.go +++ b/x/wasm/keeper/handler_plugin_test.go @@ -307,7 +307,7 @@ func TestIBCRawPacketHandler(t *testing.T) { t.Run(name, func(t *testing.T) { capturedPacket = nil // when - h := NewIBCRawPacketHandler(spec.chanKeeper, spec.capKeeper) + h := NewIBCRawPacketHandler(spec.chanKeeper, spec.chanKeeper, spec.capKeeper) evts, data, gotErr := h.DispatchMsg(ctx, RandomAccountAddress(t), ibcPort, wasmvmtypes.CosmosMsg{IBC: &wasmvmtypes.IBCMsg{SendPacket: &spec.srcMsg}}) // then require.True(t, spec.expErr.Is(gotErr), "exp %v but got %#+v", spec.expErr, gotErr) diff --git a/x/wasm/keeper/keeper_cgo.go b/x/wasm/keeper/keeper_cgo.go index 06a0ccabe6..8b94f5465c 100644 --- a/x/wasm/keeper/keeper_cgo.go +++ b/x/wasm/keeper/keeper_cgo.go @@ -11,6 +11,8 @@ import ( paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/CosmWasm/wasmd/x/wasm/types" + + ibctransfertypes "github.com/cosmos/ibc-go/v4/modules/core/05-port/types" ) // NewKeeper creates a new contract Keeper instance @@ -23,6 +25,7 @@ func NewKeeper( bankKeeper types.BankKeeper, stakingKeeper types.StakingKeeper, distKeeper types.DistributionKeeper, + ics4Wrapper ibctransfertypes.ICS4Wrapper, channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper, capabilityKeeper types.CapabilityKeeper, @@ -52,7 +55,7 @@ func NewKeeper( accountPruner: NewVestingCoinBurner(bankKeeper), portKeeper: portKeeper, capabilityKeeper: capabilityKeeper, - messenger: NewDefaultMessageHandler(router, channelKeeper, capabilityKeeper, bankKeeper, cdc, portSource), + messenger: NewDefaultMessageHandler(router, ics4Wrapper, channelKeeper, capabilityKeeper, bankKeeper, cdc, portSource), queryGasLimit: wasmConfig.SmartQueryGasLimit, paramSpace: paramSpace, gasRegister: NewDefaultWasmGasRegister(), diff --git a/x/wasm/keeper/keeper_test.go b/x/wasm/keeper/keeper_test.go index 872f0925bc..bf25c52284 100644 --- a/x/wasm/keeper/keeper_test.go +++ b/x/wasm/keeper/keeper_test.go @@ -749,7 +749,7 @@ func TestInstantiateWithContractFactoryChildQueriesParent(t *testing.T) { router := baseapp.NewMsgServiceRouter() router.SetInterfaceRegistry(keepers.EncodingConfig.InterfaceRegistry) types.RegisterMsgServer(router, NewMsgServerImpl(NewDefaultPermissionKeeper(keeper))) - keeper.messenger = NewDefaultMessageHandler(router, nil, nil, nil, keepers.EncodingConfig.Marshaler, nil) + keeper.messenger = NewDefaultMessageHandler(router, nil, nil, nil, nil, keepers.EncodingConfig.Marshaler, nil) // overwrite wasmvm in response handler keeper.wasmVMResponseHandler = NewDefaultWasmVMContractResponseHandler(NewMessageDispatcher(keeper.messenger, keeper)) diff --git a/x/wasm/keeper/options_test.go b/x/wasm/keeper/options_test.go index 1c4bd921bf..d15b3bf58a 100644 --- a/x/wasm/keeper/options_test.go +++ b/x/wasm/keeper/options_test.go @@ -115,7 +115,7 @@ func TestConstructorOptions(t *testing.T) { } for name, spec := range specs { t.Run(name, func(t *testing.T) { - k := NewKeeper(nil, nil, paramtypes.NewSubspace(nil, nil, nil, nil, ""), authkeeper.AccountKeeper{}, &bankkeeper.BaseKeeper{}, stakingkeeper.Keeper{}, distributionkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, spec.srcOpt) + k := NewKeeper(nil, nil, paramtypes.NewSubspace(nil, nil, nil, nil, ""), authkeeper.AccountKeeper{}, &bankkeeper.BaseKeeper{}, stakingkeeper.Keeper{}, distributionkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, spec.srcOpt) spec.verify(t, k) }) } diff --git a/x/wasm/keeper/test_common.go b/x/wasm/keeper/test_common.go index b84e0025a3..67461a7871 100644 --- a/x/wasm/keeper/test_common.go +++ b/x/wasm/keeper/test_common.go @@ -380,6 +380,7 @@ func createTestInput( bankKeeper, stakingKeeper, distKeeper, + ibcKeeper.ChannelKeeper, // ICS4Wrapper ibcKeeper.ChannelKeeper, &ibcKeeper.PortKeeper, scopedWasmKeeper,