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

reorganize channel handshake handler #647

Merged
merged 7 commits into from
Dec 21, 2021
124 changes: 105 additions & 19 deletions modules/core/04-channel/keeper/handshake.go
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"
Expand Down Expand Up @@ -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)
Expand All @@ -82,7 +100,12 @@ func (k Keeper) ChanOpenInit(
),
})

return channelID, capKey, nil
ctx.EventManager().EmitEvents(sdk.Events{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

someone can do a followup pr to add a function EmitOpenInitEvents to the 04-channel/keeper/events.go file for each handshake step

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
Expand Down Expand Up @@ -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,
)

Expand All @@ -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))
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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 ChanOpenAck step, channelID: %s, portID: %s", channelID, portID))
colin-axner marked this conversation as resolved.
Show resolved Hide resolved
}

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")
Expand All @@ -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
Expand Down
Loading