-
Notifications
You must be signed in to change notification settings - Fork 337
/
grpc_query.go
204 lines (169 loc) · 6.6 KB
/
grpc_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package keeper
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/kava-labs/kava/x/committee/types"
)
type queryServer struct {
keeper Keeper
}
// NewQueryServerImpl creates a new server for handling gRPC queries.
func NewQueryServerImpl(k Keeper) types.QueryServer {
return &queryServer{keeper: k}
}
var _ types.QueryServer = queryServer{}
// Committees implements the gRPC service handler for querying committees.
func (s queryServer) Committees(ctx context.Context, req *types.QueryCommitteesRequest) (*types.QueryCommitteesResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
committees := s.keeper.GetCommittees(sdkCtx)
committeesAny, err := types.PackCommittees(committees)
if err != nil {
return nil, status.Errorf(codes.Unknown, "could not pack committees: %v", err)
}
return &types.QueryCommitteesResponse{Committees: committeesAny}, nil
}
// Committee implements the Query/Committee gRPC method.
func (s queryServer) Committee(c context.Context, req *types.QueryCommitteeRequest) (*types.QueryCommitteeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
committee, found := s.keeper.GetCommittee(ctx, req.CommitteeId)
if !found {
return nil, status.Errorf(codes.NotFound, "could not find committee for id: %v", req.CommitteeId)
}
committeeAny, err := types.PackCommittee(committee)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not pack committees: %v", err)
}
return &types.QueryCommitteeResponse{Committee: committeeAny}, nil
}
// Proposals implements the Query/Proposals gRPC method
func (s queryServer) Proposals(c context.Context, req *types.QueryProposalsRequest) (*types.QueryProposalsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
proposals := s.keeper.GetProposalsByCommittee(ctx, req.CommitteeId)
var proposalsResp []types.QueryProposalResponse
for _, proposal := range proposals {
proposalsResp = append(proposalsResp, s.proposalResponseFromProposal(proposal))
}
return &types.QueryProposalsResponse{
Proposals: proposalsResp,
}, nil
}
// Proposal implements the Query/Proposal gRPC method
func (s queryServer) Proposal(c context.Context, req *types.QueryProposalRequest) (*types.QueryProposalResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
proposal, found := s.keeper.GetProposal(ctx, req.ProposalId)
if !found {
return nil, status.Errorf(codes.NotFound, "cannot find proposal: %v", req.ProposalId)
}
proposalResp := s.proposalResponseFromProposal(proposal)
return &proposalResp, nil
}
// NextProposalID implements the Query/NextProposalID gRPC method
func (s queryServer) NextProposalID(c context.Context, req *types.QueryNextProposalIDRequest) (*types.QueryNextProposalIDResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
proposalID, err := s.keeper.GetNextProposalID(ctx)
if err != nil {
return nil, status.Errorf(codes.NotFound, "cannot find next proposal id: %v", err)
}
return &types.QueryNextProposalIDResponse{NextProposalID: proposalID}, nil
}
// Votes implements the Query/Votes gRPC method
func (s queryServer) Votes(c context.Context, req *types.QueryVotesRequest) (*types.QueryVotesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
var queryResults []types.QueryVoteResponse
store := ctx.KVStore(s.keeper.storeKey)
votesStore := prefix.NewStore(store, append(types.VoteKeyPrefix, types.GetKeyFromID(req.ProposalId)...))
pageRes, err := query.Paginate(votesStore, req.Pagination, func(key []byte, value []byte) error {
var vote types.Vote
if err := s.keeper.cdc.Unmarshal(value, &vote); err != nil {
return err
}
queryResults = append(queryResults, s.votesResponseFromVote(vote))
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryVotesResponse{
Votes: queryResults,
Pagination: pageRes,
}, nil
}
// Vote implements the Query/Vote gRPC method
func (s queryServer) Vote(c context.Context, req *types.QueryVoteRequest) (*types.QueryVoteResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
voter, err := sdk.AccAddressFromBech32(req.Voter)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid voter address: %v", err)
}
vote, found := s.keeper.GetVote(ctx, req.ProposalId, voter)
if !found {
return nil, status.Errorf(codes.NotFound, "proposal id: %d, voter: %s", req.ProposalId, req.Voter)
}
voteResp := s.votesResponseFromVote(vote)
return &voteResp, nil
}
// Tally implements the Query/Tally gRPC method
func (s queryServer) Tally(c context.Context, req *types.QueryTallyRequest) (*types.QueryTallyResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
tally, found := s.keeper.GetProposalTallyResponse(ctx, req.ProposalId)
if !found {
return nil, status.Errorf(codes.NotFound, "proposal id: %d", req.ProposalId)
}
return tally, nil
}
// RawParams implements the Query/RawParams gRPC method
func (s queryServer) RawParams(c context.Context, req *types.QueryRawParamsRequest) (*types.QueryRawParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
subspace, found := s.keeper.paramKeeper.GetSubspace(req.Subspace)
if !found {
return nil, status.Errorf(codes.NotFound, "subspace not found: %s", req.Subspace)
}
rawParams := subspace.GetRaw(ctx, []byte(req.Key))
return &types.QueryRawParamsResponse{RawData: string(rawParams)}, nil
}
func (s queryServer) proposalResponseFromProposal(proposal types.Proposal) types.QueryProposalResponse {
return types.QueryProposalResponse{
PubProposal: proposal.Content,
ID: proposal.ID,
CommitteeID: proposal.CommitteeID,
Deadline: proposal.Deadline,
}
}
func (s queryServer) votesResponseFromVote(vote types.Vote) types.QueryVoteResponse {
return types.QueryVoteResponse{
ProposalID: vote.ProposalID,
Voter: vote.Voter.String(),
VoteType: vote.VoteType,
}
}