Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/gov: gRPC query Service #6491

Merged
merged 100 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
483d2c4
WIP: adding gRPC for gov
atheeshp Jun 22, 2020
e6a82f6
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/52…
atheeshp Jun 22, 2020
48dccf7
removed passing new store
atheeshp Jun 23, 2020
cd1ff33
fixed error
atheeshp Jun 23, 2020
5fc80d6
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/52…
atheeshp Jun 23, 2020
d48b22c
added register query service in module
atheeshp Jun 23, 2020
8c97327
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/52…
atheeshp Jun 23, 2020
3c08159
order of imports changed
atheeshp Jun 23, 2020
cc03051
order of imports changed
atheeshp Jun 23, 2020
56dc7df
Fix proto file
sahith-narahari Jun 23, 2020
c6a67b5
added get all proposals grpc
atheeshp Jun 23, 2020
b42f53b
added more tests
atheeshp Jun 23, 2020
3bb64ee
added doc in tests
atheeshp Jun 23, 2020
1991b24
added grpc for votes
atheeshp Jun 23, 2020
85811e1
Added grpc for Deposits
atheeshp Jun 23, 2020
d86453f
updated protos
atheeshp Jun 24, 2020
a440df0
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jun 24, 2020
902ce25
added grpc for proposal, vote, deposit, tally
atheeshp Jun 24, 2020
e34baf5
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jun 24, 2020
6cc66f8
WIP: adding params grpc
atheeshp Jun 24, 2020
34acb6f
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jun 24, 2020
ee49038
added params in gRPC
atheeshp Jun 25, 2020
2e9f8a0
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jun 25, 2020
bea8cd8
updated error messages
atheeshp Jun 25, 2020
56a54af
fixed error check
atheeshp Jun 25, 2020
8496497
added more tests
atheeshp Jun 26, 2020
99f4a1c
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jun 26, 2020
88ed518
updated tests
atheeshp Jun 26, 2020
aea2b1e
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jun 29, 2020
f5b5543
added yaml types
atheeshp Jun 29, 2020
1691d14
review changes and lint issues
atheeshp Jun 29, 2020
a071ff9
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jun 29, 2020
b2ba29d
updated tests
atheeshp Jun 29, 2020
ee53887
code cleanup
sahith-narahari Jul 1, 2020
d05d272
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 2, 2020
b422756
removed cosmos.gov prefixes
atheeshp Jul 2, 2020
c1c60d2
Merge branch 'atheesh/5921-grpc-x-gov' of github.com:cosmos/cosmos-sd…
atheeshp Jul 2, 2020
08df270
added more checks
atheeshp Jul 2, 2020
940f008
added more test checks
atheeshp Jul 2, 2020
406ec2e
added filtered pagination
atheeshp Jul 2, 2020
091bf6a
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 2, 2020
21013ec
removed test check
atheeshp Jul 2, 2020
c95ac10
added tests for filtered pagination
atheeshp Jul 2, 2020
b176733
Fix Proposals
sahith-narahari Jul 3, 2020
2db581e
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 3, 2020
d256ef2
lint
atheeshp Jul 3, 2020
1ff7524
fixed error in tests
atheeshp Jul 3, 2020
6cc0224
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 3, 2020
b112263
lint issues
atheeshp Jul 3, 2020
f525912
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 3, 2020
c05154e
Add nil check for params
sahith-narahari Jul 3, 2020
7cd8efb
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 4, 2020
eec0293
Merge branch 'atheesh/5921-grpc-x-gov' of github.com:cosmos/cosmos-sd…
atheeshp Jul 4, 2020
e266bd9
Added unpacker
atheeshp Jul 4, 2020
0f4660e
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 5, 2020
c66b5ea
removed casttypes
atheeshp Jul 5, 2020
f2aeef5
review changes
atheeshp Jul 5, 2020
a629f41
Merge branch 'master' into atheesh/5921-grpc-x-gov
sahith-narahari Jul 6, 2020
44184ff
Merge branch 'master' into atheesh/5921-grpc-x-gov
anilcse Jul 7, 2020
c19eccd
use suite in grpc query tests
sahith-narahari Jul 7, 2020
3226715
migrated tests to use suite
atheeshp Jul 7, 2020
1aa5e2a
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 7, 2020
34c24cd
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 8, 2020
3321818
Merge branch 'atheesh/5921-grpc-x-gov' of github.com:cosmos/cosmos-sd…
sahith-narahari Jul 8, 2020
bf8847d
fix non-determinism
sahith-narahari Jul 9, 2020
ae380a0
Merge branch 'master' into atheesh/5921-grpc-x-gov
sahith-narahari Jul 9, 2020
f4060b6
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 9, 2020
261327f
Merge branch 'atheesh/5921-grpc-x-gov' of github.com:cosmos/cosmos-sd…
atheeshp Jul 9, 2020
416a66b
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 9, 2020
00407eb
tests migrated to table driven tests
atheeshp Jul 9, 2020
bb39caa
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 9, 2020
b4d4286
fixed doc typo
atheeshp Jul 10, 2020
bdd4ab1
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 10, 2020
6bdbda4
revert change
atheeshp Jul 13, 2020
1cbb799
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 13, 2020
3ddbb5c
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 13, 2020
321ce4c
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 13, 2020
29a4161
review changes
atheeshp Jul 13, 2020
1be2274
review changes
atheeshp Jul 13, 2020
4415769
review changes
atheeshp Jul 13, 2020
690446e
review change
atheeshp Jul 13, 2020
e64978a
review changes
atheeshp Jul 13, 2020
7d28b8d
docs updated
atheeshp Jul 13, 2020
97c276a
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 14, 2020
3cb526d
review change
atheeshp Jul 14, 2020
0edada2
review changes
atheeshp Jul 14, 2020
7e162d2
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 15, 2020
34d3b35
review changes
atheeshp Jul 15, 2020
042e9e7
review changes
atheeshp Jul 15, 2020
a902cb3
Update x/gov/keeper/keeper_test.go
fedekunze Jul 15, 2020
248284a
Merge branch 'master' into atheesh/5921-grpc-x-gov
fedekunze Jul 15, 2020
4a7f3a0
Merge branch 'master' into atheesh/5921-grpc-x-gov
fedekunze Jul 15, 2020
89937aa
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/59…
atheeshp Jul 16, 2020
3684239
Merge branch 'atheesh/5921-grpc-x-gov' of github.com:cosmos/cosmos-sd…
atheeshp Jul 16, 2020
89f2777
Merge branch 'master' into atheesh/5921-grpc-x-gov
mergify[bot] Jul 16, 2020
c56d2bb
Merge branch 'master' into atheesh/5921-grpc-x-gov
mergify[bot] Jul 16, 2020
77701b3
Merge branch 'master' into atheesh/5921-grpc-x-gov
mergify[bot] Jul 16, 2020
50de9f2
Merge branch 'master' into atheesh/5921-grpc-x-gov
mergify[bot] Jul 16, 2020
efe17b8
Merge branch 'master' into atheesh/5921-grpc-x-gov
mergify[bot] Jul 16, 2020
1e7dde4
Merge branch 'master' into atheesh/5921-grpc-x-gov
mergify[bot] Jul 16, 2020
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
115 changes: 115 additions & 0 deletions proto/cosmos/gov/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
syntax = "proto3";
package cosmos.gov;

