Skip to content

Commit

Permalink
Add Wormhole Module Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Sep 12, 2024
1 parent 0bf3a41 commit bcc1e67
Show file tree
Hide file tree
Showing 7 changed files with 531 additions and 0 deletions.
90 changes: 90 additions & 0 deletions wormchain/x/wormhole/keeper/allowlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
keepertest "github.com/wormhole-foundation/wormchain/testutil/keeper"
"github.com/wormhole-foundation/wormchain/x/wormhole/types"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
)

// TestAllowedAddressStore tests the setting, getting, and removing of allowed addresses
func TestAllowedAddressStore(t *testing.T) {
k, ctx := keepertest.WormholeKeeper(t)

value := types.ValidatorAllowedAddress{
ValidatorAddress: "wormhole1du4amsmvx8yqr8whw7qc5m3c0zpwknmzelwqy6",
AllowedAddress: "wormhole13ztxpktzsng3ewkepe2w39ugxzfdf23teptu9n",
Name: "User1",
}

// Set validator allowed list
k.SetValidatorAllowedAddress(ctx, value)

// Check if address exists
hasAddr := k.HasValidatorAllowedAddress(ctx, value.AllowedAddress)
require.True(t, hasAddr)

// Check faulty address - does not exist
hasAddr = k.HasValidatorAllowedAddress(ctx, "invalid")
require.False(t, hasAddr)

// Retrieve & validate
res := k.GetValidatorAllowedAddress(ctx, value.AllowedAddress)
require.Equal(t, value.ValidatorAddress, res.ValidatorAddress)
require.Equal(t, value.AllowedAddress, res.AllowedAddress)
require.Equal(t, value.Name, res.Name)

// Get all allowed addresses
addrList := k.GetAllAllowedAddresses(ctx)
require.Equal(t, 1, len(addrList))
res = addrList[0]
require.Equal(t, value.ValidatorAddress, res.ValidatorAddress)
require.Equal(t, value.AllowedAddress, res.AllowedAddress)
require.Equal(t, value.Name, res.Name)

// Remove address
k.RemoveValidatorAllowedAddress(ctx, value.AllowedAddress)

// Check if address exists
hasAddr = k.HasValidatorAllowedAddress(ctx, value.AllowedAddress)
require.False(t, hasAddr)
}

func TestValidatorAsAllowedAddress(t *testing.T) {
k, ctx := keepertest.WormholeKeeper(t)

// Create guardian set
guardians, _ := createNGuardianValidator(k, ctx, 10)
k.SetConfig(ctx, types.Config{
GovernanceEmitter: vaa.GovernanceEmitter[:],
GovernanceChain: uint32(vaa.GovernanceChain),
ChainId: uint32(vaa.ChainIDWormchain),
GuardianSetExpiration: 86400,
})

createNewGuardianSet(k, ctx, guardians)
k.SetConsensusGuardianSetIndex(ctx, types.ConsensusGuardianSetIndex{
Index: 0,
})

// Get validator addr
addr, err := sdk.Bech32ifyAddressBytes("wormhole", guardians[0].ValidatorAddr)
require.NoError(t, err)

// Check if validator belongs to a guardian
_, found := k.GetGuardianValidatorByValidatorAddress(ctx, addr)
require.True(t, found)

// Check if validator is a current/future validator
isVal := k.IsAddressValidatorOrFutureValidator(ctx, addr)
require.True(t, isVal)

// Check invalid addresses
_, found = k.GetGuardianValidatorByValidatorAddress(ctx, "wormhole13ztxpktzsng3ewkepe2w39ugxzfdf23teptu9n")
require.False(t, found)
isVal = k.IsAddressValidatorOrFutureValidator(ctx, "wormhole13ztxpktzsng3ewkepe2w39ugxzfdf23teptu9n")
require.False(t, isVal)
}
64 changes: 64 additions & 0 deletions wormchain/x/wormhole/keeper/grpc_query_allowlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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"
)

// TestQueryAllowlist tests the allow list queries
func TestQueryAllowlist(t *testing.T) {
k, ctx := keepertest.WormholeKeeper(t)

// Check if no allowlist exists
res, err := k.AllowlistAll(ctx, &types.QueryAllValidatorAllowlist{})
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 0, len(res.Allowlist))

value := types.ValidatorAllowedAddress{
ValidatorAddress: "wormhole1du4amsmvx8yqr8whw7qc5m3c0zpwknmzelwqy6",
AllowedAddress: "wormhole13ztxpktzsng3ewkepe2w39ugxzfdf23teptu9n",
Name: "User1",
}

// Set validator allowed list
k.SetValidatorAllowedAddress(ctx, value)

// Query all allow lists
res, err = k.AllowlistAll(ctx, &types.QueryAllValidatorAllowlist{})
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 1, len(res.Allowlist))
require.Equal(t, value.ValidatorAddress, res.Allowlist[0].ValidatorAddress)
require.Equal(t, value.AllowedAddress, res.Allowlist[0].AllowedAddress)
require.Equal(t, value.Name, res.Allowlist[0].Name)

// Invalid query all
_, err = k.Allowlist(ctx, nil)
require.Error(t, err)

