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

feat: ParseKeyCounterpartyRelayer function #1047

Merged
merged 5 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 4 additions & 9 deletions modules/apps/29-fee/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper

import (
"strings"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
Expand Down Expand Up @@ -162,12 +160,9 @@ func (k Keeper) GetAllRelayerAddresses(ctx sdk.Context) []types.RegisteredRelaye

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

addr := types.RegisteredRelayerAddress{
Address: keySplit[1],
CounterpartyAddress: string(iterator.Value()),
ChannelId: keySplit[2],
addr, err := types.ParseKeyCounterpartyRelayer(string(iterator.Key()), string(iterator.Value()))
Copy link
Member

Choose a reason for hiding this comment

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

Let's call this registeredArr. I expect a variable like addr being a byte array or string not a struct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'll just have the function return the address and channel-id values from the key

if err != nil {
panic(err)
}

registeredAddrArr = append(registeredAddrArr, addr)
Expand Down Expand Up @@ -265,7 +260,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 packetID
// 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
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 @@ -71,11 +71,27 @@ func ParseKeyFeeEnabled(key string) (portID, channelID string, err error) {
return portID, channelID, nil
}

// KeyCounterpartyRelayer returns the key for relayer address -> counteryparty address mapping
// KeyCounterpartyRelayer returns the key for relayer address -> counterparty address mapping
func KeyCounterpartyRelayer(address, channelID string) []byte {
return []byte(fmt.Sprintf("%s/%s/%s", CounterpartyRelayerAddressKeyPrefix, address, channelID))
}

// ParseKeyCounterpartyRelayer returns the registered relayer address -> counterparty address mapping
func ParseKeyCounterpartyRelayer(key string, counterpartyAddress string) (RegisteredRelayerAddress, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this function should just parse the key. It should be up to the caller to construct RegisteredRelayerAddress

Copy link
Member

Choose a reason for hiding this comment

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

Agreed

keySplit := strings.Split(key, "/")
if len(keySplit) != 3 {
return RegisteredRelayerAddress{}, sdkerrors.Wrapf(
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 3, len(keySplit),
)
}

return RegisteredRelayerAddress{
Address: keySplit[1],
CounterpartyAddress: counterpartyAddress,
ChannelId: keySplit[2],
}, nil
}

// 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))
Expand Down
36 changes: 36 additions & 0 deletions modules/apps/29-fee/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,39 @@ func TestParseKeyForwardRelayerAddress(t *testing.T) {
}
}
}

func TestParseKeyCounterpartyRelayer(t *testing.T) {
var (
relayerAddress = "relayer_address"
counterpartyRelayerAddress = "counterparty_address"
)

testCases := []struct {
name string
key string
expPass bool
}{
{
"success",
string(types.KeyCounterpartyRelayer(relayerAddress, ibctesting.FirstChannelID)),
true,
},
{
"incorrect key - key split has incorrect length",
"relayerAddress/relayer_address/transfer/channel-0",
false,
},
}

for _, tc := range testCases {
registeredAddr, err := types.ParseKeyCounterpartyRelayer(tc.key, counterpartyRelayerAddress)
validRegisteredAddr := types.RegisteredRelayerAddress{relayerAddress, counterpartyRelayerAddress, ibctesting.FirstChannelID}
Copy link
Contributor

Choose a reason for hiding this comment

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

This could probably be removed now and you could do the require checks using the channelID and relayerAddress directly


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