Skip to content

Commit

Permalink
Remove callbacks wiring in ibc go simapp (#4340) (#4463)
Browse files Browse the repository at this point in the history
(cherry picked from commit 96090a7)

Co-authored-by: Cian Hatton <[email protected]>
  • Loading branch information
mergify[bot] and chatton authored Aug 25, 2023
1 parent 8b6fc4a commit 02650e9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 187 deletions.
15 changes: 8 additions & 7 deletions modules/apps/callbacks/ibc_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types"
ibccallbacks "github.com/cosmos/ibc-go/v7/modules/apps/callbacks"
"github.com/cosmos/ibc-go/v7/modules/apps/callbacks/testing/simapp"
"github.com/cosmos/ibc-go/v7/modules/apps/callbacks/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand All @@ -30,14 +31,14 @@ func (s *CallbacksTestSuite) TestNewIBCMiddleware() {
{
"success",
func() {
_ = ibccallbacks.NewIBCMiddleware(ibcmock.IBCModule{}, channelkeeper.Keeper{}, ibcmock.ContractKeeper{}, maxCallbackGas)
_ = ibccallbacks.NewIBCMiddleware(ibcmock.IBCModule{}, channelkeeper.Keeper{}, simapp.ContractKeeper{}, maxCallbackGas)
},
nil,
},
{
"panics with nil underlying app",
func() {
_ = ibccallbacks.NewIBCMiddleware(nil, channelkeeper.Keeper{}, ibcmock.ContractKeeper{}, maxCallbackGas)
_ = ibccallbacks.NewIBCMiddleware(nil, channelkeeper.Keeper{}, simapp.ContractKeeper{}, maxCallbackGas)
},
fmt.Errorf("underlying application does not implement %T", (*types.CallbacksCompatibleModule)(nil)),
},
Expand All @@ -51,14 +52,14 @@ func (s *CallbacksTestSuite) TestNewIBCMiddleware() {
{
"panics with nil ics4Wrapper",
func() {
_ = ibccallbacks.NewIBCMiddleware(ibcmock.IBCModule{}, nil, ibcmock.ContractKeeper{}, maxCallbackGas)
_ = ibccallbacks.NewIBCMiddleware(ibcmock.IBCModule{}, nil, simapp.ContractKeeper{}, maxCallbackGas)
},
fmt.Errorf("ICS4Wrapper cannot be nil"),
},
{
"panics with zero maxCallbackGas",
func() {
_ = ibccallbacks.NewIBCMiddleware(ibcmock.IBCModule{}, channelkeeper.Keeper{}, ibcmock.ContractKeeper{}, uint64(0))
_ = ibccallbacks.NewIBCMiddleware(ibcmock.IBCModule{}, channelkeeper.Keeper{}, simapp.ContractKeeper{}, uint64(0))
},
fmt.Errorf("maxCallbackGas cannot be zero"),
},
Expand Down Expand Up @@ -128,7 +129,7 @@ func (s *CallbacksTestSuite) TestSendPacket() {
{
"failure: callback execution fails, sender is not callback address",
func() {
packetData.Sender = ibcmock.MockCallbackUnauthorizedAddress
packetData.Sender = simapp.MockCallbackUnauthorizedAddress
},
types.CallbackTypeSendPacket,
false,
Expand Down Expand Up @@ -257,7 +258,7 @@ func (s *CallbacksTestSuite) TestOnAcknowledgementPacket() {
{
"failure: callback execution fails, unauthorized address",
func() {
packetData.Sender = ibcmock.MockCallbackUnauthorizedAddress
packetData.Sender = simapp.MockCallbackUnauthorizedAddress
packet.Data = packetData.GetBytes()
},
callbackFailed,
Expand Down Expand Up @@ -407,7 +408,7 @@ func (s *CallbacksTestSuite) TestOnTimeoutPacket() {
{
"failure: callback execution fails, unauthorized address",
func() {
packetData.Sender = ibcmock.MockCallbackUnauthorizedAddress
packetData.Sender = simapp.MockCallbackUnauthorizedAddress
packet.Data = packetData.GetBytes()
},
callbackFailed,
Expand Down
154 changes: 0 additions & 154 deletions testing/mock/contract_keeper.go

This file was deleted.

3 changes: 0 additions & 3 deletions testing/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const (
)

var (
StatefulCounterKey = "stateful-callback-counter"

MockAcknowledgement = channeltypes.NewResultAcknowledgement([]byte("mock acknowledgement"))
MockFailAcknowledgement = channeltypes.NewErrorAcknowledgement(fmt.Errorf("mock failed acknowledgement"))
MockPacketData = []byte("mock packet data")
Expand All @@ -40,7 +38,6 @@ var (
MockRecvCanaryCapabilityName = "mock receive canary capability name"
MockAckCanaryCapabilityName = "mock acknowledgement canary capability name"
MockTimeoutCanaryCapabilityName = "mock timeout canary capability name"
MockCallbackUnauthorizedAddress = "cosmos15ulrf36d4wdtrtqzkgaan9ylwuhs7k7qz753uk"
// MockApplicationCallbackError should be returned when an application callback should fail. It is possible to
// test that this error was returned using ErrorIs.
MockApplicationCallbackError error = &applicationCallbackError{}
Expand Down
28 changes: 5 additions & 23 deletions testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ import (
ibcfee "github.com/cosmos/ibc-go/v7/modules/apps/29-fee"
ibcfeekeeper "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/keeper"
ibcfeetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
ibccallbacks "github.com/cosmos/ibc-go/v7/modules/apps/callbacks"
transfer "github.com/cosmos/ibc-go/v7/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v7/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
Expand Down Expand Up @@ -243,9 +242,6 @@ type SimApp struct {
ScopedIBCMockKeeper capabilitykeeper.ScopedKeeper
ScopedICAMockKeeper capabilitykeeper.ScopedKeeper

// mock contract keeper used for testing
MockContractKeeper ibcmock.ContractKeeper

// make IBC modules public for test purposes
// these modules are never directly routed to by the IBC Router
ICAAuthModule ibcmock.IBCModule
Expand Down Expand Up @@ -380,10 +376,6 @@ func NewSimApp(
appCodec, keys[ibcexported.StoreKey], app.GetSubspace(ibcexported.ModuleName), app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
)

// NOTE: The mock ContractKeeper is only created for testing.
// Real applications should not use the mock ContractKeeper
app.MockContractKeeper = ibcmock.NewContractKeeper(memKeys[ibcmock.MemStoreKey])

// register the proposal types
govRouter := govv1beta1.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govv1beta1.ProposalHandler).
Expand Down Expand Up @@ -445,11 +437,9 @@ func NewSimApp(
ibcRouter := porttypes.NewRouter()

// Middleware Stacks
maxCallbackGas := uint64(1_000_000)

// Create Transfer Keeper and pass IBCFeeKeeper as expected Channel and PortKeeper
// since fee middleware will wrap the IBCKeeper for underlying application.
// NOTE: the Transfer Keeper's ICS4Wrapper can later be replaced.
app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey], app.GetSubspace(ibctransfertypes.ModuleName),
app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
Expand All @@ -470,43 +460,36 @@ func NewSimApp(

// Create Transfer Stack
// SendPacket, since it is originating from the application to core IBC:
// transferKeeper.SendPacket -> fee.SendPacket -> callbacks.SendPacket -> channel.SendPacket
// transferKeeper.SendPacket -> fee.SendPacket -> channel.SendPacket

// RecvPacket, message that originates from core IBC and goes down to app, the flow is the other way
// channel.RecvPacket -> callbacks.OnRecvPacket -> fee.OnRecvPacket -> transfer.OnRecvPacket
// channel.RecvPacket -> fee.OnRecvPacket -> transfer.OnRecvPacket

// transfer stack contains (from top to bottom):
// - IBC Callbacks Middleware
// - IBC Fee Middleware
// - Transfer

// create IBC module from bottom to top of stack
var transferStack porttypes.IBCModule
transferStack = transfer.NewIBCModule(app.TransferKeeper)
transferStack = ibcfee.NewIBCMiddleware(transferStack, app.IBCFeeKeeper)
transferStack = ibccallbacks.NewIBCMiddleware(transferStack, app.IBCFeeKeeper, app.MockContractKeeper, maxCallbackGas)
// Since the callbacks middleware itself is an ics4wrapper, it needs to be passed to the transfer keeper
app.TransferKeeper.WithICS4Wrapper(transferStack.(porttypes.Middleware))

// Add transfer stack to IBC Router
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)

// Create Interchain Accounts Stack
// SendPacket, since it is originating from the application to core IBC:
// icaAuthModuleKeeper.SendTx -> icaController.SendPacket -> fee.SendPacket -> callbacks.SendPacket -> channel.SendPacket
// icaAuthModuleKeeper.SendTx -> icaController.SendPacket -> fee.SendPacket -> channel.SendPacket

// initialize ICA module with mock module as the authentication module on the controller side
var icaControllerStack porttypes.IBCModule
icaControllerStack = ibcmock.NewIBCModule(&mockModule, ibcmock.NewIBCApp("", scopedICAMockKeeper))
app.ICAAuthModule = icaControllerStack.(ibcmock.IBCModule)
icaControllerStack = icacontroller.NewIBCMiddleware(icaControllerStack, app.ICAControllerKeeper)
icaControllerStack = ibcfee.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper)
icaControllerStack = ibccallbacks.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper, app.MockContractKeeper, maxCallbackGas)
// Since the callbacks middleware itself is an ics4wrapper, it needs to be passed to the ica controller keeper
app.ICAControllerKeeper.WithICS4Wrapper(icaControllerStack.(porttypes.Middleware))

// RecvPacket, message that originates from core IBC and goes down to app, the flow is:
// channel.RecvPacket -> callbacks.OnRecvPacket -> fee.OnRecvPacket -> icaHost.OnRecvPacket
// channel.RecvPacket -> fee.OnRecvPacket -> icaHost.OnRecvPacket

var icaHostStack porttypes.IBCModule
icaHostStack = icahost.NewIBCModule(app.ICAHostKeeper)
Expand Down Expand Up @@ -534,8 +517,7 @@ func NewSimApp(
// create fee wrapped mock module
feeMockModule := ibcmock.NewIBCModule(&mockModule, ibcmock.NewIBCApp(MockFeePort, scopedFeeMockKeeper))
app.FeeMockModule = feeMockModule
var feeWithMockModule porttypes.Middleware = ibcfee.NewIBCMiddleware(feeMockModule, app.IBCFeeKeeper)
feeWithMockModule = ibccallbacks.NewIBCMiddleware(feeWithMockModule, app.IBCFeeKeeper, app.MockContractKeeper, maxCallbackGas)
feeWithMockModule := ibcfee.NewIBCMiddleware(feeMockModule, app.IBCFeeKeeper)
ibcRouter.AddRoute(MockFeePort, feeWithMockModule)

// Seal the IBC Router
Expand Down

0 comments on commit 02650e9

Please sign in to comment.