Skip to content

Commit

Permalink
add ParseKeyForwardRelayerAddress function + test (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleenfei authored Mar 2, 2022
1 parent d788adf commit 478db4f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
41 changes: 18 additions & 23 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 19 additions & 1 deletion modules/apps/29-fee/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 38 additions & 2 deletions modules/apps/29-fee/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 478db4f

Please sign in to comment.