From 7cd0ceec88cf9f90c464cd33e827fd444ac711b1 Mon Sep 17 00:00:00 2001 From: bznein Date: Mon, 7 Oct 2024 15:52:21 +0100 Subject: [PATCH 1/4] chore: create helper function for counterparty falllback. --- modules/core/04-channel/v2/keeper/relay.go | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/modules/core/04-channel/v2/keeper/relay.go b/modules/core/04-channel/v2/keeper/relay.go index 6a9910b2c60..1eff9bc466e 100644 --- a/modules/core/04-channel/v2/keeper/relay.go +++ b/modules/core/04-channel/v2/keeper/relay.go @@ -15,6 +15,18 @@ import ( "github.com/cosmos/ibc-go/v9/modules/core/packet-server/types" ) +// getV1Counterparty attempts to retrieve a v1 channel from the channel keeper if it exists, then converts it +// to a v2 counterparty and stores it in the packet server keeper for future use +func (k *Keeper) getV1Counterparty(ctx context.Context, sourcePort, sourceID string) (channeltypesv2.Counterparty, bool) { + if counterparty, ok := k.AliasV1Channel(ctx, sourcePort, sourceID); ok { + // we can key on just the source channel here since channel ids are globally unique + k.SetCounterparty(ctx, sourceID, counterparty) + return counterparty, true + } + + return channeltypesv2.Counterparty{}, false +} + // sendPacket constructs a packet from the input arguments, writes a packet commitment to state // in order for the packet to be sent to the counterparty. func (k *Keeper) sendPacket( @@ -26,15 +38,9 @@ func (k *Keeper) sendPacket( // Lookup counterparty associated with our source channel to retrieve the destination channel counterparty, ok := k.GetCounterparty(ctx, sourceID) if !ok { - // If the counterparty is not found, attempt to retrieve a v1 channel from the channel keeper - // if it exists, then we will convert it to a v2 counterparty and store it in the packet server keeper - // for future use. // TODO: figure out how aliasing will work when more than one packet data is sent. - if counterparty, ok = k.AliasV1Channel(ctx, data[0].SourcePort, sourceID); ok { - // we can key on just the source channel here since channel ids are globally unique - k.SetCounterparty(ctx, sourceID, counterparty) - } else { - // if neither a counterparty nor channel is found then simply return an error + counterparty, ok = k.getV1Counterparty(ctx, data[0].SourcePort, sourceID) + if !ok { return 0, errorsmod.Wrap(types.ErrCounterpartyNotFound, sourceID) } } @@ -107,15 +113,9 @@ func (k Keeper) recvPacket( // that the packet was indeed sent by our counterparty. counterparty, ok := k.GetCounterparty(ctx, packet.DestinationId) if !ok { - // If the counterparty is not found, attempt to retrieve a v1 channel from the channel keeper - // if it exists, then we will convert it to a v2 counterparty and store it in the packet server keeper - // for future use. // TODO: figure out how aliasing will work when more than one packet data is sent. - if counterparty, ok = k.AliasV1Channel(ctx, packet.Data[0].SourcePort, packet.SourceId); ok { - // we can key on just the source channel here since channel ids are globally unique - k.SetCounterparty(ctx, packet.SourceId, counterparty) - } else { - // if neither a counterparty nor channel is found then simply return an error + counterparty, ok = k.getV1Counterparty(ctx, packet.Data[0].SourcePort, packet.SourceId) + if !ok { return errorsmod.Wrap(types.ErrCounterpartyNotFound, packet.SourceId) } } From c62a5fba859c94af49779c02e055037d405b258c Mon Sep 17 00:00:00 2001 From: bznein Date: Mon, 7 Oct 2024 16:02:06 +0100 Subject: [PATCH 2/4] Fix source/dest ID --- modules/core/04-channel/v2/keeper/relay.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/core/04-channel/v2/keeper/relay.go b/modules/core/04-channel/v2/keeper/relay.go index 1eff9bc466e..304146fe164 100644 --- a/modules/core/04-channel/v2/keeper/relay.go +++ b/modules/core/04-channel/v2/keeper/relay.go @@ -17,10 +17,10 @@ import ( // getV1Counterparty attempts to retrieve a v1 channel from the channel keeper if it exists, then converts it // to a v2 counterparty and stores it in the packet server keeper for future use -func (k *Keeper) getV1Counterparty(ctx context.Context, sourcePort, sourceID string) (channeltypesv2.Counterparty, bool) { - if counterparty, ok := k.AliasV1Channel(ctx, sourcePort, sourceID); ok { +func (k *Keeper) getV1Counterparty(ctx context.Context, port, id string) (channeltypesv2.Counterparty, bool) { + if counterparty, ok := k.AliasV1Channel(ctx, port, id); ok { // we can key on just the source channel here since channel ids are globally unique - k.SetCounterparty(ctx, sourceID, counterparty) + k.SetCounterparty(ctx, id, counterparty) return counterparty, true } @@ -114,9 +114,9 @@ func (k Keeper) recvPacket( counterparty, ok := k.GetCounterparty(ctx, packet.DestinationId) 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.SourceId) + counterparty, ok = k.getV1Counterparty(ctx, packet.Data[0].DestinationPort, packet.DestinationId) if !ok { - return errorsmod.Wrap(types.ErrCounterpartyNotFound, packet.SourceId) + return errorsmod.Wrap(types.ErrCounterpartyNotFound, packet.DestinationId) } } if counterparty.ClientId != packet.SourceId { From eb0429e747cf8d456ef410ceb50757e633b9280f Mon Sep 17 00:00:00 2001 From: Nikolas De Giorgis Date: Mon, 7 Oct 2024 16:06:08 +0100 Subject: [PATCH 3/4] Update modules/core/04-channel/v2/keeper/relay.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- modules/core/04-channel/v2/keeper/relay.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/04-channel/v2/keeper/relay.go b/modules/core/04-channel/v2/keeper/relay.go index 304146fe164..e67e16fbb63 100644 --- a/modules/core/04-channel/v2/keeper/relay.go +++ b/modules/core/04-channel/v2/keeper/relay.go @@ -16,7 +16,7 @@ import ( ) // getV1Counterparty attempts to retrieve a v1 channel from the channel keeper if it exists, then converts it -// to a v2 counterparty and stores it in the packet server keeper for future use +// 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) (channeltypesv2.Counterparty, bool) { if counterparty, ok := k.AliasV1Channel(ctx, port, id); ok { // we can key on just the source channel here since channel ids are globally unique From 199db30540e8f28a39d282374b3b13f75c6ebfc2 Mon Sep 17 00:00:00 2001 From: Nikolas De Giorgis Date: Mon, 7 Oct 2024 16:06:14 +0100 Subject: [PATCH 4/4] Update modules/core/04-channel/v2/keeper/relay.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- modules/core/04-channel/v2/keeper/relay.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/core/04-channel/v2/keeper/relay.go b/modules/core/04-channel/v2/keeper/relay.go index e67e16fbb63..57b3542c6b5 100644 --- a/modules/core/04-channel/v2/keeper/relay.go +++ b/modules/core/04-channel/v2/keeper/relay.go @@ -19,7 +19,7 @@ import ( // 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) (channeltypesv2.Counterparty, bool) { if counterparty, ok := k.AliasV1Channel(ctx, port, id); ok { - // we can key on just the source channel here since channel ids are globally unique + // we can key on just the channel here since channel ids are globally unique k.SetCounterparty(ctx, id, counterparty) return counterparty, true }