Skip to content

Commit

Permalink
Add Keeper Test
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Sep 11, 2024
1 parent 4914a07 commit 8b4cc6d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 7 deletions.
6 changes: 0 additions & 6 deletions wormchain/testutil/keeper/wormhole.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -20,11 +19,6 @@ 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
3 changes: 2 additions & 1 deletion wormchain/x/ibc-composability-mw/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func TestGenesis(t *testing.T) {
TransposedDataInFlight: tc.dataInFlight,
}

_, keeper, ctx := keepertest.WormholeKeeperAndIBCComposabilityMwKeeper(t)
app, ctx := keepertest.SetupWormchainAndContext(t)
keeper := app.IbcComposabilityMwKeeper

keeper.InitGenesis(ctx, genesisState)

Expand Down
118 changes: 118 additions & 0 deletions wormchain/x/ibc-composability-mw/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package keeper_test

import (
_ "embed"
"encoding/json"
"testing"

transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/stretchr/testify/require"
keepertest "github.com/wormhole-foundation/wormchain/testutil/keeper"
"github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types"
wormholetypes "github.com/wormhole-foundation/wormchain/x/wormhole/types"
)

// TestPackets ensure that packets are handled correctly by the ibc composability middleware.
// This test will only be able to process IBC hooks messages and not PFM because the test
// requires a full chain setup with interchaintest.
func TestPackets(t *testing.T) {
// setup app & get keepers & ctx
app, ctx := keepertest.SetupWormchainAndContext(t)
whKeeper := app.WormholeKeeper
keeper := app.IbcComposabilityMwKeeper

// set ibc composability contract
whKeeper.StoreIbcComposabilityMwContract(ctx, wormholetypes.IbcComposabilityMwContract{
ContractAddress: "wormhole1du4amsmvx8yqr8whw7qc5m3c0zpwknmzelwqy6",
})

// define a packet with no ibc token bridge payload
packetDataNoPayload, err := json.Marshal(transfertypes.FungibleTokenPacketData{
Denom: "uworm",
Amount: "100",
Sender: "sender",
Receiver: "receiver",
Memo: "",
})
require.NoError(t, err)

// define gateway payload for packet
gatewayTBPayload, err := json.Marshal(types.GatewayIbcTokenBridgePayload{
GatewayIbcTokenBridgePayloadObj: types.GatewayIbcTokenBridgePayloadObj{
Transfer: types.GatewayTransfer{
Chain: 1,
Recipient: []byte("recipient"),
Fee: "0uworm",
Nonce: 1,
},
},
})
require.NoError(t, err)

// define a packet with a valid ibc token bridge payload
packetDataWithPayload, err := json.Marshal(transfertypes.FungibleTokenPacketData{
Denom: "uworm",
Amount: "100",
Sender: "sender",
Receiver: "receiver",
Memo: string(gatewayTBPayload),
})
require.NoError(t, err)

for _, tc := range []struct {
testName string
packet channeltypes.Packet
shouldErr bool
}{
{
testName: "empty packet - expect error",
shouldErr: true,
},
{
testName: "packet with no data - expect error",
packet: channeltypes.Packet{
Data: []byte("wrong data format"),
},
shouldErr: true,
},
{
testName: "packet with no memo in data - expect error",
packet: channeltypes.Packet{
Data: packetDataNoPayload,
},
shouldErr: true,
},
{
testName: "packet with payload - expect success",
packet: channeltypes.Packet{
Sequence: 1,
SourcePort: "transfer",
SourceChannel: "channel-0",
DestinationPort: "transfer",
DestinationChannel: "channel-0",
Data: packetDataWithPayload,
},
shouldErr: false,
},
} {

packet, ack := keeper.OnRecvPacket(ctx, tc.packet)

t.Run(tc.testName, func(t *testing.T) {

if tc.shouldErr {
require.NotNil(t, ack)
return
} else {
require.NotNil(t, packet)
require.Nil(t, ack)
}

// Should return nil because the packet is not transposed (it is an ibc hooks packet)
res := keeper.GetAndClearTransposedData(ctx, tc.packet.DestinationChannel, tc.packet.DestinationPort, tc.packet.Sequence)
require.Nil(t, res)
})

}
}

0 comments on commit 8b4cc6d

Please sign in to comment.