Skip to content

Commit

Permalink
Set Counterparty Last Sequence Send (#4005)
Browse files Browse the repository at this point in the history
  • Loading branch information
chatton authored Jul 5, 2023
1 parent 25fcad4 commit 5767ad7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
10 changes: 4 additions & 6 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (k Keeper) ChanUpgradeTry(

// WriteUpgradeTryChannel writes the channel end and upgrade to state after successfully passing the UpgradeTry handshake step.
// An event is emitted for the handshake step.
func (k Keeper) WriteUpgradeTryChannel(ctx sdk.Context, portID, channelID string, upgrade types.Upgrade, upgradeVersion string) (types.Channel, types.Upgrade) {
func (k Keeper) WriteUpgradeTryChannel(ctx sdk.Context, portID, channelID string, upgrade types.Upgrade, upgradeVersion string, counterpartyLastSequenceSend uint64) (types.Channel, types.Upgrade) {
defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-try")

channel, found := k.GetChannel(ctx, portID, channelID)
Expand All @@ -199,6 +199,7 @@ func (k Keeper) WriteUpgradeTryChannel(ctx sdk.Context, portID, channelID string

upgrade.Fields.Version = upgradeVersion

k.SetCounterpartyLastPacketSequence(ctx, portID, channelID, counterpartyLastSequenceSend)
k.SetChannel(ctx, portID, channelID, channel)
k.SetUpgrade(ctx, portID, channelID, upgrade)

Expand Down Expand Up @@ -278,11 +279,7 @@ func (k Keeper) ChanUpgradeAck(
// WriteUpgradeAckChannel writes a channel which has successfully passed the UpgradeAck handshake step as well as
// setting the upgrade for that channel.
// An event is emitted for the handshake step.
func (k Keeper) WriteUpgradeAckChannel(
ctx sdk.Context,
portID, channelID string,
upgradeVersion string,
) {
func (k Keeper) WriteUpgradeAckChannel(ctx sdk.Context, portID, channelID, upgradeVersion string, counterpartyLastSequenceSend uint64) {
defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-ack")

channel, found := k.GetChannel(ctx, portID, channelID)
Expand All @@ -298,6 +295,7 @@ func (k Keeper) WriteUpgradeAckChannel(
channel.FlushStatus = types.FLUSHCOMPLETE
}

k.SetCounterpartyLastPacketSequence(ctx, portID, channelID, counterpartyLastSequenceSend)
k.SetChannel(ctx, portID, channelID, channel)

upgrade, found := k.GetUpgrade(ctx, portID, channelID)
Expand Down
19 changes: 17 additions & 2 deletions modules/core/04-channel/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,14 @@ func (suite *KeeperTestSuite) TestWriteUpgradeTry() {

tc.malleate()

upgradedChannelEnd, upgradeWithAppCallbackVersion := suite.chainB.GetSimApp().IBCKeeper.ChannelKeeper.WriteUpgradeTryChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, proposedUpgrade, proposedUpgrade.Fields.Version)
upgradedChannelEnd, upgradeWithAppCallbackVersion := suite.chainB.GetSimApp().IBCKeeper.ChannelKeeper.WriteUpgradeTryChannel(
suite.chainB.GetContext(),
path.EndpointB.ChannelConfig.PortID,
path.EndpointB.ChannelID,
proposedUpgrade,
proposedUpgrade.Fields.Version,
proposedUpgrade.LatestSequenceSend,
)

channel := path.EndpointB.GetChannel()
suite.Require().Equal(upgradedChannelEnd, channel)
Expand All @@ -377,6 +384,10 @@ func (suite *KeeperTestSuite) TestWriteUpgradeTry() {
suite.Require().Equal(types.TRYUPGRADE, channel.State)
suite.Require().Equal(upgradeWithAppCallbackVersion, upgrade)

actualCounterpartyLastSequenceSend, ok := suite.chainB.GetSimApp().IBCKeeper.ChannelKeeper.GetCounterpartyLastPacketSequence(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)
suite.Require().True(ok)
suite.Require().Equal(proposedUpgrade.LatestSequenceSend, actualCounterpartyLastSequenceSend)

if tc.hasPacketCommitments {
suite.Require().Equal(types.FLUSHING, channel.FlushStatus)
} else {
Expand Down Expand Up @@ -601,12 +612,16 @@ func (suite *KeeperTestSuite) TestWriteChannelUpgradeAck() {

suite.Require().NoError(path.EndpointB.ChanUpgradeTry())

suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.WriteUpgradeAckChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, mock.UpgradeVersion)
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.WriteUpgradeAckChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, mock.UpgradeVersion, proposedUpgrade.LatestSequenceSend)

channel := path.EndpointA.GetChannel()
upgrade := path.EndpointA.GetChannelUpgrade()
suite.Require().Equal(mock.UpgradeVersion, upgrade.Fields.Version)

actualCounterpartyLastSequenceSend, ok := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetCounterpartyLastPacketSequence(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
suite.Require().True(ok)
suite.Require().Equal(proposedUpgrade.LatestSequenceSend, actualCounterpartyLastSequenceSend)

if tc.hasPacketCommitments {
suite.Require().Equal(types.FLUSHING, channel.FlushStatus)
} else {
Expand Down
5 changes: 2 additions & 3 deletions modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,9 @@ func (k Keeper) ChannelUpgradeTry(goCtx context.Context, msg *channeltypes.MsgCh

return &channeltypes.MsgChannelUpgradeTryResponse{Result: channeltypes.FAILURE}, nil
}

writeFn()

channel, upgrade := k.ChannelKeeper.WriteUpgradeTryChannel(ctx, msg.PortId, msg.ChannelId, upgrade, upgradeVersion)
channel, upgrade := k.ChannelKeeper.WriteUpgradeTryChannel(ctx, msg.PortId, msg.ChannelId, upgrade, upgradeVersion, msg.CounterpartyProposedUpgrade.LatestSequenceSend)

ctx.Logger().Info("channel upgrade try succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId)

Expand Down Expand Up @@ -853,7 +852,7 @@ func (k Keeper) ChannelUpgradeAck(goCtx context.Context, msg *channeltypes.MsgCh

writeFn()

k.ChannelKeeper.WriteUpgradeAckChannel(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyUpgrade.Fields.Version)
k.ChannelKeeper.WriteUpgradeAckChannel(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyUpgrade.Fields.Version, msg.CounterpartyUpgrade.LatestSequenceSend)

ctx.Logger().Info("channel upgrade ack succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId)

Expand Down

0 comments on commit 5767ad7

Please sign in to comment.