Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(wormchain): wormhole module testing #78

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions wormchain/x/wormhole/keeper/allowlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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"
)

const (
WormholeAddress1 = "wormhole1du4amsmvx8yqr8whw7qc5m3c0zpwknmzelwqy6"
WormholeAddress2 = "wormhole13ztxpktzsng3ewkepe2w39ugxzfdf23teptu9n"
)

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

value := types.ValidatorAllowedAddress{
ValidatorAddress: WormholeAddress1,
AllowedAddress: WormholeAddress2,
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)
}

// TestValidatorAsAllowedAddress tests if a validator is a guardian or future validator.
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, WormholeAddress1)
require.False(t, found)
isVal = k.IsAddressValidatorOrFutureValidator(ctx, WormholeAddress1)
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: WormholeAddress1,
AllowedAddress: WormholeAddress2,
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 := WormholeContractAddress1
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: WormholeContractAddress1,
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
Loading