forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
234 lines (188 loc) · 7.15 KB
/
querier.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package keeper
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
// NewQuerier creates a new gov Querier instance
func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryParams:
return queryParams(ctx, path[1:], req, keeper, legacyQuerierCdc)
case types.QueryProposals:
return queryProposals(ctx, path[1:], req, keeper, legacyQuerierCdc)
case types.QueryProposal:
return queryProposal(ctx, path[1:], req, keeper, legacyQuerierCdc)
case types.QueryDeposits:
return queryDeposits(ctx, path[1:], req, keeper, legacyQuerierCdc)
case types.QueryDeposit:
return queryDeposit(ctx, path[1:], req, keeper, legacyQuerierCdc)
case types.QueryVotes:
return queryVotes(ctx, path[1:], req, keeper, legacyQuerierCdc)
case types.QueryVote:
return queryVote(ctx, path[1:], req, keeper, legacyQuerierCdc)
case types.QueryTally:
return queryTally(ctx, path[1:], req, keeper, legacyQuerierCdc)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
}
}
}
func queryParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
switch path[0] {
case types.ParamDeposit:
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetDepositParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
case types.ParamVoting:
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetVotingParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
case types.ParamTallying:
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetTallyParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "%s is not a valid query request path", req.Path)
}
}
// nolint: unparam
func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposal, ok := keeper.GetProposal(ctx, params.ProposalID)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", params.ProposalID)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposal)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryDepositParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
deposit, _ := keeper.GetDeposit(ctx, params.ProposalID, params.Depositor)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, deposit)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryVoteParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
vote, _ := keeper.GetVote(ctx, params.ProposalID, params.Voter)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, vote)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
deposits := keeper.GetDeposits(ctx, params.ProposalID)
if deposits == nil {
deposits = types.Deposits{}
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, deposits)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposalID := params.ProposalID
proposal, ok := keeper.GetProposal(ctx, proposalID)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", 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 = keeper.Tally(ctx, proposal)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, tallyResult)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryProposalVotesParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
votes := keeper.GetVotes(ctx, params.ProposalID)
if votes == nil {
votes = types.Votes{}
} else {
start, end := client.Paginate(len(votes), params.Page, params.Limit, 100)
if start < 0 || end < 0 {
votes = types.Votes{}
} else {
votes = votes[start:end]
}
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, votes)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryProposals(ctx sdk.Context, _ []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryProposalsParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposals := keeper.GetProposalsFiltered(ctx, params)
if proposals == nil {
proposals = types.Proposals{}
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposals)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}