From 6fa295c857839de672e00a2c44ee3b00426bb47f Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 7 Jul 2020 04:03:43 -0400 Subject: [PATCH] fix NextSeqAck increment logic (#6619) Co-authored-by: Alexander Bezobchuk Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- x/ibc/04-channel/keeper/packet.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/x/ibc/04-channel/keeper/packet.go b/x/ibc/04-channel/keeper/packet.go index 94aea8068..338911ecc 100644 --- a/x/ibc/04-channel/keeper/packet.go +++ b/x/ibc/04-channel/keeper/packet.go @@ -391,6 +391,7 @@ func (k Keeper) AcknowledgePacket( return sdkerrors.Wrap(err, "invalid acknowledgement on counterparty chain") } + // assert packets acknowledged in order if channel.Ordering == types.ORDERED { nextSequenceAck, found := k.GetNextSequenceAck(ctx, packet.GetDestPort(), packet.GetDestChannel()) if !found { @@ -406,8 +407,6 @@ func (k Keeper) AcknowledgePacket( "packet sequence ≠ next ack sequence (%d ≠ %d)", packet.GetSequence(), nextSequenceAck, ) } - - k.SetNextSequenceAck(ctx, packet.GetDestPort(), packet.GetDestChannel(), nextSequenceAck+1) } // NOTE: the remaining code is located in the AcknowledgementExecuted function @@ -423,8 +422,7 @@ func (k Keeper) AcknowledgementExecuted( chanCap *capabilitytypes.Capability, packet exported.PacketI, ) error { - // sanity check - _, found := k.GetChannel(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) + channel, found := k.GetChannel(ctx, packet.GetSourcePort(), packet.GetSourceChannel()) if !found { return sdkerrors.Wrapf( types.ErrChannelNotFound, @@ -442,6 +440,21 @@ func (k Keeper) AcknowledgementExecuted( k.deletePacketCommitment(ctx, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + // increment NextSequenceAck + if channel.Ordering == types.ORDERED { + nextSequenceAck, found := k.GetNextSequenceAck(ctx, packet.GetDestPort(), packet.GetDestChannel()) + if !found { + return sdkerrors.Wrapf( + types.ErrSequenceAckNotFound, + "destination port: %s, destination channel: %s", packet.GetDestPort(), packet.GetDestChannel(), + ) + } + + nextSequenceAck++ + + k.SetNextSequenceAck(ctx, packet.GetDestPort(), packet.GetDestChannel(), nextSequenceAck) + } + // log that a packet has been acknowledged k.Logger(ctx).Info(fmt.Sprintf("packet acknowledged: %v", packet))