Skip to content

Middleware Wiring Improvements #8528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

AdityaSripal
Copy link
Member

Description

closes: #5814


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Linked to GitHub issue with discussion and accepted design, OR link to spec that describes this work.
  • Include changelog entry when appropriate (e.g. chores should be omitted from changelog).
  • Wrote unit and integration tests if relevant.
  • Updated documentation (docs/) if anything is changed.
  • Added godoc comments if relevant.
  • Self-reviewed Files changed in the GitHub PR explorer.
  • Provide a conventional commit message to follow the repository standards.

Copy link

codecov bot commented Jun 13, 2025

Codecov Report

Attention: Patch coverage is 53.68421% with 88 lines in your changes missing coverage. Please review.

Project coverage is 57.59%. Comparing base (5f8fd48) to head (166bc4a).

Files with missing lines Patch % Lines
modules/apps/rate-limiting/ibc_middleware.go 0.00% 30 Missing ⚠️
modules/core/05-port/types/stack.go 0.00% 26 Missing ⚠️
modules/apps/callbacks/testing/simapp/app.go 0.00% 17 Missing ⚠️
...s/apps/packet-forward-middleware/ibc_middleware.go 77.77% 6 Missing ⚠️
modules/apps/rate-limiting/keeper/ics4.go 33.33% 4 Missing ⚠️
modules/apps/callbacks/ibc_middleware.go 83.33% 1 Missing and 1 partial ⚠️
modules/apps/rate-limiting/module.go 0.00% 2 Missing ⚠️
...es/apps/packet-forward-middleware/keeper/keeper.go 90.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8528   +/-   ##
=======================================
  Coverage   57.58%   57.59%           
=======================================
  Files         317      319    +2     
  Lines       22666    22733   +67     
