Skip to content

Commit

Permalink
Merge pull request cosmos#281 from CosmWasm/nil-raw-query-test
Browse files Browse the repository at this point in the history
Test WasmQuery::Raw with missing data
  • Loading branch information
ethanfrey authored Oct 7, 2020
2 parents ad4262a + 569539c commit 4c93c3f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/CosmWasm/wasmd
go 1.14

require (
github.com/CosmWasm/go-cosmwasm v0.11.0-alpha2
github.com/CosmWasm/go-cosmwasm v0.11.0-rc
github.com/cosmos/cosmos-sdk v0.39.1-0.20200727135228-9d00f712e334
github.com/golang/mock v1.4.3 // indirect
github.com/google/gofuzz v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/CosmWasm/go-cosmwasm v0.11.0-alpha1 h1:5c87JcnA+ncQlSJO/mEK6z9oIi/oS4
github.com/CosmWasm/go-cosmwasm v0.11.0-alpha1/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc=
github.com/CosmWasm/go-cosmwasm v0.11.0-alpha2 h1:w42GtYC4P/6dXOVlqEutr96tSsy/EO0aC9d3sbkk5hs=
github.com/CosmWasm/go-cosmwasm v0.11.0-alpha2/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc=
github.com/CosmWasm/go-cosmwasm v0.11.0-rc h1:qKMUR/84LluMStwuOgaWxupQj8STmnRaxFgFXrhY95o=
github.com/CosmWasm/go-cosmwasm v0.11.0-rc/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
Expand Down
2 changes: 0 additions & 2 deletions x/wasm/internal/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,10 @@ func TestQueryContractState(t *testing.T) {

// if smart query, check custom response
if spec.srcPath[2] != QueryMethodContractStateAll {
fmt.Printf("path: %s\n", spec.srcPath[2])
require.Equal(t, spec.expRes, binResult)
return
}

fmt.Printf("all state: %s\n", spec.srcPath[2])
// otherwise, check returned models
var r []types.Model
if spec.expErr == nil {
Expand Down
47 changes: 47 additions & 0 deletions x/wasm/internal/keeper/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,54 @@ func TestMaskReflectWasmQueries(t *testing.T) {
var reflectStateRes maskState
mustParse(t, reflectRawRes.Data, &reflectStateRes)
require.Equal(t, reflectStateRes.Owner, []byte(creator))
}

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)
accKeeper, keeper := keepers.AccountKeeper, keepers.WasmKeeper

deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
creator := createFakeFundedAccount(ctx, accKeeper, deposit)

// upload mask code
maskCode, err := ioutil.ReadFile("./testdata/reflect.wasm")
require.NoError(t, err)
maskID, err := keeper.Create(ctx, creator, maskCode, "", "", nil)
require.NoError(t, err)
require.Equal(t, uint64(1), maskID)

// creator instantiates a contract and gives it tokens
maskStart := sdk.NewCoins(sdk.NewInt64Coin("denom", 40000))
maskAddr, err := keeper.Instantiate(ctx, maskID, creator, nil, []byte("{}"), "mask contract 2", maskStart)
require.NoError(t, err)
require.NotEmpty(t, maskAddr)

// control: query directly
missingKey := []byte{0, 1, 2, 3, 4}
raw := keeper.QueryRaw(ctx, maskAddr, missingKey)
require.Nil(t, raw)

// and with queryRaw
reflectQuery := MaskQueryMsg{Chain: &ChainQuery{Request: &wasmTypes.QueryRequest{Wasm: &wasmTypes.WasmQuery{
Raw: &wasmTypes.RawQuery{
ContractAddr: maskAddr.String(),
Key: missingKey,
},
}}}}
reflectStateBin := buildMaskQuery(t, &reflectQuery)
res, err := keeper.QuerySmart(ctx, maskAddr, reflectStateBin)
require.NoError(t, err)

// first we pull out the data from chain response, before parsing the original response
var reflectRawRes ChainResponse
mustParse(t, res, &reflectRawRes)
// and make sure there is no data
require.Empty(t, reflectRawRes.Data)
// we get an empty byte slice not nil (if anyone care in go-land)
require.Equal(t, []byte{}, reflectRawRes.Data)
}

func checkAccount(t *testing.T, ctx sdk.Context, accKeeper auth.AccountKeeper, addr sdk.AccAddress, expected sdk.Coins) {
Expand Down

0 comments on commit 4c93c3f

Please sign in to comment.