querySmart returns base64-encoded contract result

queryRaw and queryAllState return []model object
(array of key-hex and value-base64)
This commit is contained in:
Ethan Frey 2020-02-10 20:08:48 +01:00
parent 2f32ffa4a8
commit 28591613fa
4 changed files with 29 additions and 5 deletions

View File

@ -138,6 +138,15 @@ func queryContractStateAllHandlerFn(cliCtx context.CLIContext) http.HandlerFunc
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)
}
}
@ -156,12 +165,20 @@ 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)
}
}
@ -188,7 +205,8 @@ 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)
rest.PostProcessResponse(w, cliCtx, res)
}
}

View File

@ -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(),
@ -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)

View File

@ -2,6 +2,7 @@ package types
import (
"encoding/json"
tmBytes "github.com/tendermint/tendermint/libs/bytes"
wasmTypes "github.com/confio/go-cosmwasm/types"
@ -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

View File

@ -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) {