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

ICS4: Write ErrorReceipt for previous upgrade on Reinitialization #5732

Merged
merged 7 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
6 changes: 6 additions & 0 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (k Keeper) WriteUpgradeInitChannel(ctx sdk.Context, portID, channelID strin
panic(fmt.Errorf("could not find existing channel when updating channel state in successful ChanUpgradeInit step, channelID: %s, portID: %s", channelID, portID))
}

_, priorUpgradeExists := k.GetUpgrade(ctx, portID, channelID)
if priorUpgradeExists {
// invalidating previous upgrade
k.WriteErrorReceipt(ctx, portID, channelID, types.NewUpgradeError(channel.UpgradeSequence, types.ErrInvalidUpgrade))
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can add HasUpgrade to keeper to save on minimal gas

if k.HasUpgrade(ctx, portId, channelID) {
    k.WriteErrorReceipt(ctx, portID, channelID, upgradeErr)
}


channel.UpgradeSequence++

upgrade.Fields.Version = upgradeVersion
Expand Down
78 changes: 78 additions & 0 deletions modules/core/04-channel/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,14 @@ func (suite *KeeperTestSuite) TestChanUpgradeAck() {
},
nil,
},
{
"failure if initializing chain reinitializes before ACK",
func() {
err := path.EndpointA.ChanUpgradeInit()
suite.Require().NoError(err)
},
commitmenttypes.ErrInvalidProof, // sequences are out of sync
},
{
"channel not found",
func() {
Expand Down Expand Up @@ -864,6 +872,76 @@ func (suite *KeeperTestSuite) TestWriteChannelUpgradeAck() {
}
}

func (suite *KeeperTestSuite) TestChanUpgrade_ReinitializedBeforeAck() {
var path *ibctesting.Path
suite.Run("setup path", func() {
path = ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)

path.EndpointA.ChannelConfig.ProposedUpgrade.Fields.Version = mock.UpgradeVersion
path.EndpointB.ChannelConfig.ProposedUpgrade.Fields.Version = mock.UpgradeVersion
})

suite.Run("chainA upgrade init", func() {
err := path.EndpointA.ChanUpgradeInit()
suite.Require().NoError(err)

channel := path.EndpointA.GetChannel()
suite.Require().Equal(uint64(1), channel.UpgradeSequence)
})

suite.Run("chainB upgrade try", func() {
err := path.EndpointB.ChanUpgradeTry()
suite.Require().NoError(err)
})

suite.Run("chainA upgrade init reinitialized after ack", func() {
err := path.EndpointA.ChanUpgradeInit()
suite.Require().NoError(err)

channel := path.EndpointA.GetChannel()
suite.Require().Equal(uint64(2), channel.UpgradeSequence)
})

suite.Run("chan upgrade ack fails", func() {
err := path.EndpointA.ChanUpgradeAck()
suite.Require().Error(err)
})

suite.Run("chainB upgrade cancel", func() {
err := path.EndpointB.ChanUpgradeCancel()
suite.Require().NoError(err)
})

suite.Run("upgrade handshake succeeds on new upgrade attempt", func() {
err := path.EndpointB.ChanUpgradeTry()
suite.Require().NoError(err)

err = path.EndpointA.ChanUpgradeAck()
suite.Require().NoError(err)

err = path.EndpointB.ChanUpgradeConfirm()
suite.Require().NoError(err)

err = path.EndpointA.ChanUpgradeOpen()
suite.Require().NoError(err)
})

suite.Run("assert successful upgrade expected channel state", func() {
channelA := path.EndpointA.GetChannel()
suite.Require().Equal(types.OPEN, channelA.State, "channel should be in OPEN state")
suite.Require().Equal(mock.UpgradeVersion, channelA.Version, "version should be correctly upgraded")
suite.Require().Equal(mock.UpgradeVersion, path.EndpointB.GetChannel().Version, "version should be correctly upgraded")
suite.Require().Equal(uint64(2), channelA.UpgradeSequence, "upgrade sequence should be incremented")

channelB := path.EndpointB.GetChannel()
suite.Require().Equal(types.OPEN, channelB.State, "channel should be in OPEN state")
suite.Require().Equal(mock.UpgradeVersion, channelB.Version, "version should be correctly upgraded")
suite.Require().Equal(mock.UpgradeVersion, channelB.Version, "version should be correctly upgraded")
suite.Require().Equal(uint64(2), channelB.UpgradeSequence, "upgrade sequence should be incremented")
})
}

func (suite *KeeperTestSuite) TestChanUpgradeConfirm() {
var (
path *ibctesting.Path
Expand Down
Loading