From 2ca4787976767e574cc297e0e9846f5ef3101e61 Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Wed, 11 Sep 2024 10:21:28 -0500 Subject: [PATCH] Start PFM Unit Testing --- wormchain/testutil/keeper/wormhole.go | 6 ++ .../keeper/genesis_test.go | 44 ++++++++++ .../types/gateway_payload_test.go | 85 +++++++++++++++++++ .../types/ibc_hooks_test.go | 85 +++++++++++++++++++ .../x/ibc-composability-mw/types/pfm_test.go | 7 ++ 5 files changed, 227 insertions(+) create mode 100644 wormchain/x/ibc-composability-mw/keeper/genesis_test.go create mode 100644 wormchain/x/ibc-composability-mw/types/gateway_payload_test.go create mode 100644 wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go create mode 100644 wormchain/x/ibc-composability-mw/types/pfm_test.go diff --git a/wormchain/testutil/keeper/wormhole.go b/wormchain/testutil/keeper/wormhole.go index 134b819871..0bf7184f83 100644 --- a/wormchain/testutil/keeper/wormhole.go +++ b/wormchain/testutil/keeper/wormhole.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/wormhole-foundation/wormchain/app" "github.com/wormhole-foundation/wormchain/app/apptesting" + ibccomposabilitymw "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/keeper" wormholekeeper "github.com/wormhole-foundation/wormchain/x/wormhole/keeper" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" @@ -19,6 +20,11 @@ func WormholeKeeper(t *testing.T) (*wormholekeeper.Keeper, sdk.Context) { return &app.WormholeKeeper, ctx } +func WormholeKeeperAndIBCComposabilityMwKeeper(t *testing.T) (*wormholekeeper.Keeper, *ibccomposabilitymw.Keeper, sdk.Context) { + app, ctx := SetupWormchainAndContext(t) + return &app.WormholeKeeper, app.IbcComposabilityMwKeeper, ctx +} + func WormholeKeeperAndWasmd(t *testing.T) (*wormholekeeper.Keeper, *wasmkeeper.Keeper, *wasmkeeper.PermissionedKeeper, sdk.Context) { app, ctx := SetupWormchainAndContext(t) diff --git a/wormchain/x/ibc-composability-mw/keeper/genesis_test.go b/wormchain/x/ibc-composability-mw/keeper/genesis_test.go new file mode 100644 index 0000000000..d9f3df0d32 --- /dev/null +++ b/wormchain/x/ibc-composability-mw/keeper/genesis_test.go @@ -0,0 +1,44 @@ +package keeper_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + keepertest "github.com/wormhole-foundation/wormchain/testutil/keeper" + "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types" +) + +// TestGenesis ensures genesis state can be initialiazed and exported correctly +func TestGenesis(t *testing.T) { + for _, tc := range []struct { + dataInFlight map[string][]byte + }{ + { + dataInFlight: map[string][]byte{}, + }, + { + dataInFlight: map[string][]byte{ + "key1": []byte("value1"), + }, + }, + { + dataInFlight: map[string][]byte{ + "key1": []byte("value1"), + "key2": []byte("value2"), + "key3": []byte("value3"), + }, + }, + } { + genesisState := types.GenesisState{ + TransposedDataInFlight: tc.dataInFlight, + } + + _, keeper, ctx := keepertest.WormholeKeeperAndIBCComposabilityMwKeeper(t) + + keeper.InitGenesis(ctx, genesisState) + + outputState := keeper.ExportGenesis(ctx) + + require.Equal(t, genesisState, *outputState) + } +} diff --git a/wormchain/x/ibc-composability-mw/types/gateway_payload_test.go b/wormchain/x/ibc-composability-mw/types/gateway_payload_test.go new file mode 100644 index 0000000000..5321ccfe45 --- /dev/null +++ b/wormchain/x/ibc-composability-mw/types/gateway_payload_test.go @@ -0,0 +1,85 @@ +package types_test + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" + "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types" +) + +func TestGatewayPayloads(t *testing.T) { + + for _, tc := range []struct { + memo string // use memo if present, otherwise marshal tbPayload with json + tbPayload types.GatewayIbcTokenBridgePayload + shouldErr bool + }{ + { + memo: "abc123", + shouldErr: true, + }, + { + tbPayload: types.GatewayIbcTokenBridgePayload{}, + shouldErr: true, + }, + { + tbPayload: types.GatewayIbcTokenBridgePayload{ + GatewayIbcTokenBridgePayloadObj: types.GatewayIbcTokenBridgePayloadObj{ + Transfer: types.GatewayTransfer{ + Chain: 1, + Recipient: []byte("recipient"), + Fee: "0uworm", + Nonce: 1, + }, + }, + }, + shouldErr: false, + }, + { + tbPayload: types.GatewayIbcTokenBridgePayload{ + GatewayIbcTokenBridgePayloadObj: types.GatewayIbcTokenBridgePayloadObj{ + TransferWithPayload: types.GatewayTransferWithPayload{ + Chain: 1, + Contract: []byte("contract"), + Payload: []byte("{\"payload\":\"data\"}"), + Nonce: 1, + }, + }, + }, + shouldErr: false, + }, + } { + + memo := tc.memo + + if memo == "" { + bz, err := json.Marshal(tc.tbPayload) + require.NoError(t, err) + memo = string(bz) + } + + payload, err := types.VerifyAndParseGatewayPayload(memo) + + if tc.shouldErr { + require.Error(t, err) + // continue to next case if err + continue + } else { + require.NoError(t, err) + } + + // validate payload was parsed correctly + if payload.NoPayload { + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Chain, payload.ChainId) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Recipient, payload.Recipient) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Fee, payload.Fee) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Nonce, payload.Nonce) + } else { + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Chain, payload.ChainId) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Contract, payload.Recipient) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Payload, payload.Payload) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Nonce, payload.Nonce) + } + } +} diff --git a/wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go b/wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go new file mode 100644 index 0000000000..e61de326b6 --- /dev/null +++ b/wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go @@ -0,0 +1,85 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types" +) + +func TestFormatIbcHooksMemo(t *testing.T) { + + ibcTranslatorContract := "wormhole123abc" + + for _, tc := range []struct { + payload types.ParsedPayload + shouldErr bool + }{ + // Normal w/ no payload + { + payload: types.ParsedPayload{ + NoPayload: true, + ChainId: 1, + Recipient: []byte{'a', 'b', 'c'}, + Fee: "0uworm", + Nonce: 1, + Payload: nil, + }, + shouldErr: false, + }, + // Provide payload when unnecessary + { + payload: types.ParsedPayload{ + NoPayload: true, + ChainId: 1, + Recipient: []byte{'a', 'b', 'c'}, + Fee: "0uworm", + Nonce: 1, + Payload: []byte("{\"payload\":\"data\"}"), + }, + shouldErr: false, + }, + // Normal w/ payload + { + payload: types.ParsedPayload{ + NoPayload: false, + ChainId: 1, + Recipient: []byte{'a', 'b', 'c'}, + Fee: "0uworm", + Nonce: 1, + Payload: []byte("{\"payload\":\"data\"}"), + }, + shouldErr: false, + }, + // Nil payload should not err + { + payload: types.ParsedPayload{ + NoPayload: true, + ChainId: 1, + Recipient: []byte{'a', 'b', 'c'}, + Fee: "0uworm", + Nonce: 1, + Payload: nil, + }, + shouldErr: false, + }, + } { + res, err := types.FormatIbcHooksMemo(tc.payload, ibcTranslatorContract) + + if tc.shouldErr { + require.Error(t, err) + } else { + require.NoError(t, err) + require.NotNil(t, res) + } + + if tc.payload.NoPayload { + require.NotContains(t, res, "gateway_convert_and_transfer_with_payload") + require.Contains(t, res, "recipient") + } else { + require.Contains(t, res, "gateway_convert_and_transfer_with_payload") + require.NotContains(t, res, "recipient") + require.Contains(t, res, "payload") + } + } +} diff --git a/wormchain/x/ibc-composability-mw/types/pfm_test.go b/wormchain/x/ibc-composability-mw/types/pfm_test.go new file mode 100644 index 0000000000..d9be07a2b5 --- /dev/null +++ b/wormchain/x/ibc-composability-mw/types/pfm_test.go @@ -0,0 +1,7 @@ +package types_test + +import "testing" + +func TestFormatPfmMemo(t *testing.T) { + +}