Better test setup

This commit is contained in:
Alex Peters 2020-10-27 11:48:24 +01:00
parent 5cc3e6dc8b
commit e54fbb9eb8
No known key found for this signature in database
GPG Key ID: 4E18CD9F98929E11
10 changed files with 88 additions and 272 deletions

View File

@ -12,8 +12,7 @@ type contractState struct {
}
func TestInitGenesis(t *testing.T) {
data, cleanup := setupTest(t)
defer cleanup()
data := setupTest(t)
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 5000))
@ -122,8 +121,7 @@ func TestInitGenesis(t *testing.T) {
genState := ExportGenesis(data.ctx, data.keeper)
// create new app to import genstate into
newData, newCleanup := setupTest(t)
defer newCleanup()
newData := setupTest(t)
q2 := newData.module.LegacyQuerierHandler(nil)
// initialize new app with genstate

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"testing"
"time"
@ -22,18 +21,12 @@ import (
const SupportedFeatures = "staking"
func TestNewKeeper(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
_, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
_, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
require.NotNil(t, keepers.WasmKeeper)
}
func TestCreate(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -82,11 +75,7 @@ func TestCreateStoresInstantiatePermission(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
keeper.setParams(ctx, types.Params{
CodeUploadAccess: types.AllowEverybody,
@ -105,10 +94,7 @@ func TestCreateStoresInstantiatePermission(t *testing.T) {
}
func TestCreateWithParamPermissions(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, bankKeeper, keeper := keepers.AccountKeeper, keepers.BankKeeper, keepers.WasmKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -155,10 +141,7 @@ func TestCreateWithParamPermissions(t *testing.T) {
}
func TestCreateDuplicate(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -187,10 +170,7 @@ func TestCreateDuplicate(t *testing.T) {
}
func TestCreateWithSimulation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
ctx = ctx.WithBlockHeader(tmproto.Header{Height: 1}).
@ -208,7 +188,7 @@ func TestCreateWithSimulation(t *testing.T) {
require.Equal(t, uint64(1), contractID)
// then try to create it in non-simulation mode (should not fail)
ctx, keepers = CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers = CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper = keepers.AccountKeeper, keepers.WasmKeeper
contractID, err = keeper.Create(ctx, creator, wasmCode, "https://github.com/CosmWasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", "any/builder:tag", nil)
require.NoError(t, err)
@ -246,10 +226,7 @@ func TestIsSimulationMode(t *testing.T) {
}
func TestCreateWithGzippedPayload(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -270,10 +247,7 @@ func TestCreateWithGzippedPayload(t *testing.T) {
}
func TestInstantiate(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -357,10 +331,7 @@ func TestInstantiateWithDeposit(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, bankKeeper, keeper := keepers.AccountKeeper, keepers.BankKeeper, keepers.WasmKeeper
if spec.fundAddr {
@ -430,11 +401,7 @@ func TestInstantiateWithPermissions(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, bankKeeper, keeper := keepers.AccountKeeper, keepers.BankKeeper, keepers.WasmKeeper
fundAccounts(t, ctx, accKeeper, bankKeeper, spec.srcActor, deposit)
@ -448,17 +415,12 @@ func TestInstantiateWithPermissions(t *testing.T) {
}
func TestInstantiateWithNonExistingCodeID(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
creator := createFakeFundedAccount(t, ctx, accKeeper, bankKeeper, deposit)
require.NoError(t, err)
initMsg := HackatomExampleInitMsg{}
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
@ -470,10 +432,7 @@ func TestInstantiateWithNonExistingCodeID(t *testing.T) {
}
func TestExecute(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -590,10 +549,7 @@ func TestExecuteWithDeposit(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, bankKeeper, keeper := keepers.AccountKeeper, keepers.BankKeeper, keepers.WasmKeeper
if spec.fundAddr {
@ -625,10 +581,7 @@ func TestExecuteWithDeposit(t *testing.T) {
}
func TestExecuteWithNonExistingAddress(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -636,15 +589,12 @@ func TestExecuteWithNonExistingAddress(t *testing.T) {
// unauthorized - trialCtx so we don't change state
nonExistingAddress := addrFromUint64(9999)
_, err = keeper.Execute(ctx, nonExistingAddress, creator, []byte(`{}`), nil)
_, err := keeper.Execute(ctx, nonExistingAddress, creator, []byte(`{}`), nil)
require.True(t, types.ErrNotFound.Is(err), err)
}
func TestExecuteWithPanic(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -677,10 +627,7 @@ func TestExecuteWithPanic(t *testing.T) {
}
func TestExecuteWithCpuLoop(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -725,10 +672,7 @@ func TestExecuteWithCpuLoop(t *testing.T) {
}
func TestExecuteWithStorageLoop(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -772,10 +716,7 @@ func TestExecuteWithStorageLoop(t *testing.T) {
}
func TestMigrate(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -916,10 +857,7 @@ func TestMigrate(t *testing.T) {
}
func TestMigrateWithDispatchedMessage(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -1028,10 +966,7 @@ func mustMarshal(t *testing.T, r interface{}) []byte {
}
func TestUpdateContractAdmin(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -1103,10 +1038,7 @@ func TestUpdateContractAdmin(t *testing.T) {
}
func TestClearContractAdmin(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
@ -17,10 +16,7 @@ import (
)
func TestLegacyQueryContractState(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -155,10 +151,7 @@ func TestLegacyQueryContractState(t *testing.T) {
}
func TestLegacyQueryContractListByCodeOrdering(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 1000000))
@ -223,10 +216,7 @@ func TestLegacyQueryContractListByCodeOrdering(t *testing.T) {
}
func TestLegacyQueryContractHistory(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper
var (
@ -341,10 +331,7 @@ func TestLegacyQueryCodeList(t *testing.T) {
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper
for _, codeID := range spec.codeIDs {

View File

@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
@ -17,11 +16,7 @@ import (
)
func TestStoreCodeProposal(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, "staking", nil, nil)
ctx, keepers := CreateTestInput(t, false, "staking", nil, nil)
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper
wasmKeeper.setParams(ctx, types.Params{CodeUploadAccess: types.AllowNobody, InstantiateDefaultPermission: types.AccessTypeNobody})
wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
@ -58,11 +53,7 @@ func TestStoreCodeProposal(t *testing.T) {
}
func TestInstantiateProposal(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, "staking", nil, nil)
ctx, keepers := CreateTestInput(t, false, "staking", nil, nil)
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper
wasmKeeper.setParams(ctx, types.Params{CodeUploadAccess: types.AllowNobody, InstantiateDefaultPermission: types.AccessTypeNobody})
@ -114,11 +105,7 @@ func TestInstantiateProposal(t *testing.T) {
}
func TestMigrateProposal(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, "staking", nil, nil)
ctx, keepers := CreateTestInput(t, false, "staking", nil, nil)
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper
wasmKeeper.setParams(ctx, types.Params{CodeUploadAccess: types.AllowNobody, InstantiateDefaultPermission: types.AccessTypeNobody})
@ -247,10 +234,7 @@ func TestAdminProposals(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, "staking", nil, nil)
ctx, keepers := CreateTestInput(t, false, "staking", nil, nil)
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper
wasmKeeper.setParams(ctx, types.Params{CodeUploadAccess: types.AllowNobody, InstantiateDefaultPermission: types.AccessTypeNobody})
@ -276,11 +260,7 @@ func TestAdminProposals(t *testing.T) {
}
func TestUpdateParamsProposal(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, "staking", nil, nil)
ctx, keepers := CreateTestInput(t, false, "staking", nil, nil)
govKeeper, wasmKeeper := keepers.GovKeeper, keepers.WasmKeeper
var (
@ -290,6 +270,7 @@ func TestUpdateParamsProposal(t *testing.T) {
)
nobodyJson, err := json.Marshal(types.AccessTypeNobody)
require.NoError(t, err)
specs := map[string]struct {
src proposal.ParamChange
expUploadConfig types.AccessConfig

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/CosmWasm/wasmd/x/wasm/internal/types"
@ -16,19 +15,16 @@ import (
)
func TestQueryAllContractState(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper
exampleContract := InstantiateHackatomExampleContract(t, ctx, accKeeper, bankKeeper, err, keeper)
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
contractAddr := exampleContract.Contract
contractModel := []types.Model{
{Key: []byte("foo"), Value: []byte(`"bar"`)},
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
}
keeper.importContractState(ctx, contractAddr, contractModel)
require.NoError(t, keeper.importContractState(ctx, contractAddr, contractModel))
q := NewQuerier(keeper)
specs := map[string]struct {
@ -60,13 +56,10 @@ func TestQueryAllContractState(t *testing.T) {
}
func TestQuerySmartContractState(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper
exampleContract := InstantiateHackatomExampleContract(t, ctx, accKeeper, bankKeeper, err, keeper)
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
contractAddr := exampleContract.Contract
q := NewQuerier(keeper)
@ -106,19 +99,16 @@ func TestQuerySmartContractState(t *testing.T) {
}
func TestQueryRawContractState(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper
exampleContract := InstantiateHackatomExampleContract(t, ctx, accKeeper, bankKeeper, err, keeper)
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
contractAddr := exampleContract.Contract
contractModel := []types.Model{
{Key: []byte("foo"), Value: []byte(`"bar"`)},
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
}
keeper.importContractState(ctx, contractAddr, contractModel)
require.NoError(t, keeper.importContractState(ctx, contractAddr, contractModel))
q := NewQuerier(keeper)
specs := map[string]struct {
@ -164,10 +154,7 @@ func TestQueryRawContractState(t *testing.T) {
}
func TestQueryContractListByCodeOrdering(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 1000000))
@ -226,10 +213,7 @@ func TestQueryContractListByCodeOrdering(t *testing.T) {
}
func TestQueryContractHistory(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper
var (
@ -340,10 +324,7 @@ func TestQueryCodeList(t *testing.T) {
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, nil)
keeper := keepers.WasmKeeper
for _, codeID := range spec.codeIDs {

View File

@ -2,8 +2,6 @@ package keeper
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
wasmTypes "github.com/CosmWasm/go-cosmwasm/types"
@ -38,12 +36,8 @@ type recurseResponse struct {
// number os wasm queries called from a contract
var totalWasmQueryCounter int
func initRecurseContract(t *testing.T) (contract sdk.AccAddress, creator sdk.AccAddress, ctx sdk.Context, keeper Keeper, cleanup func()) {
func initRecurseContract(t *testing.T) (contract sdk.AccAddress, creator sdk.AccAddress, ctx sdk.Context, keeper Keeper) {
// we do one basic setup before all test cases (which are read-only and don't change state)
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
cleanup = func() { os.RemoveAll(tempDir) }
var realWasmQuerier func(ctx sdk.Context, request *wasmTypes.WasmQuery) ([]byte, error)
countingQuerier := &QueryPlugins{
Wasm: func(ctx sdk.Context, request *wasmTypes.WasmQuery) ([]byte, error) {
@ -51,39 +45,20 @@ func initRecurseContract(t *testing.T) (contract sdk.AccAddress, creator sdk.Acc
return realWasmQuerier(ctx, request)
},
}
ctx, keepers := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, countingQuerier)
accKeeper, bankKeeper, keeper := keepers.AccountKeeper, keepers.BankKeeper, keepers.WasmKeeper
ctx, keepers := CreateTestInput(t, false, SupportedFeatures, nil, countingQuerier)
keeper = keepers.WasmKeeper
realWasmQuerier = WasmQuerier(&keeper)
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
creator = createFakeFundedAccount(t, ctx, accKeeper, bankKeeper, deposit.Add(deposit...))
// store the code
wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)
codeID, err := keeper.Create(ctx, creator, wasmCode, "", "", nil)
require.NoError(t, err)
// instantiate the contract
_, _, bob := keyPubAddr()
_, _, fred := keyPubAddr()
initMsg := HackatomExampleInitMsg{
Verifier: fred,
Beneficiary: bob,
}
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
contractAddr, err := keeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, "recursive contract", deposit)
require.NoError(t, err)
return contractAddr, creator, ctx, keeper, cleanup
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
return exampleContract.Contract, exampleContract.CreatorAddr, ctx, keeper
}
func TestGasCostOnQuery(t *testing.T) {
const (
GasNoWork uint64 = InstanceCost + 2_926
GasNoWork uint64 = InstanceCost + 2_938
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork50 uint64 = 0xbdfa // this is a little shy of 50k gas - to keep an eye on the limit
GasWork50 uint64 = 48646 // this is a little shy of 50k gas - to keep an eye on the limit
GasReturnUnhashed uint64 = 393
GasReturnHashed uint64 = 342
@ -132,8 +107,7 @@ func TestGasCostOnQuery(t *testing.T) {
},
}
contractAddr, creator, ctx, keeper, cleanup := initRecurseContract(t)
defer cleanup()
contractAddr, creator, ctx, keeper := initRecurseContract(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
@ -209,8 +183,7 @@ func TestGasOnExternalQuery(t *testing.T) {
},
}
contractAddr, _, ctx, keeper, cleanup := initRecurseContract(t)
defer cleanup()
contractAddr, _, ctx, keeper := initRecurseContract(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
@ -249,7 +222,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
const (
// Note: about 100 SDK gas (10k wasmer gas) for each round of sha256
GasWork2k uint64 = 0x42c8c // = InstanceCost + x // we have 6x gas used in cpu than in the instance
GasWork2k uint64 = 273_560 // = InstanceCost + x // we have 6x gas used in cpu than in the instance
// This is overhead for calling into a sub-contract
GasReturnHashed uint64 = 349
)
@ -295,8 +268,7 @@ func TestLimitRecursiveQueryGas(t *testing.T) {
},
}
contractAddr, _, ctx, keeper, cleanup := initRecurseContract(t)
defer cleanup()
contractAddr, _, ctx, keeper := initRecurseContract(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {

View File

@ -3,7 +3,6 @@ package keeper
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"testing"
@ -73,11 +72,8 @@ func mustParse(t *testing.T, data []byte, res interface{}) {
const MaskFeatures = "staking,mask"
func TestMaskReflectContractSend(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
cdc := MakeTestCodec()
ctx, keepers := CreateTestInput(t, false, tempDir, MaskFeatures, maskEncoders(cdc), nil)
ctx, keepers := CreateTestInput(t, false, MaskFeatures, maskEncoders(cdc), nil)
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -158,11 +154,8 @@ func TestMaskReflectContractSend(t *testing.T) {
}
func TestMaskReflectCustomMsg(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
cdc := MakeTestCodec()
ctx, keepers := CreateTestInput(t, false, tempDir, MaskFeatures, maskEncoders(cdc), maskPlugins())
ctx, keepers := CreateTestInput(t, false, MaskFeatures, maskEncoders(cdc), maskPlugins())
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -255,11 +248,8 @@ func TestMaskReflectCustomMsg(t *testing.T) {
}
func TestMaskReflectCustomQuery(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
cdc := MakeTestCodec()
ctx, keepers := CreateTestInput(t, false, tempDir, MaskFeatures, maskEncoders(cdc), maskPlugins())
ctx, keepers := CreateTestInput(t, false, MaskFeatures, maskEncoders(cdc), maskPlugins())
accKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -312,10 +302,7 @@ type maskState struct {
}
func TestMaskReflectWasmQueries(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, MaskFeatures, maskEncoders(MakeTestCodec()), nil)
ctx, keepers := CreateTestInput(t, false, MaskFeatures, maskEncoders(MakeTestCodec()), nil)
accKeeper, keeper := keepers.AccountKeeper, keepers.WasmKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
@ -386,10 +373,7 @@ func TestMaskReflectWasmQueries(t *testing.T) {
}
func TestWasmRawQueryWithNil(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keepers := CreateTestInput(t, false, tempDir, MaskFeatures, maskEncoders(MakeTestCodec()), nil)
ctx, keepers := CreateTestInput(t, false, MaskFeatures, maskEncoders(MakeTestCodec()), nil)
accKeeper, keeper := keepers.AccountKeeper, keepers.WasmKeeper
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))

View File

@ -3,7 +3,6 @@ package keeper
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
wasmTypes "github.com/CosmWasm/go-cosmwasm/types"
@ -89,10 +88,7 @@ type InvestmentResponse struct {
}
func TestInitializeStaking(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, k := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, k := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, stakingKeeper, keeper, bankKeeper := k.AccountKeeper, k.StakingKeeper, k.WasmKeeper, k.BankKeeper
valAddr := addValidator(t, ctx, stakingKeeper, accKeeper, bankKeeper, sdk.NewInt64Coin("stake", 1234567))
@ -162,14 +158,10 @@ type initInfo struct {
distKeeper distributionkeeper.Keeper
wasmKeeper Keeper
bankKeeper bankkeeper.Keeper
cleanup func()
}
func initializeStaking(t *testing.T) initInfo {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
ctx, k := CreateTestInput(t, false, tempDir, SupportedFeatures, nil, nil)
ctx, k := CreateTestInput(t, false, SupportedFeatures, nil, nil)
accKeeper, stakingKeeper, keeper, bankKeeper := k.AccountKeeper, k.StakingKeeper, k.WasmKeeper, k.BankKeeper
valAddr := addValidator(t, ctx, stakingKeeper, accKeeper, bankKeeper, sdk.NewInt64Coin("stake", 1000000))
@ -222,13 +214,11 @@ func initializeStaking(t *testing.T) initInfo {
wasmKeeper: keeper,
distKeeper: k.DistKeeper,
bankKeeper: bankKeeper,
cleanup: func() { os.RemoveAll(tempDir) },
}
}
func TestBonding(t *testing.T) {
initInfo := initializeStaking(t)
defer initInfo.cleanup()
ctx, valAddr, contractAddr := initInfo.ctx, initInfo.valAddr, initInfo.contractAddr
keeper, stakingKeeper, accKeeper, bankKeeper := initInfo.wasmKeeper, initInfo.stakingKeeper, initInfo.accKeeper, initInfo.bankKeeper
@ -277,7 +267,6 @@ func TestBonding(t *testing.T) {
func TestUnbonding(t *testing.T) {
initInfo := initializeStaking(t)
defer initInfo.cleanup()
ctx, valAddr, contractAddr := initInfo.ctx, initInfo.valAddr, initInfo.contractAddr
keeper, stakingKeeper, accKeeper, bankKeeper := initInfo.wasmKeeper, initInfo.stakingKeeper, initInfo.accKeeper, initInfo.bankKeeper
@ -343,7 +332,6 @@ func TestUnbonding(t *testing.T) {
func TestReinvest(t *testing.T) {
initInfo := initializeStaking(t)
defer initInfo.cleanup()
ctx, valAddr, contractAddr := initInfo.ctx, initInfo.valAddr, initInfo.contractAddr
keeper, stakingKeeper, accKeeper, bankKeeper := initInfo.wasmKeeper, initInfo.stakingKeeper, initInfo.accKeeper, initInfo.bankKeeper
distKeeper := initInfo.distKeeper
@ -413,7 +401,6 @@ func TestReinvest(t *testing.T) {
func TestQueryStakingInfo(t *testing.T) {
// STEP 1: take a lot of setup from TestReinvest so we have non-zero info
initInfo := initializeStaking(t)
defer initInfo.cleanup()
ctx, valAddr, contractAddr := initInfo.ctx, initInfo.valAddr, initInfo.contractAddr
keeper, stakingKeeper, accKeeper := initInfo.wasmKeeper, initInfo.stakingKeeper, initInfo.accKeeper
distKeeper := initInfo.distKeeper
@ -556,7 +543,6 @@ func TestQueryStakingInfo(t *testing.T) {
func TestQueryStakingPlugin(t *testing.T) {
// STEP 1: take a lot of setup from TestReinvest so we have non-zero info
initInfo := initializeStaking(t)
defer initInfo.cleanup()
ctx, valAddr, contractAddr := initInfo.ctx, initInfo.valAddr, initInfo.contractAddr
keeper, stakingKeeper, accKeeper := initInfo.wasmKeeper, initInfo.stakingKeeper, initInfo.accKeeper
distKeeper := initInfo.distKeeper

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
@ -121,7 +122,11 @@ type TestKeepers struct {
}
// encoders can be nil to accept the defaults, or set it to override some of the message handlers (like default)
func CreateTestInput(t *testing.T, isCheckTx bool, tempDir string, supportedFeatures string, encoders *MessageEncoders, queriers *QueryPlugins) (sdk.Context, TestKeepers) {
func CreateTestInput(t *testing.T, isCheckTx bool, supportedFeatures string, encoders *MessageEncoders, queriers *QueryPlugins) (sdk.Context, TestKeepers) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(tempDir) })
keyWasm := sdk.NewKVStoreKey(wasmTypes.StoreKey)
keyAcc := sdk.NewKVStoreKey(authtypes.StoreKey)
keyBank := sdk.NewKVStoreKey(banktypes.StoreKey)
@ -141,8 +146,7 @@ func CreateTestInput(t *testing.T, isCheckTx bool, tempDir string, supportedFeat
ms.MountStoreWithDB(keyDistro, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
ms.MountStoreWithDB(keyGov, sdk.StoreTypeIAVL, db)
err := ms.LoadLatestVersion()
require.Nil(t, err)
require.NoError(t, ms.LoadLatestVersion())
ctx := sdk.NewContext(ms, tmproto.Header{
Height: 1234567,
@ -311,7 +315,7 @@ func handleExecute(ctx sdk.Context, k Keeper, msg *wasmtypes.MsgExecuteContract)
return res, nil
}
func AnyAccAddress(t *testing.T) sdk.AccAddress {
func AnyAccAddress(_ *testing.T) sdk.AccAddress {
_, _, addr := keyPubAddr()
return addr
}
@ -328,17 +332,17 @@ type HackatomExampleContract struct {
}
// InstantiateHackatomExampleContract load and instantiate the "./testdata/hackatom.wasm" contract
func InstantiateHackatomExampleContract(t *testing.T, ctx sdk.Context, accKeeper authkeeper.AccountKeeper, bankKeeper bankkeeper.Keeper, err error, keeper Keeper) HackatomExampleContract {
func InstantiateHackatomExampleContract(t *testing.T, ctx sdk.Context, keepers TestKeepers) HackatomExampleContract {
anyAmount := sdk.NewCoins(sdk.NewInt64Coin("denom", 1000))
creator, _, creatorAddr := keyPubAddr()
fundAccounts(t, ctx, accKeeper, bankKeeper, creatorAddr, anyAmount)
fundAccounts(t, ctx, keepers.AccountKeeper, keepers.BankKeeper, creatorAddr, anyAmount)
verifier, _, verifierAddr := keyPubAddr()
fundAccounts(t, ctx, accKeeper, bankKeeper, verifierAddr, anyAmount)
fundAccounts(t, ctx, keepers.AccountKeeper, keepers.BankKeeper, verifierAddr, anyAmount)
wasmCode, err := ioutil.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)
contractID, err := keeper.Create(ctx, creatorAddr, wasmCode, "", "", nil)
contractID, err := keepers.WasmKeeper.Create(ctx, creatorAddr, wasmCode, "", "", nil)
require.NoError(t, err)
beneficiary, _, beneficiaryAddr := keyPubAddr()
@ -349,7 +353,7 @@ func InstantiateHackatomExampleContract(t *testing.T, ctx sdk.Context, accKeeper
initMsgBz, err := json.Marshal(initMsg)
require.NoError(t, err)
initialAmount := sdk.NewCoins(sdk.NewInt64Coin("denom", 100))
contractAddr, err := keeper.Instantiate(ctx, contractID, creatorAddr, nil, initMsgBz, "demo contract to query", initialAmount)
contractAddr, err := keepers.WasmKeeper.Instantiate(ctx, contractID, creatorAddr, nil, initMsgBz, "demo contract to query", initialAmount)
require.NoError(t, err)
return HackatomExampleContract{
InitialAmount: initialAmount,

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
wasmTypes "github.com/CosmWasm/go-cosmwasm/types"
@ -30,11 +29,8 @@ type testData struct {
}
// returns a cleanup function, which must be defered on
func setupTest(t *testing.T) (testData, func()) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
ctx, keepers := CreateTestInput(t, false, tempDir, "staking", nil, nil)
func setupTest(t *testing.T) testData {
ctx, keepers := CreateTestInput(t, false, "staking", nil, nil)
acctKeeper, keeper, bankKeeper := keepers.AccountKeeper, keepers.WasmKeeper, keepers.BankKeeper
data := testData{
module: NewAppModule(keeper),
@ -43,8 +39,7 @@ func setupTest(t *testing.T) (testData, func()) {
keeper: keeper,
bankKeeper: bankKeeper,
}
cleanup := func() { os.RemoveAll(tempDir) }
return data, cleanup
return data
}
func keyPubAddr() (crypto.PrivKey, crypto.PubKey, sdk.AccAddress) {
@ -111,8 +106,7 @@ func TestHandleCreate(t *testing.T) {
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
data, cleanup := setupTest(t)
defer cleanup()
data := setupTest(t)
h := data.module.Route().Handler()
q := data.module.LegacyQuerierHandler(nil)
@ -142,8 +136,7 @@ type state struct {
}
func TestHandleInstantiate(t *testing.T) {
data, cleanup := setupTest(t)
defer cleanup()
data := setupTest(t)
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
creator := createFakeFundedAccount(t, data.ctx, data.acctKeeper, data.bankKeeper, deposit)
@ -200,8 +193,7 @@ func TestHandleInstantiate(t *testing.T) {
}
func TestHandleExecute(t *testing.T) {
data, cleanup := setupTest(t)
defer cleanup()
data := setupTest(t)
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 5000))
@ -314,8 +306,7 @@ func TestHandleExecute(t *testing.T) {
}
func TestHandleExecuteEscrow(t *testing.T) {
data, cleanup := setupTest(t)
defer cleanup()
data := setupTest(t)
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 5000))