-
Notifications
You must be signed in to change notification settings - Fork 40
/
query_server.go
206 lines (161 loc) · 5.78 KB
/
query_server.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
package keeper
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/hub/x/session/types"
)
var (
_ types.QueryServiceServer = (*queryServer)(nil)
)
type queryServer struct {
Keeper
}
func NewQueryServiceServer(keeper Keeper) types.QueryServiceServer {
return &queryServer{Keeper: keeper}
}
func (q *queryServer) QuerySession(c context.Context, req *types.QuerySessionRequest) (*types.QuerySessionResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
item, found := q.GetSession(ctx, req.Id)
if !found {
return nil, status.Errorf(codes.NotFound, "session does not exist for id %d", req.Id)
}
return &types.QuerySessionResponse{Session: item}, nil
}
func (q *queryServer) QuerySessions(c context.Context, req *types.QuerySessionsRequest) (*types.QuerySessionsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
var (
items types.Sessions
ctx = sdk.UnwrapSDKContext(c)
store = prefix.NewStore(q.Store(ctx), types.SessionKeyPrefix)
)
pagination, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
var item types.Session
if err := q.cdc.Unmarshal(value, &item); err != nil {
return err
}
items = append(items, item)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySessionsResponse{Sessions: items, Pagination: pagination}, nil
}
func (q *queryServer) QuerySessionsForAccount(c context.Context, req *types.QuerySessionsForAccountRequest) (*types.QuerySessionsForAccountResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address %s", req.Address)
}
var (
items types.Sessions
ctx = sdk.UnwrapSDKContext(c)
store = prefix.NewStore(q.Store(ctx), types.GetSessionForAccountKeyPrefix(addr))
)
pagination, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
item, found := q.GetSession(ctx, sdk.BigEndianToUint64(key))
if !found {
return fmt.Errorf("session for key %X does not exist", key)
}
items = append(items, item)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySessionsForAccountResponse{Sessions: items, Pagination: pagination}, nil
}
func (q *queryServer) QuerySessionsForNode(c context.Context, req *types.QuerySessionsForNodeRequest) (*types.QuerySessionsForNodeResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
addr, err := hubtypes.NodeAddressFromBech32(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address %s", req.Address)
}
var (
items types.Sessions
ctx = sdk.UnwrapSDKContext(c)
store = prefix.NewStore(q.Store(ctx), types.GetSessionForNodeKeyPrefix(addr))
)
pagination, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
item, found := q.GetSession(ctx, sdk.BigEndianToUint64(key))
if !found {
return fmt.Errorf("session for key %X does not exist", key)
}
items = append(items, item)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySessionsForNodeResponse{Sessions: items, Pagination: pagination}, nil
}
func (q *queryServer) QuerySessionsForSubscription(c context.Context, req *types.QuerySessionsForSubscriptionRequest) (*types.QuerySessionsForSubscriptionResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
var (
items types.Sessions
ctx = sdk.UnwrapSDKContext(c)
store = prefix.NewStore(q.Store(ctx), types.GetSessionForSubscriptionKeyPrefix(req.Id))
)
pagination, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
item, found := q.GetSession(ctx, sdk.BigEndianToUint64(key))
if !found {
return fmt.Errorf("session for key %X does not exist", key)
}
items = append(items, item)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySessionsForSubscriptionResponse{Sessions: items, Pagination: pagination}, nil
}
func (q *queryServer) QuerySessionsForAllocation(c context.Context, req *types.QuerySessionsForAllocationRequest) (*types.QuerySessionsForAllocationResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
addr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address %s", req.Address)
}
var (
items types.Sessions
ctx = sdk.UnwrapSDKContext(c)
store = prefix.NewStore(q.Store(ctx), types.GetSessionForAllocationKeyPrefix(req.Id, addr))
)
pagination, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
item, found := q.GetSession(ctx, sdk.BigEndianToUint64(key))
if !found {
return fmt.Errorf("session for key %X does not exist", key)
}
items = append(items, item)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QuerySessionsForAllocationResponse{Sessions: items, Pagination: pagination}, nil
}
func (q *queryServer) QueryParams(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
var (
ctx = sdk.UnwrapSDKContext(c)
params = q.GetParams(ctx)
)
return &types.QueryParamsResponse{Params: params}, nil
}