Skip to content

Commit

Permalink
chore: add ParseKeyFeesInEscrow helper function (#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner authored Feb 24, 2022
1 parent 9350d53 commit f1ba06f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
11 changes: 4 additions & 7 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,18 +335,15 @@ func (k Keeper) GetAllIdentifiedPacketFees(ctx sdk.Context) []types.IdentifiedPa

var identifiedFees []types.IdentifiedPacketFees
for ; iterator.Valid(); iterator.Next() {
keySplit := strings.Split(string(iterator.Key()), "/")

feesInEscrow := k.MustUnmarshalFees(iterator.Value())

channelID, portID := keySplit[2], keySplit[1]
seq, err := strconv.ParseUint(keySplit[3], 10, 64)
packetID, err := types.ParseKeyFeesInEscrow(string(iterator.Key()))
if err != nil {
panic(err)
}

feesInEscrow := k.MustUnmarshalFees(iterator.Value())

identifiedFee := types.IdentifiedPacketFees{
PacketId: channeltypes.NewPacketId(channelID, portID, seq),
PacketId: packetID,
PacketFees: feesInEscrow.PacketFees,
}

Expand Down
22 changes: 22 additions & 0 deletions modules/apps/29-fee/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package types

import (
"fmt"
"strconv"
"strings"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
)
Expand Down Expand Up @@ -67,6 +71,24 @@ func KeyFeesInEscrow(packetID channeltypes.PacketId) []byte {
return []byte(fmt.Sprintf("%s/%d", KeyFeesInEscrowChannelPrefix(packetID.PortId, packetID.ChannelId), packetID.Sequence))
}

// ParseKeyFeesInEscrow parses the key used to store fees in escrow and returns the packet id
func ParseKeyFeesInEscrow(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
}

// 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
39 changes: 39 additions & 0 deletions modules/apps/29-fee/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
)

func TestKeyCounterpartyRelayer(t *testing.T) {
Expand All @@ -18,3 +20,40 @@ func TestKeyCounterpartyRelayer(t *testing.T) {
key := types.KeyCounterpartyRelayer(relayerAddress, channelID)
require.Equal(t, string(key), fmt.Sprintf("%s/%s/%s", types.CounterpartyRelayerAddressKeyPrefix, relayerAddress, channelID))
}

func TestParseKeyFeesInEscrow(t *testing.T) {
validPacketID := channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1)

testCases := []struct {
name string
key string
expPass bool
}{
{
"success",
string(types.KeyFeesInEscrow(validPacketID)),
true,
},
{
"incorrect key - key split has incorrect length",
string(types.FeeEnabledKey(validPacketID.PortId, validPacketID.ChannelId)),
false,
},
{
"incorrect key - sequence cannot be parsed",
fmt.Sprintf("%s/%s", types.KeyFeesInEscrowChannelPrefix(validPacketID.PortId, validPacketID.ChannelId), "sequence"),
false,
},
}

for _, tc := range testCases {
packetId, err := types.ParseKeyFeesInEscrow(tc.key)

if tc.expPass {
require.NoError(t, err)
require.Equal(t, validPacketID, packetId)
} else {
require.Error(t, err)
}
}
}

0 comments on commit f1ba06f

Please sign in to comment.