forked from wormhole-foundation/wormhole
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from strangelove-ventures/joel/wormhole-module…
…-testing chore(wormchain): wormhole module testing
- Loading branch information
Showing
12 changed files
with
715 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
36 changes: 36 additions & 0 deletions
36
wormchain/x/wormhole/keeper/grpc_query_ibc_composability_mw_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
47 changes: 47 additions & 0 deletions
47
wormchain/x/wormhole/keeper/grpc_query_latest_guardian_set_index_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
39 changes: 39 additions & 0 deletions
39
wormchain/x/wormhole/keeper/grpc_query_wasm_instantiate_allowlist_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.