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

WriteUpgradeTimeout #3797

Merged
merged 21 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
50b1163
writeupgradetimeout method, pull in util methods
charleenfei Jun 8, 2023
ba1fde4
update method to use abort method instead of restoreChannel
charleenfei Jun 9, 2023
8cd8c0b
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 9, 2023
c3fc032
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 13, 2023
4e5d866
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 13, 2023
940646d
add errors
charleenfei Jun 13, 2023
6063131
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 13, 2023
8695fb2
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 13, 2023
126c833
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 14, 2023
7556af7
Merge branch 'charly/#37500-write-upgrade-timeout' of github.com:cosm…
charleenfei Jun 14, 2023
178b2d3
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 14, 2023
e2d367a
pr comments
charleenfei Jun 14, 2023
af566f7
return value
charleenfei Jun 15, 2023
e6d089d
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 15, 2023
8473fca
update response with getter
charleenfei Jun 15, 2023
0199a14
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 15, 2023
324fac8
Merge branch 'charly/#37500-write-upgrade-timeout' of github.com:cosm…
charleenfei Jun 16, 2023
715ef16
pr comments
charleenfei Jun 16, 2023
0107b53
proto back to original
charleenfei Jun 16, 2023
d16acce
tx.pb
charleenfei Jun 16, 2023
5f2a911
Merge branch '04-channel-upgrades' into charly/#37500-write-upgrade-t…
charleenfei Jun 16, 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
22 changes: 22 additions & 0 deletions modules/core/04-channel/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,28 @@ func emitChannelUpgradeOpenEvent(ctx sdk.Context, portID string, channelID strin
})
}

// emitChannelUpgradeTimeoutEvent emits an upgrade timeout event.
func emitChannelUpgradeTimeoutEvent(ctx sdk.Context, portID string, channelID string, currentChannel types.Channel, upgrade types.Upgrade) {
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeChannelUpgradeTimeout,
sdk.NewAttribute(types.AttributeKeyPortID, portID),
sdk.NewAttribute(types.AttributeKeyChannelID, channelID),
sdk.NewAttribute(types.AttributeCounterpartyPortID, currentChannel.Counterparty.PortId),
sdk.NewAttribute(types.AttributeCounterpartyChannelID, currentChannel.Counterparty.ChannelId),
sdk.NewAttribute(types.AttributeKeyUpgradeConnectionHops, upgrade.Fields.ConnectionHops[0]),
sdk.NewAttribute(types.AttributeKeyUpgradeVersion, upgrade.Fields.Version),
sdk.NewAttribute(types.AttributeKeyUpgradeOrdering, upgrade.Fields.Ordering.String()),
sdk.NewAttribute(types.AttributeKeyUpgradeTimeout, upgrade.Timeout.String()),
sdk.NewAttribute(types.AttributeKeyUpgradeSequence, fmt.Sprintf("%d", currentChannel.UpgradeSequence)),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
})
}

