* Start cosmos-sdk v0.47 integration (#1136) * Upgrade to sdk v0.47 branch * More integration work * SDK version upgrade; fixes * More fixes * Fixes * Deactivate failing tests * SDK + ibc-go version upgrades * limix gas fix (cherry picked from commit f7f841768e5051d96d243b42ce4f231a33020326) * with valset in bench (cherry picked from commit 35b2a8fd2c23d6160fca540771fd348913f7f143) * Revert staking query handler; fix tests * Minor cleanup * Rebased * Address linter issues * Set legacy router proper * Deactivate failing test. Race condition needs to handled in SDK * Address some code smells * Bump sdk version * Use gov v1 internally for votes * Activate test after sdk fix * Add group test * Add config template for wasm fields * Add Rust backtrace flag for more debug output on simulations * Set unique node folder for tests * Revert "Add Rust backtrace flag for more debug output on simulations" This reverts commit 218c3c6ce137dc02f7bc38391408d3460fb27e6f. * Simulations * Run also im/export + deterministic sims * Add package prefix to interfaces * Add signer annotation (https://github.com/cosmos/cosmos-sdk/issues/10933), minor cleanup * Bump sdk version * Review comments Co-authored-by: vuong <nguyenvuong1122000@gmail.com> * Bump bufbuild/buf-setup-action from 1.11.0 to 1.12.0 Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.11.0 to 1.12.0. - [Release notes](https://github.com/bufbuild/buf-setup-action/releases) - [Commits](https://github.com/bufbuild/buf-setup-action/compare/v1.11.0...v1.12.0) --- updated-dependencies: - dependency-name: bufbuild/buf-setup-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> (cherry picked from commitf4905955b5) * Remove intertx for vanilla ICA * fix msg format in EVENTS.md (cherry picked from commit38d466adfd) * Better to sdk coin convertion (#1164) * Better to sdk coin convertion * Review feedback (cherry picked from commita925a9ed61) * Disallow only address permission (#1163) * Remove AccessTypeOnlyAddress for store msg * Remove AccessTypeOnlyAddress for update config msg * Review feedback Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> (cherry picked from commit8991633de2) * Integrate wasmvm v1.2.0 (backport #1161) (#1175) * Integrate wasmvm v1.2.0 (#1161) * Bump wasmvm version * Bump wasm test contracts * Encode weighted votes * Encode instantiate2 * Handle code info query; better wasmvm errors * Fix readme * Make linter happy * add non cgo build * Review comments * Bump wasmvm to release version Co-authored-by: jhernandezb <contact@jhernandez.me> (cherry picked from commit957b38e0a5) # Conflicts: # x/wasm/keeper/handler_plugin_encoders.go # x/wasm/keeper/handler_plugin_encoders_test.go # x/wasm/keeper/keeper.go # x/wasm/keeper/keeper_test.go * Adress merge conflicts Co-authored-by: Alexander Peters <alpe@users.noreply.github.com> * Bump bufbuild/buf-setup-action from 1.12.0 to 1.13.0 Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.12.0 to 1.13.0. - [Release notes](https://github.com/bufbuild/buf-setup-action/releases) - [Commits](https://github.com/bufbuild/buf-setup-action/compare/v1.12.0...v1.13.0) --- updated-dependencies: - dependency-name: bufbuild/buf-setup-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> (cherry picked from commitffa0e5e5e1) * Emit events for setContractAdmin + setAccessConfig (#1179) (cherry picked from commitc9e7830ac1) * Dependency upgrades (#1172) * Bump sdk version to lastest * Bump ibc-go version to lastest * Remove channel hack * Update to ibc-go v7 + protoVer=0.11.5 * Bump bufbuild/buf-setup-action from 1.13.0 to 1.13.1 Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.13.0 to 1.13.1. - [Release notes](https://github.com/bufbuild/buf-setup-action/releases) - [Commits](https://github.com/bufbuild/buf-setup-action/compare/v1.13.0...v1.13.1) --- updated-dependencies: - dependency-name: bufbuild/buf-setup-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> (cherry picked from commitde27e7f82f) * Fix typos (backport #1185) (#1194) * Fix typos (cherry picked from commitc88b8194cb) # Conflicts: # proto/cosmwasm/wasm/v1/tx.proto * Fix merge conflict --------- Co-authored-by: Alex Peters <alpe@users.noreply.github.com> * Bump bufbuild/buf-setup-action from 1.13.1 to 1.14.0 (#1200) Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.13.1 to 1.14.0. - [Release notes](https://github.com/bufbuild/buf-setup-action/releases) - [Commits](https://github.com/bufbuild/buf-setup-action/compare/v1.13.1...v1.14.0) --- updated-dependencies: - dependency-name: bufbuild/buf-setup-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> (cherry picked from commitf3fc31c386) * list-contract-by-code bugfix (cherry picked from commit2ccffed778) * fix: stargate querier does not reset the state (cherry picked from commitfd0323541d) * test: add unit test (cherry picked from commit6d8018ac59) * Add Windows client support (#1197) * Add Windows client support * Separate server and windows client --------- Co-authored-by: Alex Peters <alpe@users.noreply.github.com> (cherry picked from commit8a20779518) * Bump bufbuild/buf-setup-action from 1.14.0 to 1.15.0 Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.14.0 to 1.15.0. - [Release notes](https://github.com/bufbuild/buf-setup-action/releases) - [Commits](https://github.com/bufbuild/buf-setup-action/compare/v1.14.0...v1.15.0) --- updated-dependencies: - dependency-name: bufbuild/buf-setup-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> (cherry picked from commite5fab3da5a) * Rename windows client binary (cherry picked from commitde09c7fe2b) * Return IBC packet sequence number (backport #1225) (#1233) * Return IBC packet sequence number (#1225) * Return IBC packet sequence number * Fix review feedbacks * Remove names to return values in DispatchMsg method * Fix comments (cherry picked from commit4f1c57fc12) # Conflicts: # x/wasm/keeper/handler_plugin.go * Fix merge conflict --------- Co-authored-by: pinosu <95283998+pinosu@users.noreply.github.com> Co-authored-by: Alex Peters <alpe@users.noreply.github.com> * Test rust panic for regression (cherry picked from commita52e604966) * Fix client checksum verification (#1234) * Fix client checksum verification * Review comments (cherry picked from commit1a8019b380) # Conflicts: # x/wasm/client/cli/gov_tx.go * Fix merge conflict * Fix linters * Configure sonarcloud analysis (cherry picked from commit85cf1614fb) * Bump bufbuild/buf-setup-action from 1.15.0 to 1.15.1 Bumps [bufbuild/buf-setup-action](https://github.com/bufbuild/buf-setup-action) from 1.15.0 to 1.15.1. - [Release notes](https://github.com/bufbuild/buf-setup-action/releases) - [Commits](https://github.com/bufbuild/buf-setup-action/compare/v1.15.0...v1.15.1) --- updated-dependencies: - dependency-name: bufbuild/buf-setup-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> (cherry picked from commit730ea5a1cf) * Make `CaptureIbcEvents` in ibctesting public. Before this change, it wasn't possible to implement the `chain.SendMsgs` method without [copying](https://github.com/public-awesome/ics721/blob/main/e2e/suite_helpers.go#L81-L98) them over. (cherry picked from commitb64fa078a3) * Upgrade to wasmvm 1.2.1 (backport #1245) (#1254) * Upgrade to wasmvm 1.2.1 (#1245) * Use wasmvm store adapter * Bump wasmvm to v1.2.1 (cherry picked from commit850f901b2e) # Conflicts: # go.mod # go.sum # x/wasm/keeper/keeper.go * Resolve conflicts --------- Co-authored-by: Alexander Peters <alpe@users.noreply.github.com> * WIP All cometbft (#1244) * Dep upgrade; use CometBft * Remove duplicte message events * Add changelog for v0.31.0 (#1188) * Start changelog for v0.31.0 * Add ICA upgrade * Add proto version link to buf.build * Update changelog (#1239) * Update changelog * Update changelog with latest changes * Set release date --------- Co-authored-by: pinosu <95283998+pinosu@users.noreply.github.com> (cherry picked from commitbc0e817912) * Remove new message type event * Support msg update params gov proposal (#1247) * Add MsgUpdateParams support * Implement UpdateParams msg * Fix test UpdateParams * Add migration test * Fix * Fix lint issues * Revert changes according to review feedback * Remove more x/params dependencies * Remove x/params from genesis test * Formatting * Restore old changes * fix lint * Fix tests and restructure migrations * Rename alias for convention --------- Co-authored-by: Alex Peters <alpe@users.noreply.github.com> * Fix test data generator (#1263) * linting 47 pr (#1261) * lint cosmwasm for sdk 47 * fix * remove setGenesis * remove additional unused functions * pass tests * use SDK's errors module * unecessary conversions * unnecessary conversions * remove unneeded event manager * complete linting of tests for 47 * add test for reimportation * check errors * Update x/wasm/keeper/proposal_integration_test.go Co-authored-by: Alexander Peters <alpe@users.noreply.github.com> * apply suggestion * suggestions * lints * don't return error in when making new transactions * no todo's in the code * Fix test data generator * Update x/wasm/types/genesis_test.go Co-authored-by: Alexander Peters <alpe@users.noreply.github.com> * use the full string invalid address (2 words) always --------- Co-authored-by: Alexander Peters <alpe@users.noreply.github.com> * Regenerate from proto; remove dead code; polish code * Set SDK version to v0.47x.0 (#1262) * Set SDK version to v0.47x.0 * Set chainID * Minor updates * Set chainID for simulations * Buf mod update * Use sdk tag instead of hash in buf * Bump ibc-go to v7.0.0 * faddat/re merge main (#1274) undefined --------- Co-authored-by: vuong <nguyenvuong1122000@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: llllllluc <58892938+llllllluc@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Gjermund Garaba <gjermund@garaba.net> Co-authored-by: Nikhil Suri <nikhilsuri@comcast.net> Co-authored-by: Paul <p22626262@gmail.com> Co-authored-by: pinosu <95283998+pinosu@users.noreply.github.com> Co-authored-by: ekez <zekemedley@gmail.com> Co-authored-by: Jacob Gadikian <jacobgadikian@gmail.com>
920 lines
28 KiB
Go
920 lines
28 KiB
Go
package keeper
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
wasmvm "github.com/CosmWasm/wasmvm"
|
|
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
|
|
"github.com/cometbft/cometbft/libs/log"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkErrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
|
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/CosmWasm/wasmd/x/wasm/keeper/wasmtesting"
|
|
"github.com/CosmWasm/wasmd/x/wasm/types"
|
|
)
|
|
|
|
func TestQueryAllContractState(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
|
|
contractAddr := exampleContract.Contract
|
|
contractModel := []types.Model{
|
|
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
|
|
{Key: []byte("foo"), Value: []byte(`"bar"`)},
|
|
}
|
|
require.NoError(t, keeper.importContractState(ctx, contractAddr, contractModel))
|
|
|
|
q := Querier(keeper)
|
|
specs := map[string]struct {
|
|
srcQuery *types.QueryAllContractStateRequest
|
|
expModelContains []types.Model
|
|
expModelContainsNot []types.Model
|
|
expErr *errorsmod.Error
|
|
}{
|
|
"query all": {
|
|
srcQuery: &types.QueryAllContractStateRequest{Address: contractAddr.String()},
|
|
expModelContains: contractModel,
|
|
},
|
|
"query all with unknown address": {
|
|
srcQuery: &types.QueryAllContractStateRequest{Address: RandomBech32AccountAddress(t)},
|
|
expErr: types.ErrNotFound,
|
|
},
|
|
"with pagination offset": {
|
|
srcQuery: &types.QueryAllContractStateRequest{
|
|
Address: contractAddr.String(),
|
|
Pagination: &query.PageRequest{
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expModelContains: []types.Model{
|
|
{Key: []byte("foo"), Value: []byte(`"bar"`)},
|
|
},
|
|
expModelContainsNot: []types.Model{
|
|
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
|
|
},
|
|
},
|
|
"with pagination limit": {
|
|
srcQuery: &types.QueryAllContractStateRequest{
|
|
Address: contractAddr.String(),
|
|
Pagination: &query.PageRequest{
|
|
Limit: 1,
|
|
},
|
|
},
|
|
expModelContains: []types.Model{
|
|
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
|
|
},
|
|
expModelContainsNot: []types.Model{
|
|
{Key: []byte("foo"), Value: []byte(`"bar"`)},
|
|
},
|
|
},
|
|
"with pagination next key": {
|
|
srcQuery: &types.QueryAllContractStateRequest{
|
|
Address: contractAddr.String(),
|
|
Pagination: &query.PageRequest{
|
|
Key: fromBase64("Y29uZmln"),
|
|
},
|
|
},
|
|
expModelContains: []types.Model{
|
|
{Key: []byte("foo"), Value: []byte(`"bar"`)},
|
|
},
|
|
expModelContainsNot: []types.Model{
|
|
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
|
|
},
|
|
},
|
|
}
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
got, err := q.AllContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
|
|
require.True(t, spec.expErr.Is(err), err)
|
|
if spec.expErr != nil {
|
|
return
|
|
}
|
|
for _, exp := range spec.expModelContains {
|
|
assert.Contains(t, got.Models, exp)
|
|
}
|
|
for _, exp := range spec.expModelContainsNot {
|
|
assert.NotContains(t, got.Models, exp)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQuerySmartContractState(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
|
|
contractAddr := exampleContract.Contract.String()
|
|
|
|
q := Querier(keeper)
|
|
specs := map[string]struct {
|
|
srcAddr sdk.AccAddress
|
|
srcQuery *types.QuerySmartContractStateRequest
|
|
expResp string
|
|
expErr error
|
|
}{
|
|
"query smart": {
|
|
srcQuery: &types.QuerySmartContractStateRequest{Address: contractAddr, QueryData: []byte(`{"verifier":{}}`)},
|
|
expResp: fmt.Sprintf(`{"verifier":"%s"}`, exampleContract.VerifierAddr.String()),
|
|
},
|
|
"query smart invalid request": {
|
|
srcQuery: &types.QuerySmartContractStateRequest{Address: contractAddr, QueryData: []byte(`{"raw":{"key":"config"}}`)},
|
|
expErr: types.ErrQueryFailed,
|
|
},
|
|
"query smart with invalid json": {
|
|
srcQuery: &types.QuerySmartContractStateRequest{Address: contractAddr, QueryData: []byte(`not a json string`)},
|
|
expErr: status.Error(codes.InvalidArgument, "invalid query data"),
|
|
},
|
|
"query smart with unknown address": {
|
|
srcQuery: &types.QuerySmartContractStateRequest{Address: RandomBech32AccountAddress(t), QueryData: []byte(`{"verifier":{}}`)},
|
|
expErr: types.ErrNotFound,
|
|
},
|
|
}
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
got, err := q.SmartContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
|
|
require.True(t, errors.Is(err, spec.expErr), "but got %+v", err)
|
|
if spec.expErr != nil {
|
|
return
|
|
}
|
|
assert.JSONEq(t, string(got.Data), spec.expResp)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQuerySmartContractPanics(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
contractAddr := BuildContractAddressClassic(1, 1)
|
|
keepers.WasmKeeper.storeCodeInfo(ctx, 1, types.CodeInfo{})
|
|
keepers.WasmKeeper.storeContractInfo(ctx, contractAddr, &types.ContractInfo{
|
|
CodeID: 1,
|
|
Created: types.NewAbsoluteTxPosition(ctx),
|
|
})
|
|
ctx = ctx.WithGasMeter(sdk.NewGasMeter(DefaultInstanceCost)).WithLogger(log.TestingLogger())
|
|
|
|
specs := map[string]struct {
|
|
doInContract func()
|
|
expErr *errorsmod.Error
|
|
}{
|
|
"out of gas": {
|
|
doInContract: func() {
|
|
ctx.GasMeter().ConsumeGas(ctx.GasMeter().Limit()+1, "test - consume more than limit")
|
|
},
|
|
expErr: sdkErrors.ErrOutOfGas,
|
|
},
|
|
"other panic": {
|
|
doInContract: func() {
|
|
panic("my panic")
|
|
},
|
|
expErr: sdkErrors.ErrPanic,
|
|
},
|
|
}
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
keepers.WasmKeeper.wasmVM = &wasmtesting.MockWasmer{QueryFn: func(checksum wasmvm.Checksum, env wasmvmtypes.Env, queryMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) ([]byte, uint64, error) {
|
|
spec.doInContract()
|
|
return nil, 0, nil
|
|
}}
|
|
// when
|
|
q := Querier(keepers.WasmKeeper)
|
|
got, err := q.SmartContractState(sdk.WrapSDKContext(ctx), &types.QuerySmartContractStateRequest{
|
|
Address: contractAddr.String(),
|
|
QueryData: types.RawContractMessage("{}"),
|
|
})
|
|
require.True(t, spec.expErr.Is(err), "got error: %+v", err)
|
|
assert.Nil(t, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryRawContractState(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
exampleContract := InstantiateHackatomExampleContract(t, ctx, keepers)
|
|
contractAddr := exampleContract.Contract.String()
|
|
contractModel := []types.Model{
|
|
{Key: []byte("foo"), Value: []byte(`"bar"`)},
|
|
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
|
|
}
|
|
require.NoError(t, keeper.importContractState(ctx, exampleContract.Contract, contractModel))
|
|
|
|
q := Querier(keeper)
|
|
specs := map[string]struct {
|
|
srcQuery *types.QueryRawContractStateRequest
|
|
expData []byte
|
|
expErr *errorsmod.Error
|
|
}{
|
|
"query raw key": {
|
|
srcQuery: &types.QueryRawContractStateRequest{Address: contractAddr, QueryData: []byte("foo")},
|
|
expData: []byte(`"bar"`),
|
|
},
|
|
"query raw contract binary key": {
|
|
srcQuery: &types.QueryRawContractStateRequest{Address: contractAddr, QueryData: []byte{0x0, 0x1}},
|
|
expData: []byte(`{"count":8}`),
|
|
},
|
|
"query non-existent raw key": {
|
|
srcQuery: &types.QueryRawContractStateRequest{Address: contractAddr, QueryData: []byte("not existing key")},
|
|
expData: nil,
|
|
},
|
|
"query empty raw key": {
|
|
srcQuery: &types.QueryRawContractStateRequest{Address: contractAddr, QueryData: []byte("")},
|
|
expData: nil,
|
|
},
|
|
"query nil raw key": {
|
|
srcQuery: &types.QueryRawContractStateRequest{Address: contractAddr},
|
|
expData: nil,
|
|
},
|
|
"query raw with unknown address": {
|
|
srcQuery: &types.QueryRawContractStateRequest{Address: RandomBech32AccountAddress(t), QueryData: []byte("foo")},
|
|
expErr: types.ErrNotFound,
|
|
},
|
|
}
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
got, err := q.RawContractState(sdk.WrapSDKContext(ctx), spec.srcQuery)
|
|
require.True(t, spec.expErr.Is(err), err)
|
|
if spec.expErr != nil {
|
|
return
|
|
}
|
|
assert.Equal(t, spec.expData, got.Data)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryContractListByCodeOrdering(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 1000000))
|
|
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 500))
|
|
creator := keepers.Faucet.NewFundedRandomAccount(ctx, deposit...)
|
|
anyAddr := keepers.Faucet.NewFundedRandomAccount(ctx, topUp...)
|
|
|
|
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
|
|
require.NoError(t, err)
|
|
|
|
codeID, _, err := keepers.ContractKeeper.Create(ctx, creator, wasmCode, nil)
|
|
require.NoError(t, err)
|
|
|
|
_, bob := keyPubAddr()
|
|
initMsg := HackatomExampleInitMsg{
|
|
Verifier: anyAddr,
|
|
Beneficiary: bob,
|
|
}
|
|
initMsgBz, err := json.Marshal(initMsg)
|
|
require.NoError(t, err)
|
|
|
|
// manage some realistic block settings
|
|
var h int64 = 10
|
|
setBlock := func(ctx sdk.Context, height int64) sdk.Context {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
meter := sdk.NewGasMeter(1000000)
|
|
ctx = ctx.WithGasMeter(meter)
|
|
ctx = ctx.WithBlockGasMeter(meter)
|
|
return ctx
|
|
}
|
|
|
|
// create 10 contracts with real block/gas setup
|
|
for i := 0; i < 10; i++ {
|
|
// 3 tx per block, so we ensure both comparisons work
|
|
if i%3 == 0 {
|
|
ctx = setBlock(ctx, h)
|
|
h++
|
|
}
|
|
_, _, err = keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// query and check the results are properly sorted
|
|
q := Querier(keeper)
|
|
res, err := q.ContractsByCode(sdk.WrapSDKContext(ctx), &types.QueryContractsByCodeRequest{CodeId: codeID})
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 10, len(res.Contracts))
|
|
|
|
for _, contractAddr := range res.Contracts {
|
|
assert.NotEmpty(t, contractAddr)
|
|
}
|
|
}
|
|
|
|
func TestQueryContractHistory(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
var (
|
|
myContractBech32Addr = RandomBech32AccountAddress(t)
|
|
otherBech32Addr = RandomBech32AccountAddress(t)
|
|
)
|
|
|
|
specs := map[string]struct {
|
|
srcHistory []types.ContractCodeHistoryEntry
|
|
req types.QueryContractHistoryRequest
|
|
expContent []types.ContractCodeHistoryEntry
|
|
}{
|
|
"response with internal fields cleared": {
|
|
srcHistory: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeGenesis,
|
|
CodeID: firstCodeID,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2},
|
|
Msg: []byte(`"init message"`),
|
|
}},
|
|
req: types.QueryContractHistoryRequest{Address: myContractBech32Addr},
|
|
expContent: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeGenesis,
|
|
CodeID: firstCodeID,
|
|
Msg: []byte(`"init message"`),
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2},
|
|
}},
|
|
},
|
|
"response with multiple entries": {
|
|
srcHistory: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeInit,
|
|
CodeID: firstCodeID,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2},
|
|
Msg: []byte(`"init message"`),
|
|
}, {
|
|
Operation: types.ContractCodeHistoryOperationTypeMigrate,
|
|
CodeID: 2,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 3, TxIndex: 4},
|
|
Msg: []byte(`"migrate message 1"`),
|
|
}, {
|
|
Operation: types.ContractCodeHistoryOperationTypeMigrate,
|
|
CodeID: 3,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 5, TxIndex: 6},
|
|
Msg: []byte(`"migrate message 2"`),
|
|
}},
|
|
req: types.QueryContractHistoryRequest{Address: myContractBech32Addr},
|
|
expContent: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeInit,
|
|
CodeID: firstCodeID,
|
|
Msg: []byte(`"init message"`),
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2},
|
|
}, {
|
|
Operation: types.ContractCodeHistoryOperationTypeMigrate,
|
|
CodeID: 2,
|
|
Msg: []byte(`"migrate message 1"`),
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 3, TxIndex: 4},
|
|
}, {
|
|
Operation: types.ContractCodeHistoryOperationTypeMigrate,
|
|
CodeID: 3,
|
|
Msg: []byte(`"migrate message 2"`),
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 5, TxIndex: 6},
|
|
}},
|
|
},
|
|
"with pagination offset": {
|
|
srcHistory: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeInit,
|
|
CodeID: firstCodeID,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2},
|
|
Msg: []byte(`"init message"`),
|
|
}, {
|
|
Operation: types.ContractCodeHistoryOperationTypeMigrate,
|
|
CodeID: 2,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 3, TxIndex: 4},
|
|
Msg: []byte(`"migrate message 1"`),
|
|
}},
|
|
req: types.QueryContractHistoryRequest{
|
|
Address: myContractBech32Addr,
|
|
Pagination: &query.PageRequest{
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expContent: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeMigrate,
|
|
CodeID: 2,
|
|
Msg: []byte(`"migrate message 1"`),
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 3, TxIndex: 4},
|
|
}},
|
|
},
|
|
"with pagination limit": {
|
|
srcHistory: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeInit,
|
|
CodeID: firstCodeID,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2},
|
|
Msg: []byte(`"init message"`),
|
|
}, {
|
|
Operation: types.ContractCodeHistoryOperationTypeMigrate,
|
|
CodeID: 2,
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 3, TxIndex: 4},
|
|
Msg: []byte(`"migrate message 1"`),
|
|
}},
|
|
req: types.QueryContractHistoryRequest{
|
|
Address: myContractBech32Addr,
|
|
Pagination: &query.PageRequest{
|
|
Limit: 1,
|
|
},
|
|
},
|
|
expContent: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeInit,
|
|
CodeID: firstCodeID,
|
|
Msg: []byte(`"init message"`),
|
|
Updated: &types.AbsoluteTxPosition{BlockHeight: 1, TxIndex: 2},
|
|
}},
|
|
},
|
|
"unknown contract address": {
|
|
req: types.QueryContractHistoryRequest{Address: otherBech32Addr},
|
|
srcHistory: []types.ContractCodeHistoryEntry{{
|
|
Operation: types.ContractCodeHistoryOperationTypeGenesis,
|
|
CodeID: firstCodeID,
|
|
Updated: types.NewAbsoluteTxPosition(ctx),
|
|
Msg: []byte(`"init message"`),
|
|
}},
|
|
expContent: nil,
|
|
},
|
|
}
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
xCtx, _ := ctx.CacheContext()
|
|
|
|
cAddr, _ := sdk.AccAddressFromBech32(myContractBech32Addr)
|
|
keeper.appendToContractHistory(xCtx, cAddr, spec.srcHistory...)
|
|
|
|
// when
|
|
q := Querier(keeper)
|
|
got, err := q.ContractHistory(sdk.WrapSDKContext(xCtx), &spec.req)
|
|
|
|
// then
|
|
if spec.expContent == nil {
|
|
require.Error(t, types.ErrEmpty)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
assert.Equal(t, spec.expContent, got.Entries)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryCodeList(t *testing.T) {
|
|
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
|
|
require.NoError(t, err)
|
|
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
specs := map[string]struct {
|
|
storedCodeIDs []uint64
|
|
req types.QueryCodesRequest
|
|
expCodeIDs []uint64
|
|
}{
|
|
"none": {},
|
|
"no gaps": {
|
|
storedCodeIDs: []uint64{1, 2, 3},
|
|
expCodeIDs: []uint64{1, 2, 3},
|
|
},
|
|
"with gaps": {
|
|
storedCodeIDs: []uint64{2, 4, 6},
|
|
expCodeIDs: []uint64{2, 4, 6},
|
|
},
|
|
"with pagination offset": {
|
|
storedCodeIDs: []uint64{1, 2, 3},
|
|
req: types.QueryCodesRequest{
|
|
Pagination: &query.PageRequest{
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expCodeIDs: []uint64{2, 3},
|
|
},
|
|
"with pagination limit": {
|
|
storedCodeIDs: []uint64{1, 2, 3},
|
|
req: types.QueryCodesRequest{
|
|
Pagination: &query.PageRequest{
|
|
Limit: 2,
|
|
},
|
|
},
|
|
expCodeIDs: []uint64{1, 2},
|
|
},
|
|
"with pagination next key": {
|
|
storedCodeIDs: []uint64{1, 2, 3},
|
|
req: types.QueryCodesRequest{
|
|
Pagination: &query.PageRequest{
|
|
Key: fromBase64("AAAAAAAAAAI="),
|
|
},
|
|
},
|
|
expCodeIDs: []uint64{2, 3},
|
|
},
|
|
}
|
|
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
xCtx, _ := ctx.CacheContext()
|
|
|
|
for _, codeID := range spec.storedCodeIDs {
|
|
require.NoError(t, keeper.importCode(xCtx, codeID,
|
|
types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode)),
|
|
wasmCode),
|
|
)
|
|
}
|
|
// when
|
|
q := Querier(keeper)
|
|
got, err := q.Codes(sdk.WrapSDKContext(xCtx), &spec.req)
|
|
|
|
// then
|
|
require.NoError(t, err)
|
|
require.NotNil(t, got.CodeInfos)
|
|
require.Len(t, got.CodeInfos, len(spec.expCodeIDs))
|
|
for i, exp := range spec.expCodeIDs {
|
|
assert.EqualValues(t, exp, got.CodeInfos[i].CodeID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryContractInfo(t *testing.T) {
|
|
var (
|
|
contractAddr = RandomAccountAddress(t)
|
|
anyDate = time.Now().UTC()
|
|
)
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
// register an example extension. must be protobuf
|
|
keepers.EncodingConfig.InterfaceRegistry.RegisterImplementations(
|
|
(*types.ContractInfoExtension)(nil),
|
|
&govv1beta1.Proposal{},
|
|
)
|
|
govv1beta1.RegisterInterfaces(keepers.EncodingConfig.InterfaceRegistry)
|
|
|
|
k := keepers.WasmKeeper
|
|
querier := NewGrpcQuerier(k.cdc, k.storeKey, k, k.queryGasLimit)
|
|
myExtension := func(info *types.ContractInfo) {
|
|
// abuse gov proposal as a random protobuf extension with an Any type
|
|
myExt, err := govv1beta1.NewProposal(&govv1beta1.TextProposal{Title: "foo", Description: "bar"}, 1, anyDate, anyDate)
|
|
require.NoError(t, err)
|
|
myExt.TotalDeposit = nil
|
|
err = info.SetExtension(&myExt)
|
|
require.NoError(t, err)
|
|
}
|
|
specs := map[string]struct {
|
|
src *types.QueryContractInfoRequest
|
|
stored types.ContractInfo
|
|
expRsp *types.QueryContractInfoResponse
|
|
expErr bool
|
|
}{
|
|
"found": {
|
|
src: &types.QueryContractInfoRequest{Address: contractAddr.String()},
|
|
stored: types.ContractInfoFixture(),
|
|
expRsp: &types.QueryContractInfoResponse{
|
|
Address: contractAddr.String(),
|
|
ContractInfo: types.ContractInfoFixture(),
|
|
},
|
|
},
|
|
"with extension": {
|
|
src: &types.QueryContractInfoRequest{Address: contractAddr.String()},
|
|
stored: types.ContractInfoFixture(myExtension),
|
|
expRsp: &types.QueryContractInfoResponse{
|
|
Address: contractAddr.String(),
|
|
ContractInfo: types.ContractInfoFixture(myExtension),
|
|
},
|
|
},
|
|
"not found": {
|
|
src: &types.QueryContractInfoRequest{Address: RandomBech32AccountAddress(t)},
|
|
stored: types.ContractInfoFixture(),
|
|
expErr: true,
|
|
},
|
|
}
|
|
for name, spec := range specs {
|
|
t.Run(name, func(t *testing.T) {
|
|
xCtx, _ := ctx.CacheContext()
|
|
k.storeContractInfo(xCtx, contractAddr, &spec.stored)
|
|
// when
|
|
gotRsp, gotErr := querier.ContractInfo(sdk.WrapSDKContext(xCtx), spec.src)
|
|
if spec.expErr {
|
|
require.Error(t, gotErr)
|
|
return
|
|
}
|
|
require.NoError(t, gotErr)
|
|
assert.Equal(t, spec.expRsp, gotRsp)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryPinnedCodes(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
exampleContract1 := InstantiateHackatomExampleContract(t, ctx, keepers)
|
|
exampleContract2 := InstantiateIBCReflectContract(t, ctx, keepers)
|
|
require.NoError(t, keeper.pinCode(ctx, exampleContract1.CodeID))
|
|
require.NoError(t, keeper.pinCode(ctx, exampleContract2.CodeID))
|
|
|
|
q := Querier(keeper)
|
|
specs := map[string]struct {
|
|
srcQuery *types.QueryPinnedCodesRequest
|
|
expCodeIDs []uint64
|
|
expErr *errorsmod.Error
|
|
}{
|
|
"query all": {
|
|
srcQuery: &types.QueryPinnedCodesRequest{},
|
|
expCodeIDs: []uint64{exampleContract1.CodeID, exampleContract2.CodeID},
|
|
},
|
|
"with pagination offset": {
|
|
srcQuery: &types.QueryPinnedCodesRequest{
|
|
Pagination: &query.PageRequest{
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expCodeIDs: []uint64{exampleContract2.CodeID},
|
|
},
|
|
"with pagination limit": {
|
|
srcQuery: &types.QueryPinnedCodesRequest{
|
|
Pagination: &query.PageRequest{
|
|
Limit: 1,
|
|
},
|
|
},
|
|
expCodeIDs: []uint64{exampleContract1.CodeID},
|
|
},
|
|
"with pagination next key": {
|
|
srcQuery: &types.QueryPinnedCodesRequest{
|
|
Pagination: &query.PageRequest{
|
|
Key: fromBase64("AAAAAAAAAAM="),
|
|
},
|
|
},
|
|
expCodeIDs: []uint64{exampleContract2.CodeID},
|
|
},
|
|
}
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
got, err := q.PinnedCodes(sdk.WrapSDKContext(ctx), spec.srcQuery)
|
|
require.True(t, spec.expErr.Is(err), err)
|
|
if spec.expErr != nil {
|
|
return
|
|
}
|
|
require.NotNil(t, got)
|
|
assert.Equal(t, spec.expCodeIDs, got.CodeIDs)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryParams(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
q := Querier(keeper)
|
|
|
|
paramsResponse, err := q.Params(sdk.WrapSDKContext(ctx), &types.QueryParamsRequest{})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, paramsResponse)
|
|
|
|
defaultParams := types.DefaultParams()
|
|
|
|
require.Equal(t, paramsResponse.Params.CodeUploadAccess, defaultParams.CodeUploadAccess)
|
|
require.Equal(t, paramsResponse.Params.InstantiateDefaultPermission, defaultParams.InstantiateDefaultPermission)
|
|
|
|
err = keeper.SetParams(ctx, types.Params{
|
|
CodeUploadAccess: types.AllowNobody,
|
|
InstantiateDefaultPermission: types.AccessTypeNobody,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
paramsResponse, err = q.Params(sdk.WrapSDKContext(ctx), &types.QueryParamsRequest{})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, paramsResponse)
|
|
|
|
require.Equal(t, paramsResponse.Params.CodeUploadAccess, types.AllowNobody)
|
|
require.Equal(t, paramsResponse.Params.InstantiateDefaultPermission, types.AccessTypeNobody)
|
|
}
|
|
|
|
func TestQueryCodeInfo(t *testing.T) {
|
|
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
|
|
require.NoError(t, err)
|
|
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
anyAddress, err := sdk.AccAddressFromBech32("cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz")
|
|
require.NoError(t, err)
|
|
specs := map[string]struct {
|
|
codeID uint64
|
|
accessConfig types.AccessConfig
|
|
}{
|
|
"everybody": {
|
|
codeID: 1,
|
|
accessConfig: types.AllowEverybody,
|
|
},
|
|
"nobody": {
|
|
codeID: 10,
|
|
accessConfig: types.AllowNobody,
|
|
},
|
|
"with_address": {
|
|
codeID: 20,
|
|
accessConfig: types.AccessTypeOnlyAddress.With(anyAddress),
|
|
},
|
|
}
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
codeInfo := types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode))
|
|
codeInfo.InstantiateConfig = spec.accessConfig
|
|
require.NoError(t, keeper.importCode(ctx, spec.codeID,
|
|
codeInfo,
|
|
wasmCode),
|
|
)
|
|
|
|
q := Querier(keeper)
|
|
got, err := q.Code(sdk.WrapSDKContext(ctx), &types.QueryCodeRequest{
|
|
CodeId: spec.codeID,
|
|
})
|
|
require.NoError(t, err)
|
|
expectedResponse := &types.QueryCodeResponse{
|
|
CodeInfoResponse: &types.CodeInfoResponse{
|
|
CodeID: spec.codeID,
|
|
Creator: codeInfo.Creator,
|
|
DataHash: codeInfo.CodeHash,
|
|
InstantiatePermission: spec.accessConfig,
|
|
},
|
|
Data: wasmCode,
|
|
}
|
|
require.NotNil(t, got.CodeInfoResponse)
|
|
require.EqualValues(t, expectedResponse, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryCodeInfoList(t *testing.T) {
|
|
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
|
|
require.NoError(t, err)
|
|
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
keeper := keepers.WasmKeeper
|
|
|
|
anyAddress, err := sdk.AccAddressFromBech32("cosmos100dejzacpanrldpjjwksjm62shqhyss44jf5xz")
|
|
require.NoError(t, err)
|
|
codeInfoWithConfig := func(accessConfig types.AccessConfig) types.CodeInfo {
|
|
codeInfo := types.CodeInfoFixture(types.WithSHA256CodeHash(wasmCode))
|
|
codeInfo.InstantiateConfig = accessConfig
|
|
return codeInfo
|
|
}
|
|
|
|
codes := []struct {
|
|
name string
|
|
codeID uint64
|
|
codeInfo types.CodeInfo
|
|
}{
|
|
{
|
|
name: "everybody",
|
|
codeID: 1,
|
|
codeInfo: codeInfoWithConfig(types.AllowEverybody),
|
|
},
|
|
{
|
|
codeID: 10,
|
|
name: "nobody",
|
|
codeInfo: codeInfoWithConfig(types.AllowNobody),
|
|
},
|
|
{
|
|
name: "with_address",
|
|
codeID: 20,
|
|
codeInfo: codeInfoWithConfig(types.AccessTypeOnlyAddress.With(anyAddress)),
|
|
},
|
|
}
|
|
|
|
allCodesResponse := make([]types.CodeInfoResponse, 0)
|
|
for _, code := range codes {
|
|
t.Run(fmt.Sprintf("import_%s", code.name), func(t *testing.T) {
|
|
require.NoError(t, keeper.importCode(ctx, code.codeID,
|
|
code.codeInfo,
|
|
wasmCode),
|
|
)
|
|
})
|
|
|
|
allCodesResponse = append(allCodesResponse, types.CodeInfoResponse{
|
|
CodeID: code.codeID,
|
|
Creator: code.codeInfo.Creator,
|
|
DataHash: code.codeInfo.CodeHash,
|
|
InstantiatePermission: code.codeInfo.InstantiateConfig,
|
|
})
|
|
}
|
|
q := Querier(keeper)
|
|
got, err := q.Codes(sdk.WrapSDKContext(ctx), &types.QueryCodesRequest{
|
|
Pagination: &query.PageRequest{
|
|
Limit: 3,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
require.Len(t, got.CodeInfos, 3)
|
|
require.EqualValues(t, allCodesResponse, got.CodeInfos)
|
|
}
|
|
|
|
func TestQueryContractsByCreatorList(t *testing.T) {
|
|
ctx, keepers := CreateTestInput(t, false, AvailableCapabilities)
|
|
|
|
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 1000000))
|
|
topUp := sdk.NewCoins(sdk.NewInt64Coin("denom", 500))
|
|
creator := keepers.Faucet.NewFundedRandomAccount(ctx, deposit...)
|
|
anyAddr := keepers.Faucet.NewFundedRandomAccount(ctx, topUp...)
|
|
|
|
wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
|
|
require.NoError(t, err)
|
|
|
|
codeID, _, err := keepers.ContractKeeper.Create(ctx, creator, wasmCode, nil)
|
|
require.NoError(t, err)
|
|
|
|
_, bob := keyPubAddr()
|
|
initMsg := HackatomExampleInitMsg{
|
|
Verifier: anyAddr,
|
|
Beneficiary: bob,
|
|
}
|
|
initMsgBz, err := json.Marshal(initMsg)
|
|
require.NoError(t, err)
|
|
|
|
// manage some realistic block settings
|
|
var h int64 = 10
|
|
setBlock := func(ctx sdk.Context, height int64) sdk.Context {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
meter := sdk.NewGasMeter(1000000)
|
|
ctx = ctx.WithGasMeter(meter)
|
|
ctx = ctx.WithBlockGasMeter(meter)
|
|
return ctx
|
|
}
|
|
|
|
var allExpecedContracts []string
|
|
// create 10 contracts with real block/gas setup
|
|
for i := 0; i < 10; i++ {
|
|
ctx = setBlock(ctx, h)
|
|
h++
|
|
contract, _, err := keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, fmt.Sprintf("contract %d", i), topUp)
|
|
allExpecedContracts = append(allExpecedContracts, contract.String())
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
specs := map[string]struct {
|
|
srcQuery *types.QueryContractsByCreatorRequest
|
|
expContractAddr []string
|
|
expErr error
|
|
}{
|
|
"query all": {
|
|
srcQuery: &types.QueryContractsByCreatorRequest{
|
|
CreatorAddress: creator.String(),
|
|
},
|
|
expContractAddr: allExpecedContracts,
|
|
expErr: nil,
|
|
},
|
|
"with pagination offset": {
|
|
srcQuery: &types.QueryContractsByCreatorRequest{
|
|
CreatorAddress: creator.String(),
|
|
Pagination: &query.PageRequest{
|
|
Offset: 1,
|
|
},
|
|
},
|
|
expContractAddr: allExpecedContracts[1:],
|
|
expErr: nil,
|
|
},
|
|
"with pagination limit": {
|
|
srcQuery: &types.QueryContractsByCreatorRequest{
|
|
CreatorAddress: creator.String(),
|
|
Pagination: &query.PageRequest{
|
|
Limit: 1,
|
|
},
|
|
},
|
|
expContractAddr: allExpecedContracts[0:1],
|
|
expErr: nil,
|
|
},
|
|
"nil creator": {
|
|
srcQuery: &types.QueryContractsByCreatorRequest{
|
|
Pagination: &query.PageRequest{},
|
|
},
|
|
expContractAddr: allExpecedContracts,
|
|
expErr: errors.New("empty address string is not allowed"),
|
|
},
|
|
"nil req": {
|
|
srcQuery: nil,
|
|
expContractAddr: allExpecedContracts,
|
|
expErr: status.Error(codes.InvalidArgument, "empty request"),
|
|
},
|
|
}
|
|
|
|
q := Querier(keepers.WasmKeeper)
|
|
for msg, spec := range specs {
|
|
t.Run(msg, func(t *testing.T) {
|
|
got, err := q.ContractsByCreator(sdk.WrapSDKContext(ctx), spec.srcQuery)
|
|
|
|
if spec.expErr != nil {
|
|
require.Equal(t, spec.expErr, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
require.NotNil(t, got)
|
|
assert.Equal(t, spec.expContractAddr, got.ContractAddresses)
|
|
})
|
|
}
|
|
}
|
|
|
|
func fromBase64(s string) []byte {
|
|
r, err := base64.StdEncoding.DecodeString(s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return r
|
|
}
|