From c1167956a48802d1803a3f3c61e412761ebd288e Mon Sep 17 00:00:00 2001 From: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com> Date: Thu, 9 Nov 2023 17:37:31 -0800 Subject: [PATCH] add some small tests --- app/gmpmiddleware/handler.go | 60 +++++++++++++++------------------ app/gmpmiddleware/types.go | 26 ++++++++++++++ app/gmpmiddleware/types_test.go | 51 ++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 33 deletions(-) create mode 100644 app/gmpmiddleware/types_test.go diff --git a/app/gmpmiddleware/handler.go b/app/gmpmiddleware/handler.go index 41d2d841..05a058eb 100644 --- a/app/gmpmiddleware/handler.go +++ b/app/gmpmiddleware/handler.go @@ -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" @@ -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 } @@ -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 } diff --git a/app/gmpmiddleware/types.go b/app/gmpmiddleware/types.go index 70ae8892..a89cbe6d 100644 --- a/app/gmpmiddleware/types.go +++ b/app/gmpmiddleware/types.go @@ -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 { @@ -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 +} diff --git a/app/gmpmiddleware/types_test.go b/app/gmpmiddleware/types_test.go new file mode 100644 index 00000000..5c90c49b --- /dev/null +++ b/app/gmpmiddleware/types_test.go @@ -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) +}