From dff6643bf8a56a72359371bdf29578202f377ab5 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Thu, 20 Jul 2023 21:44:45 +0200 Subject: [PATCH 1/4] add IsUpgradeError function --- modules/core/04-channel/types/upgrade.go | 9 ++++ modules/core/04-channel/types/upgrade_test.go | 44 +++++++++++++++++++ modules/core/keeper/msg_server.go | 8 ++-- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/modules/core/04-channel/types/upgrade.go b/modules/core/04-channel/types/upgrade.go index 9789b50472f..da7120328b5 100644 --- a/modules/core/04-channel/types/upgrade.go +++ b/modules/core/04-channel/types/upgrade.go @@ -117,3 +117,12 @@ func (u *UpgradeError) GetErrorReceipt() ErrorReceipt { Message: fmt.Sprintf("ABCI code: %d: %s", code, restoreErrorString), } } + +// IsUpgradeError returns true if err is of type UpgradeErroe, otherwise false. +func IsUpgradeError(err error) bool { + if err == nil { + return false + } + _, ok := err.(*UpgradeError) + return ok +} diff --git a/modules/core/04-channel/types/upgrade_test.go b/modules/core/04-channel/types/upgrade_test.go index 5dc3babc8d9..b897e9cff03 100644 --- a/modules/core/04-channel/types/upgrade_test.go +++ b/modules/core/04-channel/types/upgrade_test.go @@ -171,3 +171,47 @@ func (suite *TypesTestSuite) TestUpgradeErrorUnwrap() { suite.Require().Equal(types.ErrInvalidChannel, unWrapped, "unwrapped error was not equal to base underlying error") suite.Require().Equal(originalUpgradeError, postUnwrapUpgradeError, "original error was modified when unwrapped") } + +func (suite *TypesTestSuite) TestIsUpgradeError() { + var ( + err error + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "true", + func() {}, + true, + }, + { + "false with non upgrade error", + func() { + err = errors.New("error") + }, + false, + }, + { + "false with nil error", + func() { + err = nil + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + suite.Run(tc.msg, func() { + err = types.NewUpgradeError(1, types.ErrInvalidChannel) + + tc.malleate() + + res := types.IsUpgradeError(err) + suite.Require().Equal(tc.expPass, res) + }) + } +} diff --git a/modules/core/keeper/msg_server.go b/modules/core/keeper/msg_server.go index aa02af43ce5..0fa6e073d97 100644 --- a/modules/core/keeper/msg_server.go +++ b/modules/core/keeper/msg_server.go @@ -768,8 +768,8 @@ func (k Keeper) ChannelUpgradeTry(goCtx context.Context, msg *channeltypes.MsgCh upgrade, err := k.ChannelKeeper.ChanUpgradeTry(ctx, msg.PortId, msg.ChannelId, msg.ProposedUpgradeConnectionHops, msg.UpgradeTimeout, msg.CounterpartyProposedUpgrade, msg.CounterpartyUpgradeSequence, msg.ProofChannel, msg.ProofUpgrade, msg.ProofHeight) if err != nil { ctx.Logger().Error("channel upgrade try failed", "error", errorsmod.Wrap(err, "channel upgrade try failed")) - if upgradeErr, ok := err.(*channeltypes.UpgradeError); ok { - k.ChannelKeeper.MustAbortUpgrade(ctx, msg.PortId, msg.ChannelId, upgradeErr) + if channeltypes.IsUpgradeError(err) { + k.ChannelKeeper.MustAbortUpgrade(ctx, msg.PortId, msg.ChannelId, err) cbs.OnChanUpgradeRestore(ctx, msg.PortId, msg.ChannelId) // NOTE: a FAILURE result is returned to the client and an error receipt is written to state. @@ -823,8 +823,8 @@ func (k Keeper) ChannelUpgradeAck(goCtx context.Context, msg *channeltypes.MsgCh err = k.ChannelKeeper.ChanUpgradeAck(ctx, msg.PortId, msg.ChannelId, msg.CounterpartyFlushStatus, msg.CounterpartyUpgrade, msg.ProofChannel, msg.ProofUpgrade, msg.ProofHeight) if err != nil { ctx.Logger().Error("channel upgrade ack failed", "error", errorsmod.Wrap(err, "channel upgrade ack failed")) - if upgradeErr, ok := err.(*channeltypes.UpgradeError); ok { - k.ChannelKeeper.MustAbortUpgrade(ctx, msg.PortId, msg.ChannelId, upgradeErr) + if channeltypes.IsUpgradeError(err) { + k.ChannelKeeper.MustAbortUpgrade(ctx, msg.PortId, msg.ChannelId, err) cbs.OnChanUpgradeRestore(ctx, msg.PortId, msg.ChannelId) // NOTE: a FAILURE result is returned to the client and an error receipt is written to state. From f6eba5f9601c39d6f4a74241a0ed4bae88485321 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Thu, 20 Jul 2023 22:41:14 +0200 Subject: [PATCH 2/4] Update modules/core/04-channel/types/upgrade.go Co-authored-by: Jim Fasarakis-Hilliard --- modules/core/04-channel/types/upgrade.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/core/04-channel/types/upgrade.go b/modules/core/04-channel/types/upgrade.go index da7120328b5..ffce925292c 100644 --- a/modules/core/04-channel/types/upgrade.go +++ b/modules/core/04-channel/types/upgrade.go @@ -120,9 +120,6 @@ func (u *UpgradeError) GetErrorReceipt() ErrorReceipt { // IsUpgradeError returns true if err is of type UpgradeErroe, otherwise false. func IsUpgradeError(err error) bool { - if err == nil { - return false - } _, ok := err.(*UpgradeError) return ok } From cc00c6a6dff0ab310327a73285d24f2e8e06b2fd Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Thu, 20 Jul 2023 22:49:52 +0200 Subject: [PATCH 3/4] gofumpt --- modules/core/04-channel/types/upgrade_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/core/04-channel/types/upgrade_test.go b/modules/core/04-channel/types/upgrade_test.go index b897e9cff03..87749cc5d49 100644 --- a/modules/core/04-channel/types/upgrade_test.go +++ b/modules/core/04-channel/types/upgrade_test.go @@ -173,9 +173,7 @@ func (suite *TypesTestSuite) TestUpgradeErrorUnwrap() { } func (suite *TypesTestSuite) TestIsUpgradeError() { - var ( - err error - ) + var err error testCases := []struct { msg string From 317d344991b58e95b94f56299ee1c321df7779db Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Fri, 21 Jul 2023 16:03:53 +0200 Subject: [PATCH 4/4] Update modules/core/04-channel/types/upgrade.go Co-authored-by: Charly --- modules/core/04-channel/types/upgrade.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/04-channel/types/upgrade.go b/modules/core/04-channel/types/upgrade.go index ffce925292c..44eda872f31 100644 --- a/modules/core/04-channel/types/upgrade.go +++ b/modules/core/04-channel/types/upgrade.go @@ -118,7 +118,7 @@ func (u *UpgradeError) GetErrorReceipt() ErrorReceipt { } } -// IsUpgradeError returns true if err is of type UpgradeErroe, otherwise false. +// IsUpgradeError returns true if err is of type UpgradeError, otherwise false. func IsUpgradeError(err error) bool { _, ok := err.(*UpgradeError) return ok