-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
208 lines (175 loc) · 7.04 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
package keeper
import (
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/elysium-station/black/x/committee/types"
)
// NewQuerier creates a new gov Querier instance
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
switch path[0] {
case types.QueryCommittees:
return queryCommittees(ctx, req, k, legacyQuerierCdc)
case types.QueryCommittee:
return queryCommittee(ctx, req, k, legacyQuerierCdc)
case types.QueryProposals:
return queryProposals(ctx, req, k, legacyQuerierCdc)
case types.QueryProposal:
return queryProposal(ctx, req, k, legacyQuerierCdc)
case types.QueryVotes:
return queryVotes(ctx, req, k, legacyQuerierCdc)
case types.QueryVote:
return queryVote(ctx, req, k, legacyQuerierCdc)
case types.QueryTally:
return queryTally(ctx, req, k, legacyQuerierCdc)
case types.QueryNextProposalID:
return queryNextProposalID(ctx, req, k, legacyQuerierCdc)
case types.QueryRawParams:
return queryRawParams(ctx, req, k, legacyQuerierCdc)
default:
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
}
}
}
// ------------------------------------------
// Committees
// ------------------------------------------
func queryCommittees(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
committees := keeper.GetCommittees(ctx)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, committees)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryCommittee(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryCommitteeParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
committee, found := keeper.GetCommittee(ctx, params.CommitteeID)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownCommittee, "%d", params.CommitteeID)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, committee)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// ------------------------------------------
// Proposals
// ------------------------------------------
func queryProposals(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryCommitteeParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposals := keeper.GetProposalsByCommittee(ctx, params.CommitteeID)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposals)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryProposal(ctx sdk.Context, 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, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposal, found := keeper.GetProposal(ctx, params.ProposalID)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownProposal, "%d", params.ProposalID)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposal)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryNextProposalID(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
nextProposalID, _ := k.GetNextProposalID(ctx)
bz, err := types.ModuleCdc.LegacyAmino.MarshalJSON(nextProposalID)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
return bz, nil
}
// ------------------------------------------
// Votes
// ------------------------------------------
func queryVotes(ctx sdk.Context, 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, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
votes := keeper.GetVotesByProposal(ctx, params.ProposalID)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, votes)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryVote(ctx sdk.Context, 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, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
vote, found := keeper.GetVote(ctx, params.ProposalID, params.Voter)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownVote, "proposal id: %d, voter: %s", params.ProposalID, params.Voter)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, vote)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// ------------------------------------------
// Tally
// ------------------------------------------
func queryTally(ctx sdk.Context, 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, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
tally, found := keeper.GetProposalTallyResponse(ctx, params.ProposalID)
if !found {
return nil, errorsmod.Wrapf(types.ErrNotFoundProposalTally, "proposal id: %d", params.ProposalID)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, tally)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// ------------------------------------------
// Raw Params
// ------------------------------------------
func queryRawParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryRawParamsParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
subspace, found := keeper.paramKeeper.GetSubspace(params.Subspace)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownSubspace, "subspace: %s", params.Subspace)
}
rawParams := subspace.GetRaw(ctx, []byte(params.Key))
// encode the raw params as json, which converts them to a base64 string
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rawParams)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}