-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
grpc_query.go
281 lines (228 loc) · 8.61 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package keeper
import (
"context"
"errors"
"sort"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
var _ types.QueryServer = queryServer{}
func NewQueryServer(k AccountKeeper) types.QueryServer {
return queryServer{k: k}
}
type queryServer struct{ k AccountKeeper }
func (s queryServer) AccountAddressByID(ctx context.Context, req *types.QueryAccountAddressByIDRequest) (*types.QueryAccountAddressByIDResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
if req.Id != 0 { // ignoring `0` case since it is default value.
return nil, status.Error(codes.InvalidArgument, "requesting with id isn't supported, try to request using account-id")
}
accID := req.AccountId
address, err := s.k.Accounts.Indexes.Number.MatchExact(ctx, accID)
if err != nil {
return nil, status.Errorf(codes.NotFound, "account address not found with account number %d", accID)
}
addr, err := s.k.addressCodec.BytesToString(address)
if err != nil {
return nil, err
}
return &types.QueryAccountAddressByIDResponse{AccountAddress: addr}, nil
}
func (s queryServer) Accounts(ctx context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
accounts, pageRes, err := query.CollectionPaginate(
ctx,
s.k.Accounts,
req.Pagination,
func(_ sdk.AccAddress, value sdk.AccountI) (*codectypes.Any, error) {
return codectypes.NewAnyWithValue(value)
},
)
return &types.QueryAccountsResponse{Accounts: accounts, Pagination: pageRes}, err
}
// Account returns account details based on address
func (s queryServer) Account(ctx context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
if req.Address == "" {
return nil, status.Error(codes.InvalidArgument, "Address cannot be empty")
}
addr, err := s.k.addressCodec.StringToBytes(req.Address)
if err != nil {
return nil, err
}
account := s.k.GetAccount(ctx, addr)
if account == nil {
xAccount, err := s.getFromXAccounts(ctx, addr)
if err != nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
}
return &types.QueryAccountResponse{Account: xAccount.Account}, nil
}
any, err := codectypes.NewAnyWithValue(account)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryAccountResponse{Account: any}, nil
}
// Params returns parameters of auth module
func (s queryServer) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
params := s.k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// ModuleAccounts returns all the existing Module Accounts
func (s queryServer) ModuleAccounts(ctx context.Context, req *types.QueryModuleAccountsRequest) (*types.QueryModuleAccountsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
// For deterministic output, sort the permAddrs by module name.
sortedPermAddrs := make([]string, 0, len(s.k.permAddrs))
for moduleName := range s.k.permAddrs {
sortedPermAddrs = append(sortedPermAddrs, moduleName)
}
sort.Strings(sortedPermAddrs)
modAccounts := make([]*codectypes.Any, 0, len(s.k.permAddrs))
for _, moduleName := range sortedPermAddrs {
account := s.k.GetModuleAccount(ctx, moduleName)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName)
}
any, err := codectypes.NewAnyWithValue(account)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
modAccounts = append(modAccounts, any)
}
return &types.QueryModuleAccountsResponse{Accounts: modAccounts}, nil
}
// ModuleAccountByName returns module account by module name
func (s queryServer) ModuleAccountByName(ctx context.Context, req *types.QueryModuleAccountByNameRequest) (*types.QueryModuleAccountByNameResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
if len(req.Name) == 0 {
return nil, status.Error(codes.InvalidArgument, "module name is empty")
}
moduleName := req.Name
account := s.k.GetModuleAccount(ctx, moduleName)
if account == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", moduleName)
}
any, err := codectypes.NewAnyWithValue(account)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryModuleAccountByNameResponse{Account: any}, nil
}
// Bech32Prefix returns the keeper internally stored bech32 prefix.
func (s queryServer) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
bech32Prefix, err := s.k.getBech32Prefix()
if err != nil {
return nil, err
}
if bech32Prefix == "" {
return &types.Bech32PrefixResponse{Bech32Prefix: "bech32 is not used on this chain"}, nil
}
return &types.Bech32PrefixResponse{Bech32Prefix: bech32Prefix}, nil
}
// AddressBytesToString converts an address from bytes to string, using the
// keeper's bech32 prefix.
func (s queryServer) AddressBytesToString(ctx context.Context, req *types.AddressBytesToStringRequest) (*types.AddressBytesToStringResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if len(req.AddressBytes) == 0 {
return nil, errors.New("empty address bytes is not allowed")
}
text, err := s.k.addressCodec.BytesToString(req.AddressBytes)
if err != nil {
return nil, err
}
return &types.AddressBytesToStringResponse{AddressString: text}, nil
}
// AddressStringToBytes converts an address from string to bytes, using the
// keeper's bech32 prefix.
func (s queryServer) AddressStringToBytes(ctx context.Context, req *types.AddressStringToBytesRequest) (*types.AddressStringToBytesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if len(strings.TrimSpace(req.AddressString)) == 0 {
return nil, errors.New("empty address string is not allowed")
}
bz, err := s.k.addressCodec.StringToBytes(req.AddressString)
if err != nil {
return nil, err
}
return &types.AddressStringToBytesResponse{AddressBytes: bz}, nil
}
// AccountInfo implements the AccountInfo query.
func (s queryServer) AccountInfo(ctx context.Context, req *types.QueryAccountInfoRequest) (*types.QueryAccountInfoResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}
if req.Address == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
addr, err := s.k.addressCodec.StringToBytes(req.Address)
if err != nil {
return nil, err
}
account := s.k.GetAccount(ctx, addr)
if account == nil {
xAccount, err := s.getFromXAccounts(ctx, addr)
// account info is nil it means that the account can be encapsulated into a
// legacy account representation but not a base account one.
if err != nil || xAccount.Base == nil {
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address)
}
return &types.QueryAccountInfoResponse{Info: xAccount.Base}, nil
}
// if there is no public key, avoid serializing the nil value
pubKey := account.GetPubKey()
var pkAny *codectypes.Any
if pubKey != nil {
pkAny, err = codectypes.NewAnyWithValue(account.GetPubKey())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
return &types.QueryAccountInfoResponse{
Info: &types.BaseAccount{
Address: req.Address,
PubKey: pkAny,
AccountNumber: account.GetAccountNumber(),
Sequence: account.GetSequence(),
},
}, nil
}
var (
errNotXAccount = errors.New("not an x/account")
errInvalidLegacyAccountImpl = errors.New("invalid legacy account implementation")
)
func (s queryServer) getFromXAccounts(ctx context.Context, address []byte) (*types.QueryLegacyAccountResponse, error) {
if !s.k.AccountsModKeeper.IsAccountsModuleAccount(ctx, address) {
return nil, errNotXAccount
}
// attempt to check if it can be queried for a legacy account representation.
resp, err := s.k.AccountsModKeeper.Query(ctx, address, &types.QueryLegacyAccount{})
if err != nil {
return nil, err
}
typedResp, ok := resp.(*types.QueryLegacyAccountResponse)
if !ok {
return nil, errInvalidLegacyAccountImpl
}
return typedResp, nil
}