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

check counterparty nextSequenceSend in recvPacket #5469

Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b7fbb39
add logic for setting next seq recv, update tests
charleenfei Dec 18, 2023
a3a10ab
update next seq recv and ack logic to spec
charleenfei Dec 19, 2023
045fdd2
Merge branch '04-channel-upgrades' into charly/set_nextseqrecv
charleenfei Dec 19, 2023
129cc62
typos
charleenfei Dec 19, 2023
c95192b
update test
charleenfei Dec 19, 2023
d7379a0
Merge branch 'charly/set_nextseqrecv' of github.com:cosmos/ibc-go int…
charleenfei Dec 19, 2023
972faa6
wip changes for spec
charleenfei Dec 19, 2023
3864e91
wip set counterparty upgrade
charleenfei Dec 19, 2023
122c25b
update naming
charleenfei Dec 19, 2023
81c31ee
update endpoint
charleenfei Dec 19, 2023
0ca4bca
spec changes
charleenfei Dec 19, 2023
a7a0834
test that counterparty upgrade has been set
charleenfei Dec 19, 2023
adaac86
Merge branch '04-channel-upgrades' into charly/set_nextseqrecv
charleenfei Dec 19, 2023
9a6f4c1
update err message
charleenfei Dec 19, 2023
ed80a45
pr review
charleenfei Dec 19, 2023
1dd6dcd
Merge branch 'charly/set_nextseqrecv' of github.com:cosmos/ibc-go int…
charleenfei Dec 19, 2023
7b2b487
naming nit
Dec 19, 2023
ac742e5
feat: check counterparty next sequence send in recv packet
Dec 19, 2023
61251a7
add comment
Dec 19, 2023
c0cd6ea
lint
Dec 19, 2023
73b255b
add error doc
charleenfei Dec 20, 2023
63dada9
only do defensive check if counterpartyUpgrade is set
AdityaSripal Dec 20, 2023
9e6f552
remove unnecessary 0 check
AdityaSripal Dec 20, 2023
3637bfe
Merge branch '04-channel-upgrades' of github.com:cosmos/ibc-go into c…
colin-axner Dec 20, 2023
5d66985
merge conflict fix: add back deleted test
colin-axner Dec 20, 2023
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
21 changes: 21 additions & 0 deletions modules/core/04-channel/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ func (k Keeper) RecvPacket(
return errorsmod.Wrapf(types.ErrInvalidChannelState, "expected channel state to be one of [%s, %s], but got %s", types.OPEN, types.FLUSHING, channel.State)
}

// In the case of the channel being in FLUSHING we need to ensure that the
// packet sequence is < counterparty next sequence send. This is a defensive
// check and if the counterparty is implemented correctly, this should never abort.
if channel.State == types.FLUSHING {
counterpartyUpgrade, found := k.GetCounterpartyUpgrade(ctx, packet.GetDestPort(), packet.GetDestChannel())
if !found {
return errorsmod.Wrapf(types.ErrUpgradeNotFound, "counterparty upgrade not found: portID (%s), channelID (%s)", packet.GetDestPort(), packet.GetDestChannel())
Copy link
Contributor

Choose a reason for hiding this comment

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

this block receives after try completes but before ack or confirm completes

Copy link
Member

Choose a reason for hiding this comment

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

hmm i would be in favor of just not performing this defensive check and allowing it through until we get the info stored. nice catch!

}

// only error if the counterparty next sequence send is set (> 0)
// this error should never be reached, as under normal circumstances the counterparty
// should not send any packets after the upgrade has been started.
counterpartyNextSequenceSend := counterpartyUpgrade.NextSequenceSend
if counterpartyNextSequenceSend != 0 && packet.GetSequence() >= counterpartyNextSequenceSend {
return errorsmod.Wrapf(
Copy link
Member

Choose a reason for hiding this comment

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

document that this is unexpected error and that counterparty has messed up if this error is reached

types.ErrInvalidPacket,
"failed to receive packet, cannot flush packet at sequence greater than or equal to counterparty next sequence send (%d) ≥ (%d)", packet.GetSequence(), counterpartyNextSequenceSend,
)
}
}

// Authenticate capability to ensure caller has authority to receive packet on this channel
capName := host.ChannelCapabilityPath(packet.GetDestPort(), packet.GetDestChannel())
if !k.scopedKeeper.AuthenticateCapability(ctx, chanCap, capName) {
Expand Down
47 changes: 42 additions & 5 deletions modules/core/04-channel/keeper/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,14 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)

