From dc99ee43c1ade80094d56acd00ca16097b308d44 Mon Sep 17 00:00:00 2001 From: Charly Date: Mon, 8 Jan 2024 12:58:58 -0300 Subject: [PATCH] test: add unit tests for channel upgrade event emission (#5475) --- modules/core/04-channel/keeper/upgrade.go | 6 - .../core/04-channel/keeper/upgrade_test.go | 146 +--------- modules/core/keeper/msg_server.go | 22 +- modules/core/keeper/msg_server_test.go | 270 +++++++++++++++++- 4 files changed, 280 insertions(+), 164 deletions(-) diff --git a/modules/core/04-channel/keeper/upgrade.go b/modules/core/04-channel/keeper/upgrade.go index beb93b76480..767df52d5b1 100644 --- a/modules/core/04-channel/keeper/upgrade.go +++ b/modules/core/04-channel/keeper/upgrade.go @@ -669,18 +669,12 @@ func (k Keeper) WriteUpgradeCancelChannel(ctx sdk.Context, portID, channelID str panic(fmt.Errorf("could not find existing channel when updating channel state, channelID: %s, portID: %s", channelID, portID)) } - upgrade, found := k.GetUpgrade(ctx, portID, channelID) - if !found { - panic(fmt.Errorf("could not find upgrade when updating channel state, channelID: %s, portID: %s", channelID, portID)) - } - previousState := channel.State channel = k.restoreChannel(ctx, portID, channelID, sequence, channel) k.WriteErrorReceipt(ctx, portID, channelID, types.NewUpgradeError(sequence, types.ErrInvalidUpgrade)) k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousState, "new-state", types.OPEN.String()) - EmitChannelUpgradeCancelEvent(ctx, portID, channelID, channel, upgrade) } // ChanUpgradeTimeout times out an outstanding upgrade. diff --git a/modules/core/04-channel/keeper/upgrade_test.go b/modules/core/04-channel/keeper/upgrade_test.go index 463d2cb569d..d270d93e493 100644 --- a/modules/core/04-channel/keeper/upgrade_test.go +++ b/modules/core/04-channel/keeper/upgrade_test.go @@ -110,28 +110,10 @@ func (suite *KeeperTestSuite) TestChanUpgradeInit() { suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.WriteUpgradeInitChannel(ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, upgrade, upgrade.Fields.Version) channel := path.EndpointA.GetChannel() - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // types.EventTypeChannelUpgradeInit: { - // types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, - // types.AttributeKeyChannelID: path.EndpointA.ChannelID, - // types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, - // types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, - // types.AttributeKeyUpgradeConnectionHops: upgradeFields.ConnectionHops[0], - // types.AttributeKeyUpgradeVersion: upgradeFields.Version, - // types.AttributeKeyUpgradeOrdering: upgradeFields.Ordering.String(), - // types.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), - // }, - // sdk.EventTypeMessage: { - // sdk.AttributeKeyModule: types.AttributeValueCategory, - // }, - // } - suite.Require().NoError(err) suite.Require().Equal(expSequence, channel.UpgradeSequence) suite.Require().Equal(mock.Version, channel.Version) suite.Require().Equal(types.OPEN, channel.State) - } else { suite.Require().Error(err) } @@ -627,23 +609,6 @@ func (suite *KeeperTestSuite) TestWriteUpgradeTry() { upgrade, found := suite.chainB.GetSimApp().IBCKeeper.ChannelKeeper.GetUpgrade(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) suite.Require().True(found) suite.Require().Equal(upgradeWithAppCallbackVersion, upgrade) - - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // types.EventTypeChannelUpgradeTry: { - // types.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, - // types.AttributeKeyChannelID: path.EndpointB.ChannelID, - // types.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, - // types.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, - // types.AttributeKeyUpgradeConnectionHops: upgrade.Fields.ConnectionHops[0], - // types.AttributeKeyUpgradeVersion: upgrade.Fields.Version, - // types.AttributeKeyUpgradeOrdering: upgrade.Fields.Ordering.String(), - // types.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), - // }, - // sdk.EventTypeMessage: { - // sdk.AttributeKeyModule: types.AttributeValueCategory, - // }, - // } }) } } @@ -889,23 +854,6 @@ func (suite *KeeperTestSuite) TestWriteChannelUpgradeAck() { upgrade := path.EndpointA.GetChannelUpgrade() suite.Require().Equal(mock.UpgradeVersion, upgrade.Fields.Version) - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // types.EventTypeChannelUpgradeAck: { - // types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, - // types.AttributeKeyChannelID: path.EndpointA.ChannelID, - // types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, - // types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, - // types.AttributeKeyUpgradeConnectionHops: upgrade.Fields.ConnectionHops[0], - // types.AttributeKeyUpgradeVersion: upgrade.Fields.Version, - // types.AttributeKeyUpgradeOrdering: upgrade.Fields.Ordering.String(), - // types.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), - // }, - // sdk.EventTypeMessage: { - // sdk.AttributeKeyModule: types.AttributeValueCategory, - // }, - // } - if !tc.hasPacketCommitments { suite.Require().Equal(types.FLUSHCOMPLETE, channel.State) } @@ -1178,21 +1126,6 @@ func (suite *KeeperTestSuite) TestWriteUpgradeConfirm() { upgrade := path.EndpointA.GetChannelUpgrade() suite.Require().Equal(mock.UpgradeVersion, upgrade.Fields.Version) - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // types.EventTypeChannelUpgradeConfirm: { - // types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, - // types.AttributeKeyChannelID: path.EndpointA.ChannelID, - // types.AttributeKeyChannelState: channel.State.String(), - // types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, - // types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, - // types.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), - // }, - // sdk.EventTypeMessage: { - // sdk.AttributeKeyModule: types.AttributeValueCategory, - // }, - // } - if !tc.hasPacketCommitments { suite.Require().Equal(types.FLUSHCOMPLETE, channel.State) } else { @@ -1432,25 +1365,6 @@ func (suite *KeeperTestSuite) TestWriteUpgradeOpenChannel() { counterpartyUpgrade, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetCounterpartyUpgrade(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().Equal(types.Upgrade{}, counterpartyUpgrade) suite.Require().False(found) - - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // types.EventTypeChannelUpgradeOpen: { - // types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, - // types.AttributeKeyChannelID: path.EndpointA.ChannelID, - // types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, - // types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, - // types.AttributeKeyChannelState: types.OPEN.String(), - // types.AttributeKeyUpgradeConnectionHops: channel.ConnectionHops[0], - // types.AttributeKeyUpgradeVersion: channel.Version, - // types.AttributeKeyUpgradeOrdering: channel.Ordering.String(), - // types.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), - // }, - // sdk.EventTypeMessage: { - // sdk.AttributeKeyModule: types.AttributeValueCategory, - // }, - // } - // ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) } }) } @@ -1514,6 +1428,7 @@ func (suite *KeeperTestSuite) TestWriteUpgradeOpenChannel_Ordering() { suite.Require().Equal(uint64(1), seq) // Assert that pruning sequence start has been initialized (set to 1) + suite.Require().True(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.HasPruningSequenceStart(ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) pruningSeq, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetPruningSequenceStart(ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().True(found) suite.Require().Equal(uint64(1), pruningSeq) @@ -1574,6 +1489,7 @@ func (suite *KeeperTestSuite) TestWriteUpgradeOpenChannel_Ordering() { suite.Require().Equal(uint64(2), seq) // Assert that pruning sequence start has been initialized (set to 1) + suite.Require().True(suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.HasPruningSequenceStart(ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) pruningSeq, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetPruningSequenceStart(ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().True(found) suite.Require().Equal(uint64(1), pruningSeq) @@ -1640,25 +1556,6 @@ func (suite *KeeperTestSuite) TestWriteUpgradeOpenChannel_Ordering() { counterpartyUpgrade, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetCounterpartyUpgrade(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().Equal(types.Upgrade{}, counterpartyUpgrade) suite.Require().False(found) - - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // types.EventTypeChannelUpgradeOpen: { - // types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, - // types.AttributeKeyChannelID: path.EndpointA.ChannelID, - // types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, - // types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, - // types.AttributeKeyChannelState: types.OPEN.String(), - // types.AttributeKeyUpgradeConnectionHops: channel.ConnectionHops[0], - // types.AttributeKeyUpgradeVersion: channel.Version, - // types.AttributeKeyUpgradeOrdering: channel.Ordering.String(), - // types.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), - // }, - // sdk.EventTypeMessage: { - // sdk.AttributeKeyModule: types.AttributeValueCategory, - // }, - // } - // ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }) } } @@ -1945,13 +1842,6 @@ func (suite *KeeperTestSuite) TestWriteUpgradeCancelChannel() { }, expPanic: true, }, - { - name: "upgrade not found", - malleate: func() { - path.EndpointA.Chain.DeleteKey(host.ChannelUpgradeKey(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) - }, - expPanic: true, - }, } for _, tc := range testCases { @@ -2012,26 +1902,6 @@ func (suite *KeeperTestSuite) TestWriteUpgradeCancelChannel() { counterpartyUpgrade, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.GetCounterpartyUpgrade(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().Equal(types.Upgrade{}, counterpartyUpgrade) suite.Require().False(found) - - // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. - // proposedUpgrade := path.EndpointA.GetProposedUpgrade() - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // types.EventTypeChannelUpgradeCancel: { - // types.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, - // types.AttributeKeyChannelID: path.EndpointA.ChannelID, - // types.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, - // types.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, - // types.AttributeKeyUpgradeConnectionHops: proposedUpgrade.Fields.ConnectionHops[0], - // types.AttributeKeyUpgradeVersion: proposedUpgrade.Fields.Version, - // types.AttributeKeyUpgradeOrdering: proposedUpgrade.Fields.Ordering.String(), - // types.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), - // }, - // sdk.EventTypeMessage: { - // sdk.AttributeKeyModule: types.AttributeValueCategory, - // }, - // } - } }) } @@ -2541,18 +2411,6 @@ func (suite *KeeperTestSuite) TestAbortUpgrade() { channelKeeper.MustAbortUpgrade(ctx, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, upgradeError) }) - // events := ctx.EventManager().Events().ToABCIEvents() - // expEvents := ibctesting.EventsMap{ - // "channel_upgrade_error": { - // "port_id": path.EndpointA.ChannelConfig.PortID, - // "channel_id": path.EndpointA.ChannelID, - // "counterparty_port_id": path.EndpointB.ChannelConfig.PortID, - // "counterparty_channel_id": path.EndpointB.ChannelID, - // "upgrade_sequence": fmt.Sprintf("%d", path.EndpointA.GetChannel().UpgradeSequence), - // "upgrade_error_receipt": upgradeError.Error(), - // }, - // } - channel, found := channelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().True(found, "channel should be found") diff --git a/modules/core/keeper/msg_server.go b/modules/core/keeper/msg_server.go index ee3bfe30f5f..ed2adada3e2 100644 --- a/modules/core/keeper/msg_server.go +++ b/modules/core/keeper/msg_server.go @@ -952,7 +952,7 @@ func (k Keeper) ChannelUpgradeConfirm(goCtx context.Context, msg *channeltypes.M return nil, errorsmod.Wrapf(channeltypes.ErrUpgradeNotFound, "failed to retrieve channel upgrade: port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) } - k.ChannelKeeper.WriteUpgradeOpenChannel(ctx, msg.PortId, msg.ChannelId) + channel := k.ChannelKeeper.WriteUpgradeOpenChannel(ctx, msg.PortId, msg.ChannelId) cbs.OnChanUpgradeOpen(ctx, msg.PortId, msg.ChannelId, upgrade.Fields.Ordering, upgrade.Fields.ConnectionHops, upgrade.Fields.Version) ctx.Logger().Info("channel upgrade open succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) @@ -1070,12 +1070,19 @@ func (k Keeper) ChannelUpgradeCancel(goCtx context.Context, msg *channeltypes.Ms // then we can restore immediately without any additional checks isAuthority := k.GetAuthority() == msg.Signer if isAuthority && channel.State != channeltypes.FLUSHCOMPLETE { + upgrade, found := k.ChannelKeeper.GetUpgrade(ctx, msg.PortId, msg.ChannelId) + if !found { + return nil, errorsmod.Wrapf(channeltypes.ErrUpgradeNotFound, "failed to retrieve channel upgrade: port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) + } + k.ChannelKeeper.WriteUpgradeCancelChannel(ctx, msg.PortId, msg.ChannelId, channel.UpgradeSequence) cbs.OnChanUpgradeRestore(ctx, msg.PortId, msg.ChannelId) ctx.Logger().Info("channel upgrade cancel succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + keeper.EmitChannelUpgradeCancelEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + return &channeltypes.MsgChannelUpgradeCancelResponse{}, nil } @@ -1084,12 +1091,25 @@ func (k Keeper) ChannelUpgradeCancel(goCtx context.Context, msg *channeltypes.Ms return nil, errorsmod.Wrap(err, "channel upgrade cancel failed") } + // get upgrade here since it will be deleted in WriteUpgradeCancelChannel + upgrade, found := k.ChannelKeeper.GetUpgrade(ctx, msg.PortId, msg.ChannelId) + if !found { + return nil, errorsmod.Wrapf(channeltypes.ErrUpgradeNotFound, "failed to retrieve channel upgrade: port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) + } + k.ChannelKeeper.WriteUpgradeCancelChannel(ctx, msg.PortId, msg.ChannelId, msg.ErrorReceipt.Sequence) cbs.OnChanUpgradeRestore(ctx, msg.PortId, msg.ChannelId) ctx.Logger().Info("channel upgrade cancel succeeded", "port-id", msg.PortId, "channel-id", msg.ChannelId) + // get channel here again to get latest state after write + channel, found = k.ChannelKeeper.GetChannel(ctx, msg.PortId, msg.ChannelId) + if !found { + return nil, errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", msg.PortId, msg.ChannelId) + } + keeper.EmitChannelUpgradeCancelEvent(ctx, msg.PortId, msg.ChannelId, channel, upgrade) + return &channeltypes.MsgChannelUpgradeCancelResponse{}, nil } diff --git a/modules/core/keeper/msg_server_test.go b/modules/core/keeper/msg_server_test.go index a737bb2ddcd..6f50ed67f60 100644 --- a/modules/core/keeper/msg_server_test.go +++ b/modules/core/keeper/msg_server_test.go @@ -871,6 +871,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { name string malleate func() expResult func(res *channeltypes.MsgChannelUpgradeInitResponse, err error) + expEvents bool }{ { "success", @@ -887,6 +888,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { suite.Require().NotNil(res) suite.Require().Equal(uint64(1), res.UpgradeSequence) }, + true, }, { "authority is not signer of the upgrade init msg", @@ -903,6 +905,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { suite.Require().ErrorContains(err, ibcerrors.ErrUnauthorized.Error()) suite.Require().Nil(res) }, + false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -924,6 +927,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) }, + false, }, } @@ -941,9 +945,32 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { tc.malleate() - res, err := keeper.Keeper.ChannelUpgradeInit(*suite.chainA.App.GetIBCKeeper(), suite.chainA.GetContext(), msg) - + ctx := suite.chainA.GetContext() + res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeInit(ctx, msg) tc.expResult(res, err) + + if tc.expEvents { + upgrade := path.EndpointA.GetChannelUpgrade() + channel := path.EndpointA.GetChannel() + events := ctx.EventManager().Events().ToABCIEvents() + + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeInit: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeConnectionHops: upgrade.Fields.ConnectionHops[0], + channeltypes.AttributeKeyUpgradeVersion: upgrade.Fields.Version, + channeltypes.AttributeKeyUpgradeOrdering: upgrade.Fields.Ordering.String(), + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) + } }) } } @@ -958,6 +985,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { name string malleate func() expResult func(res *channeltypes.MsgChannelUpgradeTryResponse, err error) + expEvents bool }{ { "success", @@ -971,6 +999,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { suite.Require().Equal(channeltypes.FLUSHING, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "module capability not found", @@ -984,6 +1013,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) }, + false, }, { "unsynchronized upgrade sequence writes upgrade error receipt", @@ -1003,6 +1033,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { suite.Require().True(found) suite.Require().Equal(uint64(99), errorReceipt.Sequence) }, + false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1020,6 +1051,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) }, + false, }, } @@ -1061,9 +1093,32 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { tc.malleate() - res, err := suite.chainB.GetSimApp().GetIBCKeeper().ChannelUpgradeTry(suite.chainB.GetContext(), msg) - + ctx := suite.chainB.GetContext() + res, err := suite.chainB.GetSimApp().GetIBCKeeper().ChannelUpgradeTry(ctx, msg) tc.expResult(res, err) + + if tc.expEvents { + upgrade := path.EndpointB.GetChannelUpgrade() + channel := path.EndpointB.GetChannel() + events := ctx.EventManager().Events().ToABCIEvents() + + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeTry: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeConnectionHops: upgrade.Fields.ConnectionHops[0], + channeltypes.AttributeKeyUpgradeVersion: upgrade.Fields.Version, + channeltypes.AttributeKeyUpgradeOrdering: upgrade.Fields.Ordering.String(), + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) + } }) } } @@ -1078,6 +1133,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { name string malleate func() expResult func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) + expEvents bool }{ { "success, no pending in-flight packets", @@ -1091,6 +1147,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { suite.Require().Equal(channeltypes.FLUSHCOMPLETE, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "success, pending in-flight packets", @@ -1109,6 +1166,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { suite.Require().Equal(channeltypes.FLUSHING, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "module capability not found", @@ -1122,6 +1180,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) }, + false, }, { "core handler returns error and no upgrade error receipt is written", @@ -1141,6 +1200,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { suite.Require().Empty(errorReceipt) suite.Require().False(found) }, + false, }, { "core handler returns error and writes upgrade error receipt", @@ -1161,6 +1221,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { suite.Require().True(found) suite.Require().Equal(uint64(1), errorReceipt.Sequence) }, + false, }, { "application callback returns error and error receipt is written", @@ -1188,6 +1249,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(exported.ModuleName)) suite.Require().False(store.Has([]byte("foo"))) }, + false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1205,6 +1267,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) }, + false, }, } @@ -1245,9 +1308,32 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { tc.malleate() - res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeAck(suite.chainA.GetContext(), msg) - + ctx := suite.chainA.GetContext() + res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeAck(ctx, msg) tc.expResult(res, err) + + if tc.expEvents { + upgrade := path.EndpointA.GetChannelUpgrade() + channel := path.EndpointA.GetChannel() + events := ctx.EventManager().Events().ToABCIEvents() + + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeAck: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeConnectionHops: upgrade.Fields.ConnectionHops[0], + channeltypes.AttributeKeyUpgradeVersion: upgrade.Fields.Version, + channeltypes.AttributeKeyUpgradeOrdering: upgrade.Fields.Ordering.String(), + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) + } }) } } @@ -1262,6 +1348,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { name string malleate func() expResult func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) + expEvents bool }{ { "success, no pending in-flight packets", @@ -1275,6 +1362,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { suite.Require().Equal(channeltypes.OPEN, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "success, pending in-flight packets on init chain", @@ -1330,6 +1418,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { suite.Require().Equal(channeltypes.FLUSHCOMPLETE, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "success, pending in-flight packets on try chain", @@ -1346,6 +1435,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { suite.Require().Equal(channeltypes.FLUSHING, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "module capability not found", @@ -1359,6 +1449,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) }, + false, }, { "core handler returns error and no upgrade error receipt is written", @@ -1378,6 +1469,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { suite.Require().Empty(errorReceipt) suite.Require().False(found) }, + false, }, { "core handler returns error and writes upgrade error receipt", @@ -1410,6 +1502,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { suite.Require().True(found) suite.Require().Equal(uint64(1), errorReceipt.Sequence) }, + false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1427,6 +1520,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) }, + false, }, } @@ -1472,9 +1566,67 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { tc.malleate() - res, err := suite.chainB.GetSimApp().GetIBCKeeper().ChannelUpgradeConfirm(suite.chainB.GetContext(), msg) - + ctx := suite.chainB.GetContext() + res, err := suite.chainB.GetSimApp().GetIBCKeeper().ChannelUpgradeConfirm(ctx, msg) tc.expResult(res, err) + + if tc.expEvents { + channel := path.EndpointB.GetChannel() + events := ctx.EventManager().Events().ToABCIEvents() + var expEvents ibctesting.EventsMap + + switch state := channel.State; state { + case channeltypes.OPEN: + expEvents = ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeConfirm: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyChannelState: channeltypes.FLUSHCOMPLETE.String(), + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + channeltypes.EventTypeChannelUpgradeOpen: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyChannelState: channeltypes.OPEN.String(), + channeltypes.AttributeKeyUpgradeConnectionHops: channel.ConnectionHops[0], + channeltypes.AttributeKeyUpgradeVersion: channel.Version, + channeltypes.AttributeKeyUpgradeOrdering: channel.Ordering.String(), + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + case channeltypes.FLUSHCOMPLETE: + expEvents = ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeConfirm: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyChannelState: channeltypes.FLUSHCOMPLETE.String(), + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + } + case channeltypes.FLUSHING: + expEvents = ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeConfirm: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyChannelState: channeltypes.FLUSHING.String(), + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + } + } + + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) + } }) } } @@ -1489,6 +1641,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { name string malleate func() expResult func(res *channeltypes.MsgChannelUpgradeOpenResponse, err error) + expEvents bool }{ { "success", @@ -1500,6 +1653,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { channel := path.EndpointA.GetChannel() suite.Require().Equal(channeltypes.OPEN, channel.State) }, + true, }, { "module capability not found", @@ -1513,6 +1667,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) }, + false, }, { "core handler fails", @@ -1527,6 +1682,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { suite.Require().ErrorIs(err, channeltypes.ErrInvalidChannelState) }, + false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1544,6 +1700,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) }, + false, }, } @@ -1589,9 +1746,31 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { tc.malleate() - res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeOpen(suite.chainA.GetContext(), msg) - + ctx := suite.chainA.GetContext() + res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeOpen(ctx, msg) tc.expResult(res, err) + + if tc.expEvents { + channel := path.EndpointA.GetChannel() + events := ctx.EventManager().Events().ToABCIEvents() + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeOpen: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyChannelState: channeltypes.OPEN.String(), + channeltypes.AttributeKeyUpgradeConnectionHops: channel.ConnectionHops[0], + channeltypes.AttributeKeyUpgradeVersion: channel.Version, + channeltypes.AttributeKeyUpgradeOrdering: channel.Ordering.String(), + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) + } }) } } @@ -1606,6 +1785,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { name string malleate func() expResult func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) + expEvents bool }{ { "success: keeper is not authority, valid error receipt so channnel changed to match error receipt seq", @@ -1620,6 +1800,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Upgrade sequence should be changed to match sequence on error receipt. suite.Require().Equal(uint64(2), channel.UpgradeSequence) }, + true, }, { "success: keeper is authority & channel state in FLUSHING, so error receipt is ignored and channel is restored to initial upgrade sequence", @@ -1641,6 +1822,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Upgrade sequence should be changed to match initial upgrade sequence. suite.Require().Equal(uint64(3), channel.UpgradeSequence) }, + true, }, { "success: keeper is authority & channel state in FLUSHING, can be cancelled even with invalid error receipt", @@ -1663,6 +1845,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Upgrade sequence should be changed to match initial upgrade sequence. suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "success: keeper is authority & channel state in FLUSHING, can be cancelled even with empty error receipt", @@ -1685,6 +1868,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Upgrade sequence should be changed to match initial upgrade sequence. suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + true, }, { "success: keeper is authority but channel state in FLUSHCOMPLETE, requires valid error receipt", @@ -1706,6 +1890,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Upgrade sequence should be changed to match error receipt sequence. suite.Require().Equal(uint64(2), channel.UpgradeSequence) }, + true, }, { "capability not found", @@ -1722,6 +1907,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Upgrade sequence should not be changed. suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + false, }, { "core handler fails: invalid proof", @@ -1741,6 +1927,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Upgrade sequence should not be changed. suite.Require().Equal(uint64(1), channel.UpgradeSequence) }, + false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1758,6 +1945,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) }, + false, }, } for _, tc := range cases { @@ -1810,8 +1998,33 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { tc.malleate() - res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeCancel(suite.chainA.GetContext(), msg) + ctx := suite.chainA.GetContext() + res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeCancel(ctx, msg) tc.expResult(res, err) + + if tc.expEvents { + // get latest channel state + channel = path.EndpointA.GetChannel() + // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. + proposedUpgrade := path.EndpointA.GetProposedUpgrade() + events := ctx.EventManager().Events().ToABCIEvents() + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeCancel: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeConnectionHops: proposedUpgrade.Fields.ConnectionHops[0], + channeltypes.AttributeKeyUpgradeVersion: proposedUpgrade.Fields.Version, + channeltypes.AttributeKeyUpgradeOrdering: proposedUpgrade.Fields.Ordering.String(), + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) + } }) } } @@ -1833,6 +2046,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { name string malleate func() expResult func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, err error) + expEvents bool }{ { "success", @@ -1848,7 +2062,6 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { msg.ProofHeight = proofHeight }, func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, err error) { - suite.Require().NoError(err) channel := path.EndpointA.GetChannel() suite.Require().Equalf(channeltypes.OPEN, channel.State, "channel state should be %s", channeltypes.OPEN.String()) @@ -1857,7 +2070,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { suite.Require().False(found, "channel upgrade should be nil") suite.Require().NotNil(res) + suite.Require().NoError(err) }, + true, }, { "capability not found", @@ -1875,6 +2090,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { _, found := path.EndpointA.Chain.GetSimApp().IBCKeeper.ChannelKeeper.GetUpgrade(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().True(found, "channel upgrade should not be nil") }, + false, }, { "core handler fails: invalid proof", @@ -1899,6 +2115,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { _, found := path.EndpointA.Chain.GetSimApp().IBCKeeper.ChannelKeeper.GetUpgrade(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().True(found, "channel upgrade should not be nil") }, + false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1916,6 +2133,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) }, + false, }, } @@ -1950,8 +2168,34 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeTimeout(ctx, msg) - tc.expResult(res, err) + + if tc.expEvents { + channel := path.EndpointA.GetChannel() + // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. + proposedUpgrade := path.EndpointA.GetProposedUpgrade() + // use the timeout we set in the malleate function + timeout := channeltypes.NewTimeout(clienttypes.ZeroHeight(), 1) + events := ctx.EventManager().Events().ToABCIEvents() + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeTimeout: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeConnectionHops: proposedUpgrade.Fields.ConnectionHops[0], + channeltypes.AttributeKeyUpgradeVersion: proposedUpgrade.Fields.Version, + channeltypes.AttributeKeyUpgradeOrdering: proposedUpgrade.Fields.Ordering.String(), + channeltypes.AttributeKeyUpgradeTimeout: timeout.String(), + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + }, + + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) + } }) } }