-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathgrpc.go
409 lines (371 loc) · 10.8 KB
/
grpc.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package api
import (
"context"
"fmt"
"google.golang.org/grpc"
cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc"
"github.com/oasisprotocol/oasis-core/go/common/pubsub"
)
var (
// serviceName is the gRPC service name.
serviceName = cmnGrpc.NewServiceName("Beacon")
// methodGetBaseEpoch is the GetBaseEpoch method.
methodGetBaseEpoch = serviceName.NewMethod("GetBaseEpoch", nil)
// methodGetEpoch is the GetEpoch method.
methodGetEpoch = serviceName.NewMethod("GetEpoch", int64(0))
// methodGetFutureEpoch is the GetFutureEpoch method.
methodGetFutureEpoch = serviceName.NewMethod("GetFutureEpoch", int64(0))
// methodGetEpochBlock is the GetEpochBlock method.
methodGetEpochBlock = serviceName.NewMethod("GetEpochBlock", EpochTime(0))
// methodWaitEpoch is the WaitEpoch method.
methodWaitEpoch = serviceName.NewMethod("WaitEpoch", EpochTime(0))
// methodGetBeacon is the GetBeacon method.
methodGetBeacon = serviceName.NewMethod("GetBeacon", int64(0))
// methodStateToGenesis is the StateToGenesis method.
methodStateToGenesis = serviceName.NewMethod("StateToGenesis", int64(0))
// methodConsensusParameters is the ConsensusParameters method.
methodConsensusParameters = serviceName.NewMethod("ConsensusParameters", int64(0))
// methodWatchEpochs is the WatchEpochs method.
methodWatchEpochs = serviceName.NewMethod("WatchEpochs", nil)
// serviceDesc is the gRCP service descriptor.
serviceDesc = grpc.ServiceDesc{
ServiceName: string(serviceName),
HandlerType: (*Backend)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: methodGetBaseEpoch.ShortName(),
Handler: handlerGetBaseEpoch,
},
{
MethodName: methodGetEpoch.ShortName(),
Handler: handlerGetEpoch,
},
{
MethodName: methodGetFutureEpoch.ShortName(),
Handler: handlerGetFutureEpoch,
},
{
MethodName: methodWaitEpoch.ShortName(),
Handler: handlerWaitEpoch,
},
{
MethodName: methodGetEpochBlock.ShortName(),
Handler: handlerGetEpochBlock,
},
{
MethodName: methodGetBeacon.ShortName(),
Handler: handlerGetBeacon,
},
{
MethodName: methodStateToGenesis.ShortName(),
Handler: handlerStateToGenesis,
},
{
MethodName: methodConsensusParameters.ShortName(),
Handler: handlerConsensusParameters,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: methodWatchEpochs.ShortName(),
Handler: handlerWatchEpochs,
ServerStreams: true,
},
},
}
)
func handlerGetBaseEpoch( //nolint:golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
if interceptor == nil {
return srv.(Backend).GetBaseEpoch(ctx)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetBaseEpoch.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).GetBaseEpoch(ctx)
}
return interceptor(ctx, nil, info, handler)
}
func handlerGetEpoch( //nolint:golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var height int64
if err := dec(&height); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Backend).GetEpoch(ctx, height)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetEpoch.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).GetEpoch(ctx, req.(int64))
}
return interceptor(ctx, height, info, handler)
}
func handlerGetFutureEpoch( //nolint:golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var height int64
if err := dec(&height); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Backend).GetFutureEpoch(ctx, height)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetFutureEpoch.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).GetFutureEpoch(ctx, req.(int64))
}
return interceptor(ctx, height, info, handler)
}
func handlerWaitEpoch( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var epoch EpochTime
if err := dec(&epoch); err != nil {
return nil, err
}
if interceptor == nil {
return nil, srv.(Backend).WaitEpoch(ctx, epoch)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodWaitEpoch.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, srv.(Backend).WaitEpoch(ctx, req.(EpochTime))
}
return interceptor(ctx, epoch, info, handler)
}
func handlerGetEpochBlock( //nolint:golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var epoch EpochTime
if err := dec(&epoch); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Backend).GetEpochBlock(ctx, epoch)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetEpochBlock.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).GetEpochBlock(ctx, req.(EpochTime))
}
return interceptor(ctx, epoch, info, handler)
}
func handlerGetBeacon( //nolint:golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var height int64
if err := dec(&height); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Backend).GetBeacon(ctx, height)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetBeacon.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).GetBeacon(ctx, req.(int64))
}
return interceptor(ctx, height, info, handler)
}
func handlerStateToGenesis( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var height int64
if err := dec(&height); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Backend).StateToGenesis(ctx, height)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodStateToGenesis.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).StateToGenesis(ctx, req.(int64))
}
return interceptor(ctx, height, info, handler)
}
func handlerConsensusParameters( //nolint:golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var height int64
if err := dec(&height); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Backend).ConsensusParameters(ctx, height)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodConsensusParameters.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Backend).ConsensusParameters(ctx, req.(int64))
}
return interceptor(ctx, height, info, handler)
}
func handlerWatchEpochs(srv interface{}, stream grpc.ServerStream) error {
if err := stream.RecvMsg(nil); err != nil {
return err
}
ctx := stream.Context()
ch, sub, err := srv.(Backend).WatchEpochs(ctx)
if err != nil {
return err
}
defer sub.Close()
for {
select {
case epoch, ok := <-ch:
if !ok {
return nil
}
if err := stream.SendMsg(epoch); err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// RegisterService registers a new beacon service with the given gRPC server.
func RegisterService(server *grpc.Server, service Backend) {
server.RegisterService(&serviceDesc, service)
}
type beaconClient struct {
conn *grpc.ClientConn
}
func (c *beaconClient) GetBaseEpoch(ctx context.Context) (EpochTime, error) {
var rsp EpochTime
if err := c.conn.Invoke(ctx, methodGetBaseEpoch.FullName(), nil, &rsp); err != nil {
return 0, err
}
return rsp, nil
}
func (c *beaconClient) GetEpoch(ctx context.Context, height int64) (EpochTime, error) {
var rsp EpochTime
if err := c.conn.Invoke(ctx, methodGetEpoch.FullName(), height, &rsp); err != nil {
return 0, err
}
return rsp, nil
}
func (c *beaconClient) GetFutureEpoch(ctx context.Context, height int64) (*EpochTimeState, error) {
var rsp EpochTimeState
if err := c.conn.Invoke(ctx, methodGetFutureEpoch.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *beaconClient) GetEpochBlock(ctx context.Context, epoch EpochTime) (int64, error) {
var rsp int64
if err := c.conn.Invoke(ctx, methodGetEpochBlock.FullName(), epoch, &rsp); err != nil {
return 0, err
}
return rsp, nil
}
func (c *beaconClient) WaitEpoch(ctx context.Context, epoch EpochTime) error {
return c.conn.Invoke(ctx, methodWaitEpoch.FullName(), epoch, nil)
}
func (c *beaconClient) WatchEpochs(ctx context.Context) (<-chan EpochTime, pubsub.ClosableSubscription, error) {
ctx, sub := pubsub.NewContextSubscription(ctx)
stream, err := c.conn.NewStream(ctx, &serviceDesc.Streams[0], methodWatchEpochs.FullName())
if err != nil {
return nil, nil, err
}
if err = stream.SendMsg(nil); err != nil {
return nil, nil, err
}
if err = stream.CloseSend(); err != nil {
return nil, nil, err
}
ch := make(chan EpochTime)
go func() {
defer close(ch)
for {
var epoch EpochTime
if serr := stream.RecvMsg(&epoch); serr != nil {
return
}
select {
case ch <- epoch:
case <-ctx.Done():
return
}
}
}()
return ch, sub, nil
}
func (c *beaconClient) WatchLatestEpoch(ctx context.Context) (<-chan EpochTime, pubsub.ClosableSubscription, error) {
// The only thing that uses this is the registration worker, and it
// is not over gRPC.
return nil, nil, fmt.Errorf("beacon: gRPC method not implemented")
}
func (c *beaconClient) GetBeacon(ctx context.Context, height int64) ([]byte, error) {
var rsp []byte
if err := c.conn.Invoke(ctx, methodGetBeacon.FullName(), height, &rsp); err != nil {
return nil, err
}
return rsp, nil
}
func (c *beaconClient) StateToGenesis(ctx context.Context, height int64) (*Genesis, error) {
var rsp Genesis
if err := c.conn.Invoke(ctx, methodStateToGenesis.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *beaconClient) ConsensusParameters(ctx context.Context, height int64) (*ConsensusParameters, error) {
var rsp ConsensusParameters
if err := c.conn.Invoke(ctx, methodConsensusParameters.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *beaconClient) Cleanup() {
}
// NewBeaconClient creates a new gRPC scheduler client service.
func NewBeaconClient(c *grpc.ClientConn) Backend {
return &beaconClient{c}
}