Skip to content

Commit

Permalink
Start PFM Unit Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Sep 11, 2024
1 parent 4add7f4 commit 2ca4787
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 0 deletions.
6 changes: 6 additions & 0 deletions wormchain/testutil/keeper/wormhole.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down
44 changes: 44 additions & 0 deletions wormchain/x/ibc-composability-mw/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
85 changes: 85 additions & 0 deletions wormchain/x/ibc-composability-mw/types/gateway_payload_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
85 changes: 85 additions & 0 deletions wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
7 changes: 7 additions & 0 deletions wormchain/x/ibc-composability-mw/types/pfm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types_test

import "testing"

func TestFormatPfmMemo(t *testing.T) {

}

0 comments on commit 2ca4787

Please sign in to comment.