-
Notifications
You must be signed in to change notification settings - Fork 636
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
colin-axner
merged 2 commits into
carlos/upgrade-pr-branch
from
colin/refactor-redundancy-checktx
Apr 25, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
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
commit 8910aa3c2c2f156e01d8591ca2255d77d33c3ccd
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package keeper | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)