Skip to content

Commit

Permalink
Merge pull request #13 from tellor-io/query-reports
Browse files Browse the repository at this point in the history
set and query reports by address
  • Loading branch information
akremstudy authored Oct 4, 2023
2 parents 67cdad6 + 129cb47 commit 74a323b
Show file tree
Hide file tree
Showing 12 changed files with 1,275 additions and 76 deletions.
120 changes: 120 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46489,6 +46489,109 @@ paths:
type: string
tags:
- Query
/layer/oracle/get_reportsby_reporter/{reporter}:
get:
operationId: LayerOracleGetReportsbyReporter
responses:
'200':
description: A successful response.
schema:
type: object
properties:
microReports:
type: array
items:
type: object
properties:
reporter:
type: string
qid:
type: string
value:
type: string
timestamp:
type: string
format: uint64
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
parameters:
- name: reporter
in: path
required: true
type: string
tags:
- Query
/layer/oracle/get_reportsby_reporter_qid/{reporter}/{qid}:
get:
operationId: LayerOracleGetReportsbyReporterQid
responses:
'200':
description: A successful response.
schema:
type: object
properties:
reports:
type: object
properties:
microReports:
type: array
items:
type: object
properties:
reporter:
type: string
qid:
type: string
value:
type: string
timestamp:
type: string
format: uint64
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
parameters:
- name: reporter
in: path
required: true
type: string
- name: qid
in: path
required: true
type: string
tags:
- Query
/layer/oracle/params:
get:
summary: Parameters queries the parameters of the module.
Expand Down Expand Up @@ -75418,6 +75521,23 @@ definitions:
timestamp:
type: string
format: uint64
layer.oracle.QueryGetReportsbyReporterResponse:
type: object
properties:
microReports:
type: array
items:
type: object
properties:
reporter:
type: string
qid:
type: string
value:
type: string
timestamp:
type: string
format: uint64
layer.oracle.QueryParamsResponse:
type: object
properties:
Expand Down
24 changes: 23 additions & 1 deletion proto/layer/oracle/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "layer/oracle/params.proto";
import "layer/oracle/reports.proto";

import "layer/oracle/micro_report.proto";
option go_package = "github.com/tellor-io/layer/x/oracle/types";

// Query defines the gRPC querier service.
Expand All @@ -24,6 +24,16 @@ service Query {
option (google.api.http).get = "/layer/oracle/get_reportsby_qid/{qId}";

}

rpc GetReportsbyReporter (QueryGetReportsbyReporterRequest) returns (QueryGetReportsbyReporterResponse) {
option (google.api.http).get = "/layer/oracle/get_reportsby_reporter/{reporter}";

}

rpc GetReportsbyReporterQid (QueryGetReportsbyReporterQidRequest) returns (QueryGetReportsbyQidResponse) {
option (google.api.http).get = "/layer/oracle/get_reportsby_reporter_qid/{reporter}/{qid}";

}
}
// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}
Expand All @@ -43,3 +53,15 @@ message QueryGetReportsbyQidResponse {
Reports reports = 1 [(gogoproto.nullable) = false];
}

message QueryGetReportsbyReporterRequest {
string reporter = 1;
}

message QueryGetReportsbyReporterResponse {
repeated MicroReport microReports = 1 [(gogoproto.nullable) = false];
}

message QueryGetReportsbyReporterQidRequest {
string reporter = 1;
string qid = 2;
}
2 changes: 2 additions & 0 deletions x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdGetReportsbyQid())
cmd.AddCommand(CmdGetReportsbyAddress())
cmd.AddCommand(CmdGetReportsbyAddressQid())

// this line is used by starport scaffolding # 1

Expand Down
46 changes: 46 additions & 0 deletions x/oracle/client/cli/query_get_reportsby_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
"github.com/tellor-io/layer/x/oracle/types"
)

var _ = strconv.Itoa(0)

