Skip to content

Commit

Permalink
refactor: stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
seantking committed Apr 19, 2022
1 parent 68f4843 commit 6624b73
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type IBCMiddleware struct {
}

// IBCMiddleware creates a new IBCMiddleware given the associated keeper and underlying application
func NewIBCMiddleware(k keeper.Keeper, app porttypes.IBCModule) IBCMiddleware {
func NewIBCMiddleware(app porttypes.IBCModule, k keeper.Keeper) IBCMiddleware {
return IBCMiddleware{
keeper: k,
app: app,
keeper: k,
}
}

Expand Down
45 changes: 25 additions & 20 deletions testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,11 @@ func NewSimApp(
)

// IBC Fee Module keeper
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(appCodec, keys[ibcfeetypes.StoreKey], app.GetSubspace(ibcfeetypes.ModuleName),
app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper, app.AccountKeeper, app.BankKeeper,
app.IBCFeeKeeper = ibcfeekeeper.NewKeeper(
appCodec, keys[ibcfeetypes.StoreKey], app.GetSubspace(ibcfeetypes.ModuleName),
app.IBCKeeper.ChannelKeeper, // may be replaced with IBC middleware
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper, app.AccountKeeper, app.BankKeeper,
)

// ICA Controller keeper
Expand Down Expand Up @@ -389,15 +392,24 @@ func NewSimApp(
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
)

// Mock Module Stack

// Mock Module setup for testing IBC and also acts as the interchain accounts authentication module
// NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do
// not replicate if you do not need to test core IBC or light clients.
mockModule := ibcmock.NewAppModule(&app.IBCKeeper.PortKeeper)

// The mock module is used for testing IBC
mockIBCModule := ibcmock.NewIBCModule(&mockModule, ibcmock.NewMockIBCApp(ibcmock.ModuleName, scopedIBCMockKeeper))
ibcRouter.AddRoute(ibcmock.ModuleName, mockIBCModule)

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

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

transferModule := transfer.NewAppModule(app.TransferKeeper)

// transfer stack contains (from top to bottom):
// - IBC Fee Middleware
// - Transfer
Expand All @@ -410,20 +422,19 @@ func NewSimApp(
// Add transfer stack to IBC Router
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)

// Mock Module setup for testing IBC and acts as the interchain accounts authentication module
// NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do
// not replicate if you do not need to test core IBC or light clients.
mockModule := ibcmock.NewAppModule(&app.IBCKeeper.PortKeeper)

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

// OnAckPacket as this is where fee's are paid out
// mockModule.OnAcknowledgementPacket -> fee.OnAcknowledgementPacket -> channel.OnAcknowledgementPacket

// initialize ICA module with mock module as the authentication module on the controller side
var icaControllerStack porttypes.IBCModule
icaControllerStack = ibcmock.NewIBCModule(&mockModule, ibcmock.NewMockIBCApp("", scopedICAMockKeeper))
app.ICAAuthModule = icaControllerStack.(ibcmock.IBCModule)
icaControllerStack = icacontroller.NewIBCMiddleware(app.ICAControllerKeeper, icaControllerStack)
icaControllerStack = ibcfee.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper)
icaControllerStack = icacontroller.NewIBCMiddleware(icaControllerStack, app.ICAControllerKeeper)

icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper)

Expand All @@ -450,13 +461,7 @@ func NewSimApp(
feeMockModule := ibcmock.NewIBCModule(&mockModule, ibcmock.NewMockIBCApp(MockFeePort, scopedFeeMockKeeper))
app.FeeMockModule = feeMockModule
feeWithMockModule := ibcfee.NewIBCMiddleware(feeMockModule, app.IBCFeeKeeper)

mockIBCModule := ibcmock.NewIBCModule(&mockModule, ibcmock.NewMockIBCApp(ibcmock.ModuleName, scopedIBCMockKeeper))

// Add Mock Module & Mock Fee Module to IBC Router
ibcRouter.
AddRoute(ibcmock.ModuleName, mockIBCModule).
AddRoute(MockFeePort, feeWithMockModule)
ibcRouter.AddRoute(MockFeePort, feeWithMockModule)

// Seal the IBC Router
app.IBCKeeper.SetRouter(ibcRouter)
Expand Down Expand Up @@ -500,7 +505,7 @@ func NewSimApp(
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),

// IBC modules
transferModule,
transfer.NewAppModule(app.TransferKeeper),
ibcfee.NewAppModule(app.IBCFeeKeeper),
ica.NewAppModule(&app.ICAControllerKeeper, &app.ICAHostKeeper),
mockModule,
Expand Down Expand Up @@ -562,7 +567,7 @@ func NewSimApp(
evidence.NewAppModule(app.EvidenceKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
ibc.NewAppModule(app.IBCKeeper),
transferModule,
transfer.NewAppModule(app.TransferKeeper),
)

app.sm.RegisterStoreDecoders()
Expand Down

0 comments on commit 6624b73

Please sign in to comment.