cosmos-sdk/x/gov/keeper/grpc_query.go

262 lines
7.5 KiB
Go
Raw Normal View History

x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
package keeper
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
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.Error(codes.InvalidArgument, "invalid request")
}
if req.ProposalId == 0 {
return nil, status.Error(codes.InvalidArgument, "proposal id can not be 0")
}
ctx := sdk.UnwrapSDKContext(c)
proposal, found := q.GetProposal(ctx, req.ProposalId)
if !found {
return nil, status.Errorf(codes.NotFound, "proposal %d doesn't exist", req.ProposalId)
}
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) {
var filteredProposals types.Proposals
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(q.storeKey)
proposalStore := prefix.NewStore(store, types.ProposalsKeyPrefix)
pageRes, err := query.FilteredPaginate(proposalStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
var p types.Proposal
if err := q.cdc.UnmarshalBinaryBare(value, &p); err != nil {
return false, status.Error(codes.Internal, err.Error())
}
matchVoter, matchDepositor, matchStatus := true, true, true
// match status (if supplied/valid)
if types.ValidProposalStatus(req.ProposalStatus) {
matchStatus = p.Status == req.ProposalStatus
}
// match voter address (if supplied)
if len(req.Voter) > 0 {
_, matchVoter = q.GetVote(ctx, p.ProposalId, req.Voter)
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
}
// match depositor (if supplied)
if len(req.Depositor) > 0 {
_, matchDepositor = q.GetDeposit(ctx, p.ProposalId, req.Depositor)
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
}
if matchVoter && matchDepositor && matchStatus {
if accumulate {
filteredProposals = append(filteredProposals, p)
}
return true, nil
}
return false, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryProposalsResponse{Proposals: filteredProposals, Pagination: pageRes}, nil
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
}
// 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.Error(codes.InvalidArgument, "invalid request")
}
if req.ProposalId == 0 {
return nil, status.Error(codes.InvalidArgument, "proposal id can not be 0")
}
if req.Voter == nil {
return nil, status.Error(codes.InvalidArgument, "empty voter address")
}
ctx := sdk.UnwrapSDKContext(c)
vote, found := q.GetVote(ctx, req.ProposalId, req.Voter)
if !found {
return nil, status.Errorf(codes.InvalidArgument,
"voter: %v not found for proposal: %v", req.Voter, req.ProposalId)
}
return &types.QueryVoteResponse{Vote: vote}, nil
}
// Votes returns single proposal's votes
func (q Keeper) Votes(c context.Context, req *types.QueryVotesRequest) (*types.QueryVotesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.ProposalId == 0 {
return nil, status.Error(codes.InvalidArgument, "proposal id can not be 0")
}
var votes types.Votes
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(q.storeKey)
votesStore := prefix.NewStore(store, types.VotesKey(req.ProposalId))
pageRes, err := query.Paginate(votesStore, req.Pagination, func(key []byte, value []byte) error {
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
var vote types.Vote
if err := q.cdc.UnmarshalBinaryBare(value, &vote); err != nil {
return err
}
votes = append(votes, vote)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryVotesResponse{Votes: votes, Pagination: pageRes}, nil
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
}
// Params queries all params
func (q Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
switch req.ParamsType {
case types.ParamDeposit:
depositParmas := q.GetDepositParams(ctx)
return &types.QueryParamsResponse{DepositParams: depositParmas}, nil
case types.ParamVoting:
votingParmas := q.GetVotingParams(ctx)
return &types.QueryParamsResponse{VotingParams: votingParmas}, nil
case types.ParamTallying:
tallyParams := q.GetTallyParams(ctx)
return &types.QueryParamsResponse{TallyParams: tallyParams}, nil
default:
return nil, status.Errorf(codes.InvalidArgument,
"%s is not a valid parameter type", 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.Error(codes.InvalidArgument, "invalid request")
}
if req.ProposalId == 0 {
return nil, status.Error(codes.InvalidArgument, "proposal id can not be 0")
}
if req.Depositor == nil {
return nil, status.Error(codes.InvalidArgument, "empty depositor address")
}
ctx := sdk.UnwrapSDKContext(c)
deposit, found := q.GetDeposit(ctx, req.ProposalId, req.Depositor)
if !found {
return nil, status.Errorf(codes.InvalidArgument,
"depositer: %v not found for proposal: %v", req.Depositor, req.ProposalId)
}
return &types.QueryDepositResponse{Deposit: deposit}, nil
}
// Deposits returns single proposal's all deposits
func (q Keeper) Deposits(c context.Context, req *types.QueryDepositsRequest) (*types.QueryDepositsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.ProposalId == 0 {
return nil, status.Error(codes.InvalidArgument, "proposal id can not be 0")
}
var deposits types.Deposits
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(q.storeKey)
depositStore := prefix.NewStore(store, types.DepositsKey(req.ProposalId))
pageRes, err := query.Paginate(depositStore, req.Pagination, func(key []byte, value []byte) error {
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
var deposit types.Deposit
if err := q.cdc.UnmarshalBinaryBare(value, &deposit); err != nil {
return err
}
deposits = append(deposits, deposit)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryDepositsResponse{Deposits: deposits, Pagination: pageRes}, nil
x/gov: gRPC query Service (#6491) * WIP: adding gRPC for gov * removed passing new store * fixed error * added register query service in module * order of imports changed * order of imports changed * Fix proto file * added get all proposals grpc * added more tests * added doc in tests * added grpc for votes * Added grpc for Deposits * updated protos * added grpc for proposal, vote, deposit, tally * WIP: adding params grpc * added params in gRPC * updated error messages * fixed error check * added more tests * updated tests * added yaml types * review changes and lint issues * updated tests * code cleanup * removed cosmos.gov prefixes * added more checks * added more test checks * added filtered pagination * removed test check * added tests for filtered pagination * Fix Proposals * lint * fixed error in tests * lint issues * Add nil check for params * Added unpacker * removed casttypes * review changes * use suite in grpc query tests * migrated tests to use suite * fix non-determinism * tests migrated to table driven tests * fixed doc typo * revert change * Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/5921-grpc-x-gov * review changes * review changes * review changes * review change * review changes * docs updated * review change * review changes * review changes * review changes * Update x/gov/keeper/keeper_test.go Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Anil Kumar Kammari <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-07-16 02:16:23 -07:00
}
// TallyResult queries the tally of a proposal vote
func (q Keeper) TallyResult(c context.Context, req *types.QueryTallyResultRequest) (*types.QueryTallyResultResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.ProposalId == 0 {
return nil, status.Error(codes.InvalidArgument, "proposal id can not be 0")
}
ctx := sdk.UnwrapSDKContext(c)
proposal, ok := q.GetProposal(ctx, req.ProposalId)
if !ok {
return nil, status.Errorf(codes.NotFound, "proposal %d doesn't exist", req.ProposalId)
}
var tallyResult types.TallyResult
switch {
case proposal.Status == types.StatusDepositPeriod:
tallyResult = types.EmptyTallyResult()
case proposal.Status == types.StatusPassed || proposal.Status == types.StatusRejected:
tallyResult = proposal.FinalTallyResult
default:
// proposal is in voting period
_, _, tallyResult = q.Tally(ctx, proposal)
}
return &types.QueryTallyResultResponse{Tally: tallyResult}, nil
}