From 74b1a44b25d1d51366cdce2483b3e588f21c5e1c Mon Sep 17 00:00:00 2001 From: Joel Smith Date: Thu, 12 Sep 2024 16:42:04 -0500 Subject: [PATCH] Add More Unit Tests --- ...ver_execute_gateway_governance_vaa_test.go | 127 ++++++++++++++++++ .../x/wormhole/keeper/msg_server_test.go | 16 --- ..._server_wasm_instantiate_allowlist_test.go | 14 +- wormchain/x/wormhole/keeper/vaa_test.go | 2 +- .../keeper/wasm_instantiate_allowlist_test.go | 44 ++++++ 5 files changed, 179 insertions(+), 24 deletions(-) create mode 100644 wormchain/x/wormhole/keeper/msg_server_execute_gateway_governance_vaa_test.go delete mode 100644 wormchain/x/wormhole/keeper/msg_server_test.go create mode 100644 wormchain/x/wormhole/keeper/wasm_instantiate_allowlist_test.go diff --git a/wormchain/x/wormhole/keeper/msg_server_execute_gateway_governance_vaa_test.go b/wormchain/x/wormhole/keeper/msg_server_execute_gateway_governance_vaa_test.go new file mode 100644 index 0000000000..2578b3abf0 --- /dev/null +++ b/wormchain/x/wormhole/keeper/msg_server_execute_gateway_governance_vaa_test.go @@ -0,0 +1,127 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "github.com/wormhole-foundation/wormchain/x/wormhole/types" + "github.com/wormhole-foundation/wormhole/sdk/vaa" +) + +// TestExecuteGatewayGovernanceVaaUpgrades tests creating and cancelling upgrades +func TestExecuteGatewayGovernanceVaaUpgrades(t *testing.T) { + _, ctx, msgServer, privateKeys, signer, guardianSet := setupWormholeMessageServer(t) + + // Create upgrade payload + payload, err := vaa.BodyGatewayScheduleUpgrade{ + Name: "v5.0.0", + Height: uint64(100), + }.Serialize() + require.NoError(t, err) + + // Generate VAA + v := generateVaa(guardianSet.Index, privateKeys, vaa.ChainID(vaa.GovernanceChain), payload) + vBz, err := v.Marshal() + require.NoError(t, err) + + // Submit upgrade governance VAA + res, err := msgServer.ExecuteGatewayGovernanceVaa(ctx, &types.MsgExecuteGatewayGovernanceVaa{ + Signer: signer.String(), + Vaa: vBz, + }) + require.NoError(t, err) + require.NotNil(t, res) + + // Create cancel upgrade payload + payload, err = vaa.EmptyPayloadVaa(vaa.GatewayModuleStr, vaa.ActionCancelUpgrade, vaa.ChainIDWormchain) + require.NoError(t, err) + + // Generate VAA + v = generateVaa(guardianSet.Index, privateKeys, vaa.ChainID(vaa.GovernanceChain), payload) + vBz, err = v.Marshal() + require.NoError(t, err) + + // Submit cancel upgrade governance VAA + res, err = msgServer.ExecuteGatewayGovernanceVaa(ctx, &types.MsgExecuteGatewayGovernanceVaa{ + Signer: signer.String(), + Vaa: vBz, + }) + require.NoError(t, err) + require.NotNil(t, res) +} + +// TestExecuteGatewayGovernanceVaaSetIbcComposabilityMwContract tests setting the IBC composability contract +func TestExecuteGatewayGovernanceVaaSetIbcComposabilityMwContract(t *testing.T) { + k, ctx, msgServer, privateKeys, signer, guardianSet := setupWormholeMessageServer(t) + + // Get contract bytes + contractAddr := "wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh" + contractAddrBz, err := sdk.AccAddressFromBech32(contractAddr) + require.NoError(t, err) + + // Create payload + payload, err := vaa.BodyGatewayIbcComposabilityMwContract{ + ContractAddr: [32]byte(contractAddrBz), + }.Serialize() + require.NoError(t, err) + + // Generate VAA + v := generateVaa(guardianSet.Index, privateKeys, vaa.ChainID(vaa.GovernanceChain), payload) + vBz, err := v.Marshal() + require.NoError(t, err) + + // Submit governance VAA + res, err := msgServer.ExecuteGatewayGovernanceVaa(ctx, &types.MsgExecuteGatewayGovernanceVaa{ + Signer: signer.String(), + Vaa: vBz, + }) + require.NoError(t, err) + require.NotNil(t, res) + + // Validate the contract was set + contract := k.GetIbcComposabilityMwContract(ctx) + require.Equal(t, contractAddr, contract.ContractAddress) +} + +// TestExecuteGatewayGovernanceVaaUnknownAction tests submitting an unknown action +func TestExecuteGatewayGovernanceVaaUnknownAction(t *testing.T) { + _, ctx, msgServer, privateKeys, signer, guardianSet := setupWormholeMessageServer(t) + + // Create payload + payload, err := vaa.EmptyPayloadVaa(vaa.GatewayModuleStr, vaa.GovernanceAction(100), vaa.ChainIDWormchain) + require.NoError(t, err) + + // Generate VAA + v := generateVaa(guardianSet.Index, privateKeys, vaa.ChainID(vaa.GovernanceChain), payload) + vBz, err := v.Marshal() + require.NoError(t, err) + + // Submit governance VAA + _, err = msgServer.ExecuteGatewayGovernanceVaa(ctx, &types.MsgExecuteGatewayGovernanceVaa{ + Signer: signer.String(), + Vaa: vBz, + }) + require.Error(t, err) +} + +// TestExecuteGatewayGovernanceVaaInvalidVAA tests submitting an invalid VAA +func TestExecuteGatewayGovernanceVaaInvalidVAA(t *testing.T) { + _, ctx, msgServer, _, signer, guardianSet := setupWormholeMessageServer(t) + + // Create payload + payload, err := vaa.EmptyPayloadVaa(vaa.GatewayModuleStr, vaa.ActionCancelUpgrade, vaa.ChainIDWormchain) + require.NoError(t, err) + + // Generate VAA + v := generateVaa(guardianSet.Index, nil, vaa.ChainID(vaa.GovernanceChain), payload) + vBz, err := v.Marshal() + require.NoError(t, err) + + // Submit governance VAA + _, err = msgServer.ExecuteGatewayGovernanceVaa(ctx, &types.MsgExecuteGatewayGovernanceVaa{ + Signer: signer.String(), + Vaa: vBz, + }) + require.Error(t, err) +} diff --git a/wormchain/x/wormhole/keeper/msg_server_test.go b/wormchain/x/wormhole/keeper/msg_server_test.go deleted file mode 100644 index 723ef7874b..0000000000 --- a/wormchain/x/wormhole/keeper/msg_server_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package keeper_test - -import ( - "context" - "testing" - - sdk "github.com/cosmos/cosmos-sdk/types" - keepertest "github.com/wormhole-foundation/wormchain/testutil/keeper" - "github.com/wormhole-foundation/wormchain/x/wormhole/keeper" - "github.com/wormhole-foundation/wormchain/x/wormhole/types" -) - -func setupMsgServer(t *testing.T) (types.MsgServer, context.Context) { - k, ctx := keepertest.WormholeKeeper(t) - return keeper.NewMsgServerImpl(*k), sdk.WrapSDKContext(ctx) -} diff --git a/wormchain/x/wormhole/keeper/msg_server_wasm_instantiate_allowlist_test.go b/wormchain/x/wormhole/keeper/msg_server_wasm_instantiate_allowlist_test.go index 3e4464d4c9..a57c57b0ed 100644 --- a/wormchain/x/wormhole/keeper/msg_server_wasm_instantiate_allowlist_test.go +++ b/wormchain/x/wormhole/keeper/msg_server_wasm_instantiate_allowlist_test.go @@ -12,9 +12,9 @@ import ( "github.com/wormhole-foundation/wormhole/sdk/vaa" ) -// setupAllowlistMsgServer creates a keeper, context, msg server, private keys, signer, and guardian set for +// setupWormholeMessageServer creates a keeper, context, msg server, private keys, signer, and guardian set for // testing the wasm allowlist msg server -func setupAllowlistMsgServer(t *testing.T) (keeper.Keeper, sdk.Context, types.MsgServer, []*ecdsa.PrivateKey, sdk.AccAddress, *types.GuardianSet) { +func setupWormholeMessageServer(t *testing.T) (keeper.Keeper, sdk.Context, types.MsgServer, []*ecdsa.PrivateKey, sdk.AccAddress, *types.GuardianSet) { k, ctx := keepertest.WormholeKeeper(t) msgServer := keeper.NewMsgServerImpl(*k) @@ -36,7 +36,7 @@ func setupAllowlistMsgServer(t *testing.T) (keeper.Keeper, sdk.Context, types.Ms // TestWasmAllowlistMsgServer tests the endpoints of the wasm allowlist msg server (happy path) func TestWasmAllowlistMsgServer(t *testing.T) { - k, ctx, msgServer, privateKeys, signer, guardianSet := setupAllowlistMsgServer(t) + k, ctx, msgServer, privateKeys, signer, guardianSet := setupWormholeMessageServer(t) bech32ContractAddr := "wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh" @@ -100,7 +100,7 @@ func TestWasmAllowlistMsgServer(t *testing.T) { // TestWasmAllowlistMsgServerMismatchedCodeId tests the endpoints of the wasm allowlist msg server // with mismatched code id func TestWasmAllowlistMsgServerMismatchedCodeId(t *testing.T) { - _, ctx, msgServer, privateKeys, signer, guardianSet := setupAllowlistMsgServer(t) + _, ctx, msgServer, privateKeys, signer, guardianSet := setupWormholeMessageServer(t) bech32ContractAddr := "wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh" codeId := uint64(1) @@ -135,7 +135,7 @@ func TestWasmAllowlistMsgServerMismatchedCodeId(t *testing.T) { // TestWasmAllowlistMsgServerMismatchedContractAddr tests the endpoints of the wasm allowlist msg server // with mismatched contract addresses func TestWasmAllowlistMsgServerMismatchedContractAddr(t *testing.T) { - _, ctx, msgServer, privateKeys, signer, guardianSet := setupAllowlistMsgServer(t) + _, ctx, msgServer, privateKeys, signer, guardianSet := setupWormholeMessageServer(t) bech32ContractAddr := "wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh" codeId := uint64(1) @@ -166,7 +166,7 @@ func TestWasmAllowlistMsgServerMismatchedContractAddr(t *testing.T) { // TestWasmAllowlistMsgServerMismatchedVaaAction tests the endpoints of the wasm allowlist msg server // with mismatched vaa action func TestWasmAllowlistMsgServerMismatchedVaaAction(t *testing.T) { - _, ctx, msgServer, privateKeys, signer, guardianSet := setupAllowlistMsgServer(t) + _, ctx, msgServer, privateKeys, signer, guardianSet := setupWormholeMessageServer(t) bech32ContractAddr := "wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh" codeId := uint64(1) @@ -197,7 +197,7 @@ func TestWasmAllowlistMsgServerMismatchedVaaAction(t *testing.T) { // TestWasmAllowlistMsgServerInvalidVAA tests the endpoints of the wasm allowlist msg server // with invalid vaa func TestWasmAllowlistMsgServerInvalidVAA(t *testing.T) { - _, ctx, msgServer, _, signer, guardianSet := setupAllowlistMsgServer(t) + _, ctx, msgServer, _, signer, guardianSet := setupWormholeMessageServer(t) bech32ContractAddr := "wormhole1466nf3zuxpya8q9emxukd7vftaf6h4psr0a07srl5zw74zh84yjq4lyjmh" codeId := uint64(1) diff --git a/wormchain/x/wormhole/keeper/vaa_test.go b/wormchain/x/wormhole/keeper/vaa_test.go index fd70c76893..bc4b0d1c87 100644 --- a/wormchain/x/wormhole/keeper/vaa_test.go +++ b/wormchain/x/wormhole/keeper/vaa_test.go @@ -270,7 +270,7 @@ func generateVaa(index uint32, signers []*ecdsa.PrivateKey, emitterChain vaa.Cha Nonce: uint32(1), Sequence: uint64(lastestSequence), ConsistencyLevel: uint8(32), - EmitterChain: vaa.ChainIDSolana, + EmitterChain: emitterChain, EmitterAddress: vaa.Address(vaa.GovernanceEmitter), Payload: payload, } diff --git a/wormchain/x/wormhole/keeper/wasm_instantiate_allowlist_test.go b/wormchain/x/wormhole/keeper/wasm_instantiate_allowlist_test.go new file mode 100644 index 0000000000..4cc891a6ab --- /dev/null +++ b/wormchain/x/wormhole/keeper/wasm_instantiate_allowlist_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/wormhole/types" +) + +// TestWasmInstantiateAllowlist tests the setting, getting, and removing of allowed addresses +func TestWasmInstantiateAllowlist(t *testing.T) { + k, ctx := keepertest.WormholeKeeper(t) + + // Create entry + entry := types.WasmInstantiateAllowedContractCodeId{ + ContractAddress: "wormhole1du4amsmvx8yqr8whw7qc5m3c0zpwknmzelwqy6", + CodeId: 1, + } + + // Add contract to allow list + k.SetWasmInstantiateAllowlist(ctx, entry) + + // Check if address exists + hasAddr := k.HasWasmInstantiateAllowlist(ctx, entry.ContractAddress, entry.CodeId) + require.True(t, hasAddr) + + // Check faulty address - does not exist + hasAddr = k.HasWasmInstantiateAllowlist(ctx, "invalid", 0) + require.False(t, hasAddr) + + // Get all allowed addresses + addrList := k.GetAllWasmInstiateAllowedAddresses(ctx) + require.Equal(t, 1, len(addrList)) + require.Equal(t, entry.ContractAddress, addrList[0].ContractAddress) + require.Equal(t, entry.CodeId, addrList[0].CodeId) + + // Remove address + k.KeeperDeleteWasmInstantiateAllowlist(ctx, entry) + + // Check if address exists + hasAddr = k.HasWasmInstantiateAllowlist(ctx, entry.ContractAddress, entry.CodeId) + require.False(t, hasAddr) +}