Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
44 changes: 22 additions & 22 deletions modules/core/04-channel/v2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ func (k Keeper) ChannelStore(ctx context.Context, channelID string) storetypes.K
return prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), channelPrefix)
}

// SetCounterparty sets the Counterparty for a given client identifier.
func (k *Keeper) SetCounterparty(ctx context.Context, clientID string, counterparty types.Counterparty) {
bz := k.cdc.MustMarshal(&counterparty)
k.ChannelStore(ctx, clientID).Set([]byte(types.CounterpartyKey), bz)
// SetChannel sets the Channel for a given client identifier.
func (k *Keeper) SetChannel(ctx context.Context, clientID string, channel types.Channel) {
bz := k.cdc.MustMarshal(&channel)
k.ChannelStore(ctx, clientID).Set([]byte(types.ChannelKey), bz)
}

// GetCounterparty gets the Counterparty for a given client identifier.
func (k *Keeper) GetCounterparty(ctx context.Context, clientID string) (types.Counterparty, bool) {
// GetChannel gets the Channel for a given client identifier.
func (k *Keeper) GetChannel(ctx context.Context, clientID string) (types.Channel, bool) {
store := k.ChannelStore(ctx, clientID)
bz := store.Get([]byte(types.CounterpartyKey))
bz := store.Get([]byte(types.ChannelKey))
if len(bz) == 0 {
return types.Counterparty{}, false
return types.Channel{}, false
}

var counterparty types.Counterparty
k.cdc.MustUnmarshal(bz, &counterparty)
return counterparty, true
var channel types.Channel
k.cdc.MustUnmarshal(bz, &channel)
return channel, true
}

// GetPacketReceipt returns the packet receipt from the packet receipt path based on the sourceID and sequence.
Expand Down Expand Up @@ -180,38 +180,38 @@ func (k *Keeper) SetNextSequenceSend(ctx context.Context, sourceID string, seque

// AliasV1Channel returns a version 2 channel for the given port and channel ID
// by converting the channel into a version 2 channel.
func (k *Keeper) AliasV1Channel(ctx context.Context, portID, channelID string) (types.Counterparty, bool) {
func (k *Keeper) AliasV1Channel(ctx context.Context, portID, channelID string) (types.Channel, bool) {
channel, ok := k.channelKeeperV1.GetChannel(ctx, portID, channelID)
if !ok {
return types.Counterparty{}, false
return types.Channel{}, false
}
// Do not allow channel to be converted into a version 2 counterparty
// Do not allow channel to be converted into a version 2 channel
// if the channel is not OPEN or if it is ORDERED
if channel.State != channeltypesv1.OPEN || channel.Ordering == channeltypesv1.ORDERED {
return types.Counterparty{}, false
return types.Channel{}, false
}
connection, ok := k.connectionKeeper.GetConnection(ctx, channel.ConnectionHops[0])
if !ok {
return types.Counterparty{}, false
return types.Channel{}, false
}
merklePathPrefix := commitmentv2types.NewMerklePath(connection.Counterparty.Prefix.KeyPrefix, []byte(""))

counterparty := types.Counterparty{
channelv2 := types.Channel{
CounterpartyChannelId: channel.Counterparty.ChannelId,
ClientId: connection.ClientId,
MerklePathPrefix: merklePathPrefix,
}
return counterparty, true
return channelv2, true
}

// getV1Counterparty attempts to retrieve a v1 channel from the channel keeper if it exists, then converts it
// convertV1Channel attempts to retrieve a v1 channel from the channel keeper if it exists, then converts it
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

convertV1Channel makes way more sense as a name than getV1Channel imo given what the function does

// to a v2 counterparty and stores it in the v2 channel keeper for future use
func (k *Keeper) getV1Counterparty(ctx context.Context, port, id string) (types.Counterparty, bool) {
func (k *Keeper) convertV1Channel(ctx context.Context, port, id string) (types.Channel, bool) {
if counterparty, ok := k.AliasV1Channel(ctx, port, id); ok {
// we can key on just the channel here since channel ids are globally unique
k.SetCounterparty(ctx, id, counterparty)
k.SetChannel(ctx, id, counterparty)
return counterparty, true
}

return types.Counterparty{}, false
return types.Channel{}, false
}
8 changes: 4 additions & 4 deletions modules/core/04-channel/v2/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ func (suite *KeeperTestSuite) TestAliasV1Channel() {

tc.malleate()

counterparty, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeperV2.AliasV1Channel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
channel, found := suite.chainA.GetSimApp().IBCKeeper.ChannelKeeperV2.AliasV1Channel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)

if tc.expPass {
suite.Require().True(found)

merklePath := commitmentv2types.NewMerklePath([]byte("ibc"), []byte(""))
expCounterparty := channeltypes2.NewCounterparty(path.EndpointA.ClientID, path.EndpointB.ChannelID, merklePath)
suite.Require().Equal(counterparty, expCounterparty)
expChannel := channeltypes2.NewChannel(path.EndpointA.ClientID, path.EndpointB.ChannelID, merklePath)
suite.Require().Equal(channel, expChannel)
} else {
suite.Require().False(found)
suite.Require().Equal(counterparty, channeltypes2.Counterparty{})
suite.Require().Equal(channel, channeltypes2.Channel{})
}
})
}
Expand Down
31 changes: 16 additions & 15 deletions modules/core/04-channel/v2/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ func (k *Keeper) sendPacket(
timeoutTimestamp uint64,
data []channeltypesv2.PacketData,
) (uint64, error) {
// Lookup counterparty associated with our source channel to retrieve the destination channel
counterparty, ok := k.GetCounterparty(ctx, sourceID)
// Lookup channel associated with our source channel to retrieve the destination channel
channel, ok := k.GetChannel(ctx, sourceID)
if !ok {
// TODO: figure out how aliasing will work when more than one packet data is sent.
counterparty, ok = k.getV1Counterparty(ctx, data[0].SourcePort, sourceID)
channel, ok = k.convertV1Channel(ctx, data[0].SourcePort, sourceID)
if !ok {
return 0, errorsmod.Wrap(types.ErrChannelNotFound, sourceID)
}
}

destID := counterparty.CounterpartyChannelId
clientID := counterparty.ClientId
destID := channel.CounterpartyChannelId
clientID := channel.ClientId

// retrieve the sequence send for this channel
// if no packets have been sent yet, initialize the sequence to 1.
Expand Down Expand Up @@ -100,17 +100,18 @@ func (k *Keeper) recvPacket(
proof []byte,
proofHeight exported.Height,
) error {
// Lookup counterparty associated with our channel and ensure
// that the packet was indeed sent by our counterparty.
counterparty, ok := k.GetCounterparty(ctx, packet.DestinationChannel)
// Lookup channel associated with destination channel ID and ensure
// that the packet was indeed sent by our counterparty by verifying
// packet sender is our channel's counterparty channel id.
channel, ok := k.GetChannel(ctx, packet.DestinationChannel)
if !ok {
// TODO: figure out how aliasing will work when more than one packet data is sent.
counterparty, ok = k.getV1Counterparty(ctx, packet.Data[0].DestinationPort, packet.DestinationChannel)
channel, ok = k.convertV1Channel(ctx, packet.Data[0].DestinationPort, packet.DestinationChannel)
if !ok {
return errorsmod.Wrap(types.ErrChannelNotFound, packet.DestinationChannel)
}
}
if counterparty.ClientId != packet.SourceChannel {
if channel.ClientId != packet.SourceChannel {
return channeltypes.ErrInvalidChannelIdentifier
}

Expand All @@ -134,7 +135,7 @@ func (k *Keeper) recvPacket(
}

path := hostv2.PacketCommitmentKey(packet.SourceChannel, packet.Sequence)
merklePath := types.BuildMerklePath(counterparty.MerklePathPrefix, path)
merklePath := types.BuildMerklePath(channel.MerklePathPrefix, path)

commitment := channeltypesv2.CommitPacket(packet)

Expand Down Expand Up @@ -163,7 +164,7 @@ func (k *Keeper) recvPacket(
func (k *Keeper) acknowledgePacket(ctx context.Context, packet channeltypesv2.Packet, acknowledgement channeltypesv2.Acknowledgement, proof []byte, proofHeight exported.Height) error {
// Lookup counterparty associated with our channel and ensure
// that the packet was indeed sent by our counterparty.
counterparty, ok := k.GetCounterparty(ctx, packet.SourceChannel)
counterparty, ok := k.GetChannel(ctx, packet.SourceChannel)
if !ok {
return errorsmod.Wrap(types.ErrChannelNotFound, packet.SourceChannel)
}
Expand Down Expand Up @@ -231,10 +232,10 @@ func (k *Keeper) timeoutPacket(
) error {
// Lookup counterparty associated with our channel and ensure
// that the packet was indeed sent by our counterparty.
counterparty, ok := k.GetCounterparty(ctx, packet.SourceChannel)
channel, ok := k.GetChannel(ctx, packet.SourceChannel)
if !ok {
// TODO: figure out how aliasing will work when more than one packet data is sent.
counterparty, ok = k.getV1Counterparty(ctx, packet.Data[0].SourcePort, packet.SourceChannel)
channel, ok = k.convertV1Channel(ctx, packet.Data[0].SourcePort, packet.SourceChannel)
if !ok {
return errorsmod.Wrap(types.ErrChannelNotFound, packet.DestinationChannel)
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func (k *Keeper) timeoutPacket(

// verify packet receipt absence
path := hostv2.PacketReceiptKey(packet.SourceChannel, packet.Sequence)
merklePath := types.BuildMerklePath(counterparty.MerklePathPrefix, path)
merklePath := types.BuildMerklePath(channel.MerklePathPrefix, path)

if err := k.ClientKeeper.VerifyNonMembership(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

// NewCounterparty creates a new Counterparty instance
func NewCounterparty(clientID, counterpartyChannelID string, merklePathPrefix commitmenttypes.MerklePath) Counterparty {
return Counterparty{
// NewChannel creates a new Channel instance
func NewChannel(clientID, counterpartyChannelID string, merklePathPrefix commitmenttypes.MerklePath) Channel {
return Channel{
ClientId: clientID,
CounterpartyChannelId: counterpartyChannelID,
MerklePathPrefix: merklePathPrefix,
}
}

// Validate validates the Counterparty
func (c Counterparty) Validate() error {
// Validate validates the Channel
func (c Channel) Validate() error {
if err := host.ClientIdentifierValidator(c.ClientId); err != nil {
return err
}
Expand All @@ -27,7 +27,7 @@ func (c Counterparty) Validate() error {
}

if err := c.MerklePathPrefix.ValidateAsPrefix(); err != nil {
return errorsmod.Wrap(ErrInvalidCounterparty, err.Error())
return errorsmod.Wrap(ErrInvalidChannel, err.Error())
}

return nil
Expand Down
Loading