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 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: 6 additions & 5 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 @@ -173,12 +171,15 @@ func (k Keeper) GetAllRelayerAddresses(ctx sdk.Context) []types.RegisteredRelaye

var registeredAddrArr []types.RegisteredRelayerAddress
for ; iterator.Valid(); iterator.Next() {
keySplit := strings.Split(string(iterator.Key()), "/")
address, channelID, err := types.ParseKeyCounterpartyRelayer(string(iterator.Key()))
if err != nil {
panic(err)
}

addr := types.RegisteredRelayerAddress{
Address: keySplit[1],
Address: address,
CounterpartyAddress: string(iterator.Value()),
ChannelId: keySplit[2],
ChannelId: channelID,
}

registeredAddrArr = append(registeredAddrArr, addr)
Expand Down
14 changes: 13 additions & 1 deletion modules/apps/29-fee/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,23 @@ 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 and channelID used to store the counterpartyrelayer address
func ParseKeyCounterpartyRelayer(key string) (address string, channelID string, error error) {
keySplit := strings.Split(key, "/")
if len(keySplit) != 3 {
return "", "", sdkerrors.Wrapf(
sdkerrors.ErrLogic, "key provided is incorrect: the key split has incorrect length, expected %d, got %d", 3, len(keySplit),
)
}

return keySplit[1], 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
35 changes: 35 additions & 0 deletions modules/apps/29-fee/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,38 @@ func TestParseKeyForwardRelayerAddress(t *testing.T) {
}
}
}

func TestParseKeyCounterpartyRelayer(t *testing.T) {
var (
relayerAddress = "relayer_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 {
address, channelID, err := types.ParseKeyCounterpartyRelayer(tc.key)

if tc.expPass {
require.NoError(t, err)
require.Equal(t, relayerAddress, address)
require.Equal(t, ibctesting.FirstChannelID, channelID)
} else {
require.Error(t, err)
}
}
}