Skip to content

Commit 5f8fc9f

Browse files
authored
feat: Add ParseKeyFeeEnabled and rename FeeEnabledKey -> KeyFeeEnabled (#1023)
* chore: add ParseKeyFeesInEscrow helper function * feat: add ParseKeyFeeEnabled function and rename FeeEnabledKey to KeyFeeEnabled
1 parent e51e2c9 commit 5f8fc9f

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

modules/apps/29-fee/keeper/keeper.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ func (k Keeper) GetFeeModuleAddress() sdk.AccAddress {
8181
// identified by channel and port identifiers.
8282
func (k Keeper) SetFeeEnabled(ctx sdk.Context, portID, channelID string) {
8383
store := ctx.KVStore(k.storeKey)
84-
store.Set(types.FeeEnabledKey(portID, channelID), []byte{1})
84+
store.Set(types.KeyFeeEnabled(portID, channelID), []byte{1})
8585
}
8686

8787
// DeleteFeeEnabled deletes the fee enabled flag for a given portID and channelID
8888
func (k Keeper) DeleteFeeEnabled(ctx sdk.Context, portID, channelID string) {
8989
store := ctx.KVStore(k.storeKey)
90-
store.Delete(types.FeeEnabledKey(portID, channelID))
90+
store.Delete(types.KeyFeeEnabled(portID, channelID))
9191
}
9292

9393
// IsFeeEnabled returns whether fee handling logic should be run for the given port. It will check the
9494
// fee enabled flag for the given port and channel identifiers
9595
func (k Keeper) IsFeeEnabled(ctx sdk.Context, portID, channelID string) bool {
9696
store := ctx.KVStore(k.storeKey)
97-
return store.Get(types.FeeEnabledKey(portID, channelID)) != nil
97+
return store.Get(types.KeyFeeEnabled(portID, channelID)) != nil
9898
}
9999

100100
// GetAllFeeEnabledChannels returns a list of all ics29 enabled channels containing portID & channelID that are stored in state
@@ -105,11 +105,13 @@ func (k Keeper) GetAllFeeEnabledChannels(ctx sdk.Context) []types.FeeEnabledChan
105105

106106
var enabledChArr []types.FeeEnabledChannel
107107
for ; iterator.Valid(); iterator.Next() {
108-
keySplit := strings.Split(string(iterator.Key()), "/")
109-
108+
portID, channelID, err := types.ParseKeyFeeEnabled(string(iterator.Key()))
109+
if err != nil {
110+
panic(err)
111+
}
110112
ch := types.FeeEnabledChannel{
111-
PortId: keySplit[1],
112-
ChannelId: keySplit[2],
113+
PortId: portID,
114+
ChannelId: channelID,
113115
}
114116

115117
enabledChArr = append(enabledChArr, ch)

modules/apps/29-fee/types/keys.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,32 @@ const (
4545
AttributeKeyTimeoutFee = "timeout_fee"
4646
)
4747

48-
// FeeEnabledKey returns the key that stores a flag to determine if fee logic should
48+
// KeyFeeEnabled returns the key that stores a flag to determine if fee logic should
4949
// be enabled for the given port and channel identifiers.
50-
func FeeEnabledKey(portID, channelID string) []byte {
50+
func KeyFeeEnabled(portID, channelID string) []byte {
5151
return []byte(fmt.Sprintf("%s/%s/%s", FeeEnabledKeyPrefix, portID, channelID))
5252
}
5353

54+
// ParseKeyFeeEnabled parses the key used to indicate if the fee logic should be
55+
// enabled for the given port and channel identifiers.
56+
func ParseKeyFeeEnabled(key string) (portID, channelID string, err error) {
57+
keySplit := strings.Split(key, "/")
58+
if len(keySplit) != 3 {
59+
return "", "", sdkerrors.Wrapf(
60+
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 3, len(keySplit),
61+
)
62+
}
63+
64+
if keySplit[0] != FeeEnabledKeyPrefix {
65+
return "", "", sdkerrors.Wrapf(sdkerrors.ErrLogic, "key prefix is incorrect: expected %s, got %s", FeeEnabledKeyPrefix, keySplit[0])
66+
}
67+
68+
portID = keySplit[1]
69+
channelID = keySplit[2]
70+
71+
return portID, channelID, nil
72+
}
73+
5474
// KeyCounterpartyRelayer returns the key for relayer address -> counteryparty address mapping
5575
func KeyCounterpartyRelayer(address, channelID string) []byte {
5676
return []byte(fmt.Sprintf("%s/%s/%s", CounterpartyRelayerAddressKeyPrefix, address, channelID))

modules/apps/29-fee/types/keys_test.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
ibctesting "github.com/cosmos/ibc-go/v3/testing"
1212
)
1313

14+
var (
15+
validPacketID = channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1)
16+
)
17+
1418
func TestKeyCounterpartyRelayer(t *testing.T) {
1519
var (
1620
relayerAddress = "relayer_address"
@@ -21,8 +25,45 @@ func TestKeyCounterpartyRelayer(t *testing.T) {
2125
require.Equal(t, string(key), fmt.Sprintf("%s/%s/%s", types.CounterpartyRelayerAddressKeyPrefix, relayerAddress, channelID))
2226
}
2327

28+
func TestParseKeyFeeEnabled(t *testing.T) {
29+
testCases := []struct {
30+
name string
31+
key string
32+
expPass bool
33+
}{
34+
{
35+
"success",
36+
string(types.KeyFeeEnabled(ibctesting.MockPort, ibctesting.FirstChannelID)),
37+
true,
38+
},
39+
{
40+
"incorrect key - key split has incorrect length",
41+
string(types.KeyFeesInEscrow(validPacketID)),
42+
false,
43+
},
44+
{
45+
"incorrect key - key split has incorrect length",
46+
fmt.Sprintf("%s/%s/%s", "fee", ibctesting.MockPort, ibctesting.FirstChannelID),
47+
false,
48+
},
49+
}
50+
51+
for _, tc := range testCases {
52+
portID, channelID, err := types.ParseKeyFeeEnabled(tc.key)
53+
54+
if tc.expPass {
55+
require.NoError(t, err)
56+
require.Equal(t, ibctesting.MockPort, portID)
57+
require.Equal(t, ibctesting.FirstChannelID, channelID)
58+
} else {
59+
require.Error(t, err)
60+
require.Empty(t, portID)
61+
require.Empty(t, channelID)
62+
}
63+
}
64+
}
65+
2466
func TestParseKeyFeesInEscrow(t *testing.T) {
25-
validPacketID := channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1)
2667

2768
testCases := []struct {
2869
name string
@@ -36,7 +77,7 @@ func TestParseKeyFeesInEscrow(t *testing.T) {
3677
},
3778
{
3879
"incorrect key - key split has incorrect length",
39-
string(types.FeeEnabledKey(validPacketID.PortId, validPacketID.ChannelId)),
80+
string(types.KeyFeeEnabled(validPacketID.PortId, validPacketID.ChannelId)),
4081
false,
4182
},
4283
{

0 commit comments

Comments
 (0)