Skip to content
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

refactor: simplify IBC redundant relay check in given restructure of SDK v0.46 #1288

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: simplify ibc redundancy check used as sdk middleware
Instead of having the function checkRedundancy check if the call is on CheckTx or SimulateTx, only call the function on CheckTx or SimulateTx
  • Loading branch information
colin-axner committed Apr 25, 2022
commit 8910aa3c2c2f156e01d8591ca2255d77d33c3ccd
127 changes: 127 additions & 0 deletions modules/core/keeper/sdk_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package keeper
Copy link
Contributor 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 a strong preference on
modules/core/sdk_middleware/sdk_middleware.go
vs
modules/core/keeper/sdk_middleware.go

but my primary concern is I want to clearly differentiate the difference between IBC middleware and a transaction handler added into the SDK middleware stack

Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer this approach as well :)


import (
"context"

"github.com/cosmos/cosmos-sdk/types/tx"

clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
)

var _ tx.Handler = ibcTxHandler{}

type ibcTxHandler struct {
k *Keeper
next tx.Handler
}

// IBCTxMiddleware implements ibc tx handling middleware
func IBCTxMiddleware(IBCKeeper *Keeper) tx.Middleware {
return func(txh tx.Handler) tx.Handler {
return ibcTxHandler{
k: IBCKeeper,
next: txh,
}
}
}

// perform redundancy checks on IBC relays. If a transaction contains only `RecvPacket`, `AcknowledgePacket`, `TimeoutPacket/OnClose` and `UpdateClient`
// and all the relay messages are redundant, it will be dropped from the mempool.
func (itxh ibcTxHandler) checkRedundancy(ctx context.Context, req tx.Request) error {
// keep track of total packet messages and number of redundancies across `RecvPacket`, `AcknowledgePacket`, and `TimeoutPacket/OnClose`
redundancies := 0
packetMsgs := 0

for _, m := range req.Tx.GetMsgs() {
switch msg := m.(type) {
case *channeltypes.MsgRecvPacket:
response, err := itxh.k.RecvPacket(ctx, msg)
if err != nil {
return err
}

if response.Result == channeltypes.NOOP {
redundancies += 1
}
packetMsgs += 1

case *channeltypes.MsgAcknowledgement:
response, err := itxh.k.Acknowledgement(ctx, msg)
if err != nil {
return err
}

if response.Result == channeltypes.NOOP {
redundancies += 1
}
packetMsgs += 1

case *channeltypes.MsgTimeout:
response, err := itxh.k.Timeout(ctx, msg)
if err != nil {
return err
}

if response.Result == channeltypes.NOOP {
redundancies += 1
}
packetMsgs += 1

case *channeltypes.MsgTimeoutOnClose:
response, err := itxh.k.TimeoutOnClose(ctx, msg)
if err != nil {
return err
}

if response.Result == channeltypes.NOOP {
redundancies += 1
}
packetMsgs += 1

case *clienttypes.MsgUpdateClient:
if _, err := itxh.k.UpdateClient(ctx, msg); err != nil {
return err
}

default:
// if the multiMsg tx has a msg that is not a packet msg or update msg, then we will not return error
// regardless of if all packet messages are redundant. This ensures that non-packet messages get processed
// even if they get batched with redundant packet messages.
return nil
}
}

// only return error if all packet messages are redundant
if redundancies == packetMsgs && packetMsgs > 0 {
return channeltypes.ErrRedundantTx
}

return nil
}

// CheckTx implements tx.Handler.CheckTx. Run redundancy checks on CheckTx to filter any redundant
// relays in the mempool.
func (itxh ibcTxHandler) CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error) {
if err := itxh.checkRedundancy(ctx, req); err != nil {
return tx.Response{}, tx.ResponseCheckTx{}, err
}

return itxh.next.CheckTx(ctx, req, checkReq)
}

// DeliverTx implements tx.Handler.DeliverTx. Redundancy checks are not run on DeliverTx since
// the transaction has already been included in a block.
func (itxh ibcTxHandler) DeliverTx(ctx context.Context, req tx.Request) (tx.Response, error) {
return itxh.next.DeliverTx(ctx, req)
}

// SimulateTx implements tx.Handler.SimulateTx. Run redundancy checks on SimulateTx to filter any redundant
// relays in the mempool.
func (itxh ibcTxHandler) SimulateTx(ctx context.Context, req tx.Request) (tx.Response, error) {
if err := itxh.checkRedundancy(ctx, req); err != nil {
return tx.Response{}, err
}

return itxh.next.SimulateTx(ctx, req)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sdk_middleware_test
package keeper_test

import (
"context"
Expand All @@ -14,7 +14,7 @@ import (
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
ibcmiddleware "github.com/cosmos/ibc-go/v3/modules/core/middleware"
"github.com/cosmos/ibc-go/v3/modules/core/keeper"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
)

Expand Down Expand Up @@ -436,7 +436,7 @@ func (suite *MiddlewareTestSuite) TestMiddleware() {
// commiting it to a block, so that the when check tx runs with the redundant
// message they are both in the same block
k := suite.chainB.App.GetIBCKeeper()
mw := ibcmiddleware.IBCTxMiddleware(k)
mw := keeper.IBCTxMiddleware(k)
checkCtx := suite.chainB.GetContext().WithIsCheckTx(true)
txHandler := middleware.ComposeMiddlewares(noopTxHandler, mw)

Expand All @@ -462,7 +462,7 @@ func (suite *MiddlewareTestSuite) TestMiddleware() {
suite.SetupTest()

k := suite.chainB.App.GetIBCKeeper()
mw := ibcmiddleware.IBCTxMiddleware(k)
mw := keeper.IBCTxMiddleware(k)

msgs := tc.malleate(suite)

Expand Down
128 changes: 0 additions & 128 deletions modules/core/sdk_middleware/sdk_middleware.go

This file was deleted.

3 changes: 1 addition & 2 deletions testing/simapp/sdk_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
authmiddleware "github.com/cosmos/cosmos-sdk/x/auth/middleware"

"github.com/cosmos/ibc-go/v3/modules/core/keeper"
ibcmiddleware "github.com/cosmos/ibc-go/v3/modules/core/sdk_middleware"
)

// ComposeMiddlewares composes multiple middlewares on top of a tx.Handler. The
Expand Down Expand Up @@ -106,6 +105,6 @@ func NewDefaultTxHandler(options TxHandlerOptions) (tx.Handler, error) {
// should be accounted for, should go below this authmiddleware.
authmiddleware.ConsumeBlockGasMiddleware,
authmiddleware.NewTipMiddleware(options.BankKeeper),
ibcmiddleware.IBCTxMiddleware(options.IBCKeeper),
keeper.IBCTxMiddleware(options.IBCKeeper),
), nil
}