From 39ce03648441cd0cb5591ebd700a1a35e8c313af Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 24 Jan 2020 01:16:57 +0530 Subject: [PATCH 1/3] Added contract state query to rest --- x/wasm/client/rest/query.go | 72 +++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/x/wasm/client/rest/query.go b/x/wasm/client/rest/query.go index f64813c5e..97e7de9dd 100644 --- a/x/wasm/client/rest/query.go +++ b/x/wasm/client/rest/query.go @@ -1,6 +1,8 @@ package rest import ( + "encoding/base64" + "encoding/hex" "encoding/json" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" @@ -20,8 +22,8 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc("/wasm/contract/", listAllContractsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}", queryContractHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}/state", queryContractStateAllHandlerFn(cliCtx)).Methods("GET") - r.HandleFunc("/wasm/contract/{contractAddr}/smart", queryContractStateSmartHandlerFn(cliCtx)).Methods("GET") - r.HandleFunc("/wasm/contract/{contractAddr}/raw", queryContractStateRawHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc("/wasm/contract/{contractAddr}/smart/{query}/{encoding}", queryContractStateSmartHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc("/wasm/contract/{contractAddr}/raw/{key}/{encoding}", queryContractStateRawHandlerFn(cliCtx)).Methods("GET") } func listCodesHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { @@ -122,12 +124,78 @@ func queryContractStateAllHandlerFn(cliCtx context.CLIContext) http.HandlerFunc func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + decoder := newArgDecoder(hex.DecodeString) + + addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"]) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + decoder.encoding = mux.Vars(r)["encoding"] + queryData, err := decoder.DecodeString(mux.Vars(r)["query"]) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + 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)) } } func queryContractStateRawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + decoder := newArgDecoder(asciiDecodeString) + addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"]) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + decoder.encoding = mux.Vars(r)["encoding"] + queryData, err := decoder.DecodeString(mux.Vars(r)["key"]) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + 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)) + } +} + +type argumentDecoder struct { + // dec is the default decoder + dec func(string) ([]byte, error) + encoding string +} +func newArgDecoder(def func(string) ([]byte, error)) *argumentDecoder { + return &argumentDecoder{dec: def} +} + +func (a *argumentDecoder) DecodeString(s string) ([]byte, error) { + + switch a.encoding { + case "ascii": + return asciiDecodeString(s) + case "hex": + return hex.DecodeString(s) + case "base64": + return base64.StdEncoding.DecodeString(s) + default: + return a.dec(s) } } + +func asciiDecodeString(s string) ([]byte, error) { + return []byte(s), nil +} From 69fe83755a5e16da062e22b6f066760bc0557ac8 Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 24 Jan 2020 01:42:28 +0530 Subject: [PATCH 2/3] Added contract state smart query to rest --- x/wasm/client/rest/query.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/x/wasm/client/rest/query.go b/x/wasm/client/rest/query.go index 97e7de9dd..32c3c4638 100644 --- a/x/wasm/client/rest/query.go +++ b/x/wasm/client/rest/query.go @@ -122,17 +122,16 @@ func queryContractStateAllHandlerFn(cliCtx context.CLIContext) http.HandlerFunc } } -func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { +func queryContractStateRawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { decoder := newArgDecoder(hex.DecodeString) - addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"]) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } decoder.encoding = mux.Vars(r)["encoding"] - queryData, err := decoder.DecodeString(mux.Vars(r)["query"]) + queryData, err := decoder.DecodeString(mux.Vars(r)["key"]) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return @@ -144,25 +143,27 @@ func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFun return } rest.PostProcessResponse(w, cliCtx, string(res)) - } } -func queryContractStateRawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { +func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { decoder := newArgDecoder(asciiDecodeString) + addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"]) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } decoder.encoding = mux.Vars(r)["encoding"] - queryData, err := decoder.DecodeString(mux.Vars(r)["key"]) + + route := fmt.Sprintf("custom/%s/%s/%s/%s", types.QuerierRoute, keeper.QueryGetContractState, addr.String(), keeper.QueryMethodContractStateSmart) + + queryData, err := decoder.DecodeString(mux.Vars(r)["query"]) if err != nil { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) 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()) From f4ee2d971c2b97fe28e3d0446634b813137edc6b Mon Sep 17 00:00:00 2001 From: sahith-narahari Date: Fri, 24 Jan 2020 02:53:23 +0530 Subject: [PATCH 3/3] Modified url parameters --- x/wasm/client/rest/query.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/x/wasm/client/rest/query.go b/x/wasm/client/rest/query.go index 32c3c4638..ba0c04a1a 100644 --- a/x/wasm/client/rest/query.go +++ b/x/wasm/client/rest/query.go @@ -22,8 +22,8 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc("/wasm/contract/", listAllContractsHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}", queryContractHandlerFn(cliCtx)).Methods("GET") r.HandleFunc("/wasm/contract/{contractAddr}/state", queryContractStateAllHandlerFn(cliCtx)).Methods("GET") - r.HandleFunc("/wasm/contract/{contractAddr}/smart/{query}/{encoding}", queryContractStateSmartHandlerFn(cliCtx)).Methods("GET") - r.HandleFunc("/wasm/contract/{contractAddr}/raw/{key}/{encoding}", queryContractStateRawHandlerFn(cliCtx)).Methods("GET") + r.HandleFunc("/wasm/contract/{contractAddr}/smart/{query}", queryContractStateSmartHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET") + r.HandleFunc("/wasm/contract/{contractAddr}/raw/{key}", queryContractStateRawHandlerFn(cliCtx)).Queries("encoding", "{encoding}").Methods("GET") } func listCodesHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { @@ -148,7 +148,7 @@ func queryContractStateRawHandlerFn(cliCtx context.CLIContext) http.HandlerFunc func queryContractStateSmartHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - decoder := newArgDecoder(asciiDecodeString) + decoder := newArgDecoder(hex.DecodeString) addr, err := sdk.AccAddressFromBech32(mux.Vars(r)["contractAddr"]) if err != nil { @@ -186,8 +186,6 @@ func newArgDecoder(def func(string) ([]byte, error)) *argumentDecoder { func (a *argumentDecoder) DecodeString(s string) ([]byte, error) { switch a.encoding { - case "ascii": - return asciiDecodeString(s) case "hex": return hex.DecodeString(s) case "base64": @@ -196,7 +194,3 @@ func (a *argumentDecoder) DecodeString(s string) ([]byte, error) { return a.dec(s) } } - -func asciiDecodeString(s string) ([]byte, error) { - return []byte(s), nil -}