From 9ee5e8e90b47e7309f2779f1740da431b01339e0 Mon Sep 17 00:00:00 2001 From: Charly Date: Wed, 10 Jan 2024 10:32:58 -0300 Subject: [PATCH] test: event emission on writing error receipts for channel upgrades (#5549) (cherry picked from commit d487fb800e7bb4352f06802154f27f55e2e54599) --- modules/core/keeper/msg_server_test.go | 776 ++++++++++++++++--------- 1 file changed, 488 insertions(+), 288 deletions(-) diff --git a/modules/core/keeper/msg_server_test.go b/modules/core/keeper/msg_server_test.go index 043f8fcf67a..0f7bc88657e 100644 --- a/modules/core/keeper/msg_server_test.go +++ b/modules/core/keeper/msg_server_test.go @@ -9,6 +9,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + abci "github.com/cometbft/cometbft/abci/types" + capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types" @@ -870,8 +872,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { cases := []struct { name string malleate func() - expResult func(res *channeltypes.MsgChannelUpgradeInitResponse, err error) - expEvents bool + expResult func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error) }{ { "success", @@ -883,12 +884,31 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { path.EndpointA.Chain.GetSimApp().IBCKeeper.GetAuthority(), ) }, - func(res *channeltypes.MsgChannelUpgradeInitResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().Equal(uint64(1), res.UpgradeSequence) + + upgrade := path.EndpointA.GetChannelUpgrade() + channel := path.EndpointA.GetChannel() + + 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) }, - true, }, { "authority is not signer of the upgrade init msg", @@ -900,12 +920,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { path.EndpointA.Chain.SenderAccount.String(), ) }, - func(res *channeltypes.MsgChannelUpgradeInitResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().ErrorContains(err, ibcerrors.ErrUnauthorized.Error()) suite.Require().Nil(res) + + suite.Require().Empty(events) }, - false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -923,11 +944,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { path.EndpointA.Chain.GetSimApp().IBCKeeper.GetAuthority(), ) }, - func(res *channeltypes.MsgChannelUpgradeInitResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeInitResponse, events []abci.Event, err error) { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) + + suite.Require().Empty(events) }, - false, }, } @@ -947,30 +969,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeInit() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeInit(ctx, msg) - tc.expResult(res, err) + events := ctx.EventManager().Events().ToABCIEvents() - 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) - } + tc.expResult(res, events, err) }) } } @@ -984,13 +985,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { cases := []struct { name string malleate func() - expResult func(res *channeltypes.MsgChannelUpgradeTryResponse, err error) - expEvents bool + expResult func(res *channeltypes.MsgChannelUpgradeTryResponse, events []abci.Event, err error) }{ { "success", func() {}, - func(res *channeltypes.MsgChannelUpgradeTryResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTryResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().Equal(channeltypes.SUCCESS, res.Result) @@ -998,8 +998,26 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { channel := path.EndpointB.GetChannel() suite.Require().Equal(channeltypes.FLUSHING, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + upgrade := path.EndpointB.GetChannelUpgrade() + + 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) }, - true, }, { "module capability not found", @@ -1007,13 +1025,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { msg.PortId = "invalid-port" msg.ChannelId = "invalid-channel" }, - func(res *channeltypes.MsgChannelUpgradeTryResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTryResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) + suite.Require().Empty(events) }, - false, }, { "unsynchronized upgrade sequence writes upgrade error receipt", @@ -1023,7 +1041,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { path.EndpointB.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeTryResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTryResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1032,8 +1050,25 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { errorReceipt, found := suite.chainB.GetSimApp().GetIBCKeeper().ChannelKeeper.GetUpgradeErrorReceipt(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) suite.Require().True(found) suite.Require().Equal(uint64(99), errorReceipt.Sequence) + + channel := path.EndpointB.GetChannel() + + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "counterparty upgrade sequence < current upgrade sequence (1 < 99): invalid upgrade sequence", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1047,11 +1082,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { msg.PortId = path.EndpointB.ChannelConfig.PortID msg.ChannelId = path.EndpointB.ChannelID }, - func(res *channeltypes.MsgChannelUpgradeTryResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTryResponse, events []abci.Event, err error) { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) + + suite.Require().Empty(events) }, - false, }, } @@ -1095,30 +1131,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTry() { 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() + 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) - } + tc.expResult(res, events, err) }) } } @@ -1132,13 +1147,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { cases := []struct { name string malleate func() - expResult func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) - expEvents bool + expResult func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) }{ { "success, no pending in-flight packets", func() {}, - func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().Equal(channeltypes.SUCCESS, res.Result) @@ -1146,8 +1160,26 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { channel := path.EndpointA.GetChannel() suite.Require().Equal(channeltypes.FLUSHCOMPLETE, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + upgrade := path.EndpointA.GetChannelUpgrade() + + 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) }, - true, }, { "success, pending in-flight packets", @@ -1157,7 +1189,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { // Set a dummy packet commitment to simulate in-flight packets suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), portID, channelID, 1, []byte("hash")) }, - func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().Equal(channeltypes.SUCCESS, res.Result) @@ -1165,8 +1197,26 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { channel := path.EndpointA.GetChannel() suite.Require().Equal(channeltypes.FLUSHING, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + upgrade := path.EndpointA.GetChannelUpgrade() + + 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) }, - true, }, { "module capability not found", @@ -1174,13 +1224,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { msg.PortId = ibctesting.InvalidID msg.ChannelId = ibctesting.InvalidID }, - func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) + suite.Require().Empty(events) }, - false, }, { "core handler returns error and no upgrade error receipt is written", @@ -1191,7 +1241,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { path.EndpointA.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) suite.Require().ErrorIs(err, channeltypes.ErrInvalidChannelState) @@ -1199,8 +1249,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { errorReceipt, found := suite.chainA.GetSimApp().GetIBCKeeper().ChannelKeeper.GetUpgradeErrorReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().Empty(errorReceipt) suite.Require().False(found) + + suite.Require().Empty(events) }, - false, }, { "core handler returns error and writes upgrade error receipt", @@ -1211,7 +1262,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { path.EndpointA.SetChannelUpgrade(upgrade) }, - func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1220,8 +1271,25 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { errorReceipt, found := suite.chainA.GetSimApp().GetIBCKeeper().ChannelKeeper.GetUpgradeErrorReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Require().True(found) suite.Require().Equal(uint64(1), errorReceipt.Sequence) + + channel := path.EndpointB.GetChannel() + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "expected upgrade ordering (ORDER_NONE_UNSPECIFIED) to match counterparty upgrade ordering (ORDER_UNORDERED): incompatible counterparty upgrade", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - false, }, { "application callback returns error and error receipt is written", @@ -1235,7 +1303,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { return fmt.Errorf("mock app callback failed") } }, - func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1248,8 +1316,25 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { // assert application state changes are not committed store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(exported.ModuleName)) suite.Require().False(store.Has([]byte("foo"))) + + channel := path.EndpointB.GetChannel() + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "mock app callback failed", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1263,11 +1348,11 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { msg.PortId = path.EndpointB.ChannelConfig.PortID msg.ChannelId = path.EndpointB.ChannelID }, - func(res *channeltypes.MsgChannelUpgradeAckResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeAckResponse, events []abci.Event, err error) { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) + suite.Require().Empty(events) }, - false, }, } @@ -1310,30 +1395,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeAck() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeAck(ctx, msg) - tc.expResult(res, err) + events := ctx.EventManager().Events().ToABCIEvents() - 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) - } + tc.expResult(res, events, err) }) } } @@ -1347,13 +1411,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { cases := []struct { name string malleate func() - expResult func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) - expEvents bool + expResult func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) }{ { "success, no pending in-flight packets", func() {}, - func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().Equal(channeltypes.SUCCESS, res.Result) @@ -1361,8 +1424,34 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { channel := path.EndpointB.GetChannel() suite.Require().Equal(channeltypes.OPEN, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + 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, + }, + } + + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "success, pending in-flight packets on init chain", @@ -1405,7 +1494,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { Signer: suite.chainA.SenderAccount.GetAddress().String(), } }, - func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().Equal(channeltypes.SUCCESS, res.Result) @@ -1417,8 +1506,20 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { channel = path.EndpointB.GetChannel() suite.Require().Equal(channeltypes.FLUSHCOMPLETE, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + 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), + }, + } + + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "success, pending in-flight packets on try chain", @@ -1426,7 +1527,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { portID, channelID := path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID suite.chainB.GetSimApp().IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainB.GetContext(), portID, channelID, 1, []byte("hash")) }, - func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) suite.Require().Equal(channeltypes.SUCCESS, res.Result) @@ -1434,8 +1535,20 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { channel := path.EndpointB.GetChannel() suite.Require().Equal(channeltypes.FLUSHING, channel.State) suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + 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) }, - true, }, { "module capability not found", @@ -1443,13 +1556,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { msg.PortId = ibctesting.InvalidID msg.ChannelId = ibctesting.InvalidID }, - func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) + suite.Require().Empty(events) }, - false, }, { "core handler returns error and no upgrade error receipt is written", @@ -1460,7 +1573,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { path.EndpointB.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) suite.Require().ErrorIs(err, channeltypes.ErrInvalidChannelState) @@ -1468,8 +1581,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { errorReceipt, found := suite.chainB.GetSimApp().GetIBCKeeper().ChannelKeeper.GetUpgradeErrorReceipt(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) suite.Require().Empty(errorReceipt) suite.Require().False(found) + + suite.Require().Empty(events) }, - false, }, { "core handler returns error and writes upgrade error receipt", @@ -1492,7 +1606,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { msg.ProofUpgrade = proofUpgrade msg.ProofHeight = proofHeight }, - func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1501,8 +1615,26 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { errorReceipt, found := suite.chainB.GetSimApp().GetIBCKeeper().ChannelKeeper.GetUpgradeErrorReceipt(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) suite.Require().True(found) suite.Require().Equal(uint64(1), errorReceipt.Sequence) + + channel := path.EndpointB.GetChannel() + + expEvents := ibctesting.EventsMap{ + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "counterparty upgrade timeout elapsed: current timestamp: 1578269010000000000, timeout timestamp 1578268995000000000: timeout elapsed", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1516,11 +1648,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { msg.PortId = path.EndpointB.ChannelConfig.PortID msg.ChannelId = path.EndpointB.ChannelID }, - func(res *channeltypes.MsgChannelUpgradeConfirmResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeConfirmResponse, events []abci.Event, err error) { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) + + suite.Require().Empty(events) }, - false, }, } @@ -1568,65 +1701,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeConfirm() { 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), - }, - } - } + events := ctx.EventManager().Events().ToABCIEvents() - ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) - } + tc.expResult(res, events, err) }) } } @@ -1640,20 +1717,36 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { cases := []struct { name string malleate func() - expResult func(res *channeltypes.MsgChannelUpgradeOpenResponse, err error) - expEvents bool + expResult func(res *channeltypes.MsgChannelUpgradeOpenResponse, events []abci.Event, err error) }{ { "success", func() {}, - func(res *channeltypes.MsgChannelUpgradeOpenResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeOpenResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) channel := path.EndpointA.GetChannel() suite.Require().Equal(channeltypes.OPEN, channel.State) + + 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) }, - true, }, { "module capability not found", @@ -1661,13 +1754,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { msg.PortId = ibctesting.InvalidID msg.ChannelId = ibctesting.InvalidID }, - func(res *channeltypes.MsgChannelUpgradeOpenResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeOpenResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) + suite.Require().Empty(events) }, - false, }, { "core handler fails", @@ -1676,13 +1769,13 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { channel.State = channeltypes.FLUSHING path.EndpointA.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeOpenResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeOpenResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) suite.Require().ErrorIs(err, channeltypes.ErrInvalidChannelState) + suite.Require().Empty(events) }, - false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1696,11 +1789,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { msg.PortId = path.EndpointB.ChannelConfig.PortID msg.ChannelId = path.EndpointB.ChannelID }, - func(res *channeltypes.MsgChannelUpgradeOpenResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeOpenResponse, events []abci.Event, err error) { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) + + suite.Require().Empty(events) }, - false, }, } @@ -1748,29 +1842,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeOpen() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeOpen(ctx, msg) - tc.expResult(res, err) + events := ctx.EventManager().Events().ToABCIEvents() - 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) - } + tc.expResult(res, events, err) }) } } @@ -1784,13 +1858,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { cases := []struct { name string malleate func() - expResult func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) - expEvents bool + expResult func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) }{ { "success: keeper is not authority, valid error receipt so channnel changed to match error receipt seq", func() {}, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1799,8 +1872,35 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().Equal(channeltypes.OPEN, channel.State) // Upgrade sequence should be changed to match sequence on error receipt. suite.Require().Equal(uint64(2), channel.UpgradeSequence) + + // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. + proposedUpgrade := path.EndpointA.GetProposedUpgrade() + 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), + }, + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "invalid upgrade", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "success: keeper is authority & channel state in FLUSHING, so error receipt is ignored and channel is restored to initial upgrade sequence", @@ -1812,7 +1912,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { channel.UpgradeSequence = uint64(3) path.EndpointA.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1821,8 +1921,35 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().Equal(channeltypes.OPEN, channel.State) // Upgrade sequence should be changed to match initial upgrade sequence. suite.Require().Equal(uint64(3), channel.UpgradeSequence) + + // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. + proposedUpgrade := path.EndpointA.GetProposedUpgrade() + 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), + }, + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "invalid upgrade", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "success: keeper is authority & channel state in FLUSHING, can be cancelled even with invalid error receipt", @@ -1835,7 +1962,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { channel.UpgradeSequence = uint64(1) path.EndpointA.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1844,8 +1971,35 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().Equal(channeltypes.OPEN, channel.State) // Upgrade sequence should be changed to match initial upgrade sequence. suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. + proposedUpgrade := path.EndpointA.GetProposedUpgrade() + 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), + }, + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "invalid upgrade", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "success: keeper is authority & channel state in FLUSHING, can be cancelled even with empty error receipt", @@ -1858,7 +2012,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { channel.UpgradeSequence = uint64(1) path.EndpointA.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1867,8 +2021,35 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().Equal(channeltypes.OPEN, channel.State) // Upgrade sequence should be changed to match initial upgrade sequence. suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. + proposedUpgrade := path.EndpointA.GetProposedUpgrade() + 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), + }, + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "invalid upgrade", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "success: keeper is authority but channel state in FLUSHCOMPLETE, requires valid error receipt", @@ -1880,7 +2061,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { channel.UpgradeSequence = uint64(1) path.EndpointA.SetChannel(channel) }, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().NoError(err) suite.Require().NotNil(res) @@ -1889,15 +2070,42 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().Equal(channeltypes.OPEN, channel.State) // Upgrade sequence should be changed to match error receipt sequence. suite.Require().Equal(uint64(2), channel.UpgradeSequence) + + // we need to find the event values from the proposed upgrade as the actual upgrade has been deleted. + proposedUpgrade := path.EndpointA.GetProposedUpgrade() + 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), + }, + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "invalid upgrade", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "capability not found", func() { msg.ChannelId = ibctesting.InvalidID }, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) @@ -1906,8 +2114,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().Equal(channeltypes.OPEN, channel.State) // Upgrade sequence should not be changed. suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + suite.Require().Empty(events) }, - false, }, { "core handler fails: invalid proof", @@ -1916,7 +2125,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { // Force set to STATE_FLUSHCOMPLETE to check that state is not changed. suite.Require().NoError(path.EndpointA.SetChannelState(channeltypes.FLUSHCOMPLETE)) }, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().Nil(res) @@ -1926,8 +2135,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { suite.Require().Equal(channeltypes.FLUSHCOMPLETE, channel.State) // Upgrade sequence should not be changed. suite.Require().Equal(uint64(1), channel.UpgradeSequence) + + suite.Require().Empty(events) }, - false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -1941,11 +2151,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { msg.PortId = path.EndpointB.ChannelConfig.PortID msg.ChannelId = path.EndpointB.ChannelID }, - func(res *channeltypes.MsgChannelUpgradeCancelResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeCancelResponse, events []abci.Event, err error) { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) + + suite.Require().Empty(events) }, - false, }, } for _, tc := range cases { @@ -2000,31 +2211,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeCancel() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeCancel(ctx, msg) - tc.expResult(res, err) + events := ctx.EventManager().Events().ToABCIEvents() - 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) - } + tc.expResult(res, events, err) }) } } @@ -2045,8 +2234,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { cases := []struct { name string malleate func() - expResult func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, err error) - expEvents bool + expResult func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, events []abci.Event, err error) }{ { "success", @@ -2061,7 +2249,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { msg.ProofChannel = channelProof msg.ProofHeight = proofHeight }, - func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, events []abci.Event, err error) { channel := path.EndpointA.GetChannel() suite.Require().Equalf(channeltypes.OPEN, channel.State, "channel state should be %s", channeltypes.OPEN.String()) @@ -2071,15 +2259,49 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { suite.Require().NotNil(res) suite.Require().NoError(err) + + errorReceipt, found := suite.chainA.GetSimApp().GetIBCKeeper().ChannelKeeper.GetUpgradeErrorReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + suite.Require().True(found) + suite.Require().Equal(uint64(1), errorReceipt.Sequence) + + // 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) + 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), + }, + channeltypes.EventTypeChannelUpgradeError: { + channeltypes.AttributeKeyPortID: path.EndpointA.ChannelConfig.PortID, + channeltypes.AttributeKeyChannelID: path.EndpointA.ChannelID, + channeltypes.AttributeCounterpartyPortID: path.EndpointB.ChannelConfig.PortID, + channeltypes.AttributeCounterpartyChannelID: path.EndpointB.ChannelID, + channeltypes.AttributeKeyUpgradeSequence: fmt.Sprintf("%d", channel.UpgradeSequence), + // need to manually insert this because the errorReceipt is a string constant as it is written into state + channeltypes.AttributeKeyUpgradeErrorReceipt: "upgrade timed-out", + }, + sdk.EventTypeMessage: { + sdk.AttributeKeyModule: channeltypes.AttributeValueCategory, + }, + } + ibctesting.AssertEventsLegacy(&suite.Suite, expEvents, events) }, - true, }, { "capability not found", func() { msg.ChannelId = ibctesting.InvalidID }, - func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().ErrorIs(err, capabilitytypes.ErrCapabilityNotFound) @@ -2089,8 +2311,9 @@ 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") + + suite.Require().Empty(events) }, - false, }, { "core handler fails: invalid proof", @@ -2104,7 +2327,7 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { msg.ProofHeight = proofHeight msg.ProofChannel = []byte("invalid proof") }, - func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, events []abci.Event, err error) { suite.Require().Error(err) suite.Require().ErrorIs(err, commitmenttypes.ErrInvalidProof) @@ -2114,8 +2337,9 @@ 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") + + suite.Require().Empty(events) }, - false, }, { "ibc application does not implement the UpgradeableModule interface", @@ -2129,11 +2353,12 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { msg.PortId = path.EndpointB.ChannelConfig.PortID msg.ChannelId = path.EndpointB.ChannelID }, - func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, err error) { + func(res *channeltypes.MsgChannelUpgradeTimeoutResponse, events []abci.Event, err error) { suite.Require().ErrorIs(err, porttypes.ErrInvalidRoute) suite.Require().Nil(res) + + suite.Require().Empty(events) }, - false, }, } @@ -2168,34 +2393,9 @@ func (suite *KeeperTestSuite) TestChannelUpgradeTimeout() { ctx := suite.chainA.GetContext() res, err := suite.chainA.GetSimApp().GetIBCKeeper().ChannelUpgradeTimeout(ctx, msg) - tc.expResult(res, err) + events := ctx.EventManager().Events().ToABCIEvents() - 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) - } + tc.expResult(res, events, err) }) } }