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

add ParseKeyForwardRelayerAddress function + test #1046

Merged
merged 6 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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
18 changes: 17 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,7 @@ 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))
}

// KeyFeeInEscrow returns the key for escrowed fees
Expand Down Expand Up @@ -109,6 +109,22 @@ func ParseKeyFeesInEscrow(key string) (channeltypes.PacketId, error) {
return packetID, nil
}

// ParseKeyForwardRelayerAddress parses the key used to store the forward relayer address and returns the packetID
func ParseKeyForwardRelayerAddress(key string) (channeltypes.PacketId, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might make sense to put this function underneath KeyForwardRealyerAddress (or at least that's how I was organizing the code). Open to other suggestions

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)
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
return packetID, nil
}

// KeyFeeInEscrowChannelPrefix returns the key prefix for escrowed fees on the given channel
func KeyFeeInEscrowChannelPrefix(portID, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s/packet", FeeInEscrowPrefix, portID, channelID))
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