import "cosmos/query/pagination.proto";
import "gogoproto/gogo.proto";
import "cosmos/gov/gov.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/gov/types";

service Query {
// Proposal returns proposal details based on ProposalID
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
rpc Proposal (QueryProposalRequest) returns (QueryProposalResponse) {}

// Proposals queries all proposals.
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
rpc Proposals (QueryProposalsRequest) returns (QueryProposalsResponse) {}

// Vote returns Voted information based on proposalID, voterAddr
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
rpc Vote(QueryVoteRequest) returns (QueryVoteResponse) {}

// Votes queries votes of a given proposal
rpc Votes (QueryProposalRequest) returns (QueryVotesResponse) {}

// Params queries all params
// rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {}

// Deposit queries single deposit information based proposalID, depositAddr
rpc Deposit(QueryDepositRequest) returns (QueryDepositResponse) {}

// Deposits returns all deposits of a single proposal
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
rpc Deposits(QueryProposalRequest) returns (QueryDepositsResponse) {}

// TallyResult queries the tally of a proposal vote
rpc TallyResult(QueryProposalRequest) returns (QueryTallyResponse) {}
}

message QueryProposalRequest {
// unique id of the proposal
uint64 proposal_id = 1;

cosmos.query.PageRequest req = 2;
}

message QueryProposalResponse {
cosmos.gov.Proposal proposal = 1 [(gogoproto.nullable) = false];
}

