From 478db4ffdb54de8ffe0e7257c1f5475204faff69 Mon Sep 17 00:00:00 2001 From: Charly Date: Wed, 2 Mar 2022 16:49:40 +0100 Subject: [PATCH] add ParseKeyForwardRelayerAddress function + test (#1046) --- modules/apps/29-fee/keeper/keeper.go | 41 +++++++++++--------------- modules/apps/29-fee/types/keys.go | 20 ++++++++++++- modules/apps/29-fee/types/keys_test.go | 40 +++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 26 deletions(-) diff --git a/modules/apps/29-fee/keeper/keeper.go b/modules/apps/29-fee/keeper/keeper.go index 9b7583ea055..4a3dfbdeb4a 100644 --- a/modules/apps/29-fee/keeper/keeper.go +++ b/modules/apps/29-fee/keeper/keeper.go @@ -1,7 +1,6 @@ package keeper import ( - "strconv" "strings" "github.com/cosmos/cosmos-sdk/codec" @@ -178,15 +177,15 @@ func (k Keeper) GetAllRelayerAddresses(ctx sdk.Context) []types.RegisteredRelaye } // SetRelayerAddressForAsyncAck sets the forward relayer address during OnRecvPacket in case of async acknowledgement -func (k Keeper) SetRelayerAddressForAsyncAck(ctx sdk.Context, packetId channeltypes.PacketId, address string) { +func (k Keeper) SetRelayerAddressForAsyncAck(ctx sdk.Context, packetID channeltypes.PacketId, address string) { store := ctx.KVStore(k.storeKey) - store.Set(types.KeyForwardRelayerAddress(packetId), []byte(address)) + store.Set(types.KeyForwardRelayerAddress(packetID), []byte(address)) } // GetRelayerAddressForAsyncAck gets forward relayer address for a particular packet -func (k Keeper) GetRelayerAddressForAsyncAck(ctx sdk.Context, packetId channeltypes.PacketId) (string, bool) { +func (k Keeper) GetRelayerAddressForAsyncAck(ctx sdk.Context, packetID channeltypes.PacketId) (string, bool) { store := ctx.KVStore(k.storeKey) - key := types.KeyForwardRelayerAddress(packetId) + key := types.KeyForwardRelayerAddress(packetID) if !store.Has(key) { return "", false } @@ -203,18 +202,14 @@ func (k Keeper) GetAllForwardRelayerAddresses(ctx sdk.Context) []types.ForwardRe var forwardRelayerAddr []types.ForwardRelayerAddress for ; iterator.Valid(); iterator.Next() { - keySplit := strings.Split(string(iterator.Key()), "/") - - seq, err := strconv.ParseUint(keySplit[3], 0, 64) + packetID, err := types.ParseKeyForwardRelayerAddress(string(iterator.Key())) if err != nil { - panic("failed to parse packet sequence in forward relayer address mapping") + panic(err) } - packetId := channeltypes.NewPacketId(keySplit[2], keySplit[1], seq) - addr := types.ForwardRelayerAddress{ Address: string(iterator.Value()), - PacketId: packetId, + PacketId: packetID, } forwardRelayerAddr = append(forwardRelayerAddr, addr) @@ -223,10 +218,10 @@ func (k Keeper) GetAllForwardRelayerAddresses(ctx sdk.Context) []types.ForwardRe return forwardRelayerAddr } -// Deletes the forwardRelayerAddr associated with the packetId -func (k Keeper) DeleteForwardRelayerAddress(ctx sdk.Context, packetId channeltypes.PacketId) { +// Deletes the forwardRelayerAddr associated with the packetID +func (k Keeper) DeleteForwardRelayerAddress(ctx sdk.Context, packetID channeltypes.PacketId) { store := ctx.KVStore(k.storeKey) - key := types.KeyForwardRelayerAddress(packetId) + key := types.KeyForwardRelayerAddress(packetID) store.Delete(key) } @@ -238,9 +233,9 @@ func (k Keeper) SetFeeInEscrow(ctx sdk.Context, fee types.IdentifiedPacketFee) { } // Gets a Fee for a given packet -func (k Keeper) GetFeeInEscrow(ctx sdk.Context, packetId channeltypes.PacketId) (types.IdentifiedPacketFee, bool) { +func (k Keeper) GetFeeInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) (types.IdentifiedPacketFee, bool) { store := ctx.KVStore(k.storeKey) - key := types.KeyFeeInEscrow(packetId) + key := types.KeyFeeInEscrow(packetID) bz := store.Get(key) if bz == nil { return types.IdentifiedPacketFee{}, false @@ -270,7 +265,7 @@ func (k Keeper) HasFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) return store.Has(key) } -// SetFeesInEscrow sets the given packet fees in escrow keyed by the packet identifier +// SetFeesInEscrow sets the given packet fees in escrow keyed by the packetID func (k Keeper) SetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId, fees types.PacketFees) { store := ctx.KVStore(k.storeKey) bz := k.MustMarshalFees(fees) @@ -314,17 +309,17 @@ func (k Keeper) IterateChannelFeesInEscrow(ctx sdk.Context, portID, channelID st } } -// Deletes the fee associated with the given packetId -func (k Keeper) DeleteFeeInEscrow(ctx sdk.Context, packetId channeltypes.PacketId) { +// Deletes the fee associated with the given packetID +func (k Keeper) DeleteFeeInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) { store := ctx.KVStore(k.storeKey) - key := types.KeyFeeInEscrow(packetId) + key := types.KeyFeeInEscrow(packetID) store.Delete(key) } // HasFeeInEscrow returns true if there is a Fee still to be escrowed for a given packet -func (k Keeper) HasFeeInEscrow(ctx sdk.Context, packetId channeltypes.PacketId) bool { +func (k Keeper) HasFeeInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) bool { store := ctx.KVStore(k.storeKey) - key := types.KeyFeeInEscrow(packetId) + key := types.KeyFeeInEscrow(packetID) return store.Has(key) } diff --git a/modules/apps/29-fee/types/keys.go b/modules/apps/29-fee/types/keys.go index f39cd9b5d44..e619bde8806 100644 --- a/modules/apps/29-fee/types/keys.go +++ b/modules/apps/29-fee/types/keys.go @@ -78,7 +78,25 @@ func KeyCounterpartyRelayer(address, channelID string) []byte { // KeyForwardRelayerAddress returns the key for packetID -> forwardAddress mapping func KeyForwardRelayerAddress(packetId channeltypes.PacketId) []byte { - return []byte(fmt.Sprintf("%s/%s/%s/%d/", ForwardRelayerPrefix, packetId.PortId, packetId.ChannelId, packetId.Sequence)) + return []byte(fmt.Sprintf("%s/%s/%s/%d", ForwardRelayerPrefix, packetId.PortId, packetId.ChannelId, packetId.Sequence)) +} + +// ParseKeyForwardRelayerAddress parses the key used to store the forward relayer address and returns the packetID +func ParseKeyForwardRelayerAddress(key string) (channeltypes.PacketId, error) { + keySplit := strings.Split(key, "/") + if len(keySplit) != 4 { + return channeltypes.PacketId{}, sdkerrors.Wrapf( + sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 4, len(keySplit), + ) + } + + seq, err := strconv.ParseUint(keySplit[3], 10, 64) + if err != nil { + return channeltypes.PacketId{}, err + } + + packetID := channeltypes.NewPacketId(keySplit[2], keySplit[1], seq) + return packetID, nil } // KeyFeeInEscrow returns the key for escrowed fees diff --git a/modules/apps/29-fee/types/keys_test.go b/modules/apps/29-fee/types/keys_test.go index f196f5de56b..85c6b107f5f 100644 --- a/modules/apps/29-fee/types/keys_test.go +++ b/modules/apps/29-fee/types/keys_test.go @@ -88,11 +88,47 @@ func TestParseKeyFeesInEscrow(t *testing.T) { } for _, tc := range testCases { - packetId, err := types.ParseKeyFeesInEscrow(tc.key) + packetID, err := types.ParseKeyFeesInEscrow(tc.key) if tc.expPass { require.NoError(t, err) - require.Equal(t, validPacketID, packetId) + require.Equal(t, validPacketID, packetID) + } else { + require.Error(t, err) + } + } +} + +func TestParseKeyForwardRelayerAddress(t *testing.T) { + + testCases := []struct { + name string + key string + expPass bool + }{ + { + "success", + string(types.KeyForwardRelayerAddress(validPacketID)), + true, + }, + { + "incorrect key - key split has incorrect length", + "forwardRelayer/transfer/channel-0", + false, + }, + { + "incorrect key - sequence is not correct", + "forwardRelayer/transfer/channel-0/sequence", + false, + }, + } + + for _, tc := range testCases { + packetID, err := types.ParseKeyForwardRelayerAddress(tc.key) + + if tc.expPass { + require.NoError(t, err) + require.Equal(t, validPacketID, packetID) } else { require.Error(t, err) }