-
Notifications
You must be signed in to change notification settings - Fork 179
/
forwarder.go
604 lines (504 loc) · 18.5 KB
/
forwarder.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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package apiproxy
import (
"context"
"fmt"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/api"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
"github.com/onflow/flow-go/engine/access/rest/routes"
"github.com/onflow/flow-go/engine/common/rpc/convert"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/forwarder"
accessproto "github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/entities"
)
// RestForwarder - structure which handles requests to an upstream access node using gRPC API.
type RestForwarder struct {
log zerolog.Logger
*forwarder.Forwarder
}
var _ api.RestServerApi = (*RestForwarder)(nil)
// NewRestForwarder returns new RestForwarder.
func NewRestForwarder(log zerolog.Logger, identities flow.IdentityList, timeout time.Duration, maxMsgSize uint) (*RestForwarder, error) {
f, err := forwarder.NewForwarder(identities, timeout, maxMsgSize)
restForwarder := &RestForwarder{
log: log,
Forwarder: f,
}
return restForwarder, err
}
// GetTransactionByID gets a transaction by requested ID.
func (f *RestForwarder) GetTransactionByID(r request.GetTransaction, context context.Context, link models.LinkGenerator, chain flow.Chain) (models.Transaction, error) {
var response models.Transaction
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
getTransactionRequest := &accessproto.GetTransactionRequest{
Id: r.ID[:],
}
transactionResponse, err := upstream.GetTransaction(context, getTransactionRequest)
if err != nil {
return response, err
}
var transactionResultResponse *accessproto.TransactionResultResponse
// only lookup result if transaction result is to be expanded
if r.ExpandsResult {
getTransactionResultRequest := &accessproto.GetTransactionRequest{
Id: r.ID[:],
BlockId: r.BlockID[:],
CollectionId: r.CollectionID[:],
}
transactionResultResponse, err = upstream.GetTransactionResult(context, getTransactionResultRequest)
if err != nil {
return response, err
}
}
flowTransaction, err := convert.MessageToTransaction(transactionResponse.Transaction, chain)
if err != nil {
return response, err
}
flowTransactionResult := access.MessageToTransactionResult(transactionResultResponse)
response.Build(&flowTransaction, flowTransactionResult, link)
return response, nil
}
// CreateTransaction creates a new transaction from provided payload.
func (f *RestForwarder) CreateTransaction(r request.CreateTransaction, context context.Context, link models.LinkGenerator) (models.Transaction, error) {
var response models.Transaction
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
entitiesTransaction := convert.TransactionToMessage(r.Transaction)
sendTransactionRequest := &accessproto.SendTransactionRequest{
Transaction: entitiesTransaction,
}
_, err = upstream.SendTransaction(context, sendTransactionRequest)
if err != nil {
return response, err
}
response.Build(&r.Transaction, nil, link)
return response, nil
}
// GetTransactionResultByID retrieves transaction result by the transaction ID.
func (f *RestForwarder) GetTransactionResultByID(r request.GetTransactionResult, context context.Context, link models.LinkGenerator) (models.TransactionResult, error) {
var response models.TransactionResult
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
getTransactionResult := &accessproto.GetTransactionRequest{
Id: r.ID[:],
BlockId: r.BlockID[:],
CollectionId: r.CollectionID[:],
}
transactionResultResponse, err := upstream.GetTransactionResult(context, getTransactionResult)
if err != nil {
return response, err
}
flowTransactionResult := access.MessageToTransactionResult(transactionResultResponse)
response.Build(flowTransactionResult, r.ID, link)
return response, nil
}
// GetBlocksByIDs gets blocks by provided ID or list of IDs.
func (f *RestForwarder) GetBlocksByIDs(r request.GetBlockByIDs, context context.Context, expandFields map[string]bool, link models.LinkGenerator) ([]*models.Block, error) {
blocks := make([]*models.Block, len(r.IDs))
upstream, err := f.FaultTolerantClient()
if err != nil {
return blocks, err
}
for i, id := range r.IDs {
block, err := getBlockFromGrpc(routes.ForID(&id), context, expandFields, upstream, link)
if err != nil {
return nil, err
}
blocks[i] = block
}
return blocks, nil
}
// GetBlocksByHeight gets blocks by provided height.
func (f *RestForwarder) GetBlocksByHeight(r *request.Request, link models.LinkGenerator) ([]*models.Block, error) {
req, err := r.GetBlockRequest()
if err != nil {
return nil, models.NewBadRequestError(err)
}
upstream, err := f.FaultTolerantClient()
if err != nil {
return nil, err
}
if req.FinalHeight || req.SealedHeight {
block, err := getBlockFromGrpc(routes.ForFinalized(req.Heights[0]), r.Context(), r.ExpandFields, upstream, link)
if err != nil {
return nil, err
}
return []*models.Block{block}, nil
}
// if the query is /blocks/height=1000,1008,1049...
if req.HasHeights() {
blocks := make([]*models.Block, len(req.Heights))
for i, height := range req.Heights {
block, err := getBlockFromGrpc(routes.ForHeight(height), r.Context(), r.ExpandFields, upstream, link)
if err != nil {
return nil, err
}
blocks[i] = block
}
return blocks, nil
}
// support providing end height as "sealed" or "final"
if req.EndHeight == request.FinalHeight || req.EndHeight == request.SealedHeight {
getLatestBlockRequest := &accessproto.GetLatestBlockRequest{
IsSealed: req.EndHeight == request.SealedHeight,
}
blockResponse, err := upstream.GetLatestBlock(r.Context(), getLatestBlockRequest)
if err != nil {
return nil, err
}
req.EndHeight = blockResponse.Block.BlockHeader.Height // overwrite special value height with fetched
if req.StartHeight > req.EndHeight {
return nil, models.NewBadRequestError(fmt.Errorf("start height must be less than or equal to end height"))
}
}
blocks := make([]*models.Block, 0)
// start and end height inclusive
for i := req.StartHeight; i <= req.EndHeight; i++ {
block, err := getBlockFromGrpc(routes.ForHeight(i), r.Context(), r.ExpandFields, upstream, link)
if err != nil {
return nil, err
}
blocks = append(blocks, block)
}
return blocks, nil
}
// GetBlockPayloadByID gets block payload by ID
func (f *RestForwarder) GetBlockPayloadByID(r request.GetBlockPayload, context context.Context, _ models.LinkGenerator) (models.BlockPayload, error) {
var payload models.BlockPayload
upstream, err := f.FaultTolerantClient()
if err != nil {
return payload, err
}
blkProvider := routes.NewBlockFromGrpcProvider(upstream, routes.ForID(&r.ID))
block, _, statusErr := blkProvider.GetBlock(context)
if statusErr != nil {
return payload, statusErr
}
flowPayload, err := convert.PayloadFromMessage(block)
if err != nil {
return payload, err
}
err = payload.Build(flowPayload)
if err != nil {
return payload, err
}
return payload, nil
}
// GetExecutionResultByID gets execution result by the ID.
func (f *RestForwarder) GetExecutionResultByID(r request.GetExecutionResult, context context.Context, link models.LinkGenerator) (models.ExecutionResult, error) {
var response models.ExecutionResult
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
executionResultByIDRequest := &accessproto.GetExecutionResultByIDRequest{
Id: r.ID[:],
}
executionResultByIDResponse, err := upstream.GetExecutionResultByID(context, executionResultByIDRequest)
if err != nil {
return response, err
}
if executionResultByIDResponse == nil {
err := fmt.Errorf("execution result with ID: %s not found", r.ID.String())
return response, models.NewNotFoundError(err.Error(), err)
}
flowExecResult, err := convert.MessageToExecutionResult(executionResultByIDResponse.ExecutionResult)
if err != nil {
return response, err
}
err = response.Build(flowExecResult, link)
if err != nil {
return response, err
}
return response, nil
}
// GetExecutionResultsByBlockIDs gets Execution Result payload by block IDs.
func (f *RestForwarder) GetExecutionResultsByBlockIDs(r request.GetExecutionResultByBlockIDs, context context.Context, link models.LinkGenerator) ([]models.ExecutionResult, error) {
// for each block ID we retrieve execution result
results := make([]models.ExecutionResult, len(r.BlockIDs))
upstream, err := f.FaultTolerantClient()
if err != nil {
return results, err
}
for i, id := range r.BlockIDs {
getExecutionResultForBlockID := &accessproto.GetExecutionResultForBlockIDRequest{
BlockId: id[:],
}
executionResultForBlockIDResponse, err := upstream.GetExecutionResultForBlockID(context, getExecutionResultForBlockID)
if err != nil {
return nil, err
}
var response models.ExecutionResult
flowExecResult, err := convert.MessageToExecutionResult(executionResultForBlockIDResponse.ExecutionResult)
if err != nil {
return nil, err
}
err = response.Build(flowExecResult, link)
if err != nil {
return nil, err
}
results[i] = response
}
return results, nil
}
// GetCollectionByID retrieves a collection by ID and builds a response
func (f *RestForwarder) GetCollectionByID(r request.GetCollection, context context.Context, expandFields map[string]bool, link models.LinkGenerator, chain flow.Chain) (models.Collection, error) {
var response models.Collection
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
getCollectionByIDRequest := &accessproto.GetCollectionByIDRequest{
Id: r.ID[:],
}
collectionResponse, err := upstream.GetCollectionByID(context, getCollectionByIDRequest)
if err != nil {
return response, err
}
// if we expand transactions in the query retrieve each transaction data
transactions := make([]*entities.Transaction, 0)
if r.ExpandsTransactions {
for _, tid := range collectionResponse.Collection.TransactionIds {
getTransactionRequest := &accessproto.GetTransactionRequest{
Id: tid,
}
transactionResponse, err := upstream.GetTransaction(context, getTransactionRequest)
if err != nil {
return response, err
}
transactions = append(transactions, transactionResponse.Transaction)
}
}
err = response.BuildFromGrpc(collectionResponse.Collection, transactions, link, expandFields, chain)
if err != nil {
return response, err
}
return response, nil
}
// ExecuteScript handler sends the script from the request to be executed.
func (f *RestForwarder) ExecuteScript(r request.GetScript, context context.Context, _ models.LinkGenerator) ([]byte, error) {
upstream, err := f.FaultTolerantClient()
if err != nil {
return nil, err
}
if r.BlockID != flow.ZeroID {
executeScriptAtBlockIDRequest := &accessproto.ExecuteScriptAtBlockIDRequest{
BlockId: r.BlockID[:],
Script: r.Script.Source,
Arguments: r.Script.Args,
}
executeScriptAtBlockIDResponse, err := upstream.ExecuteScriptAtBlockID(context, executeScriptAtBlockIDRequest)
if err != nil {
return nil, err
}
return executeScriptAtBlockIDResponse.Value, nil
}
// default to sealed height
if r.BlockHeight == request.SealedHeight || r.BlockHeight == request.EmptyHeight {
executeScriptAtLatestBlockRequest := &accessproto.ExecuteScriptAtLatestBlockRequest{
Script: r.Script.Source,
Arguments: r.Script.Args,
}
executeScriptAtLatestBlockResponse, err := upstream.ExecuteScriptAtLatestBlock(context, executeScriptAtLatestBlockRequest)
if err != nil {
return nil, err
}
return executeScriptAtLatestBlockResponse.Value, nil
}
if r.BlockHeight == request.FinalHeight {
getLatestBlockHeaderRequest := &accessproto.GetLatestBlockHeaderRequest{
IsSealed: false,
}
getLatestBlockHeaderResponse, err := upstream.GetLatestBlockHeader(context, getLatestBlockHeaderRequest)
if err != nil {
return nil, err
}
r.BlockHeight = getLatestBlockHeaderResponse.Block.Height
}
executeScriptAtBlockHeightRequest := &accessproto.ExecuteScriptAtBlockHeightRequest{
BlockHeight: r.BlockHeight,
Script: r.Script.Source,
Arguments: r.Script.Args,
}
executeScriptAtBlockHeightResponse, err := upstream.ExecuteScriptAtBlockHeight(context, executeScriptAtBlockHeightRequest)
if err != nil {
return nil, err
}
return executeScriptAtBlockHeightResponse.Value, nil
}
// GetAccount handler retrieves account by address and returns the response.
func (f *RestForwarder) GetAccount(r request.GetAccount, context context.Context, expandFields map[string]bool, link models.LinkGenerator) (models.Account, error) {
var response models.Account
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
// in case we receive special height values 'final' and 'sealed', fetch that height and overwrite request with it
if r.Height == request.FinalHeight || r.Height == request.SealedHeight {
getLatestBlockHeaderRequest := &accessproto.GetLatestBlockHeaderRequest{
IsSealed: r.Height == request.SealedHeight,
}
blockHeaderResponse, err := upstream.GetLatestBlockHeader(context, getLatestBlockHeaderRequest)
if err != nil {
return response, err
}
r.Height = blockHeaderResponse.Block.Height
}
getAccountAtBlockHeightRequest := &accessproto.GetAccountAtBlockHeightRequest{
Address: r.Address.Bytes(),
BlockHeight: r.Height,
}
accountResponse, err := upstream.GetAccountAtBlockHeight(context, getAccountAtBlockHeightRequest)
if err != nil {
return response, models.NewNotFoundError("not found account at block height", err)
}
flowAccount, err := convert.MessageToAccount(accountResponse.Account)
if err != nil {
return response, err
}
err = response.Build(flowAccount, link, expandFields)
return response, err
}
// GetEvents for the provided block range or list of block IDs filtered by type.
func (f *RestForwarder) GetEvents(r request.GetEvents, context context.Context) (models.BlocksEvents, error) {
// if the request has block IDs provided then return events for block IDs
var blocksEvents models.BlocksEvents
upstream, err := f.FaultTolerantClient()
if err != nil {
return blocksEvents, err
}
if len(r.BlockIDs) > 0 {
var blockIds [][]byte
for _, id := range r.BlockIDs {
blockIds = append(blockIds, id[:])
}
getEventsForBlockIDsRequest := &accessproto.GetEventsForBlockIDsRequest{
Type: r.Type,
BlockIds: blockIds,
}
eventsResponse, err := upstream.GetEventsForBlockIDs(context, getEventsForBlockIDsRequest)
if err != nil {
return nil, err
}
blocksEvents.BuildFromGrpc(eventsResponse.Results)
return blocksEvents, nil
}
// if end height is provided with special values then load the height
if r.EndHeight == request.FinalHeight || r.EndHeight == request.SealedHeight {
getLatestBlockHeaderRequest := &accessproto.GetLatestBlockHeaderRequest{
IsSealed: r.EndHeight == request.SealedHeight,
}
latestBlockHeaderResponse, err := upstream.GetLatestBlockHeader(context, getLatestBlockHeaderRequest)
if err != nil {
return nil, err
}
r.EndHeight = latestBlockHeaderResponse.Block.Height
// special check after we resolve special height value
if r.StartHeight > r.EndHeight {
return nil, models.NewBadRequestError(fmt.Errorf("current retrieved end height value is lower than start height"))
}
}
// if request provided block height range then return events for that range
getEventsForHeightRangeRequest := &accessproto.GetEventsForHeightRangeRequest{
Type: r.Type,
StartHeight: r.StartHeight,
EndHeight: r.EndHeight,
}
eventsResponse, err := upstream.GetEventsForHeightRange(context, getEventsForHeightRangeRequest)
if err != nil {
return nil, err
}
blocksEvents.BuildFromGrpc(eventsResponse.Results)
return blocksEvents, nil
}
// GetNetworkParameters returns network-wide parameters of the blockchain
func (f *RestForwarder) GetNetworkParameters(r *request.Request) (models.NetworkParameters, error) {
var response models.NetworkParameters
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
getNetworkParametersRequest := &accessproto.GetNetworkParametersRequest{}
getNetworkParametersResponse, err := upstream.GetNetworkParameters(r.Context(), getNetworkParametersRequest)
if err != nil {
return response, err
}
response.BuildFromGrpc(getNetworkParametersResponse)
return response, nil
}
// GetNodeVersionInfo returns node version information
func (f *RestForwarder) GetNodeVersionInfo(r *request.Request) (models.NodeVersionInfo, error) {
var response models.NodeVersionInfo
upstream, err := f.FaultTolerantClient()
if err != nil {
return response, err
}
getNodeVersionInfoRequest := &accessproto.GetNodeVersionInfoRequest{}
getNodeVersionInfoResponse, err := upstream.GetNodeVersionInfo(r.Context(), getNodeVersionInfoRequest)
if err != nil {
return response, err
}
response.BuildFromGrpc(getNodeVersionInfoResponse.Info)
return response, nil
}
func getBlockFromGrpc(option routes.BlockRequestOption, context context.Context, expandFields map[string]bool, upstream accessproto.AccessAPIClient, link models.LinkGenerator) (*models.Block, error) {
// lookup block
blkProvider := routes.NewBlockFromGrpcProvider(upstream, option)
blk, blockStatus, err := blkProvider.GetBlock(context)
if err != nil {
return nil, err
}
// lookup execution result
// (even if not specified as expandable, since we need the execution result ID to generate its expandable link)
var block models.Block
getExecutionResultForBlockIDRequest := &accessproto.GetExecutionResultForBlockIDRequest{
BlockId: blk.Id,
}
executionResultForBlockIDResponse, err := upstream.GetExecutionResultForBlockID(context, getExecutionResultForBlockIDRequest)
if err != nil {
return nil, err
}
flowExecResult, err := convert.MessageToExecutionResult(executionResultForBlockIDResponse.ExecutionResult)
if err != nil {
return nil, err
}
flowBlock, err := convert.MessageToBlock(blk)
if err != nil {
return nil, err
}
flowBlockStatus, err := convert.MessagesToBlockStatus(blockStatus)
if err != nil {
return nil, err
}
if err != nil {
// handle case where execution result is not yet available
if se, ok := status.FromError(err); ok {
if se.Code() == codes.NotFound {
err := block.Build(flowBlock, nil, link, flowBlockStatus, expandFields)
if err != nil {
return nil, err
}
return &block, nil
}
}
return nil, err
}
err = block.Build(flowBlock, flowExecResult, link, flowBlockStatus, expandFields)
if err != nil {
return nil, err
}
return &block, nil
}