Skip to content

Commit

Permalink
Merge pull request #71 from cosmwasm/properly-encode-binary
Browse files Browse the repository at this point in the history
Properly encode contract query results
  • Loading branch information
ethanfrey authored Feb 11, 2020
2 parents 2f32ffa + 660407b commit e1a2cc3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
28 changes: 25 additions & 3 deletions x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ func queryContractStateAllHandlerFn(cliCtx context.CLIContext) http.HandlerFunc
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, string(res))

// parse res
var resultData []types.Model
err = json.Unmarshal(res, &resultData)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, resultData)
}
}

Expand All @@ -156,15 +164,27 @@ func queryContractStateRawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc
return
}
route := fmt.Sprintf("custom/%s/%s/%s/%s", types.QuerierRoute, keeper.QueryGetContractState, addr.String(), keeper.QueryMethodContractStateRaw)

res, _, err := cliCtx.QueryWithData(route, queryData)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, string(res))
// parse res
var resultData []types.Model
err = json.Unmarshal(res, &resultData)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, resultData)
}
}

type smartResponse struct {
Smart []byte `json:"smart"`
}

func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
decoder := newArgDecoder(hex.DecodeString)
Expand All @@ -188,7 +208,9 @@ func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFun
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, string(res))
// return as raw bytes (to be base64-encoded)
responseData := smartResponse{Smart: res}
rest.PostProcessResponse(w, cliCtx, responseData)
}
}

Expand Down
3 changes: 3 additions & 0 deletions x/wasm/internal/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func queryContractState(ctx sdk.Context, bech, queryMethod string, req abci.Requ
var resultData []types.Model
switch queryMethod {
case QueryMethodContractStateAll:
// this returns a serialized json object (which internally encoded binary fields properly)
for iter := keeper.GetContractState(ctx, contractAddr); iter.Valid(); iter.Next() {
resultData = append(resultData, types.Model{
Key: iter.Key(),
Expand All @@ -123,8 +124,10 @@ func queryContractState(ctx sdk.Context, bech, queryMethod string, req abci.Requ
resultData = make([]types.Model, 0)
}
case QueryMethodContractStateRaw:
// this returns a serialized json object
resultData = keeper.QueryRaw(ctx, contractAddr, req.Data)
case QueryMethodContractStateSmart:
// this returns raw bytes (must be base64-encoded)
return keeper.QuerySmart(ctx, contractAddr, req.Data)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, queryMethod)
Expand Down
7 changes: 5 additions & 2 deletions x/wasm/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"

tmBytes "github.com/tendermint/tendermint/libs/bytes"

wasmTypes "github.com/confio/go-cosmwasm/types"
Expand All @@ -14,8 +15,10 @@ const defaultQueryGasLimit = uint64(3000000)

// Model is a struct that holds a KV pair
type Model struct {
Key tmBytes.HexBytes `json:"key"`
Value json.RawMessage `json:"val"`
// hex-encode key to read it better (this is often ascii)
Key tmBytes.HexBytes `json:"key"`
// base64-encode raw value
Value []byte `json:"val"`
}

// CodeInfo is data for the uploaded contract WASM code
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func assertContractState(t *testing.T, q sdk.Querier, ctx sdk.Context, addr sdk.

expectedBz, err := json.Marshal(expected)
require.NoError(t, err)
assert.Equal(t, json.RawMessage(expectedBz), res[0].Value)
assert.Equal(t, expectedBz, res[0].Value)
}

func assertContractInfo(t *testing.T, q sdk.Querier, ctx sdk.Context, addr sdk.AccAddress, codeID uint64, creator sdk.AccAddress) {
Expand Down

0 comments on commit e1a2cc3

Please sign in to comment.