Skip to content

Commit

Permalink
Fix query handler instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Sep 13, 2021
1 parent 4e242e0 commit 2fe56bf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
5 changes: 1 addition & 4 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,7 @@ func (k Keeper) reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply was
env := types.NewEnv(ctx, contractAddress)

// prepare querier
querier := QueryHandler{
Ctx: ctx,
Plugins: k.wasmVMQueryHandler,
}
querier := k.newQueryHandler(ctx, contractAddress)
gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.Reply(codeInfo.CodeHash, env, reply, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJsonDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
Expand Down
42 changes: 34 additions & 8 deletions x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1591,28 +1591,54 @@ func TestReply(t *testing.T) {
example := SeedNewContractInstance(t, ctx, keepers, &mock)

specs := map[string]struct {
rsp wasmvmtypes.Response
replyFn func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error)
expData []byte
expErr bool
expEvt sdk.Events
}{
"all good": {
rsp: wasmvmtypes.Response{Data: []byte("foo")},
replyFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
return &wasmvmtypes.Response{Data: []byte("foo")}, 1, nil
},
expData: []byte("foo"),
expEvt: sdk.Events{sdk.NewEvent("reply", sdk.NewAttribute("_contract_address", example.Contract.String()))},
},
"with query": {
replyFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
bzRsp, err := querier.Query(wasmvmtypes.QueryRequest{
Bank: &wasmvmtypes.BankQuery{
Balance: &wasmvmtypes.BalanceQuery{Address: env.Contract.Address, Denom: "stake"},
},
}, 1_000_000)
require.NoError(t, err)
var gotBankRsp wasmvmtypes.BalanceResponse
require.NoError(t, json.Unmarshal(bzRsp, &gotBankRsp))
assert.Equal(t, wasmvmtypes.BalanceResponse{Amount: wasmvmtypes.NewCoin(0, "stake")}, gotBankRsp)
return &wasmvmtypes.Response{Data: []byte("foo")}, 1, nil
},
expData: []byte("foo"),
expEvt: sdk.Events{sdk.NewEvent("reply", sdk.NewAttribute("_contract_address", example.Contract.String()))},
},
"with query error handled": {
replyFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
bzRsp, err := querier.Query(wasmvmtypes.QueryRequest{}, 0)
require.Error(t, err)
assert.Nil(t, bzRsp)
return &wasmvmtypes.Response{Data: []byte("foo")}, 1, nil
},
expData: []byte("foo"),
expEvt: sdk.Events{sdk.NewEvent("reply", sdk.NewAttribute("_contract_address", example.Contract.String()))},
},
"error": {
replyFn: func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
return nil, 1, errors.New("testing")
},
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
mock.ReplyFn = func(codeID wasmvm.Checksum, env wasmvmtypes.Env, reply wasmvmtypes.Reply, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
if spec.expErr {
return nil, 1, errors.New("testing")
}
return &spec.rsp, 1, nil
}
mock.ReplyFn = spec.replyFn
em := sdk.NewEventManager()
gotData, gotErr := k.reply(ctx.WithEventManager(em), example.Contract, wasmvmtypes.Reply{})
if spec.expErr {
Expand Down

0 comments on commit 2fe56bf

Please sign in to comment.