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

feat: v1beta1 gov query server #11029

Merged
merged 15 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -123,6 +123,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [\#9594](https://github.com/cosmos/cosmos-sdk/pull/9594) Remove legacy REST API. Please see the [REST Endpoints Migration guide](https://docs.cosmos.network/master/migrations/rest.html) to migrate to the new REST endpoints.
* [\#9995](https://github.com/cosmos/cosmos-sdk/pull/9995) Increased gas cost for creating proposals.
* [\#11029](https://github.com/cosmos/cosmos-sdk/pull/11029) The deprecated Vote Option field is removed in gov v1beta2 and nil in v1beta1. Use Options instead.

### CLI Breaking Changes

Expand Down
177 changes: 177 additions & 0 deletions x/gov/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"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/migrations/v046"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
)

Expand Down Expand Up @@ -277,3 +279,178 @@ func (q Keeper) TallyResult(c context.Context, req *v1beta2.QueryTallyResultRequ

return &v1beta2.QueryTallyResultResponse{Tally: &tallyResult}, nil
}

var _ v1beta1.QueryServer = legacyQueryServer{}

type legacyQueryServer struct {
keeper Keeper
}

func NewLegacyQueryServer(k Keeper) v1beta1.QueryServer {
return &legacyQueryServer{keeper: k}
}

func (q legacyQueryServer) Proposal(c context.Context, req *v1beta1.QueryProposalRequest) (*v1beta1.QueryProposalResponse, error) {
resp, err := q.keeper.Proposal(c, &v1beta2.QueryProposalRequest{
ProposalId: req.ProposalId,
})
if err != nil {
return nil, err
}

proposal, err := v046.ConvertToLegacyProposal(*resp.Proposal)
if err != nil {
return nil, err
}

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

func (q legacyQueryServer) Proposals(c context.Context, req *v1beta1.QueryProposalsRequest) (*v1beta1.QueryProposalsResponse, error) {
resp, err := q.keeper.Proposals(c, &v1beta2.QueryProposalsRequest{
ProposalStatus: v1beta2.ProposalStatus(req.ProposalStatus),
Voter: req.Voter,
Depositor: req.Depositor,
Pagination: req.Pagination,
})
if err != nil {
return nil, err
}

legacyProposals := make([]v1beta1.Proposal, len(resp.Proposals))
for idx, proposal := range resp.Proposals {
legacyProposals[idx], err = v046.ConvertToLegacyProposal(*proposal)
if err != nil {
return nil, err
}
}

return &v1beta1.QueryProposalsResponse{
Proposals: legacyProposals,
Pagination: resp.Pagination,
}, nil
}

func (q legacyQueryServer) Vote(c context.Context, req *v1beta1.QueryVoteRequest) (*v1beta1.QueryVoteResponse, error) {
resp, err := q.keeper.Vote(c, &v1beta2.QueryVoteRequest{
ProposalId: req.ProposalId,
Voter: req.Voter,
})
if err != nil {
return nil, err
}

vote, err := v046.ConvertToLegacyVote(*resp.Vote)
if err != nil {
return nil, err
}

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

func (q legacyQueryServer) Votes(c context.Context, req *v1beta1.QueryVotesRequest) (*v1beta1.QueryVotesResponse, error) {
resp, err := q.keeper.Votes(c, &v1beta2.QueryVotesRequest{
ProposalId: req.ProposalId,
Pagination: req.Pagination,
})
if err != nil {
return nil, err
}

votes := make([]v1beta1.Vote, len(resp.Votes))
for i, v := range resp.Votes {
votes[i], err = v046.ConvertToLegacyVote(*v)
if err != nil {
return nil, err
}
}

return &v1beta1.QueryVotesResponse{
Votes: votes,
Pagination: resp.Pagination,
}, nil
}

func (q legacyQueryServer) Params(c context.Context, req *v1beta1.QueryParamsRequest) (*v1beta1.QueryParamsResponse, error) {
resp, err := q.keeper.Params(c, &v1beta2.QueryParamsRequest{
ParamsType: req.ParamsType,
})
if err != nil {
return nil, err
}

response := &v1beta1.QueryParamsResponse{}

if resp.DepositParams != nil {
minDeposit := sdk.NewCoins(resp.DepositParams.MinDeposit...)
response.DepositParams = v1beta1.NewDepositParams(minDeposit, *resp.DepositParams.MaxDepositPeriod)
}

if resp.VotingParams != nil {
response.VotingParams = v1beta1.NewVotingParams(*resp.VotingParams.VotingPeriod)
}

if resp.TallyParams != nil {
quorum, err := sdk.NewDecFromStr(resp.TallyParams.Quorum)
if err != nil {
return nil, err
}
threshold, err := sdk.NewDecFromStr(resp.TallyParams.Threshold)
if err != nil {
return nil, err
}
vetoThreshold, err := sdk.NewDecFromStr(resp.TallyParams.VetoThreshold)
if err != nil {
return nil, err
}

response.TallyParams = v1beta1.NewTallyParams(quorum, threshold, vetoThreshold)
}

return response, nil
}

func (q legacyQueryServer) Deposit(c context.Context, req *v1beta1.QueryDepositRequest) (*v1beta1.QueryDepositResponse, error) {
resp, err := q.keeper.Deposit(c, &v1beta2.QueryDepositRequest{
ProposalId: req.ProposalId,
Depositor: req.Depositor,
})
if err != nil {
return nil, err
}

deposit := v046.ConvertToLegacyDeposit(resp.Deposit)
return &v1beta1.QueryDepositResponse{Deposit: deposit}, nil
}

func (q legacyQueryServer) Deposits(c context.Context, req *v1beta1.QueryDepositsRequest) (*v1beta1.QueryDepositsResponse, error) {
resp, err := q.keeper.Deposits(c, &v1beta2.QueryDepositsRequest{
ProposalId: req.ProposalId,
Pagination: req.Pagination,
})
if err != nil {
return nil, err
}
deposits := make([]v1beta1.Deposit, len(resp.Deposits))
for idx, deposit := range resp.Deposits {
deposits[idx] = v046.ConvertToLegacyDeposit(deposit)
}

return &v1beta1.QueryDepositsResponse{Deposits: deposits, Pagination: resp.Pagination}, nil
}

func (q legacyQueryServer) TallyResult(c context.Context, req *v1beta1.QueryTallyResultRequest) (*v1beta1.QueryTallyResultResponse, error) {
resp, err := q.keeper.TallyResult(c, &v1beta2.QueryTallyResultRequest{
ProposalId: req.ProposalId,
})
if err != nil {
return nil, err
}

tally, err := v046.ConvertToLegacyTallyResult(resp.Tally)
if err != nil {
return nil, err
}

return &v1beta1.QueryTallyResultResponse{Tally: tally}, nil
}
Loading