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

chore: add ParseKeyFeesInEscrow function #998

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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)
}
}
}