-
Notifications
You must be signed in to change notification settings - Fork 625
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
reorganize channel handshake handler #647
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a003f8
reorganize channel handshake handler
colin-axner bce8051
Merge branch 'main' into colin/reorg-channel-handshake-handling
colin-axner f8c31d3
readjust 27-interchain-accounts to not rely on state being set before…
colin-axner a2465e3
Merge branch 'colin/reorg-channel-handshake-handling' of github.com:c…
colin-axner b406667
add changelog and migration doc entry
colin-axner db42b46
Update modules/core/04-channel/keeper/handshake.go
colin-axner ed6ad62
Merge branch 'main' into colin/reorg-channel-handshake-handling
colin-axner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/telemetry" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
|
@@ -53,14 +55,30 @@ func (k Keeper) ChanOpenInit( | |
} | ||
|
||
channelID := k.GenerateChannelIdentifier(ctx) | ||
channel := types.NewChannel(types.INIT, order, counterparty, connectionHops, version) | ||
k.SetChannel(ctx, portID, channelID, channel) | ||
|
||
capKey, err := k.scopedKeeper.NewCapability(ctx, host.ChannelCapabilityPath(portID, channelID)) | ||
if err != nil { | ||
return "", nil, sdkerrors.Wrapf(err, "could not create channel capability for port ID %s and channel ID %s", portID, channelID) | ||
} | ||
|
||
return channelID, capKey, nil | ||
} | ||
|
||
// WriteOpenInitChannel writes a channel which has successfully passed the OpenInit handshake step. | ||
// The channel is set in state and all the associated Send and Recv sequences are set to 1. | ||
// An event is emitted for the handshake step. | ||
func (k Keeper) WriteOpenInitChannel( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
order types.Order, | ||
connectionHops []string, | ||
counterparty types.Counterparty, | ||
version string, | ||
) { | ||
channel := types.NewChannel(types.INIT, order, counterparty, connectionHops, version) | ||
k.SetChannel(ctx, portID, channelID, channel) | ||
|
||
k.SetNextSequenceSend(ctx, portID, channelID, 1) | ||
k.SetNextSequenceRecv(ctx, portID, channelID, 1) | ||
k.SetNextSequenceAck(ctx, portID, channelID, 1) | ||
|
@@ -82,7 +100,12 @@ func (k Keeper) ChanOpenInit( | |
), | ||
}) | ||
|
||
return channelID, capKey, nil | ||
ctx.EventManager().EmitEvents(sdk.Events{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. someone can do a followup pr to add a function |
||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
}) | ||
} | ||
|
||
// ChanOpenTry is called by a module to accept the first step of a channel opening | ||
|
@@ -171,16 +194,13 @@ func (k Keeper) ChanOpenTry( | |
) | ||
} | ||
|
||
// NOTE: this step has been switched with the one below to reverse the connection | ||
// hops | ||
channel := types.NewChannel(types.TRYOPEN, order, counterparty, connectionHops, version) | ||
counterpartyHops := []string{connectionEnd.GetCounterparty().GetConnectionID()} | ||
|
||
// expectedCounterpaty is the counterparty of the counterparty's channel end | ||
// (i.e self) | ||
expectedCounterparty := types.NewCounterparty(portID, "") | ||
expectedChannel := types.NewChannel( | ||
types.INIT, channel.Ordering, expectedCounterparty, | ||
types.INIT, order, expectedCounterparty, | ||
counterpartyHops, counterpartyVersion, | ||
) | ||
|
||
|
@@ -202,9 +222,6 @@ func (k Keeper) ChanOpenTry( | |
return "", nil, sdkerrors.Wrapf(err, "could not create channel capability for port ID %s and channel ID %s", portID, channelID) | ||
} | ||
|
||
k.SetNextSequenceSend(ctx, portID, channelID, 1) | ||
k.SetNextSequenceRecv(ctx, portID, channelID, 1) | ||
k.SetNextSequenceAck(ctx, portID, channelID, 1) | ||
} else { | ||
// capability initialized in ChanOpenInit | ||
capKey, found = k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(portID, channelID)) | ||
|
@@ -215,6 +232,30 @@ func (k Keeper) ChanOpenTry( | |
} | ||
} | ||
|
||
return channelID, capKey, nil | ||
} | ||
|
||
// WriteOpenTryChannel writes a channel which has successfully passed the OpenTry handshake step. | ||
// The channel is set in state. If a previous channel state did not exist, all the Send and Recv | ||
// sequences are set to 1. An event is emitted for the handshake step. | ||
func (k Keeper) WriteOpenTryChannel( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
order types.Order, | ||
connectionHops []string, | ||
counterparty types.Counterparty, | ||
version string, | ||
) { | ||
previousChannel, previousChannelFound := k.GetChannel(ctx, portID, channelID) | ||
if !previousChannelFound { | ||
k.SetNextSequenceSend(ctx, portID, channelID, 1) | ||
k.SetNextSequenceRecv(ctx, portID, channelID, 1) | ||
k.SetNextSequenceAck(ctx, portID, channelID, 1) | ||
} | ||
|
||
channel := types.NewChannel(types.TRYOPEN, order, counterparty, connectionHops, version) | ||
|
||
k.SetChannel(ctx, portID, channelID, channel) | ||
|
||
k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", previousChannel.State.String(), "new-state", "TRYOPEN") | ||
|
@@ -233,8 +274,12 @@ func (k Keeper) ChanOpenTry( | |
sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), | ||
), | ||
}) | ||
|
||
return channelID, capKey, nil | ||
ctx.EventManager().EmitEvents(sdk.Events{ | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
}) | ||
} | ||
|
||
// ChanOpenAck is called by the handshake-originating module to acknowledge the | ||
|
@@ -294,17 +339,34 @@ func (k Keeper) ChanOpenAck( | |
return err | ||
} | ||
|
||
k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", channel.State.String(), "new-state", "OPEN") | ||
return nil | ||
} | ||
|
||
defer func() { | ||
telemetry.IncrCounter(1, "ibc", "channel", "open-ack") | ||
}() | ||
// WriteOpenAckChannel writes an updated channel state for the successful OpenAck handshake step. | ||
// An event is emitted for the handshake step. | ||
func (k Keeper) WriteOpenAckChannel( | ||
ctx sdk.Context, | ||
portID, | ||
channelID, | ||
counterpartyVersion, | ||
counterpartyChannelID string, | ||
) { | ||
channel, found := k.GetChannel(ctx, portID, channelID) | ||
if !found { | ||
panic(fmt.Sprintf("could not find existing channel when updating channel state in successful ChanOpenAck step, channelID: %s, portID: %s", channelID, portID)) | ||
} | ||
|
||
channel.State = types.OPEN | ||
channel.Version = counterpartyVersion | ||
channel.Counterparty.ChannelId = counterpartyChannelID | ||
k.SetChannel(ctx, portID, channelID, channel) | ||
|
||
k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", channel.State.String(), "new-state", "OPEN") | ||
|
||
defer func() { | ||
telemetry.IncrCounter(1, "ibc", "channel", "open-ack") | ||
}() | ||
|
||
ctx.EventManager().EmitEvents(sdk.Events{ | ||
sdk.NewEvent( | ||
types.EventTypeChannelOpenAck, | ||
|
@@ -315,8 +377,13 @@ func (k Keeper) ChanOpenAck( | |
sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), | ||
), | ||
}) | ||
ctx.EventManager().EmitEvents(sdk.Events{ | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
// ChanOpenConfirm is called by the counterparty module to close their end of the | ||
|
@@ -373,6 +440,21 @@ func (k Keeper) ChanOpenConfirm( | |
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// WriteOpenConfirmChannel writes an updated channel state for the successful OpenConfirm handshake step. | ||
// An event is emitted for the handshake step. | ||
func (k Keeper) WriteOpenConfirmChannel( | ||
ctx sdk.Context, | ||
portID, | ||
channelID string, | ||
) { | ||
channel, found := k.GetChannel(ctx, portID, channelID) | ||
if !found { | ||
panic(fmt.Sprintf("could not find existing channel when updating channel state in successful ChanOpenConfirm step, channelID: %s, portID: %s", channelID, portID)) | ||
} | ||
|
||
channel.State = types.OPEN | ||
k.SetChannel(ctx, portID, channelID, channel) | ||
k.Logger(ctx).Info("channel state updated", "port-id", portID, "channel-id", channelID, "previous-state", "TRYOPEN", "new-state", "OPEN") | ||
|
@@ -391,8 +473,12 @@ func (k Keeper) ChanOpenConfirm( | |
sdk.NewAttribute(types.AttributeKeyConnectionID, channel.ConnectionHops[0]), | ||
), | ||
}) | ||
|
||
return nil | ||
ctx.EventManager().EmitEvents(sdk.Events{ | ||
sdk.NewEvent( | ||
sdk.EventTypeMessage, | ||
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory), | ||
), | ||
}) | ||
} | ||
|
||
// Closing Handshake | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applications must not rely on channel state being set before the application callback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we document this somewhere? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update the docs when I do the OnChanOpenTry change, for now it is in the migration docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. I was just thinking out loud :)