Skip to content

Commit

Permalink
add query to fetch report ids and query_ids
Browse files Browse the repository at this point in the history
  • Loading branch information
akremstudy committed Dec 11, 2024
1 parent 85fef60 commit 0eb4950
Show file tree
Hide file tree
Showing 8 changed files with 2,635 additions and 363 deletions.
1,909 changes: 1,652 additions & 257 deletions api/layer/oracle/query.pulsar.go

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions api/layer/oracle/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion e2e/spinup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tellor-io/layer/e2e"
"github.com/tellor-io/layer/utils"
oracletypes "github.com/tellor-io/layer/x/oracle/types"

"cosmossdk.io/math"
)
Expand Down Expand Up @@ -89,7 +90,7 @@ func ExecProposal(ctx context.Context, keyName string, prop Proposal, tn *cosmos
return tn.ExecTx(ctx, keyName, command...)
}

func TestLearn(t *testing.T) {
func TestLayerFlow(t *testing.T) {
ctx := context.Background()
layer := e2e.LayerSpinup(t) // *cosmos.CosmosChain type
validatorI := layer.Validators[0]
Expand Down Expand Up @@ -204,3 +205,19 @@ func TestLearn(t *testing.T) {
require.NoError(t, err)
require.Equal(t, disputes.Disputes[0].Metadata.DisputeStatus, 2) // resolved
}

func TestGetCyclelist(t *testing.T) {
t.Skip("infinitely runs and calls CurrentCyclistQuery")
ctx := context.Background()
layer := e2e.LayerSpinup(t) // *cosmos.CosmosChain type
validatorI := layer.Validators[0]

queryClient := oracletypes.NewQueryClient(validatorI.GrpcConn)
for {
res, err := queryClient.CurrentCyclelistQuery(ctx, &oracletypes.QueryCurrentCyclelistQueryRequest{})
require.NoError(t, err)
// fmt.Println(res.QueryMeta)
require.NotNil(t, res.QueryData)
require.NotNil(t, res.QueryMeta)
}
}
14 changes: 14 additions & 0 deletions proto/layer/oracle/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ service Query {
rpc GetCurrentQueryByQueryId(QueryGetCurrentQueryByQueryIdRequest) returns (QueryGetCurrentQueryByQueryIdResponse) {
option (google.api.http).get = "/tellor-io/layer/oracle/get_current_query_by_query_id/{query_id}";
}
rpc ReportedIdsByReporter(QueryReportedIdsByReporterRequest) returns (QueryReportedIdsByReporterResponse) {
option (google.api.http).get = "/tellor-io/layer/oracle/reported_ids_by_reporter/{reporter_address}";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down Expand Up @@ -261,4 +264,15 @@ message QueryGetCurrentQueryByQueryIdRequest {

message QueryGetCurrentQueryByQueryIdResponse {
QueryMeta query = 1;
}

message QueryReportedIdsByReporterRequest {
string reporter_address = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message QueryReportedIdsByReporterResponse {
repeated uint64 ids = 1;
repeated bytes query_ids =2;
cosmos.base.query.v1beta1.PageResponse pagination = 3;
}
28 changes: 28 additions & 0 deletions x/oracle/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,31 @@ func (k Querier) TippedQueries(ctx context.Context, req *types.QueryTippedQuerie

return &types.QueryTippedQueriesResponse{Queries: queries}, nil
}

func (k Querier) ReportedIdsByReporter(ctx context.Context, req *types.QueryReportedIdsByReporterRequest) (*types.QueryReportedIdsByReporterResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

store := runtime.KVStoreAdapter(k.keeper.storeService.OpenKVStore(ctx))
reportsStore := prefix.NewStore(store, types.ReportsPrefix)
ids := make([]uint64, 0)
queryIds := make([][]byte, 0)

pageRes, err := query.Paginate(reportsStore, req.Pagination, func(key, value []byte) error {
keycdc := collections.TripleKeyCodec(collections.BytesKey, collections.BytesKey, collections.Uint64Key)
_, pk, err := keycdc.Decode(key)
if err != nil {
return err
}
ids = append(ids, pk.K3())
queryIds = append(queryIds, pk.K1())

return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryReportedIdsByReporterResponse{Ids: ids, QueryIds: queryIds, Pagination: pageRes}, nil
}
48 changes: 48 additions & 0 deletions x/oracle/keeper/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/require"
testkeeper "github.com/tellor-io/layer/testutil/keeper"
"github.com/tellor-io/layer/testutil/sample"
"github.com/tellor-io/layer/utils"
"github.com/tellor-io/layer/x/oracle/keeper"
"github.com/tellor-io/layer/x/oracle/types"
Expand Down Expand Up @@ -411,3 +412,50 @@ func (s *KeeperTestSuite) TestTippedQueries() {
})
}
}

func (s *KeeperTestSuite) TestReportedIdsByReporter() {
k := s.oracleKeeper
q := s.queryClient
reporter := sample.AccAddressBytes()
// fetching in descending order
pageReq := &query.PageRequest{Reverse: true}
queryReq := types.QueryReportedIdsByReporterRequest{ReporterAddress: reporter.String(), Pagination: pageReq}
for i := 1; i < 11; i++ {
s.NoError(k.Reports.Set(s.ctx,
collections.Join3([]byte("queryid1"), reporter.Bytes(), uint64(i)),
types.MicroReport{}))
}
res, err := q.ReportedIdsByReporter(s.ctx, &queryReq)
s.NoError(err)
s.Equal(res.Ids, []uint64{10, 9, 8, 7, 6, 5, 4, 3, 2, 1})

pageReq.Reverse = false
res, err = q.ReportedIdsByReporter(s.ctx, &queryReq)
s.NoError(err)
s.Equal(res.Ids, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})

pageReq.Limit = 1
res, err = q.ReportedIdsByReporter(s.ctx, &queryReq)
s.NoError(err)
s.Equal(res.Ids, []uint64{1})

pageReq.Limit = 1
pageReq.Reverse = true
res, err = q.ReportedIdsByReporter(s.ctx, &queryReq)
s.NoError(err)
s.Equal(res.Ids, []uint64{10})

pageReq.Limit = 5
pageReq.Offset = 1
pageReq.Reverse = true
res, err = q.ReportedIdsByReporter(s.ctx, &queryReq)
s.NoError(err)
s.Equal(res.Ids, []uint64{9, 8, 7, 6, 5})

pageReq.Limit = 5
pageReq.Offset = 4
pageReq.Reverse = false
res, err = q.ReportedIdsByReporter(s.ctx, &queryReq)
s.NoError(err)
s.Equal(res.Ids, []uint64{5, 6, 7, 8, 9})
}
Loading

0 comments on commit 0eb4950

Please sign in to comment.