From ed684afe2592ef0aaffb8a5cd7581cb0011d4930 Mon Sep 17 00:00:00 2001 From: Nattharat Wiriyakulnan Date: Mon, 8 Jun 2020 17:17:00 +0700 Subject: [PATCH] add new rest endpoint --- CHANGELOG.md | 1 + chain/client/client.go | 1 + chain/client/result.go | 70 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 chain/client/result.go diff --git a/CHANGELOG.md b/CHANGELOG.md index d596e7f38f..4322bba47d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ ### Chain +- (feat) [\#1905](https://github.com/bandprotocol/bandchain/pull/1905) Add request + response packet to request rest endpoint. - (impv) [\#1901](https://github.com/bandprotocol/bandchain/pull/1901) Added `moniker` field to `delegations_view` table. - (feat) [\#1879](https://github.com/bandprotocol/bandchain/pull/1879) Keep `Request` and `Report` around. We avoid premature optimization at the moment. - (feat) [\#1873](https://github.com/bandprotocol/bandchain/pull/1873) Add `in-before-resolve` field in `Report` data structure. diff --git a/chain/client/client.go b/chain/client/client.go index 2095a09a8d..fb8969378c 100644 --- a/chain/client/client.go +++ b/chain/client/client.go @@ -13,4 +13,5 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { r.HandleFunc("/bandchain/evm-validators", GetEVMValidators(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/bandchain/proof/{%s}", proof.RequestIDTag), proof.GetProofHandlerFn(cliCtx)).Methods("GET") r.HandleFunc(fmt.Sprintf("/bandchain/getfile/{%s}", Filename), GetFile()).Methods("GET") + r.HandleFunc(fmt.Sprintf("/bandchain/getresult/{%s}", proof.RequestIDTag), GetResult(cliCtx)).Methods("GET") } diff --git a/chain/client/result.go b/chain/client/result.go new file mode 100644 index 0000000000..3f46f533f3 --- /dev/null +++ b/chain/client/result.go @@ -0,0 +1,70 @@ +package rpc + +import ( + "net/http" + "strconv" + + "github.com/bandprotocol/bandchain/chain/client/proof" + "github.com/bandprotocol/bandchain/chain/pkg/obi" + "github.com/bandprotocol/bandchain/chain/x/oracle" + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + "github.com/gorilla/mux" + rpcclient "github.com/tendermint/tendermint/rpc/client" + + otypes "github.com/bandprotocol/bandchain/chain/x/oracle/types" +) + +type Result struct { + ReqPacket oracle.OracleRequestPacketData `json:"oracleRequestPacketData"` + ResPacket oracle.OracleResponsePacketData `json:"oracleResponsePacketData"` +} + +func GetResult(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + intRequestID, err := strconv.ParseUint(vars[proof.RequestIDTag], 10, 64) + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } + requestID := otypes.RequestID(intRequestID) + + commit, err := cliCtx.Client.Commit(nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + resp, err := cliCtx.Client.ABCIQueryWithOptions( + "/store/oracle/key", + otypes.ResultStoreKey(requestID), + rpcclient.ABCIQueryOptions{Height: commit.Height - 1, Prove: true}, + ) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + resValue := resp.Response.GetValue() + + type result struct { + oracle.OracleRequestPacketData + oracle.OracleResponsePacketData + } + var rs result + obi.MustDecode(resValue, &rs) + reqPacket := otypes.NewOracleRequestPacketData( + rs.OracleRequestPacketData.ClientID, rs.OracleRequestPacketData.OracleScriptID, rs.OracleRequestPacketData.Calldata, + rs.OracleRequestPacketData.AskCount, rs.OracleRequestPacketData.MinCount) + resPacket := otypes.NewOracleResponsePacketData( + rs.OracleResponsePacketData.ClientID, rs.OracleResponsePacketData.RequestID, rs.OracleResponsePacketData.AnsCount, + rs.OracleResponsePacketData.RequestTime, rs.OracleResponsePacketData.ResolveTime, rs.OracleResponsePacketData.ResolveStatus, + rs.OracleResponsePacketData.Result) + + rest.PostProcessResponse(w, cliCtx, Result{ + ReqPacket: reqPacket, + ResPacket: resPacket, + }) + } +}