// set last packet sent sequence to sequence + 1
counterpartyUpgrade := types.Upgrade{LatestSequenceSend: sequence + 1}
// set upgrade next sequence send to sequence + 1
counterpartyUpgrade := types.Upgrade{NextSequenceSend: sequence + 1}
path.EndpointB.SetChannelCounterpartyUpgrade(counterpartyUpgrade)
},
nil,
},
{
"success with an counterparty latest sequence send set to 0",
"success with an counterparty next sequence send set to 0",
func() {
suite.coordinator.Setup(path)
sequence, err := path.EndpointA.SendPacket(defaultTimeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
Expand All @@ -372,12 +372,49 @@ func (suite *KeeperTestSuite) TestRecvPacket() {
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)

// set last packet sent sequence to zero.
counterpartyUpgrade := types.Upgrade{LatestSequenceSend: 0}
// set upgrade next sequence send to zero.
counterpartyUpgrade := types.Upgrade{NextSequenceSend: 0}
path.EndpointB.SetChannelCounterpartyUpgrade(counterpartyUpgrade)
},
nil,
},
{
"failure while upgrading channel, counterparty upgrade not found",
func() {
suite.coordinator.Setup(path)
sequence, err := path.EndpointA.SendPacket(defaultTimeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)
},
types.ErrUpgradeNotFound,
},
{
"failure while upgrading channel, packet sequence ≥ counterparty next send sequence",
func() {
suite.coordinator.Setup(path)
// send 2 packets so that when NextSequenceSend is set to sequence - 1, it is not 0.
_, err := path.EndpointA.SendPacket(defaultTimeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
sequence, err := path.EndpointA.SendPacket(defaultTimeoutHeight, disabledTimeoutTimestamp, ibctesting.MockPacketData)
suite.Require().NoError(err)
packet = types.NewPacket(ibctesting.MockPacketData, sequence, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, disabledTimeoutTimestamp)
channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)

channel := path.EndpointB.GetChannel()
channel.State = types.FLUSHING
path.EndpointB.SetChannel(channel)

// set upgrade next sequence send to sequence - 1
counterpartyUpgrade := types.Upgrade{NextSequenceSend: sequence - 1}
path.EndpointB.SetChannelCounterpartyUpgrade(counterpartyUpgrade)
},
types.ErrInvalidPacket,
},
{
"failure while upgrading channel, channel in flush complete state",
func() {
Expand Down
34 changes: 24 additions & 10 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ func (k Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID string
if !k.HasInflightPackets(ctx, portID, channelID) {
channel.State = types.FLUSHCOMPLETE
k.SetChannel(ctx, portID, channelID, channel)
} else {
// the counterparty upgrade is only required if the channel is still in the FLUSHING state.
// this gets read when timing out and acknowledging packets.
k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)
}

upgrade, found := k.GetUpgrade(ctx, portID, channelID)
Expand All @@ -337,6 +333,7 @@ func (k Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID string
upgrade.Fields.Version = counterpartyUpgrade.Fields.Version

k.SetUpgrade(ctx, portID, channelID, upgrade)
k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)

k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "state", channel.State.String())
return channel, upgrade
Expand Down Expand Up @@ -432,13 +429,10 @@ func (k Keeper) WriteUpgradeConfirmChannel(ctx sdk.Context, portID, channelID st
previousState := channel.State
channel.State = types.FLUSHCOMPLETE
k.SetChannel(ctx, portID, channelID, channel)

k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", channel.State)
} else {
// the counterparty upgrade is only required if the channel is still in the FLUSHING state.
// this gets read when timing out and acknowledging packets.
k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)
}

k.SetCounterpartyUpgrade(ctx, portID, channelID, counterpartyUpgrade)
return channel
}

Expand Down Expand Up @@ -539,6 +533,26 @@ func (k Keeper) WriteUpgradeOpenChannel(ctx sdk.Context, portID, channelID strin
panic(fmt.Errorf("could not find upgrade when updating channel state, channelID: %s, portID: %s", channelID, portID))
}

counterpartyUpgrade, found := k.GetCounterpartyUpgrade(ctx, portID, channelID)
if !found {
panic(fmt.Errorf("could not find counterparty upgrade when updating channel state, channelID: %s, portID: %s", channelID, portID))
}

// next seq recv and ack is used for ordered channels to verify the packet has been received/acked in the correct order
// this is no longer necessary if the channel is UNORDERED and should be reset to 1
if channel.Ordering == types.ORDERED && upgrade.Fields.Ordering == types.UNORDERED {
k.SetNextSequenceRecv(ctx, portID, channelID, 1)
k.SetNextSequenceAck(ctx, portID, channelID, 1)
}

// next seq recv and ack should updated when moving from UNORDERED to ORDERED using the latest packet sequence sent before the upgrade
// we can be sure that the next packet we are set to receive will be the first packet the counterparty sends after reopening.
// we can be sure that our next acknowledgement will be our first packet sent after upgrade, as the counterparty processed all sent packets after flushing completes.
if channel.Ordering == types.UNORDERED && upgrade.Fields.Ordering == types.ORDERED {
k.SetNextSequenceRecv(ctx, portID, channelID, counterpartyUpgrade.NextSequenceSend)
k.SetNextSequenceAck(ctx, portID, channelID, upgrade.NextSequenceSend)
}

// Switch channel fields to upgrade fields and set channel state to OPEN
previousState := channel.State
channel.Ordering = upgrade.Fields.Ordering
Expand Down Expand Up @@ -774,7 +788,7 @@ func (k Keeper) startFlushing(ctx sdk.Context, portID, channelID string, upgrade
return errorsmod.Wrapf(types.ErrSequenceSendNotFound, "port ID (%s) channel ID (%s)", portID, channelID)
}

upgrade.LatestSequenceSend = nextSequenceSend - 1
upgrade.NextSequenceSend = nextSequenceSend
upgrade.Timeout = k.getAbsoluteUpgradeTimeout(ctx)
k.SetUpgrade(ctx, portID, channelID, *upgrade)

Expand Down
Loading
Loading