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

chore: implement msg server for ChannelUpgradeTry #3791

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ea76c13
refactor: add rest of the handler code
colin-axner Jun 7, 2023
c0d923f
Merge remote-tracking branch 'origin/04-channel-upgrades' into colin/…
colin-axner Jun 7, 2023
a5d997e
fix: update upgrade tests
colin-axner Jun 7, 2023
d9a8999
fix: test fixes for upgradeTry
colin-axner Jun 7, 2023
fe0095d
fix: upgrade tests
colin-axner Jun 7, 2023
fa6598b
imp: add error code checking to tests
colin-axner Jun 7, 2023
0505850
Merge branch '04-channel-upgrades' into colin/3739-upgrade-try-handler
colin-axner Jun 7, 2023
75b52f1
chore: add todo
colin-axner Jun 7, 2023
ec3bb0c
Merge branch 'colin/3739-upgrade-try-handler' of github.com:cosmos/ib…
colin-axner Jun 7, 2023
89a8ce8
Update modules/core/04-channel/keeper/upgrade.go
colin-axner Jun 7, 2023
e1a7d62
adding msg server scaffolding for MsgChannelUpgradeTry
damiannolan Jun 7, 2023
a3a2ac5
updating logging and comments
damiannolan Jun 7, 2023
7e0ced8
Merge remote-tracking branch 'origin/04-channel-upgrades' into damian…
colin-axner Jun 8, 2023
1a326a5
Merge branch '04-channel-upgrades' into damian/3740-chan-upgrade-try-…
damiannolan Jun 8, 2023
b732ce3
removing flushStatus arg from WriteUpgradeTryChannel
damiannolan Jun 8, 2023
4462bd7
move upgradeVersion refreshing to WriteUpgradeTryChannel, tidy code s…
damiannolan Jun 8, 2023
98ce964
adding initial testcases for upgrade try msg server
damiannolan Jun 11, 2023
912b8da
adding testing app overrides for upgrade handshake app callback handlers
damiannolan Jun 11, 2023
b3042e0
adding app callback testcases and updating assertions
damiannolan Jun 11, 2023
ab18e7e
Merge branch '04-channel-upgrades' into damian/3740-chan-upgrade-try-…
damiannolan Jun 11, 2023
acc6b97
updating tests to include application state changes and assert they a…
damiannolan Jun 12, 2023
d6e358c
adding extra context to inline comment for upgrade cancellation subpr…
damiannolan Jun 12, 2023
bb422e4
updating WriteUpgradetryChannel godoc
damiannolan Jun 12, 2023
aaee7a7
add return args to WriteUpgradeTryChannel to fulfil response args
damiannolan Jun 12, 2023
809ff19
Merge branch '04-channel-upgrades' into damian/3740-chan-upgrade-try-…
damiannolan Jun 12, 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
90 changes: 72 additions & 18 deletions modules/core/04-channel/keeper/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (k Keeper) ChanUpgradeTry(
portID,
channelID string,
proposedConnectionHops []string,
proposedUpgradeTimeout types.Timeout,
counterpartyProposedUpgrade types.Upgrade,
upgradeTimeout types.Timeout,
counterpartyUpgrade types.Upgrade,
counterpartyUpgradeSequence uint64,
proofCounterpartyChannel,
proofCounterpartyUpgrade []byte,
Expand All @@ -92,39 +92,89 @@ func (k Keeper) ChanUpgradeTry(
return types.Upgrade{}, errorsmod.Wrapf(types.ErrInvalidChannelState, "expected one of [%s, %s], got %s", types.OPEN, types.INITUPGRADE, channel.State)
}

connectionEnd, err := k.GetConnection(ctx, channel.ConnectionHops[0])
connection, err := k.GetConnection(ctx, channel.ConnectionHops[0])
if err != nil {
return types.Upgrade{}, errorsmod.Wrap(err, "failed to retrieve connection using the channel connection hops")
}

if connectionEnd.GetState() != int32(connectiontypes.OPEN) {
if connection.GetState() != int32(connectiontypes.OPEN) {
return types.Upgrade{}, errorsmod.Wrapf(
connectiontypes.ErrInvalidConnectionState, "connection state is not OPEN (got %s)", connectiontypes.State(connectionEnd.GetState()).String(),
connectiontypes.ErrInvalidConnectionState, "connection state is not OPEN (got %s)", connectiontypes.State(connection.GetState()).String(),
)
}

if hasPassed, err := counterpartyProposedUpgrade.Timeout.HasPassed(ctx); hasPassed {
if hasPassed, err := counterpartyUpgrade.Timeout.HasPassed(ctx); hasPassed {
// abort here and let counterparty timeout the upgrade
return types.Upgrade{}, errorsmod.Wrap(err, "upgrade timeout has passed")
}

// construct counterpartyChannel from existing information and provided counterpartyUpgradeSequence

// create upgrade fields from counterparty proposed upgrade and own verified connection hops
proposedUpgradeFields := types.UpgradeFields{
Ordering: counterpartyUpgrade.Fields.Ordering,
ConnectionHops: proposedConnectionHops,
Version: counterpartyUpgrade.Fields.Version,
}

// if OPEN, then initialize handshake with upgradeFields
// this should validate the upgrade fields, set the upgrade path and set the final correct sequence.
var upgrade types.Upgrade

// otherwise, if the channel state is already in INITUPGRADE (crossing hellos case),
// assert that the upgrade fields are the same as the upgrade already in progress
switch channel.State {
case types.OPEN:
// initialize handshake with upgrade fields
upgrade, err = k.ChanUpgradeInit(ctx, portID, channelID, proposedUpgradeFields, upgradeTimeout)
if err != nil {
return types.Upgrade{}, errorsmod.Wrap(err, "failed to initialize upgrade")
}

// } else if channel.State == types.INITUPGRADE {
// TODO: add fast forward feature

// if the counterparty sequence is not equal to our own at this point, either the counterparty chain is out-of-sync or the message is out-of-sync
// we write an error receipt with our own sequence so that the counterparty can update their sequence as well.
// We must then increment our sequence so both sides start the next upgrade with a fresh sequence.
var proposedUpgrade types.Upgrade
return proposedUpgrade, nil
// NOTE: OnChanUpgradeInit will not be executed

k.WriteUpgradeInitChannel(ctx, portID, channelID, upgrade)

case types.INITUPGRADE:
// crossing hello's
// assert that the upgrade fields are the same as the upgrade already in progress
upgrade, found = k.GetUpgrade(ctx, portID, channelID)
if !found {
return types.Upgrade{}, errorsmod.Wrapf(types.ErrUpgradeNotFound, "current upgrade not found despite channel state being in %s", types.INITUPGRADE)
}

if !reflect.DeepEqual(upgrade.Fields, proposedUpgradeFields) {
return types.Upgrade{}, errorsmod.Wrapf(
types.ErrInvalidUpgrade, "upgrade fields are not equal to current upgrade fields in crossing hello's case, expected %s", upgrade.Fields)
}

default:
panic(fmt.Sprintf("channel state should be asserted to be in OPEN or INITUPGRADE before reaching this check; state is %s", channel.State))
}

// construct expected counterparty channel from information in state
// only the counterpartyUpgradeSequence is provided by the relayer
counterpartyConnectionHops := []string{connection.GetCounterparty().GetConnectionID()}
counterpartyChannel := types.Channel{
State: types.INITUPGRADE,
Ordering: channel.Ordering,
Counterparty: types.NewCounterparty(portID, channelID),
ConnectionHops: counterpartyConnectionHops,
Version: channel.Version,
UpgradeSequence: counterpartyUpgradeSequence, // provided by the relayer
FlushStatus: types.NOTINFLUSH,
}

if err := k.startFlushUpgradeHandshake(
ctx,
portID, channelID,
proposedUpgradeFields,
counterpartyChannel,
counterpartyUpgrade,
proofCounterpartyChannel, proofCounterpartyUpgrade,
proofHeight,
); err != nil {
return types.Upgrade{}, err
}

return upgrade, nil
}

// WriteUpgradeTryChannel writes a channel which has successfully passed the UpgradeTry handshake step.
Expand Down Expand Up @@ -153,6 +203,10 @@ func (k Keeper) WriteUpgradeTryChannel(
emitChannelUpgradeTryEvent(ctx, portID, channelID, channel, proposedUpgrade)
}

func (k Keeper) AbortUpgrade(ctx sdk.Context, portID, channelID string, err error) error {
Copy link
Member Author

Choose a reason for hiding this comment

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

Pending #3728

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 Expand Up @@ -268,7 +322,7 @@ func (k Keeper) validateUpgradeFields(ctx sdk.Context, proposedUpgrade types.Upg
currentFields := extractUpgradeFields(currentChannel)

if reflect.DeepEqual(proposedUpgrade, currentFields) {
return errorsmod.Wrap(types.ErrChannelExists, "existing channel end is identical to proposed upgrade channel end")
return errorsmod.Wrapf(types.ErrChannelExists, "existing channel end is identical to proposed upgrade channel end: got %s", proposedUpgrade)
}

connectionID := proposedUpgrade.ConnectionHops[0]
Expand Down
Loading