Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

chain: Implement request-search REST and CLI endpoint #1910

Merged
merged 6 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Chain

- (feat) [\#1910](https://github.com/bandprotocol/bandchain/pull/1910) Implement request-search REST and CLI endpoint.
- (impv) [\#1903](https://github.com/bandprotocol/bandchain/pull/1903) Store result in store instead of result hash.
- (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.
Expand Down
20 changes: 20 additions & 0 deletions chain/x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"

clientcmn "github.com/bandprotocol/bandchain/chain/x/oracle/client/common"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

Expand All @@ -28,6 +29,7 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
GetQueryCmdDataSource(storeKey, cdc),
GetQueryCmdOracleScript(storeKey, cdc),
GetQueryCmdRequest(storeKey, cdc),
GetQueryCmdRequestSearch(storeKey, cdc),
)...)
return oracleCmd
}
Expand Down Expand Up @@ -121,3 +123,21 @@ func GetQueryCmdRequest(route string, cdc *codec.Codec) *cobra.Command {
},
}
}

// GetQueryCmdRequestSearch implements the search request command.
func GetQueryCmdRequestSearch(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "request-search [oracle-script-id] [calldata] [ask-count] [min-count]",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := clientcmn.QuerySearchLatestRequest(route, cliCtx, args[0], args[1], args[2], args[3])
if err != nil {
return err
}
var out types.QueryRequestResult
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}
62 changes: 62 additions & 0 deletions chain/x/oracle/client/common/request_search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package common

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/context"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

func queryRequest(route string, cliCtx context.CLIContext, rid string) ([]byte, int64, error) {
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryRequests, rid), nil)
if err != nil {
return nil, 0, err
}
return res, height, nil
}

func QuerySearchLatestRequest(
route string, cliCtx context.CLIContext, oid, calldata, askCount, minCount string,
) ([]byte, int64, error) {

events := []string{
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyOracleScriptID, oid),
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyCalldata, calldata),
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyAskCount, askCount),
fmt.Sprintf("%s.%s='%s'", types.EventTypeRequest, types.AttributeKeyMinCount, minCount),
}
searchResult, err := authclient.QueryTxsByEvents(cliCtx, events, 1, 30, "desc")
smiled0g marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, 0, err
}
for _, tx := range searchResult.Txs {
for _, log := range tx.Logs {
for _, ev := range log.Events {
if ev.Type != types.EventTypeRequest {
continue
}
rid := ""
ok := true
for _, attr := range ev.Attributes {
if attr.Key == types.AttributeKeyID {
rid = attr.Value
}
if attr.Key == types.AttributeKeyOracleScriptID && attr.Value != oid ||
attr.Key == types.AttributeKeyCalldata && attr.Value != calldata ||
attr.Key == types.AttributeKeyAskCount && attr.Value != askCount ||
attr.Key == types.AttributeKeyMinCount && attr.Value != minCount {
ok = false
break
}
}
if ok && rid != "" {
// TODO: Check if request has resolved.
return queryRequest(route, cliCtx, rid)
}
}
}
}
return nil, 0, fmt.Errorf("request not found")
}
20 changes: 20 additions & 0 deletions chain/x/oracle/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"

clientcmn "github.com/bandprotocol/bandchain/chain/x/oracle/client/common"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

Expand Down Expand Up @@ -111,3 +112,22 @@ func getRequestByIDHandler(cliCtx context.CLIContext, route string) http.Handler
rest.PostProcessResponse(w, cliCtx, res)
}
}

func getRequestSearchHandler(cliCtx context.CLIContext, route string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
res, height, err := clientcmn.QuerySearchLatestRequest(
route, cliCtx,
r.FormValue("oid"), r.FormValue("calldata"), r.FormValue("min_count"), r.FormValue("ask_count"),
)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
1 change: 1 addition & 0 deletions chain/x/oracle/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
r.HandleFunc(fmt.Sprintf("/%s/data_sources/{%s}", storeName, idTag), getDataSourceByIDHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/oracle_scripts/{%s}", storeName, idTag), getOracleScriptByIDHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/requests/{%s}", storeName, idTag), getRequestByIDHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/request_search", storeName), getRequestSearchHandler(cliCtx, storeName)).Methods("GET")
}
4 changes: 4 additions & 0 deletions chain/x/oracle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ func TestRequestDataSuccess(t *testing.T) {
sdk.NewEvent(
types.EventTypeRequest,
sdk.NewAttribute(types.AttributeKeyID, "1"),
sdk.NewAttribute(types.AttributeKeyOracleScriptID, "1"),
sdk.NewAttribute(types.AttributeKeyCalldata, "62656562"), // "beeb" in hex
sdk.NewAttribute(types.AttributeKeyAskCount, "2"),
sdk.NewAttribute(types.AttributeKeyMinCount, "2"),
sdk.NewAttribute(types.AttributeKeyValidator, Validator1.ValAddress.String()),
sdk.NewAttribute(types.AttributeKeyValidator, Validator3.ValAddress.String()),
),
Expand Down
9 changes: 8 additions & 1 deletion chain/x/oracle/keeper/owasm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"encoding/hex"
"fmt"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
Expand Down Expand Up @@ -55,7 +56,13 @@ func (k Keeper) PrepareRequest(ctx sdk.Context, r types.RequestSpec, ibcInfo *ty
id := k.AddRequest(ctx, req)
// Emit an event describing a data request and asked validators.
event := sdk.NewEvent(types.EventTypeRequest)
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeyID, fmt.Sprintf("%d", id)))
event = event.AppendAttributes(
sdk.NewAttribute(types.AttributeKeyID, fmt.Sprintf("%d", id)),
sdk.NewAttribute(types.AttributeKeyOracleScriptID, fmt.Sprintf("%d", req.OracleScriptID)),
sdk.NewAttribute(types.AttributeKeyCalldata, hex.EncodeToString(req.Calldata)),
sdk.NewAttribute(types.AttributeKeyAskCount, fmt.Sprintf("%d", askCount)),
sdk.NewAttribute(types.AttributeKeyMinCount, fmt.Sprintf("%d", req.MinCount)),
)
for _, val := range req.RequestedValidators {
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeyValidator, val.String()))
}
Expand Down
6 changes: 5 additions & 1 deletion chain/x/oracle/keeper/owasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestPrepareRequestSuccess(t *testing.T) {
oracleScriptID := k.AddOracleScript(ctx, os)
calldata := []byte("beeb")
askCount := uint64(1)
minCount := uint64(2)
minCount := uint64(1)
clientID := "beeb"
requestHeight := int64(0)
rawRequestID := []types.ExternalID{1, 2, 3}
Expand All @@ -51,6 +51,10 @@ func TestPrepareRequestSuccess(t *testing.T) {
sdk.NewEvent(
types.EventTypeRequest,
sdk.NewAttribute(types.AttributeKeyID, "1"),
sdk.NewAttribute(types.AttributeKeyOracleScriptID, "1"),
sdk.NewAttribute(types.AttributeKeyCalldata, "62656562"), // "beeb" in hex
sdk.NewAttribute(types.AttributeKeyAskCount, "1"),
sdk.NewAttribute(types.AttributeKeyMinCount, "1"),
sdk.NewAttribute(types.AttributeKeyValidator, Validator1.ValAddress.String()),
),
sdk.NewEvent(
Expand Down