func CmdGetReportsbyAddress() *cobra.Command {
cmd := &cobra.Command{
Use: "get-reports-by-address [address]",
Short: "Query CmdGetReportsbyAddress",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
reqAddress := args[0]

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryGetReportsbyReporterRequest{

Reporter: reqAddress,
}

res, err := queryClient.GetReportsbyReporter(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
48 changes: 48 additions & 0 deletions x/oracle/client/cli/query_get_reportsby_address_qid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
"github.com/tellor-io/layer/x/oracle/types"
)

var _ = strconv.Itoa(0)

func CmdGetReportsbyAddressQid() *cobra.Command {
cmd := &cobra.Command{
Use: "get-reports-by-address-qid [address] [qid]",
Short: "Query CmdGetReportsbyAddressQid",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
reqAddress := args[0]
reqQid := args[1]

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryGetReportsbyReporterQidRequest{

Reporter: reqAddress,
Qid: reqQid,
}

res, err := queryClient.GetReportsbyReporterQid(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
57 changes: 46 additions & 11 deletions x/oracle/keeper/msg_server_submit_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,48 @@ func (k msgServer) SubmitValue(goCtx context.Context, msg *types.MsgSubmitValue)
return &types.MsgSubmitValueResponse{}, nil
}

func (k Keeper) setValueByReporter(ctx sdk.Context, reporter, val string, queryId []byte) error {
queryIdHex := hex.EncodeToString(queryId)
reporterStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ReporterStoreKey))
// reporter-query id pair
reporterQueryIdKey := []byte(reporter + ":" + queryIdHex)
// get reports list from store and unmarshal
var reportsList types.Reports
if err := k.cdc.Unmarshal(reporterStore.Get(reporterQueryIdKey), &reportsList); err != nil {
return fmt.Errorf("failed to unmarshal reports: %v", err)
}
report := &types.MicroReport{
Reporter: reporter,
Qid: queryIdHex,
Value: val,
Timestamp: uint64(ctx.BlockTime().Unix()),
}
// append report to reports list
reportsList.MicroReports = append(reportsList.MicroReports, report)
reporterStore.Set(reporterQueryIdKey, k.cdc.MustMarshal(&reportsList))
return nil
}

func (k Keeper) setValueByQueryId(ctx sdk.Context, reporter, val string, queryId []byte) error {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ReportsKey))
report := &types.MicroReport{
Reporter: reporter,
Qid: hex.EncodeToString(queryId),
Value: val,
Timestamp: uint64(ctx.BlockTime().Unix()),
}
var reportsList types.Reports
if err := k.cdc.Unmarshal(store.Get(queryId), &reportsList); err != nil {
return fmt.Errorf("failed to unmarshal reports: %v", err)
}
reportsList.MicroReports = append(reportsList.MicroReports, report)
store.Set(queryId, k.cdc.MustMarshal(&reportsList))
return nil
}

func (k Keeper) setValue(ctx sdk.Context, reporter, val string, queryData []byte) error {
queryId := HashQueryData(queryData)
// decode query data hex to get query type, returns interface array
queryType, err := decodeQueryType(queryData)
if err != nil {
return status.Error(codes.InvalidArgument, fmt.Sprintf("failed to decode query type: %v", err))
Expand All @@ -82,19 +122,14 @@ func (k Keeper) setValue(ctx sdk.Context, reporter, val string, queryData []byte
if err != nil {
return status.Error(codes.InvalidArgument, fmt.Sprintf("failed to decode value: %v", err))
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ReportsKey))
report := &types.MicroReport{
Reporter: reporter,
Qid: hex.EncodeToString(queryId),
Value: val,
Timestamp: uint64(ctx.BlockTime().Unix()),
// set value by reporter
if err := k.setValueByReporter(ctx, reporter, val, queryId); err != nil {
return err
}
var reportsList types.Reports
if err := k.cdc.Unmarshal(store.Get(queryId), &reportsList); err != nil {
return status.Error(codes.InvalidArgument, fmt.Sprintf("failed to unmarshal reports: %v", err))
// set value by query id
if err := k.setValueByQueryId(ctx, reporter, val, queryId); err != nil {
return err
}
reportsList.MicroReports = append(reportsList.MicroReports, report)
store.Set(queryId, k.cdc.MustMarshal(&reportsList))
return nil
}
func getValueType(registry types.RegistryKeeper, cdc codec.BinaryCodec, ctx sdk.Context, queryType string) (string, error) {
Expand Down
Loading

0 comments on commit 74a323b

Please sign in to comment.