// QueryProposalRequest is the request type for the Query/Proposal RPC method
message QueryProposalsRequest {
// status of the proposals.
int32 proposal_status = 1;

// Voter address for the proposals.
bytes voter = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

// Deposit addresses from the proposals.
bytes depositor = 3 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];

cosmos.query.PageRequest req = 4;
}

message QueryProposalsResponse {
repeated cosmos.gov.Proposal proposals = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

message QueryVoteRequest {
// unique id of the proposal
uint64 proposal_id = 1;

// Voter address for the proposals.
bytes voter = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
}

message QueryVoteResponse {
cosmos.gov.Vote vote = 1 [(gogoproto.nullable) = false];
}

message QueryVotesResponse {
repeated cosmos.gov.Vote votes = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

// message QueryParamsRequest {
// string params_type = 1;
// }

// message QueryParamsResponse {
// bytes VotingParams = 1 [(gogoproto.customtype) = "VotingParams", (gogoproto.nullable) = false];
// bytes DepositParams = 2 [(gogoproto.customtype) = "DepositParams", (gogoproto.nullable) = false];
// bytes TallyParams = 3 [(gogoproto.customtype) = "TallyParams", (gogoproto.nullable) = false];
// }

message QueryDepositRequest {
// unique id of the proposal
uint64 proposal_id = 1;

// Deposit addresses from the proposals.
bytes depositor = 2 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"];
}

message QueryDepositResponse {
cosmos.gov.Deposit deposit = 1 [(gogoproto.nullable) = false];
}

message QueryDepositsResponse {
repeated cosmos.gov.Deposit deposits = 1 [(gogoproto.nullable) = false];

cosmos.query.PageResponse res = 2;
}

message QueryTallyResponse {
cosmos.gov.TallyResult tally = 1 [(gogoproto.nullable) = false];
}
210 changes: 210 additions & 0 deletions x/gov/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package keeper

import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var _ types.QueryServer = Keeper{}

// Proposal returns proposal details based on ProposalID
func (q Keeper) Proposal(c context.Context, req *types.QueryProposalRequest) (*types.QueryProposalResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

proposal, found := q.GetProposal(ctx, req.ProposalId)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
if !found {
return &types.QueryProposalResponse{}, status.Errorf(codes.InvalidArgument, "Proposal not found")
}

return &types.QueryProposalResponse{Proposal: proposal}, nil
}

// Proposals implements the Query/Proposals gRPC method
func (q Keeper) Proposals(c context.Context, req *types.QueryProposalsRequest) (*types.QueryProposalsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

var proposals types.Proposals
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(q.storeKey)
proposalStore := prefix.NewStore(store, types.ProposalsKeyPrefix)

res, err := query.Paginate(proposalStore, req.Req, func(key []byte, value []byte) error {
var result types.Proposal
err := q.cdc.UnmarshalBinaryBare(value, &result)
if err != nil {
return err
}
proposals = append(proposals, result)
return nil
})

if err != nil {
return &types.QueryProposalsResponse{}, err
}

return &types.QueryProposalsResponse{Proposals: proposals, Res: res}, nil
}

// Vote returns Voted information based on proposalID, voterAddr
func (q Keeper) Vote(c context.Context, req *types.QueryVoteRequest) (*types.QueryVoteResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

vote, found := q.GetVote(ctx, req.ProposalId, req.Voter)
if !found {
return &types.QueryVoteResponse{}, status.Errorf(codes.InvalidArgument, "Vote not found")
}

return &types.QueryVoteResponse{Vote: vote}, nil
}

// Votes returns single proposal's votes
func (q Keeper) Votes(c context.Context, req *types.QueryProposalRequest) (*types.QueryVotesResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

var votes types.Votes
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(q.storeKey)
votesStore := prefix.NewStore(store, types.VotesKey(req.ProposalId))
// proposalStore := prefix.NewStore(votesStore, types.VotesKey(proposalID))

res, err := query.Paginate(votesStore, req.Req, func(key []byte, value []byte) error {
var result types.Vote
err := q.cdc.UnmarshalBinaryBare(value, &result)
if err != nil {
return err
}
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
votes = append(votes, result)
return nil
})

if err != nil {
return &types.QueryVotesResponse{}, err
}

return &types.QueryVotesResponse{Votes: votes, Res: res}, nil
}

// // Params queries all params
// func (q Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
// if req == nil {
// return nil, status.Errorf(codes.InvalidArgument, "empty request")
// }

// ctx := sdk.UnwrapSDKContext(c)

// switch req.ParamsType {
// case types.ParamDeposit:
// depositParmas := q.GetDepositParams(ctx)

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// return &types.QueryParamsResponse{DepositParams: depositParmas}, nil

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// case types.ParamVoting:
// votingParmas := q.GetVotingParams(ctx)

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// return &types.QueryParamsResponse{VotingParams: votingParmas}, nil

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// case types.ParamTallying:
// tallyParams := q.GetTallyParams(ctx)

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// return &types.QueryParamsResponse{TallyParams: tallyParams}, nil

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// default:
// return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "%s is not a valid query request path", req.ParamsType)
// }
// }

// Deposit queries single deposit information based proposalID, depositAddr
func (q Keeper) Deposit(c context.Context, req *types.QueryDepositRequest) (*types.QueryDepositResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

deposit, found := q.GetDeposit(ctx, req.ProposalId, req.Depositor)
if !found {
return &types.QueryDepositResponse{}, status.Errorf(codes.InvalidArgument, "Vote not found")
}

return &types.QueryDepositResponse{Deposit: deposit}, nil
}

// Deposits returns single proposal's all deposits
func (q Keeper) Deposits(c context.Context, req *types.QueryProposalRequest) (*types.QueryDepositsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

var deposits types.Deposits
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(q.storeKey)
depositStore := prefix.NewStore(store, types.DepositsKey(req.ProposalId))

res, err := query.Paginate(depositStore, req.Req, func(key []byte, value []byte) error {
var result types.Deposit
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
err := q.cdc.UnmarshalBinaryBare(value, &result)
if err != nil {
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return err
}
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
deposits = append(deposits, result)
return nil
})

if err != nil {
return &types.QueryDepositsResponse{}, err
}

return &types.QueryDepositsResponse{Deposits: deposits, Res: res}, nil
}

// TallyResult queries the tally of a proposal vote
func (q Keeper) TallyResult(c context.Context, req *types.QueryProposalRequest) (*types.QueryTallyResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

proposal, ok := q.GetProposal(ctx, req.ProposalId)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", req.ProposalId)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
}

var tallyResult types.TallyResult

switch {
case proposal.Status == types.StatusDepositPeriod:
tallyResult = types.EmptyTallyResult()

atheeshp marked this conversation as resolved.
Show resolved Hide resolved
case proposal.Status == types.StatusPassed || proposal.Status == types.StatusRejected:
tallyResult = proposal.FinalTallyResult

atheeshp marked this conversation as resolved.
Show resolved Hide resolved
default:
// proposal is in voting period
_, _, tallyResult = q.Tally(ctx, proposal)
}

return &types.QueryTallyResponse{Tally: tallyResult}, nil
}
Loading