/
grpc.go
554 lines (506 loc) · 15.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package api
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/errors"
cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc"
"github.com/oasisprotocol/oasis-core/go/common/pubsub"
roothash "github.com/oasisprotocol/oasis-core/go/roothash/api"
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
)
var (
// serviceName is the gRPC service name.
serviceName = cmnGrpc.NewServiceName("RuntimeClient")
// methodSubmitTx is the SubmitTx method.
methodSubmitTx = serviceName.NewMethod("SubmitTx", SubmitTxRequest{})
// methodSubmitTxMeta is the SubmitTxMeta method.
methodSubmitTxMeta = serviceName.NewMethod("SubmitTxMeta", SubmitTxRequest{})
// methodSubmitTxNoWait is the SubmitTxNoWait method.
methodSubmitTxNoWait = serviceName.NewMethod("SubmitTxNoWait", SubmitTxRequest{})
// methodCheckTx is the CheckTx method.
methodCheckTx = serviceName.NewMethod("CheckTx", CheckTxRequest{})
// methodGetGenesisBlock is the GetGenesisBlock method.
methodGetGenesisBlock = serviceName.NewMethod("GetGenesisBlock", common.Namespace{})
// methodGetBlock is the GetBlock method.
methodGetBlock = serviceName.NewMethod("GetBlock", GetBlockRequest{})
// methodGetLastRetainedBlock is the GetLastRetainedBlock method.
methodGetLastRetainedBlock = serviceName.NewMethod("GetLastRetainedBlock", common.Namespace{})
// methodGetTransactions is the GetTransactions method.
methodGetTransactions = serviceName.NewMethod("GetTransactions", GetTransactionsRequest{})
// methodGetTransactionsWithResults is the GetTransactionsWithResults method.
methodGetTransactionsWithResults = serviceName.NewMethod("GetTransactionsWithResults", GetTransactionsRequest{})
// methodGetEvents is the GetEvents method.
methodGetEvents = serviceName.NewMethod("GetEvents", GetEventsRequest{})
// methodQuery is the Query method.
methodQuery = serviceName.NewMethod("Query", QueryRequest{})
// methodWatchBlocks is the WatchBlocks method.
methodWatchBlocks = serviceName.NewMethod("WatchBlocks", common.Namespace{})
// serviceDesc is the gRPC service descriptor.
serviceDesc = grpc.ServiceDesc{
ServiceName: string(serviceName),
HandlerType: (*RuntimeClient)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: methodSubmitTx.ShortName(),
Handler: handlerSubmitTx,
},
{
MethodName: methodSubmitTxMeta.ShortName(),
Handler: handlerSubmitTxMeta,
},
{
MethodName: methodSubmitTxNoWait.ShortName(),
Handler: handlerSubmitTxNoWait,
},
{
MethodName: methodCheckTx.ShortName(),
Handler: handlerCheckTx,
},
{
MethodName: methodGetGenesisBlock.ShortName(),
Handler: handlerGetGenesisBlock,
},
{
MethodName: methodGetBlock.ShortName(),
Handler: handlerGetBlock,
},
{
MethodName: methodGetLastRetainedBlock.ShortName(),
Handler: handlerGetLastRetainedBlock,
},
{
MethodName: methodGetTransactions.ShortName(),
Handler: handlerGetTransactions,
},
{
MethodName: methodGetTransactionsWithResults.ShortName(),
Handler: handlerGetTransactionsWithResults,
},
{
MethodName: methodGetEvents.ShortName(),
Handler: handlerGetEvents,
},
{
MethodName: methodQuery.ShortName(),
Handler: handlerQuery,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: methodWatchBlocks.ShortName(),
Handler: handlerWatchBlocks,
ServerStreams: true,
},
},
}
)
func handlerSubmitTx( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq SubmitTxRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(RuntimeClient).SubmitTx(ctx, &rq)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodSubmitTx.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(RuntimeClient).SubmitTx(ctx, req.(*SubmitTxRequest))
}
return interceptor(ctx, &rq, info, handler)
}
func handlerSubmitTxMeta( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq SubmitTxRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(RuntimeClient).SubmitTxMeta(ctx, &rq)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodSubmitTxMeta.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(RuntimeClient).SubmitTxMeta(ctx, req.(*SubmitTxRequest))
}
return interceptor(ctx, &rq, info, handler)
}
func handlerSubmitTxNoWait( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq SubmitTxRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
return nil, srv.(RuntimeClient).SubmitTxNoWait(ctx, &rq)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodSubmitTxNoWait.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, srv.(RuntimeClient).SubmitTxNoWait(ctx, req.(*SubmitTxRequest))
}
return interceptor(ctx, &rq, info, handler)
}
func handlerCheckTx( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq CheckTxRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
return nil, srv.(RuntimeClient).CheckTx(ctx, &rq)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodCheckTx.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, srv.(RuntimeClient).CheckTx(ctx, req.(*CheckTxRequest))
}
return interceptor(ctx, &rq, info, handler)
}
// wrappedErrNotFound is a wrapped ErrNotFound error so that it corresponds
// to the gRPC NotFound error code. It is required because Rust's gRPC bindings
// do not support fetching error details.
type wrappedErrNotFound struct {
err error
}
func (w wrappedErrNotFound) Unwrap() error {
return w.err
}
func (w wrappedErrNotFound) Error() string {
return w.err.Error()
}
func (w wrappedErrNotFound) GRPCStatus() *status.Status {
return status.New(codes.NotFound, w.err.Error())
}
func errorWrapNotFound(err error) error {
if !errors.Is(err, ErrNotFound) && !errors.Is(err, roothash.ErrNotFound) {
return err
}
return wrappedErrNotFound{err}
}
func handlerGetGenesisBlock( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var runtimeID common.Namespace
if err := dec(&runtimeID); err != nil {
return nil, err
}
if interceptor == nil {
rsp, err := srv.(RuntimeClient).GetGenesisBlock(ctx, runtimeID)
return rsp, errorWrapNotFound(err)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetGenesisBlock.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
rsp, err := srv.(RuntimeClient).GetGenesisBlock(ctx, req.(common.Namespace))
return rsp, errorWrapNotFound(err)
}
return interceptor(ctx, runtimeID, info, handler)
}
func handlerGetBlock( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq GetBlockRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
rsp, err := srv.(RuntimeClient).GetBlock(ctx, &rq)
return rsp, errorWrapNotFound(err)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetBlock.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
rsp, err := srv.(RuntimeClient).GetBlock(ctx, req.(*GetBlockRequest))
return rsp, errorWrapNotFound(err)
}
return interceptor(ctx, &rq, info, handler)
}
func handlerGetLastRetainedBlock( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var runtimeID common.Namespace
if err := dec(&runtimeID); err != nil {
return nil, err
}
if interceptor == nil {
rsp, err := srv.(RuntimeClient).GetLastRetainedBlock(ctx, runtimeID)
return rsp, errorWrapNotFound(err)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetLastRetainedBlock.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
rsp, err := srv.(RuntimeClient).GetLastRetainedBlock(ctx, req.(common.Namespace))
return rsp, errorWrapNotFound(err)
}
return interceptor(ctx, runtimeID, info, handler)
}
func handlerGetTransactions( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq GetTransactionsRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(RuntimeClient).GetTransactions(ctx, &rq)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetTransactions.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(RuntimeClient).GetTransactions(ctx, req.(*GetTransactionsRequest))
}
return interceptor(ctx, &rq, info, handler)
}
func handlerGetTransactionsWithResults( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq GetTransactionsRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(RuntimeClient).GetTransactionsWithResults(ctx, &rq)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetTransactionsWithResults.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(RuntimeClient).GetTransactionsWithResults(ctx, req.(*GetTransactionsRequest))
}
return interceptor(ctx, &rq, info, handler)
}
func handlerGetEvents( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq GetEventsRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(RuntimeClient).GetEvents(ctx, &rq)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodGetEvents.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(RuntimeClient).GetEvents(ctx, req.(*GetEventsRequest))
}
return interceptor(ctx, &rq, info, handler)
}
func handlerQuery( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var rq QueryRequest
if err := dec(&rq); err != nil {
return nil, err
}
if interceptor == nil {
rsp, err := srv.(RuntimeClient).Query(ctx, &rq)
return rsp, errorWrapNotFound(err)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: methodQuery.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
rsp, err := srv.(RuntimeClient).Query(ctx, req.(*QueryRequest))
return rsp, errorWrapNotFound(err)
}
return interceptor(ctx, &rq, info, handler)
}
func handlerWatchBlocks(srv interface{}, stream grpc.ServerStream) error {
var runtimeID common.Namespace
if err := stream.RecvMsg(&runtimeID); err != nil {
return err
}
ctx := stream.Context()
ch, sub, err := srv.(RuntimeClient).WatchBlocks(ctx, runtimeID)
if err != nil {
return err
}
defer sub.Close()
for {
select {
case blk, ok := <-ch:
if !ok {
return nil
}
if err := stream.SendMsg(blk); err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// RegisterService registers a new runtime client service with the given gRPC server.
func RegisterService(server *grpc.Server, service RuntimeClient) {
server.RegisterService(&serviceDesc, service)
}
type runtimeClient struct {
conn *grpc.ClientConn
}
func (c *runtimeClient) SubmitTx(ctx context.Context, request *SubmitTxRequest) ([]byte, error) {
var rsp []byte
if err := c.conn.Invoke(ctx, methodSubmitTx.FullName(), request, &rsp); err != nil {
return nil, err
}
return rsp, nil
}
func (c *runtimeClient) SubmitTxMeta(ctx context.Context, request *SubmitTxRequest) (*SubmitTxMetaResponse, error) {
var rsp SubmitTxMetaResponse
if err := c.conn.Invoke(ctx, methodSubmitTxMeta.FullName(), request, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *runtimeClient) SubmitTxNoWait(ctx context.Context, request *SubmitTxRequest) error {
return c.conn.Invoke(ctx, methodSubmitTxNoWait.FullName(), request, nil)
}
func (c *runtimeClient) CheckTx(ctx context.Context, request *CheckTxRequest) error {
return c.conn.Invoke(ctx, methodCheckTx.FullName(), request, nil)
}
func (c *runtimeClient) GetGenesisBlock(ctx context.Context, runtimeID common.Namespace) (*block.Block, error) {
var rsp block.Block
if err := c.conn.Invoke(ctx, methodGetGenesisBlock.FullName(), runtimeID, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *runtimeClient) GetBlock(ctx context.Context, request *GetBlockRequest) (*block.Block, error) {
var rsp block.Block
if err := c.conn.Invoke(ctx, methodGetBlock.FullName(), request, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *runtimeClient) GetLastRetainedBlock(ctx context.Context, runtimeID common.Namespace) (*block.Block, error) {
var rsp block.Block
if err := c.conn.Invoke(ctx, methodGetLastRetainedBlock.FullName(), runtimeID, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *runtimeClient) GetTransactions(ctx context.Context, request *GetTransactionsRequest) ([][]byte, error) {
var rsp [][]byte
if err := c.conn.Invoke(ctx, methodGetTransactions.FullName(), request, &rsp); err != nil {
return nil, err
}
return rsp, nil
}
func (c *runtimeClient) GetTransactionsWithResults(ctx context.Context, request *GetTransactionsRequest) ([]*TransactionWithResults, error) {
var rsp []*TransactionWithResults
if err := c.conn.Invoke(ctx, methodGetTransactionsWithResults.FullName(), request, &rsp); err != nil {
return nil, err
}
return rsp, nil
}
func (c *runtimeClient) GetEvents(ctx context.Context, request *GetEventsRequest) ([]*Event, error) {
var rsp []*Event
if err := c.conn.Invoke(ctx, methodGetEvents.FullName(), request, &rsp); err != nil {
return nil, err
}
return rsp, nil
}
func (c *runtimeClient) Query(ctx context.Context, request *QueryRequest) (*QueryResponse, error) {
var rsp QueryResponse
if err := c.conn.Invoke(ctx, methodQuery.FullName(), request, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}
func (c *runtimeClient) WatchBlocks(ctx context.Context, runtimeID common.Namespace) (<-chan *roothash.AnnotatedBlock, pubsub.ClosableSubscription, error) {
ctx, sub := pubsub.NewContextSubscription(ctx)
stream, err := c.conn.NewStream(ctx, &serviceDesc.Streams[0], methodWatchBlocks.FullName())
if err != nil {
return nil, nil, err
}
if err = stream.SendMsg(runtimeID); err != nil {
return nil, nil, err
}
if err = stream.CloseSend(); err != nil {
return nil, nil, err
}
ch := make(chan *roothash.AnnotatedBlock)
go func() {
defer close(ch)
for {
var blk roothash.AnnotatedBlock
if serr := stream.RecvMsg(&blk); serr != nil {
return
}
select {
case ch <- &blk:
case <-ctx.Done():
return
}
}
}()
return ch, sub, nil
}
// NewRuntimeClient creates a new gRPC runtime client service.
func NewRuntimeClient(c *grpc.ClientConn) RuntimeClient {
return &runtimeClient{
conn: c,
}
}