Skip to content

Commit

Permalink
feat: ParseKeyCounterpartyRelayer function (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleenfei authored Mar 3, 2022
1 parent b33b0a7 commit f3ee8de
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
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)
}
}
}

0 comments on commit f3ee8de

Please sign in to comment.