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

Reject Redundant Tx Antedecorator #235

Merged
merged 11 commits into from
Jul 19, 2021
55 changes: 55 additions & 0 deletions modules/core/04-channel/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package channel

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/modules/core/04-channel/keeper"
"github.com/cosmos/ibc-go/modules/core/04-channel/types"
)

type ChannelAnteDecorator struct {
k keeper.Keeper
}

func NewChannelAnteDecorator(k keeper.Keeper) ChannelAnteDecorator {
return ChannelAnteDecorator{k: k}
}

func (cad ChannelAnteDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
// do not run redundancy check on DeliverTx or simulate
if (ctx.IsCheckTx() || ctx.IsReCheckTx()) && !simulate {
// keep track of total packet messages and number of redundancies across `RecvPacket`, `AcknowledgePacket`, and `TimeoutPacket/OnClose`
msgs := 0
redundancies := 0
for _, m := range tx.GetMsgs() {
if msg, ok := m.(*types.MsgRecvPacket); ok {
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
if _, found := cad.k.GetPacketReceipt(ctx, msg.Packet.GetDestPort(), msg.Packet.GetDestChannel(), msg.Packet.GetSequence()); found {
redundancies += 1
}
msgs += 1
}
if msg, ok := m.(*types.MsgAcknowledgement); ok {
if commitment := cad.k.GetPacketCommitment(ctx, msg.Packet.GetSourcePort(), msg.Packet.GetSourceChannel(), msg.Packet.GetSequence()); len(commitment) == 0 {
redundancies += 1
}
msgs += 1
}
if msg, ok := m.(*types.MsgTimeout); ok {
if commitment := cad.k.GetPacketCommitment(ctx, msg.Packet.GetSourcePort(), msg.Packet.GetSourceChannel(), msg.Packet.GetSequence()); len(commitment) == 0 {
redundancies += 1
}
msgs += 1
}
if msg, ok := m.(*types.MsgTimeoutOnClose); ok {
if commitment := cad.k.GetPacketCommitment(ctx, msg.Packet.GetSourcePort(), msg.Packet.GetSourceChannel(), msg.Packet.GetSequence()); len(commitment) == 0 {
redundancies += 1
}
msgs += 1
}
AdityaSripal marked this conversation as resolved.
Show resolved Hide resolved
}
// return error if all packet messages are redundant
if redundancies == msgs && msgs > 0 {
return ctx, types.ErrRedundantTx
}
}
return next(ctx, tx, simulate)
}
3 changes: 3 additions & 0 deletions modules/core/04-channel/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ var (

// ORDERED channel error
ErrPacketSequenceOutOfOrder = sdkerrors.Register(SubModuleName, 21, "packet sequence is out of order")

// Antehandler error
ErrRedundantTx = sdkerrors.Register(SubModuleName, 22, "packet messages are redundant")
)