Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that the error receipt we write always has a sequence greater than the existing one. #5237

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
84d6a9c
chore: ensure new error receipts are always greater than the existing…
chatton Nov 29, 2023
68bd689
chore: added unit tests for WriteErrorReceipt
chatton Nov 29, 2023
6434b13
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
chatton Nov 29, 2023
5ee32e9
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
chatton Nov 30, 2023
c205130
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
chatton Nov 30, 2023
73151c8
chore: updated tests to use panics instead
chatton Nov 30, 2023
e9d9561
Merge branch 'cian/issue#5226-make-sure-that-the-error-receipt-we-wri…
chatton Nov 30, 2023
6f5cd0f
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
chatton Nov 30, 2023
9b2e6d5
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
charleenfei Dec 4, 2023
f39e203
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
Dec 4, 2023
1446cc1
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
damiannolan Dec 6, 2023
e0075d6
fix: compiler error
damiannolan Dec 6, 2023
faf7599
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
charleenfei Dec 8, 2023
190d568
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
chatton Dec 13, 2023
d73b138
chore: address PR feedback
chatton Dec 13, 2023
8adcfb4
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
chatton Dec 13, 2023
a761b17
Merge branch '04-channel-upgrades' into cian/issue#5226-make-sure-tha…
chatton Dec 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/core/04-channel/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ func (k Keeper) CheckForUpgradeCompatibility(ctx sdk.Context, upgradeFields, cou
func (k Keeper) SyncUpgradeSequence(ctx sdk.Context, portID, channelID string, channel types.Channel, counterpartyUpgradeSequence uint64) error {
return k.syncUpgradeSequence(ctx, portID, channelID, channel, counterpartyUpgradeSequence)
}

// WriteErrorReceipt is a wrapper around writeErrorReceipt to allow the function to be directly called in tests.
func (k Keeper) WriteErrorReceipt(ctx sdk.Context, portID, channelID string, upgradeError *types.UpgradeError) {
k.writeErrorReceipt(ctx, portID, channelID, upgradeError)
}
18 changes: 18 additions & 0 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,3 +941,21 @@ func (k Keeper) restoreChannel(ctx sdk.Context, portID, channelID string, upgrad

return channel
}

// writeErrorReceipt will write an error receipt from the provided UpgradeError.
func (k Keeper) writeErrorReceipt(ctx sdk.Context, portID, channelID string, upgradeError *types.UpgradeError) {
channel, found := k.GetChannel(ctx, portID, channelID)
if !found {
panic(errorsmod.Wrapf(types.ErrChannelNotFound, "port ID (%s) channel ID (%s)", portID, channelID))
}

errorReceiptToWrite := upgradeError.GetErrorReceipt()

existingErrorReceipt, found := k.GetUpgradeErrorReceipt(ctx, portID, channelID)
if found && existingErrorReceipt.Sequence >= errorReceiptToWrite.Sequence {
panic(errorsmod.Wrapf(types.ErrInvalidUpgradeSequence, "error receipt sequence (%d) must be greater than existing error receipt sequence (%d)", errorReceiptToWrite.Sequence, existingErrorReceipt.Sequence))
}

k.SetUpgradeErrorReceipt(ctx, portID, channelID, errorReceiptToWrite)
EmitErrorReceiptEvent(ctx, portID, channelID, channel, upgradeError)
}
68 changes: 68 additions & 0 deletions modules/core/04-channel/keeper/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2379,3 +2379,71 @@ func (suite *KeeperTestSuite) TestChanUpgradeCrossingHelloWithHistoricalProofs()
})
}
}

func (suite *KeeperTestSuite) TestWriteErrorReceipt() {
var path *ibctesting.Path
var upgradeError *types.UpgradeError

testCases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
{
"success: existing error receipt found at a lower sequence",
func() {
// write an error sequence with a lower sequence number
previousUpgradeError := types.NewUpgradeError(upgradeError.GetErrorReceipt().Sequence-1, types.ErrInvalidUpgrade)
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetUpgradeErrorReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, previousUpgradeError.GetErrorReceipt())
},
nil,
},
{
"failure: existing error receipt found at a higher sequence",
func() {
// write an error sequence with a higher sequence number
previousUpgradeError := types.NewUpgradeError(upgradeError.GetErrorReceipt().Sequence+1, types.ErrInvalidUpgrade)
suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetUpgradeErrorReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, previousUpgradeError.GetErrorReceipt())
},
errorsmod.Wrap(types.ErrInvalidUpgradeSequence, "error receipt sequence (10) must be greater than existing error receipt sequence (11)"),
},
{
"failure: channel not found",
func() {
suite.chainA.DeleteKey(host.ChannelKey(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID))
},
errorsmod.Wrap(types.ErrChannelNotFound, "port ID (mock) channel ID (channel-0)"),
},
}

for _, tc := range testCases {
tc := tc
suite.Run(tc.name, func() {
suite.SetupTest()
path = ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)

channelKeeper := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper

upgradeError = types.NewUpgradeError(10, types.ErrInvalidUpgrade)

tc.malleate()

expPass := tc.expError == nil
if expPass {
suite.NotPanics(func() {
channelKeeper.WriteErrorReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, upgradeError)
})
} else {
suite.PanicsWithError(tc.expError.Error(), func() {
channelKeeper.WriteErrorReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, upgradeError)
})
}
})
}
}
Loading