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

Merged
merged 17 commits into from
Jun 26, 2025
Merged

Middleware Wiring Improvements #8528

merged 17 commits into from
Jun 26, 2025

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.09278% with 91 lines in your changes missing coverage. Please review.

Project coverage is 57.99%. Comparing base (4d99a06) to head (b0c3bed).
Report is 1 commits behind head on main.

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% 30 Missing ⚠️
modules/apps/callbacks/testing/simapp/app.go 0.00% 16 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   58.00%   57.99%   -0.01%     
==========================================
  Files         317      319       +2     
  Lines       22743    22814      +71     
==========================================
+ Hits        13191    13231      +40     
- Misses       8948     8978      +30     
- Partials      604      605       +1     
Flag Coverage Δ
08-wasm 65.99% <ø> (ø)
e2e 1.13% <ø> (ø)
ibc-go 63.43% <53.09%> (-0.03%) ⬇️

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.

@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).

@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.

@@ -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.

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 has been the general pattern.

middleware has SetICS4Wrapper

and keepers have:

WithICS4Wrapper. Is there a place where this pattern is broken or do you take issue with the pattern itself?

@@ -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?

Copy link
Member Author

Choose a reason for hiding this comment

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

This should be a seperate issue and PR if desired

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair! I'll create an issue for that 👍

@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.

Copy link
Member Author

@AdityaSripal AdityaSripal left a comment

Choose a reason for hiding this comment

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

The rate limiting changes are correct

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.

Left a couple of small comments and fixes on doc stuff, but I think we're very close!! 🙌

@@ -55,13 +58,26 @@ customIBCModule1 := custom.NewIBCModule(customKeeper1, "portCustom1")
customIBCModule2 := custom.NewIBCModule(customKeeper2, "portCustom2")

// create IBC stacks by combining middleware with base application
// IBC Stack builders are initialized with the IBC ChannelKeeper which is the top-level ICS4Wrapper
// NOTE: since middleware2 is stateless it does not require a Keeper
// stack 1 contains mw1 -> mw3 -> transfer
Copy link
Contributor

Choose a reason for hiding this comment

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

from looking at this, although you specify the order I still don't think it is easy to understand conceptually and why one middleware should be before another in the ordering. Just having more examples doesn't really explain how you reason about the order of the stack. I think it is fine to have the mock examples with mw1 etc, but I think it would be useful to give more detail on how to reason about the underlying order. Concretely we have 3 middlewares in the repo and the assumption is users should expect to have them all for transfer so I think we should concretely relate to this.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is a good point. The code itself helps reason about what order you actually end up getting, but not necessarily which order they ought to be in.

It's hard to get too concrete on this, since it depends on which middlewares you use, but we can certainly use the ones in the repo as an example. We should def do that.

I think we could add a section on middleware ordering where we go through how the ordering works (i.e. how the ordering relates to the actual flow of a packet on send and receive) and use our own middlewares as an example to show how we decided on the order of those.

(@womensrights: Separately from this, the docs for each middleware should probably also make it clear if they have any place in the stack they should be - this is particularly relevant for rate limiting!)

Copy link
Contributor

Choose a reason for hiding this comment

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

yes agreed, like there are likely some heuristics that could help you reason through ordering i.e. should this middleware act like a gate for any further processing (e.g. rate limiting) or should this middleware always be last to execute... just from the top of my head

and yes I think we should just be very concrete with what we have under our maintenance so reasoning should be really clear for pfm, rate limiting and callbacks in a stack

Copy link
Contributor

Choose a reason for hiding this comment

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

also thinking about this more, it seems we are going to have multiple ways to wire middleware if you use the stack builder or not, I think this is going to be messy and confusing and there should only be one recommended way

Copy link
Contributor

Choose a reason for hiding this comment

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

so perhaps this is just a docs thing as people could still choose to use the old way, but I think we only show the new way from v11 docs onwards

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 don't have good context anymore on exactly what order we should be prescribing. Especially in the context of the new middlewares in the repo.

We only are showing the stack builder approach in the docs from now on and only recommending this approach. Though i imagine some people will still use the old way.

I tried addressing this, but feel im missing the information i need at this point in terms of exactly what you want here. Since its not really in scope for the stack builder approach, Id prefer to merge this and have this comment be a general improvement of the middleware docs that we should tackle separately

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll merge this now, but sounds like there are some doc improvements needed @womensrights

@gjermundgaraba gjermundgaraba merged commit 72ce552 into main Jun 26, 2025
53 of 54 checks passed
@gjermundgaraba gjermundgaraba deleted the aditya/mw-improvement branch June 26, 2025 17:24
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
3 participants