// emitErrorReceiptEvent emits an error receipt event
func emitErrorReceiptEvent(ctx sdk.Context, portID string, channelID string, currentChannel types.Channel, upgrade types.Upgrade, err error) {
ctx.EventManager().EmitEvents(sdk.Events{
Expand Down
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 @@ -35,3 +35,8 @@ func (k Keeper) ValidateUpgradeFields(ctx sdk.Context, proposedUpgrade types.Upg
func (k Keeper) WriteUpgradeOpenChannel(ctx sdk.Context, portID, channelID string) {
k.writeUpgradeOpenChannel(ctx, portID, channelID)
}

// WriteUpgradeTimeoutChannel is a wrapper around writeUpgradeTimeoutChannel to allow the function to be directly called in tests.
func (k Keeper) WriteUpgradeTimeoutChannel(ctx sdk.Context, portID, channelID string) error {
return k.writeUpgradeTimeoutChannel(ctx, portID, channelID)
}
29 changes: 29 additions & 0 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,35 @@ func (k Keeper) ChanUpgradeAck(
return nil
}

// writeUpgradeTimeoutChannel restores the channel state of an initialising chain in the event that the counterparty chain has passed the timeout set in ChanUpgradeInit to the state before the upgrade was proposed.
// Auxiliary upgrade state is also deleted.
// An event is emitted for the handshake step.
func (k Keeper) writeUpgradeTimeoutChannel(
ctx sdk.Context,
portID, channelID string,
) error {
defer telemetry.IncrCounter(1, "ibc", "channel", "upgrade-timeout")

channel, found := k.GetChannel(ctx, portID, channelID)
if !found {
panic(fmt.Sprintf("could not find existing channel when updating channel state in successful ChanUpgradeTimeout step, channelID: %s, portID: %s", channelID, portID))
}

upgrade, found := k.GetUpgrade(ctx, portID, channelID)
if !found {
panic(fmt.Sprintf("could not find existing upgrade when cancelling channel upgrade, channelID: %s, portID: %s", channelID, portID))
}

if err := k.AbortUpgrade(ctx, portID, channelID, types.NewUpgradeError(channel.UpgradeSequence, types.ErrUpgradeTimeout)); err != nil {
return errorsmod.Wrapf(types.ErrUpgradeRestoreFailed, "err: %v", err)
}
Comment on lines +381 to +383
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AbortUpgrade function writes an error receipt to state for counterparty upgrade cancellation but in this scenario the counterparty is in its original state and we are just wanting to restore our channel end to its original state also.

Its only possible to timeout an upgrade handshake if the counterparty stalls on TRY (remaining in an open state) and we have committed the INIT step which we need to revert/timeout. So writing the error receipt here is redundant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timeouts can also occur if an insufficient timeout was set during INIT


k.Logger(ctx).Info("channel state restored", "port-id", portID, "channel-id", channelID)
emitChannelUpgradeTimeoutEvent(ctx, portID, channelID, channel, upgrade)

return nil
}

// startFlushUpgradeHandshake will verify the counterparty proposed upgrade and the current channel state.
// Once the counterparty information has been verified, it will be validated against the self proposed upgrade.
// If any of the proposed upgrade fields are incompatible, an upgrade error will be returned resulting in an
Expand Down
2 changes: 2 additions & 0 deletions modules/core/04-channel/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ var (
ErrIncompatibleCounterpartyUpgrade = errorsmod.Register(SubModuleName, 31, "incompatible counterparty upgrade")
ErrInvalidUpgradeError = errorsmod.Register(SubModuleName, 32, "invalid upgrade error")
ErrInvalidFlushStatus = errorsmod.Register(SubModuleName, 33, "invalid flush status")
ErrUpgradeRestoreFailed = errorsmod.Register(SubModuleName, 34, "restore failed")
ErrUpgradeTimeout = errorsmod.Register(SubModuleName, 35, "upgrade timed-out")
)
26 changes: 14 additions & 12 deletions modules/core/04-channel/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,24 @@ const (
AttributeKeyUpgradeOrdering = "upgrade_ordering"
AttributeKeyUpgradeErrorReceipt = "upgrade_error_receipt"
AttributeKeyUpgradeChannelFlushStatus = "channel_flush_status"
AttributeKeyUpgradeTimeout = "upgrade_timeout"
)

// IBC channel events vars
var (
EventTypeChannelOpenInit = "channel_open_init"
EventTypeChannelOpenTry = "channel_open_try"
EventTypeChannelOpenAck = "channel_open_ack"
EventTypeChannelOpenConfirm = "channel_open_confirm"
EventTypeChannelCloseInit = "channel_close_init"
EventTypeChannelCloseConfirm = "channel_close_confirm"
EventTypeChannelClosed = "channel_close"
EventTypeChannelUpgradeInit = "channel_upgrade_init"
EventTypeChannelUpgradeTry = "channel_upgrade_try"
EventTypeChannelUpgradeAck = "channel_upgrade_ack"
EventTypeChannelUpgradeOpen = "channel_upgrade_open"
EventTypeChannelUpgradeCancel = "channel_upgrade_cancelled"
EventTypeChannelOpenInit = "channel_open_init"
EventTypeChannelOpenTry = "channel_open_try"
EventTypeChannelOpenAck = "channel_open_ack"
EventTypeChannelOpenConfirm = "channel_open_confirm"
EventTypeChannelCloseInit = "channel_close_init"
EventTypeChannelCloseConfirm = "channel_close_confirm"
EventTypeChannelClosed = "channel_close"
EventTypeChannelUpgradeInit = "channel_upgrade_init"
EventTypeChannelUpgradeTry = "channel_upgrade_try"
EventTypeChannelUpgradeAck = "channel_upgrade_ack"
EventTypeChannelUpgradeOpen = "channel_upgrade_open"
EventTypeChannelUpgradeTimeout = "channel_upgrade_timeout"
EventTypeChannelUpgradeCancel = "channel_upgrade_cancelled"

AttributeValueCategory = fmt.Sprintf("%s_%s", ibcexported.ModuleName, SubModuleName)
)