Skip to content

Commit

Permalink
test: add unit tests for channel upgrade event emission (#5475)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleenfei committed Jan 8, 2024
1 parent e1df7a6 commit dc99ee4
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 164 deletions.
6 changes: 0 additions & 6 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
146 changes: 2 additions & 144 deletions modules/core/04-channel/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
// },
// }
})
}
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
})
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
// },
// }

}
})
}
Expand Down Expand Up @@ -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")

Expand Down
22 changes: 21 additions & 1 deletion modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
Loading

0 comments on commit dc99ee4

Please sign in to comment.