Skip to content

Commit

Permalink
add some small tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamewozniak committed Nov 10, 2023
1 parent 6d3a9b5 commit c116795
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 33 deletions.
60 changes: 27 additions & 33 deletions app/gmpmiddleware/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package gmpmiddleware

import (
"context"
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ojo-network/ojo/x/gmp/types"
Expand Down Expand Up @@ -46,25 +44,23 @@ func (h GmpHandler) HandleGeneralMessage(
"module", "x/gmp-middleware",
)

params := h.gmp.GetParams(ctx)
if !strings.EqualFold(params.GmpAddress, sender) {
return fmt.Errorf("invalid sender address: %s", sender)
err := verifyParams(h.gmp.GetParams(ctx), sender, channel)
if err != nil {
return err
}
if !strings.EqualFold(params.GmpChannel, channel) {
return fmt.Errorf("invalid channel: %s", channel)
denoms, err := parsePayload(payload)
if err != nil {
return err
}

denomString := string(payload)
denoms := strings.Split(denomString, ",")

_, err := h.gmp.RelayPrice(ctx, &types.MsgRelayPrice{
Relayer: srcAddress,
DestinationChain: srcChain,
DestinationAddress: destAddress,
Denoms: denoms,
},
_, err = h.gmp.RelayPrice(ctx,
&types.MsgRelayPrice{
Relayer: srcAddress,
DestinationChain: srcChain,
DestinationAddress: destAddress,
Denoms: denoms,
},
)

return err
}

Expand All @@ -88,25 +84,23 @@ func (h GmpHandler) HandleGeneralMessageWithToken(
"coin", coin,
)

params := h.gmp.GetParams(ctx)
if !strings.EqualFold(params.GmpAddress, sender) {
return fmt.Errorf("invalid sender address: %s", sender)
err := verifyParams(h.gmp.GetParams(ctx), sender, channel)
if err != nil {
return err
}
if !strings.EqualFold(params.GmpChannel, channel) {
return fmt.Errorf("invalid channel: %s", channel)
denoms, err := parsePayload(payload)
if err != nil {
return err
}

denomString := string(payload)
denoms := strings.Split(denomString, ",")

_, err := h.gmp.RelayPrice(ctx, &types.MsgRelayPrice{
Relayer: srcAddress,
DestinationChain: srcChain,
DestinationAddress: destAddress,
Denoms: denoms,
Token: coin,
},
_, err = h.gmp.RelayPrice(ctx,
&types.MsgRelayPrice{
Relayer: srcAddress,
DestinationChain: srcChain,
DestinationAddress: destAddress,
Denoms: denoms,
Token: coin,
},
)

return err
}
26 changes: 26 additions & 0 deletions app/gmpmiddleware/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package gmpmiddleware

import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
gmptypes "github.com/ojo-network/ojo/x/gmp/types"
)

type GeneralMessageHandler interface {
Expand Down Expand Up @@ -61,3 +65,25 @@ func parseDenom(packet channeltypes.Packet, denom string) string {

return denom
}

func parsePayload(payload []byte) ([]string, error) {
denomString := string(payload)
if len(denomString) < 1 {
return []string{}, fmt.Errorf("unable to parse payload")
}
denoms := strings.Split(denomString, ",")
if len(denoms) < 1 {
return []string{}, fmt.Errorf("unable to parse payload")
}
return denoms, nil
}

func verifyParams(params gmptypes.Params, sender string, channel string) error {
if !strings.EqualFold(params.GmpAddress, sender) {
return fmt.Errorf("invalid sender address: %s", sender)
}
if !strings.EqualFold(params.GmpChannel, channel) {
return fmt.Errorf("invalid channel: %s", channel)
}
return nil
}
51 changes: 51 additions & 0 deletions app/gmpmiddleware/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gmpmiddleware

import (
"testing"

channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/ojo-network/ojo/x/gmp/types"
"github.com/stretchr/testify/require"
)

func TestVerifyParams(t *testing.T) {
params := types.Params{
GmpAddress: "gmpAddress",
GmpChannel: "gmpChannel",
}
err := verifyParams(params, "gmpAddress", "gmpChannel")
require.NoError(t, err)

err = verifyParams(params, "notAddress", "notChannel")
require.Error(t, err)
}

func TestParsePayload(t *testing.T) {
payload := []byte(`denom1,denom2`)
denoms, err := parsePayload(payload)
require.NoError(t, err)
require.Equal(t, []string{"denom1", "denom2"}, denoms)

payload = []byte(`denom1`)
denoms, err = parsePayload(payload)
require.NoError(t, err)
require.Equal(t, []string{"denom1"}, denoms)

payload = []byte(``)
_, err = parsePayload(payload)
require.Error(t, err)
}

func TestParseDenom(t *testing.T) {
packet := channeltypes.Packet{
SourcePort: "ibc",
SourceChannel: "channel-0",
}
denom := "ibc/1D2F3A4B5C6D7E8F9A0B1C2D3E4F5A6B7C8D9E0F"
parsedDenom := parseDenom(packet, denom)
require.Equal(t, "//ibc/1D2F3A4B5C6D7E8F9A0B1C2D3E4F5A6B7C8D9E0F", parsedDenom)

denom = "ibc/1D2F3A4B5C6D7E8F9A0B1C2D3E4F5A6B7C8D9E0F/ibc/1D2F3A4B5C6D7E8F9A0B1C2D3E4F5A6B7C8D9E0F"
parsedDenom = parseDenom(packet, denom)
require.Equal(t, "//ibc/1D2F3A4B5C6D7E8F9A0B1C2D3E4F5A6B7C8D9E0F/ibc/1D2F3A4B5C6D7E8F9A0B1C2D3E4F5A6B7C8D9E0F", parsedDenom)
}

0 comments on commit c116795

Please sign in to comment.