=======================================
+ Hits        13052    13092   +40     
- Misses       9006     9032   +26     
- Partials      608      609    +1     
Flag Coverage Δ
08-wasm 65.99% <ø> (ø)
e2e 1.13% <ø> (ø)
ibc-go 62.99% <53.68%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -26,27 +26,37 @@ var (
// ICA controller keeper and the underlying application.
type IBCMiddleware struct {
app porttypes.IBCModule
keeper keeper.Keeper
keeper *keeper.Keeper
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the keepers inside middlewares and modules to now be pointers so that I can call WithICS4Wrapper on them and have it persist in other references

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a requirement for middlewares wiring up with this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are using a keeper function to do SendPacket and WriteAck then yes. We have middlewares that don't use keepers (e.g. callbacks) so then its not needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sounds like it will be quite important to get some migration docs for all of this then!

storeService: storeService,
cdc: cdc,
ics4Wrapper: ics4Wrapper,
ics4Wrapper: channelKeeper,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewKeeper defaults to using channelKeeper as ICS4Wrapper. If you need this changed, you should use the stack builder primitive to set a new ICS4Wrapper

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to document this in the migration guide!

@AdityaSripal AdityaSripal marked this pull request as ready for review June 14, 2025 20:33
Copy link
Contributor

@gjermundgaraba gjermundgaraba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a first pass, left a couple of questions, but this is clearly a big improvement, and it should be much harder to do this incorrectly. It still will require documentation and migration guides. At least the migration guide additions would be good to get sorted in this PR (for both chains and middleware devs).

@@ -26,27 +26,37 @@ var (
// ICA controller keeper and the underlying application.
type IBCMiddleware struct {
app porttypes.IBCModule
keeper keeper.Keeper
keeper *keeper.Keeper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a requirement for middlewares wiring up with this?


// Add transfer stack to IBC Router
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack.Build(app.IBCKeeper.ChannelKeeper))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense for the channel keeper to just be set on construction instead of being passed into the build method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super important though...

// The ICS4Wrapper **must** be used for sending packets and writing acknowledgements
// to ensure that the middleware can intercept and process these calls.
// Do not use the channel keeper directly to send packets or write acknowledgements
// as this will bypass the middleware.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so I understand: this has always been a requirement of middlewares (to not use the channel keeper directly)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct, though many people don't do it anyway. Which ends up being functionally ok because most of the middlewares are modifying recvpacket and not send and writeack

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where we are on docs with this, but we should:

  1. Make sure the docs are clear about this
  2. Link to that in the migration guide

@gjermundgaraba gjermundgaraba requested a review from Copilot June 15, 2025 18:07
Copilot

This comment was marked as outdated.

@gjermundgaraba gjermundgaraba mentioned this pull request Jun 17, 2025
10 tasks
Copy link
Contributor

@gjermundgaraba gjermundgaraba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I left some more comments, but the main thing we need to get in here is docs on the wiring and migration docs for both chains and middleware devs.

FYI: I merged in main and linted, so you should probably take a look through all the code another time yourself too.
You should in particular check out the rate-limiting middleware as I integrated it myself, and made similar changes that you've made to the other middlewares.

@@ -1,70 +1,69 @@
package ratelimiting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdityaSripal I merged in rate-limiting, so make sure it looks correct, please :)

@@ -2,6 +2,9 @@ module github.com/cosmos/ibc-go/e2e

go 1.24.3

// TODO: Remove when v11 release of interchaintest is available (that is where this one is coming from)
replace github.com/cosmos/interchain-security/v7 => github.com/cosmos/interchain-security/v7 v7.0.0-20250622154438-73c73cf686e5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add this, because interchaintest pulls in this, and the changes here break it :/
The PR that has the commit referenced in the replace: cosmos/interchain-security#2622

// to ensure that the middleware can intercept and process these calls.
// Do not use the channel keeper directly to send packets or write acknowledgements
// as this will bypass the middleware.
SetICS4Wrapper(wrapper ICS4Wrapper)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things that might cause confusion:

  1. We have a mix of "SetICS4Wrapper" and "WithICS4Wrapper"
  2. We have these both in keepers and middleware (which I understand), but it makes for confusing interfaces

Can I suggest thinking about renaming so that perhaps the middleware SetICS4Wrapper is named distinctly from the keepers version of it? Ideally with something in the name that makes it clearer what it's for (and that it is middleware specific). There are also keepers that accidentally fulfill this interface, I don't think they should.

@@ -0,0 +1,51 @@
package types

type IBCStackBuilder struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an opportunity to give this extra value by adding a query to ibc core (or somewhere where it makes sense) for users to see the stack. Skip go currently keeps a manual repository of which chains support PFM, for instance. If they could just query, it would help them automate this in the future.
Additionally, chain devs could debug their stack more easily.

It could either just take list here and add another function to the Middleware interface to return a name (types.ModuleName type of response), or maybe even have it come out of ICS4Wrapper so it actually flows through the application stack.
wdyt?


// Add transfer stack to IBC Router
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack.Build(app.IBCKeeper.ChannelKeeper))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super important though...

@gjermundgaraba gjermundgaraba requested a review from Copilot June 22, 2025 21:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the middleware wiring across multiple modules by converting various keeper and module types to pointer types and standardizing the use of ICS4Wrapper across IBC modules. Key changes include updating the return types in NewKeeper functions, refactoring middleware stack building logic, and revising test cases to align with the new pointer-based designs.

Reviewed Changes

Copilot reviewed 50 out of 51 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
testing/simapp/app.go Updated keeper pointer types for ICA, Transfer, and RateLimit modules.
testing/mock/middleware.go Refactored ICS4Wrapper setter and related panic behavior.
testing/mock/ibc_module.go Modified NewIBCModule return type to pointer and adjusted ICS4Wrapper conversion.
simapp/app.go Reworked keeper instantiation and middleware wiring with pointer consistency.
modules/light-clients/08-wasm/testing/simapp/app.go Applied updates for pointer types and middleware wiring in the testing simapp.
modules/core/api/module.go Corrected naming of “PacketUnmarshalerModule” for clarity.
modules/core/ante/ante_test.go Adjusted message creation functions to use updated parameter names.
modules/core/05-port/types/stack.go Introduced IBCStackBuilder enhancements to support middleware stacking.
modules/core/05-port/types/module.go Updated ICS4Wrapper setter documentation and interface methods.
modules/apps/transfer/** Converted keeper and module functions to use pointer types and updated ICS4Wrapper wiring.
modules/apps/rate-limiting/** Updated keeper creation, query server registration, and test cases to use pointer types.
modules/apps/packet-forward-middleware/** Standardized keeper instantiation, updated ICS4Wrapper handling, and fixed type casts.
modules/apps/callbacks/** Revised middleware and application wiring for callbacks, ensuring pointer correctness.
modules/apps/27-interchain-accounts/** Updated keeper and IBCModule implementations for host and controller chains, including genesis and test files.
e2e/go.mod Added a replace directive for interchain-security dependency version.

@@ -282,6 +284,7 @@ func (k *Keeper) ForwardTransferPacket(ctx sdk.Context, inFlightPacket *types.In
"denom", token.Denom,
"error", err,
)
// TODO: Should probably have custom errors!
return errorsmod.Wrap(sdkerrors.ErrInsufficientFunds, err.Error())
Copy link
Preview

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider defining and using custom error types instead of wrapping with generic errors. This will provide clearer error context and facilitate better error handling in production code.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Middleware Wiring Improvements
2 participants