Skip to content

Commit

Permalink
Test integration update (#1975)
Browse files Browse the repository at this point in the history
* move tests to integration folder

* Update test integration

* Fix lint

* Fix gas in recurse tests

* Extract ibctesting from x/wasm

* more changes (#1976)

* Fix comments and cleanup

---------

Co-authored-by: Marko Baricevic <marko@baricevic.me>
  • Loading branch information
pinosu and tac0turtle authored Aug 30, 2024
1 parent 66fd394 commit 5519016
Show file tree
Hide file tree
Showing 42 changed files with 702 additions and 475 deletions.
19 changes: 0 additions & 19 deletions app/params/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,4 @@ const (
DefaultWeightCommunitySpendProposal int = 5
DefaultWeightTextProposal int = 5
DefaultWeightParamChangeProposal int = 5

DefaultWeightMsgStoreCode int = 50
DefaultWeightMsgInstantiateContract int = 100
DefaultWeightMsgExecuteContract int = 100
DefaultWeightMsgUpdateAdmin int = 25
DefaultWeightMsgClearAdmin int = 10
DefaultWeightMsgMigrateContract int = 50

DefaultWeightStoreCodeProposal int = 5
DefaultWeightInstantiateContractProposal int = 5
DefaultWeightUpdateAdminProposal int = 5
DefaultWeightExecuteContractProposal int = 5
DefaultWeightClearAdminProposal int = 5
DefaultWeightMigrateContractProposal int = 5
DefaultWeightSudoContractProposal int = 5
DefaultWeightPinCodesProposal int = 5
DefaultWeightUnpinCodesProposal int = 5
DefaultWeightUpdateInstantiateConfigProposal int = 5
DefaultWeightStoreAndInstantiateContractProposal int = 5
)
5 changes: 2 additions & 3 deletions benchmarks/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/CosmWasm/wasmd/app"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

func setup(db dbm.DB, withGenesis bool, invCheckPeriod uint, opts ...wasmkeeper.Option) (*app.WasmApp, app.GenesisState) { //nolint:unparam
func setup(db dbm.DB, withGenesis bool) (*app.WasmApp, app.GenesisState) {

logLevel := log.LevelOption(zerolog.InfoLevel)

Expand All @@ -48,7 +47,7 @@ func setup(db dbm.DB, withGenesis bool, invCheckPeriod uint, opts ...wasmkeeper.
// SetupWithGenesisAccountsAndValSet initializes a new WasmApp with the provided genesis
// accounts and possible balances.
func SetupWithGenesisAccountsAndValSet(b testing.TB, db dbm.DB, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *app.WasmApp {
wasmApp, genesisState := setup(db, true, 0)
wasmApp, genesisState := setup(db, true)
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
appCodec := wasmApp.AppCodec()

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/CosmWasm/wasmd/app"
"github.com/CosmWasm/wasmd/tests/e2e"
"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
"github.com/CosmWasm/wasmd/tests/ibctesting"
)

func TestGovVoteByContract(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/grants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/authz"

"github.com/CosmWasm/wasmd/tests/e2e"
"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
"github.com/CosmWasm/wasmd/tests/ibctesting"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/group"

"github.com/CosmWasm/wasmd/tests/e2e"
"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
"github.com/CosmWasm/wasmd/tests/ibctesting"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/ibc_callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/CosmWasm/wasmd/app"
"github.com/CosmWasm/wasmd/tests/e2e"
wasmibctesting "github.com/CosmWasm/wasmd/x/wasm/ibctesting"
wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/ibc_fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/address"

"github.com/CosmWasm/wasmd/app"
wasmibctesting "github.com/CosmWasm/wasmd/x/wasm/ibctesting"
wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/ica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/CosmWasm/wasmd/app"
wasmibctesting "github.com/CosmWasm/wasmd/x/wasm/ibctesting"
wasmibctesting "github.com/CosmWasm/wasmd/tests/ibctesting"
)

func TestICA(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/reflect_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
"github.com/CosmWasm/wasmd/tests/ibctesting"
"github.com/CosmWasm/wasmd/x/wasm/keeper/testdata"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
170 changes: 170 additions & 0 deletions tests/integration/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package integration

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
secp256k1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

wasmKeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/CosmWasm/wasmd/x/wasm/types"
)

const firstCodeID = 1

// ensure store code returns the expected response
func assertStoreCodeResponse(t *testing.T, data []byte, expected uint64) {
var pStoreResp types.MsgStoreCodeResponse
require.NoError(t, pStoreResp.Unmarshal(data))
require.Equal(t, pStoreResp.CodeID, expected)
}

// ensure execution returns the expected data
func assertExecuteResponse(t *testing.T, data, expected []byte) {
var pExecResp types.MsgExecuteContractResponse
require.NoError(t, pExecResp.Unmarshal(data))
require.Equal(t, pExecResp.Data, expected)
}

// ensures this returns a valid bech32 address and returns it
func parseInitResponse(t *testing.T, data []byte) string {
var pInstResp types.MsgInstantiateContractResponse
require.NoError(t, pInstResp.Unmarshal(data))
require.NotEmpty(t, pInstResp.Address)
addr := pInstResp.Address
// ensure this is a valid sdk address
_, err := sdk.AccAddressFromBech32(addr)
require.NoError(t, err)
return addr
}

func must[t any](s t, err error) t {
if err != nil {
panic(err)
}
return s
}

func mustUnmarshal(t *testing.T, data []byte, res interface{}) {
t.Helper()
err := json.Unmarshal(data, res)
require.NoError(t, err)
}

func mustMarshal(t *testing.T, r interface{}) []byte {
t.Helper()
bz, err := json.Marshal(r)
require.NoError(t, err)
return bz
}

// this will commit the current set, update the block height and set historic info
// basically, letting two blocks pass
func nextBlock(ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper) sdk.Context {
if _, err := stakingKeeper.EndBlocker(ctx); err != nil {
panic(err)
}
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
_ = stakingKeeper.BeginBlocker(ctx)
return ctx
}

func setValidatorRewards(ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper, distKeeper distributionkeeper.Keeper, valAddr sdk.ValAddress, reward string) {
// allocate some rewards
vali, err := stakingKeeper.Validator(ctx, valAddr)
if err != nil {
panic(err)
}
amount, err := sdkmath.LegacyNewDecFromStr(reward)
if err != nil {
panic(err)
}
payout := sdk.DecCoins{{Denom: "stake", Amount: amount}}
err = distKeeper.AllocateTokensToValidator(ctx, vali, payout)
if err != nil {
panic(err)
}
}

// adds a few validators and returns a list of validators that are registered
func addValidator(t *testing.T, ctx sdk.Context, stakingKeeper *stakingkeeper.Keeper, faucet *wasmKeeper.TestFaucet, value sdk.Coin) sdk.ValAddress {
owner := faucet.NewFundedRandomAccount(ctx, value)

privKey := secp256k1.GenPrivKey()
pubKey := privKey.PubKey()
valAddr := sdk.ValAddress(owner)

pkAny, err := codectypes.NewAnyWithValue(pubKey)
require.NoError(t, err)
msg := &stakingtypes.MsgCreateValidator{
Description: stakingtypes.Description{
Moniker: "Validator power",
},
Commission: stakingtypes.CommissionRates{
Rate: sdkmath.LegacyMustNewDecFromStr("0.1"),
MaxRate: sdkmath.LegacyMustNewDecFromStr("0.2"),
MaxChangeRate: sdkmath.LegacyMustNewDecFromStr("0.01"),
},
MinSelfDelegation: sdkmath.OneInt(),
DelegatorAddress: owner.String(),
ValidatorAddress: valAddr.String(),
Pubkey: pkAny,
Value: value,
}
_, err = stakingkeeper.NewMsgServerImpl(stakingKeeper).CreateValidator(ctx, msg)
require.NoError(t, err)
return valAddr
}

// reflectEncoders needs to be registered in test setup to handle custom message callbacks
func reflectEncoders(cdc codec.Codec) *wasmKeeper.MessageEncoders {
return &wasmKeeper.MessageEncoders{
Custom: fromReflectRawMsg(cdc),
}
}

/**** Code to support custom messages *****/

type reflectCustomMsg struct {
Debug string `json:"debug,omitempty"`
Raw []byte `json:"raw,omitempty"`
}

// fromReflectRawMsg decodes msg.Data to an sdk.Msg using proto Any and json encoding.
// this needs to be registered on the Encoders
func fromReflectRawMsg(cdc codec.Codec) wasmKeeper.CustomEncoder {
return func(_sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error) {
var custom reflectCustomMsg
err := json.Unmarshal(msg, &custom)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
if custom.Raw != nil {
var codecAny codectypes.Any
if err := cdc.UnmarshalJSON(custom.Raw, &codecAny); err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
var msg sdk.Msg
if err := cdc.UnpackAny(&codecAny, &msg); err != nil {
return nil, err
}
return []sdk.Msg{msg}, nil
}
if custom.Debug != "" {
return nil, errorsmod.Wrapf(types.ErrInvalidMsg, "Custom Debug: %s", custom.Debug)
}
return nil, errorsmod.Wrap(types.ErrInvalidMsg, "Unknown Custom message variant")
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wasm
package integration

import (
"encoding/json"
Expand Down
Loading

0 comments on commit 5519016

Please sign in to comment.