// Query allow list by address
res2, err := k.Allowlist(ctx, &types.QueryValidatorAllowlist{
ValidatorAddress: value.ValidatorAddress,
})
require.NoError(t, err)
require.NotNil(t, res2)
require.Equal(t, 1, len(res2.Allowlist))
require.Equal(t, value.ValidatorAddress, res2.Allowlist[0].ValidatorAddress)
require.Equal(t, value.AllowedAddress, res2.Allowlist[0].AllowedAddress)

// Query with nil request
_, err = k.Allowlist(ctx, nil)
require.Error(t, err)

// Query invalid address
res2, err = k.Allowlist(ctx, &types.QueryValidatorAllowlist{
ValidatorAddress: "invalid",
})
require.NoError(t, err)
require.NotNil(t, res2)
require.Equal(t, 0, len(res2.Allowlist))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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"
)

// TestQueryIbcComposabilityMwContract tests querying of the IbcComposabilityMwContract
func TestQueryIbcComposabilityMwContract(t *testing.T) {
k, ctx := keepertest.WormholeKeeper(t)

// Invalid query with nil request
_, err := k.IbcComposabilityMwContract(ctx, nil)
require.Error(t, err)

// Query when no contract is set
res, err := k.IbcComposabilityMwContract(ctx, &types.QueryIbcComposabilityMwContractRequest{})
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, "", res.ContractAddress)

// Set the contract in state store
contractAddr := "wormhole1du4amsmvx8yqr8whw7qc5m3c0zpwknmzelwqy6"
k.StoreIbcComposabilityMwContract(ctx, types.IbcComposabilityMwContract{
ContractAddress: contractAddr,
})

// Query IbcComposabilityMwContract
res, err = k.IbcComposabilityMwContract(ctx, &types.QueryIbcComposabilityMwContractRequest{})
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, contractAddr, res.ContractAddress)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
keepertest "github.com/wormhole-foundation/wormchain/testutil/keeper"
"github.com/wormhole-foundation/wormchain/x/wormhole/types"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
)

// TestLatestGuardianSetIndex tests the querying of the latest guardian set index
func TestLatestGuardianSetIndex(t *testing.T) {
k, ctx := keepertest.WormholeKeeper(t)

// Invalid query with nil request
_, err := k.LatestGuardianSetIndex(ctx, nil)
require.Error(t, err)

// Query the latest guardian set index - should be empty
res, err := k.LatestGuardianSetIndex(ctx, &types.QueryLatestGuardianSetIndexRequest{})
require.NoError(t, err)
require.NotNil(t, res)
fmt.Println(res)
require.Equal(t, uint32(0xffffffff), res.LatestGuardianSetIndex)

// Create guardian set
guardians, _ := createNGuardianValidator(k, ctx, 10)
k.SetConfig(ctx, types.Config{
GovernanceEmitter: vaa.GovernanceEmitter[:],
GovernanceChain: uint32(vaa.GovernanceChain),
ChainId: uint32(vaa.ChainIDWormchain),
GuardianSetExpiration: 86400,
})

createNewGuardianSet(k, ctx, guardians)
k.SetConsensusGuardianSetIndex(ctx, types.ConsensusGuardianSetIndex{
Index: 0,
})

// Query the latest guardian set index - after population
res, err = k.LatestGuardianSetIndex(ctx, &types.QueryLatestGuardianSetIndexRequest{})
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, uint32(0), res.LatestGuardianSetIndex)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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"
)

// TestWasmInstantiateAllowlistAll tests the querying of the wasm instantiate allow list
func TestWasmInstantiateAllowlistAll(t *testing.T) {
k, ctx := keepertest.WormholeKeeper(t)

// Query with nil request
_, err := k.WasmInstantiateAllowlistAll(ctx, nil)
require.Error(t, err)

// Query with no contracts
res, err := k.WasmInstantiateAllowlistAll(ctx, &types.QueryAllWasmInstantiateAllowlist{})
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 0, len(res.Allowlist))

// Set contract in allow list
contract := types.WasmInstantiateAllowedContractCodeId{
ContractAddress: "wormhole1du4amsmvx8yqr8whw7qc5m3c0zpwknmzelwqy6",
CodeId: 1,
}
k.SetWasmInstantiateAllowlist(ctx, contract)

// Query all allow lists
res, err = k.WasmInstantiateAllowlistAll(ctx, &types.QueryAllWasmInstantiateAllowlist{})
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, 1, len(res.Allowlist))
require.Equal(t, contract.ContractAddress, res.Allowlist[0].ContractAddress)
require.Equal(t, contract.CodeId, res.Allowlist[0].CodeId)
}
29 changes: 29 additions & 0 deletions wormchain/x/wormhole/keeper/ibc_composability_mw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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"
)

// TestIbcComposabilityMwContractStore tests the setting and getting of the contract
func TestIbcComposabilityMwContractStore(t *testing.T) {
k, ctx := keepertest.WormholeKeeper(t)

// Get contract, should be nil
res := k.GetIbcComposabilityMwContract(ctx)
require.Equal(t, "", res.ContractAddress)

// Set the contract
contract := types.IbcComposabilityMwContract{
ContractAddress: "contractAddress",
}
k.StoreIbcComposabilityMwContract(ctx, contract)

// Get contract from store
res = k.GetIbcComposabilityMwContract(ctx)
require.NotNil(t, res)
require.Equal(t, contract.ContractAddress, res.ContractAddress)
}
Loading

0 comments on commit bcc1e67

Please sign in to comment.