-
Notifications
You must be signed in to change notification settings - Fork 175
/
backend_transactions.go
1307 lines (1145 loc) · 44.2 KB
/
backend_transactions.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package backend
import (
"context"
"errors"
"fmt"
"time"
lru "github.com/hashicorp/golang-lru/v2"
accessproto "github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/entities"
execproto "github.com/onflow/flow/protobuf/go/flow/execution"
"github.com/rs/zerolog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rpc/connection"
"github.com/onflow/flow-go/engine/common/rpc"
"github.com/onflow/flow-go/engine/common/rpc/convert"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/state"
"github.com/onflow/flow-go/storage"
)
type backendTransactions struct {
*TransactionsLocalDataProvider
staticCollectionRPC accessproto.AccessAPIClient // rpc client tied to a fixed collection node
transactions storage.Transactions
executionReceipts storage.ExecutionReceipts
chainID flow.ChainID
transactionMetrics module.TransactionMetrics
transactionValidator *access.TransactionValidator
retry *Retry
connFactory connection.ConnectionFactory
previousAccessNodes []accessproto.AccessAPIClient
log zerolog.Logger
nodeCommunicator Communicator
txResultCache *lru.Cache[flow.Identifier, *access.TransactionResult]
txErrorMessagesCache *lru.Cache[flow.Identifier, string] // cache for transactions error messages, indexed by hash(block_id, tx_id).
txResultQueryMode IndexQueryMode
systemTxID flow.Identifier
systemTx *flow.TransactionBody
}
var _ TransactionErrorMessage = (*backendTransactions)(nil)
// SendTransaction forwards the transaction to the collection node
func (b *backendTransactions) SendTransaction(
ctx context.Context,
tx *flow.TransactionBody,
) error {
now := time.Now().UTC()
err := b.transactionValidator.Validate(tx)
if err != nil {
return status.Errorf(codes.InvalidArgument, "invalid transaction: %s", err.Error())
}
// send the transaction to the collection node if valid
err = b.trySendTransaction(ctx, tx)
if err != nil {
b.transactionMetrics.TransactionSubmissionFailed()
return rpc.ConvertError(err, "failed to send transaction to a collection node", codes.Internal)
}
b.transactionMetrics.TransactionReceived(tx.ID(), now)
// store the transaction locally
err = b.transactions.Store(tx)
if err != nil {
return status.Errorf(codes.Internal, "failed to store transaction: %v", err)
}
if b.retry.IsActive() {
go b.registerTransactionForRetry(tx)
}
return nil
}
// trySendTransaction tries to transaction to a collection node
func (b *backendTransactions) trySendTransaction(ctx context.Context, tx *flow.TransactionBody) error {
// if a collection node rpc client was provided at startup, just use that
if b.staticCollectionRPC != nil {
return b.grpcTxSend(ctx, b.staticCollectionRPC, tx)
}
// otherwise choose all collection nodes to try
collNodes, err := b.chooseCollectionNodes(tx.ID())
if err != nil {
return fmt.Errorf("failed to determine collection node for tx %x: %w", tx, err)
}
var sendError error
logAnyError := func() {
if sendError != nil {
b.log.Info().Err(err).Msg("failed to send transactions to collector nodes")
}
}
defer logAnyError()
// try sending the transaction to one of the chosen collection nodes
sendError = b.nodeCommunicator.CallAvailableNode(
collNodes,
func(node *flow.IdentitySkeleton) error {
err = b.sendTransactionToCollector(ctx, tx, node.Address)
if err != nil {
return err
}
return nil
},
nil,
)
return sendError
}
// chooseCollectionNodes finds a random subset of size sampleSize of collection node addresses from the
// collection node cluster responsible for the given tx
func (b *backendTransactions) chooseCollectionNodes(txID flow.Identifier) (flow.IdentitySkeletonList, error) {
// retrieve the set of collector clusters
clusters, err := b.state.Final().Epochs().Current().Clustering()
if err != nil {
return nil, fmt.Errorf("could not cluster collection nodes: %w", err)
}
// get the cluster responsible for the transaction
targetNodes, ok := clusters.ByTxID(txID)
if !ok {
return nil, fmt.Errorf("could not get local cluster by txID: %x", txID)
}
return targetNodes, nil
}
// sendTransactionToCollection sends the transaction to the given collection node via grpc
func (b *backendTransactions) sendTransactionToCollector(
ctx context.Context,
tx *flow.TransactionBody,
collectionNodeAddr string,
) error {
collectionRPC, closer, err := b.connFactory.GetAccessAPIClient(collectionNodeAddr, nil)
if err != nil {
return fmt.Errorf("failed to connect to collection node at %s: %w", collectionNodeAddr, err)
}
defer closer.Close()
err = b.grpcTxSend(ctx, collectionRPC, tx)
if err != nil {
return fmt.Errorf("failed to send transaction to collection node at %s: %w", collectionNodeAddr, err)
}
return nil
}
func (b *backendTransactions) grpcTxSend(ctx context.Context, client accessproto.AccessAPIClient, tx *flow.TransactionBody) error {
colReq := &accessproto.SendTransactionRequest{
Transaction: convert.TransactionToMessage(*tx),
}
clientDeadline := time.Now().Add(time.Duration(2) * time.Second)
ctx, cancel := context.WithDeadline(ctx, clientDeadline)
defer cancel()
_, err := client.SendTransaction(ctx, colReq)
return err
}
// SendRawTransaction sends a raw transaction to the collection node
func (b *backendTransactions) SendRawTransaction(
ctx context.Context,
tx *flow.TransactionBody,
) error {
// send the transaction to the collection node
return b.trySendTransaction(ctx, tx)
}
func (b *backendTransactions) GetTransaction(ctx context.Context, txID flow.Identifier) (*flow.TransactionBody, error) {
// look up transaction from storage
tx, err := b.transactions.ByID(txID)
txErr := rpc.ConvertStorageError(err)
if txErr != nil {
if status.Code(txErr) == codes.NotFound {
return b.getHistoricalTransaction(ctx, txID)
}
// Other Error trying to retrieve the transaction, return with err
return nil, txErr
}
return tx, nil
}
func (b *backendTransactions) GetTransactionsByBlockID(
_ context.Context,
blockID flow.Identifier,
) ([]*flow.TransactionBody, error) {
var transactions []*flow.TransactionBody
// TODO: consider using storage.Index.ByBlockID, the index contains collection id and seals ID
block, err := b.blocks.ByID(blockID)
if err != nil {
return nil, rpc.ConvertStorageError(err)
}
for _, guarantee := range block.Payload.Guarantees {
collection, err := b.collections.ByID(guarantee.CollectionID)
if err != nil {
return nil, rpc.ConvertStorageError(err)
}
transactions = append(transactions, collection.Transactions...)
}
transactions = append(transactions, b.systemTx)
return transactions, nil
}
func (b *backendTransactions) GetTransactionResult(
ctx context.Context,
txID flow.Identifier,
blockID flow.Identifier,
collectionID flow.Identifier,
requiredEventEncodingVersion entities.EventEncodingVersion,
) (*access.TransactionResult, error) {
// look up transaction from storage
start := time.Now()
tx, err := b.transactions.ByID(txID)
if err != nil {
txErr := rpc.ConvertStorageError(err)
if status.Code(txErr) != codes.NotFound {
return nil, txErr
}
// Tx not found. If we have historical Sporks setup, lets look through those as well
if b.txResultCache != nil {
val, ok := b.txResultCache.Get(txID)
if ok {
return val, nil
}
}
historicalTxResult, err := b.getHistoricalTransactionResult(ctx, txID)
if err != nil {
// if tx not found in old access nodes either, then assume that the tx was submitted to a different AN
// and return status as unknown
txStatus := flow.TransactionStatusUnknown
result := &access.TransactionResult{
Status: txStatus,
StatusCode: uint(txStatus),
}
if b.txResultCache != nil {
b.txResultCache.Add(txID, result)
}
return result, nil
}
if b.txResultCache != nil {
b.txResultCache.Add(txID, historicalTxResult)
}
return historicalTxResult, nil
}
block, err := b.retrieveBlock(blockID, collectionID, txID)
// an error occurred looking up the block or the requested block or collection was not found.
// If looking up the block based solely on the txID returns not found, then no error is
// returned since the block may not be finalized yet.
if err != nil {
return nil, rpc.ConvertStorageError(err)
}
var blockHeight uint64
var txResult *access.TransactionResult
// access node may not have the block if it hasn't yet been finalized, hence block can be nil at this point
if block != nil {
txResult, err = b.lookupTransactionResult(ctx, txID, block, requiredEventEncodingVersion)
if err != nil {
return nil, rpc.ConvertError(err, "failed to retrieve result", codes.Internal)
}
// an additional check to ensure the correctness of the collection ID.
expectedCollectionID, err := b.LookupCollectionIDInBlock(block, txID)
if err != nil {
// if the collection has not been indexed yet, the lookup will return a not found error.
// if the request included a blockID or collectionID in its the search criteria, not found
// should result in an error because it's not possible to guarantee that the result found
// is the correct one.
if blockID != flow.ZeroID || collectionID != flow.ZeroID {
return nil, rpc.ConvertStorageError(err)
}
}
if collectionID == flow.ZeroID {
collectionID = expectedCollectionID
} else if collectionID != expectedCollectionID {
return nil, status.Error(codes.InvalidArgument, "transaction not found in provided collection")
}
blockID = block.ID()
blockHeight = block.Header.Height
}
// If there is still no transaction result, provide one based on available information.
if txResult == nil {
var txStatus flow.TransactionStatus
// Derive the status of the transaction.
if block == nil {
txStatus, err = b.DeriveUnknownTransactionStatus(tx.ReferenceBlockID)
} else {
txStatus, err = b.DeriveTransactionStatus(blockHeight, false)
}
if err != nil {
if !errors.Is(err, state.ErrUnknownSnapshotReference) {
irrecoverable.Throw(ctx, err)
}
return nil, rpc.ConvertStorageError(err)
}
txResult = &access.TransactionResult{
BlockID: blockID,
BlockHeight: blockHeight,
TransactionID: txID,
Status: txStatus,
CollectionID: collectionID,
}
} else {
txResult.CollectionID = collectionID
}
b.transactionMetrics.TransactionResultFetched(time.Since(start), len(tx.Script))
return txResult, nil
}
// retrieveBlock function returns a block based on the input argument. The block ID lookup has the highest priority,
// followed by the collection ID lookup. If both are missing, the default lookup by transaction ID is performed.
func (b *backendTransactions) retrieveBlock(
// the requested block or collection was not found. If looking up the block based solely on the txID returns
// not found, then no error is returned.
blockID flow.Identifier,
collectionID flow.Identifier,
txID flow.Identifier,
) (*flow.Block, error) {
if blockID != flow.ZeroID {
return b.blocks.ByID(blockID)
}
if collectionID != flow.ZeroID {
return b.blocks.ByCollectionID(collectionID)
}
// find the block for the transaction
block, err := b.lookupBlock(txID)
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return nil, err
}
return block, nil
}
func (b *backendTransactions) GetTransactionResultsByBlockID(
ctx context.Context,
blockID flow.Identifier,
requiredEventEncodingVersion entities.EventEncodingVersion,
) ([]*access.TransactionResult, error) {
// TODO: consider using storage.Index.ByBlockID, the index contains collection id and seals ID
block, err := b.blocks.ByID(blockID)
if err != nil {
return nil, rpc.ConvertStorageError(err)
}
switch b.txResultQueryMode {
case IndexQueryModeExecutionNodesOnly:
return b.getTransactionResultsByBlockIDFromExecutionNode(ctx, block, requiredEventEncodingVersion)
case IndexQueryModeLocalOnly:
return b.GetTransactionResultsByBlockIDFromStorage(ctx, block, requiredEventEncodingVersion)
case IndexQueryModeFailover:
results, err := b.GetTransactionResultsByBlockIDFromStorage(ctx, block, requiredEventEncodingVersion)
if err == nil {
return results, nil
}
// If any error occurs with local storage - request transaction result from EN
return b.getTransactionResultsByBlockIDFromExecutionNode(ctx, block, requiredEventEncodingVersion)
default:
return nil, status.Errorf(codes.Internal, "unknown transaction result query mode: %v", b.txResultQueryMode)
}
}
func (b *backendTransactions) getTransactionResultsByBlockIDFromExecutionNode(
ctx context.Context,
block *flow.Block,
requiredEventEncodingVersion entities.EventEncodingVersion,
) ([]*access.TransactionResult, error) {
blockID := block.ID()
req := &execproto.GetTransactionsByBlockIDRequest{
BlockId: blockID[:],
}
execNodes, err := executionNodesForBlockID(ctx, blockID, b.executionReceipts, b.state, b.log)
if err != nil {
if IsInsufficientExecutionReceipts(err) {
return nil, status.Errorf(codes.NotFound, err.Error())
}
return nil, rpc.ConvertError(err, "failed to retrieve result from any execution node", codes.Internal)
}
resp, err := b.getTransactionResultsByBlockIDFromAnyExeNode(ctx, execNodes, req)
if err != nil {
return nil, rpc.ConvertError(err, "failed to retrieve result from execution node", codes.Internal)
}
results := make([]*access.TransactionResult, 0, len(resp.TransactionResults))
i := 0
errInsufficientResults := status.Errorf(
codes.Internal,
"number of transaction results returned by execution node is less than the number of transactions in the block",
)
for _, guarantee := range block.Payload.Guarantees {
collection, err := b.collections.LightByID(guarantee.CollectionID)
if err != nil {
return nil, rpc.ConvertStorageError(err)
}
for _, txID := range collection.Transactions {
// bounds check. this means the EN returned fewer transaction results than the transactions in the block
if i >= len(resp.TransactionResults) {
return nil, errInsufficientResults
}
txResult := resp.TransactionResults[i]
// tx body is irrelevant to status if it's in an executed block
txStatus, err := b.DeriveTransactionStatus(block.Header.Height, true)
if err != nil {
if !errors.Is(err, state.ErrUnknownSnapshotReference) {
irrecoverable.Throw(ctx, err)
}
return nil, rpc.ConvertStorageError(err)
}
events, err := convert.MessagesToEventsWithEncodingConversion(txResult.GetEvents(), resp.GetEventEncodingVersion(), requiredEventEncodingVersion)
if err != nil {
return nil, status.Errorf(codes.Internal,
"failed to convert events to message in txID %x: %v", txID, err)
}
results = append(results, &access.TransactionResult{
Status: txStatus,
StatusCode: uint(txResult.GetStatusCode()),
Events: events,
ErrorMessage: txResult.GetErrorMessage(),
BlockID: blockID,
TransactionID: txID,
CollectionID: guarantee.CollectionID,
BlockHeight: block.Header.Height,
})
i++
}
}
// after iterating through all transactions in each collection, i equals the total number of
// user transactions in the block
txCount := i
sporkRootBlockHeight := b.state.Params().SporkRootBlockHeight()
// root block has no system transaction result
if block.Header.Height > sporkRootBlockHeight {
// system chunk transaction
// resp.TransactionResults includes the system tx result, so there should be exactly one
// more result than txCount
if txCount != len(resp.TransactionResults)-1 {
if txCount >= len(resp.TransactionResults) {
return nil, errInsufficientResults
}
// otherwise there are extra results
// TODO(bft): slashable offense
return nil, status.Errorf(codes.Internal, "number of transaction results returned by execution node is more than the number of transactions in the block")
}
systemTxResult := resp.TransactionResults[len(resp.TransactionResults)-1]
systemTxStatus, err := b.DeriveTransactionStatus(block.Header.Height, true)
if err != nil {
if !errors.Is(err, state.ErrUnknownSnapshotReference) {
irrecoverable.Throw(ctx, err)
}
return nil, rpc.ConvertStorageError(err)
}
events, err := convert.MessagesToEventsWithEncodingConversion(systemTxResult.GetEvents(), resp.GetEventEncodingVersion(), requiredEventEncodingVersion)
if err != nil {
return nil, rpc.ConvertError(err, "failed to convert events from system tx result", codes.Internal)
}
results = append(results, &access.TransactionResult{
Status: systemTxStatus,
StatusCode: uint(systemTxResult.GetStatusCode()),
Events: events,
ErrorMessage: systemTxResult.GetErrorMessage(),
BlockID: blockID,
TransactionID: b.systemTxID,
BlockHeight: block.Header.Height,
})
}
return results, nil
}
// GetTransactionResultByIndex returns transactions Results for an index in a block that is executed,
// pending or finalized transactions return errors
func (b *backendTransactions) GetTransactionResultByIndex(
ctx context.Context,
blockID flow.Identifier,
index uint32,
requiredEventEncodingVersion entities.EventEncodingVersion,
) (*access.TransactionResult, error) {
// TODO: https://github.com/onflow/flow-go/issues/2175 so caching doesn't cause a circular dependency
block, err := b.blocks.ByID(blockID)
if err != nil {
return nil, rpc.ConvertStorageError(err)
}
switch b.txResultQueryMode {
case IndexQueryModeExecutionNodesOnly:
return b.getTransactionResultByIndexFromExecutionNode(ctx, block, index, requiredEventEncodingVersion)
case IndexQueryModeLocalOnly:
return b.GetTransactionResultByIndexFromStorage(ctx, block, index, requiredEventEncodingVersion)
case IndexQueryModeFailover:
result, err := b.GetTransactionResultByIndexFromStorage(ctx, block, index, requiredEventEncodingVersion)
if err == nil {
return result, nil
}
// If any error occurs with local storage - request transaction result from EN
return b.getTransactionResultByIndexFromExecutionNode(ctx, block, index, requiredEventEncodingVersion)
default:
return nil, status.Errorf(codes.Internal, "unknown transaction result query mode: %v", b.txResultQueryMode)
}
}
func (b *backendTransactions) getTransactionResultByIndexFromExecutionNode(
ctx context.Context,
block *flow.Block,
index uint32,
requiredEventEncodingVersion entities.EventEncodingVersion,
) (*access.TransactionResult, error) {
blockID := block.ID()
// create request and forward to EN
req := &execproto.GetTransactionByIndexRequest{
BlockId: blockID[:],
Index: index,
}
execNodes, err := executionNodesForBlockID(ctx, blockID, b.executionReceipts, b.state, b.log)
if err != nil {
if IsInsufficientExecutionReceipts(err) {
return nil, status.Errorf(codes.NotFound, err.Error())
}
return nil, rpc.ConvertError(err, "failed to retrieve result from any execution node", codes.Internal)
}
resp, err := b.getTransactionResultByIndexFromAnyExeNode(ctx, execNodes, req)
if err != nil {
return nil, rpc.ConvertError(err, "failed to retrieve result from execution node", codes.Internal)
}
// tx body is irrelevant to status if it's in an executed block
txStatus, err := b.DeriveTransactionStatus(block.Header.Height, true)
if err != nil {
if !errors.Is(err, state.ErrUnknownSnapshotReference) {
irrecoverable.Throw(ctx, err)
}
return nil, rpc.ConvertStorageError(err)
}
events, err := convert.MessagesToEventsWithEncodingConversion(resp.GetEvents(), resp.GetEventEncodingVersion(), requiredEventEncodingVersion)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to convert events in blockID %x: %v", blockID, err)
}
// convert to response, cache and return
return &access.TransactionResult{
Status: txStatus,
StatusCode: uint(resp.GetStatusCode()),
Events: events,
ErrorMessage: resp.GetErrorMessage(),
BlockID: blockID,
BlockHeight: block.Header.Height,
}, nil
}
// GetSystemTransaction returns system transaction
func (b *backendTransactions) GetSystemTransaction(ctx context.Context, _ flow.Identifier) (*flow.TransactionBody, error) {
return b.systemTx, nil
}
// GetSystemTransactionResult returns system transaction result
func (b *backendTransactions) GetSystemTransactionResult(ctx context.Context, blockID flow.Identifier, requiredEventEncodingVersion entities.EventEncodingVersion) (*access.TransactionResult, error) {
block, err := b.blocks.ByID(blockID)
if err != nil {
return nil, rpc.ConvertStorageError(err)
}
return b.lookupTransactionResult(ctx, b.systemTxID, block, requiredEventEncodingVersion)
}
// Error returns:
// - `storage.ErrNotFound` - collection referenced by transaction or block by a collection has not been found.
// - all other errors are unexpected and potentially symptoms of internal implementation bugs or state corruption (fatal).
func (b *backendTransactions) lookupBlock(txID flow.Identifier) (*flow.Block, error) {
collection, err := b.collections.LightByTransactionID(txID)
if err != nil {
return nil, err
}
block, err := b.blocks.ByCollectionID(collection.ID())
if err != nil {
return nil, err
}
return block, nil
}
func (b *backendTransactions) lookupTransactionResult(
ctx context.Context,
txID flow.Identifier,
block *flow.Block,
requiredEventEncodingVersion entities.EventEncodingVersion,
) (*access.TransactionResult, error) {
var txResult *access.TransactionResult
var err error
switch b.txResultQueryMode {
case IndexQueryModeExecutionNodesOnly:
txResult, err = b.getTransactionResultFromExecutionNode(ctx, block, txID, requiredEventEncodingVersion)
case IndexQueryModeLocalOnly:
txResult, err = b.GetTransactionResultFromStorage(ctx, block, txID, requiredEventEncodingVersion)
case IndexQueryModeFailover:
txResult, err = b.GetTransactionResultFromStorage(ctx, block, txID, requiredEventEncodingVersion)
if err != nil {
// If any error occurs with local storage - request transaction result from EN
txResult, err = b.getTransactionResultFromExecutionNode(ctx, block, txID, requiredEventEncodingVersion)
}
default:
return nil, status.Errorf(codes.Internal, "unknown transaction result query mode: %v", b.txResultQueryMode)
}
if err != nil {
// if either the storage or execution node reported no results or there were not enough execution results
if status.Code(err) == codes.NotFound {
// No result yet, indicate that it has not been executed
return nil, nil
}
// Other Error trying to retrieve the result, return with err
return nil, err
}
// considered executed as long as some result is returned, even if it's an error message
return txResult, nil
}
func (b *backendTransactions) getHistoricalTransaction(
ctx context.Context,
txID flow.Identifier,
) (*flow.TransactionBody, error) {
for _, historicalNode := range b.previousAccessNodes {
txResp, err := historicalNode.GetTransaction(ctx, &accessproto.GetTransactionRequest{Id: txID[:]})
if err == nil {
tx, err := convert.MessageToTransaction(txResp.Transaction, b.chainID.Chain())
if err != nil {
return nil, status.Errorf(codes.Internal, "could not convert transaction: %v", err)
}
// Found on a historical node. Report
return &tx, nil
}
// Otherwise, if not found, just continue
if status.Code(err) == codes.NotFound {
continue
}
// TODO should we do something if the error isn't not found?
}
return nil, status.Errorf(codes.NotFound, "no known transaction with ID %s", txID)
}
func (b *backendTransactions) getHistoricalTransactionResult(
ctx context.Context,
txID flow.Identifier,
) (*access.TransactionResult, error) {
for _, historicalNode := range b.previousAccessNodes {
result, err := historicalNode.GetTransactionResult(ctx, &accessproto.GetTransactionRequest{Id: txID[:]})
if err == nil {
// Found on a historical node. Report
if result.GetStatus() == entities.TransactionStatus_UNKNOWN {
// We've moved to returning Status UNKNOWN instead of an error with the NotFound status,
// Therefore we should continue and look at the next access node for answers.
continue
}
if result.GetStatus() == entities.TransactionStatus_PENDING {
// This is on a historical node. No transactions from it will ever be
// executed, therefore we should consider this expired
result.Status = entities.TransactionStatus_EXPIRED
}
return access.MessageToTransactionResult(result), nil
}
// Otherwise, if not found, just continue
if status.Code(err) == codes.NotFound {
continue
}
// TODO should we do something if the error isn't not found?
}
return nil, status.Errorf(codes.NotFound, "no known transaction with ID %s", txID)
}
func (b *backendTransactions) registerTransactionForRetry(tx *flow.TransactionBody) {
referenceBlock, err := b.state.AtBlockID(tx.ReferenceBlockID).Head()
if err != nil {
return
}
b.retry.RegisterTransaction(referenceBlock.Height, tx)
}
func (b *backendTransactions) getTransactionResultFromExecutionNode(
ctx context.Context,
block *flow.Block,
transactionID flow.Identifier,
requiredEventEncodingVersion entities.EventEncodingVersion,
) (*access.TransactionResult, error) {
blockID := block.ID()
// create an execution API request for events at blockID and transactionID
req := &execproto.GetTransactionResultRequest{
BlockId: blockID[:],
TransactionId: transactionID[:],
}
execNodes, err := executionNodesForBlockID(ctx, blockID, b.executionReceipts, b.state, b.log)
if err != nil {
// if no execution receipt were found, return a NotFound GRPC error
if IsInsufficientExecutionReceipts(err) {
return nil, status.Errorf(codes.NotFound, err.Error())
}
return nil, err
}
resp, err := b.getTransactionResultFromAnyExeNode(ctx, execNodes, req)
if err != nil {
return nil, err
}
// tx body is irrelevant to status if it's in an executed block
txStatus, err := b.DeriveTransactionStatus(block.Header.Height, true)
if err != nil {
if !errors.Is(err, state.ErrUnknownSnapshotReference) {
irrecoverable.Throw(ctx, err)
}
return nil, rpc.ConvertStorageError(err)
}
events, err := convert.MessagesToEventsWithEncodingConversion(resp.GetEvents(), resp.GetEventEncodingVersion(), requiredEventEncodingVersion)
if err != nil {
return nil, rpc.ConvertError(err, "failed to convert events to message", codes.Internal)
}
return &access.TransactionResult{
TransactionID: transactionID,
Status: txStatus,
StatusCode: uint(resp.GetStatusCode()),
Events: events,
ErrorMessage: resp.GetErrorMessage(),
BlockID: blockID,
BlockHeight: block.Header.Height,
}, nil
}
// ATTENTION: might be a source of problems in future. We run this code on finalization gorotuine,
// potentially lagging finalization events if operations take long time.
// We might need to move this logic on dedicated goroutine and provide a way to skip finalization events if they are delivered
// too often for this engine. An example of similar approach - https://github.com/onflow/flow-go/blob/10b0fcbf7e2031674c00f3cdd280f27bd1b16c47/engine/common/follower/compliance_engine.go#L201..
// No errors expected during normal operations.
func (b *backendTransactions) ProcessFinalizedBlockHeight(height uint64) error {
return b.retry.Retry(height)
}
func (b *backendTransactions) getTransactionResultFromAnyExeNode(
ctx context.Context,
execNodes flow.IdentitySkeletonList,
req *execproto.GetTransactionResultRequest,
) (*execproto.GetTransactionResultResponse, error) {
var errToReturn error
defer func() {
if errToReturn != nil {
b.log.Info().Err(errToReturn).Msg("failed to get transaction result from execution nodes")
}
}()
var resp *execproto.GetTransactionResultResponse
errToReturn = b.nodeCommunicator.CallAvailableNode(
execNodes,
func(node *flow.IdentitySkeleton) error {
var err error
resp, err = b.tryGetTransactionResult(ctx, node, req)
if err == nil {
b.log.Debug().
Str("execution_node", node.String()).
Hex("block_id", req.GetBlockId()).
Hex("transaction_id", req.GetTransactionId()).
Msg("Successfully got transaction results from any node")
return nil
}
return err
},
nil,
)
return resp, errToReturn
}
func (b *backendTransactions) tryGetTransactionResult(
ctx context.Context,
execNode *flow.IdentitySkeleton,
req *execproto.GetTransactionResultRequest,
) (*execproto.GetTransactionResultResponse, error) {
execRPCClient, closer, err := b.connFactory.GetExecutionAPIClient(execNode.Address)
if err != nil {
return nil, err
}
defer closer.Close()
resp, err := execRPCClient.GetTransactionResult(ctx, req)
if err != nil {
return nil, err
}
return resp, nil
}
func (b *backendTransactions) getTransactionResultsByBlockIDFromAnyExeNode(
ctx context.Context,
execNodes flow.IdentitySkeletonList,
req *execproto.GetTransactionsByBlockIDRequest,
) (*execproto.GetTransactionResultsResponse, error) {
var errToReturn error
defer func() {
// log the errors
if errToReturn != nil {
b.log.Err(errToReturn).Msg("failed to get transaction results from execution nodes")
}
}()
// if we were passed 0 execution nodes add a specific error
if len(execNodes) == 0 {
return nil, errors.New("zero execution nodes")
}
var resp *execproto.GetTransactionResultsResponse
errToReturn = b.nodeCommunicator.CallAvailableNode(
execNodes,
func(node *flow.IdentitySkeleton) error {
var err error
resp, err = b.tryGetTransactionResultsByBlockID(ctx, node, req)
if err == nil {
b.log.Debug().
Str("execution_node", node.String()).
Hex("block_id", req.GetBlockId()).
Msg("Successfully got transaction results from any node")
return nil
}
return err
},
nil,
)
return resp, errToReturn
}
func (b *backendTransactions) tryGetTransactionResultsByBlockID(
ctx context.Context,
execNode *flow.IdentitySkeleton,
req *execproto.GetTransactionsByBlockIDRequest,
) (*execproto.GetTransactionResultsResponse, error) {
execRPCClient, closer, err := b.connFactory.GetExecutionAPIClient(execNode.Address)
if err != nil {
return nil, err
}
defer closer.Close()
resp, err := execRPCClient.GetTransactionResultsByBlockID(ctx, req)
if err != nil {
return nil, err
}
return resp, nil
}
func (b *backendTransactions) getTransactionResultByIndexFromAnyExeNode(
ctx context.Context,
execNodes flow.IdentitySkeletonList,
req *execproto.GetTransactionByIndexRequest,
) (*execproto.GetTransactionResultResponse, error) {
var errToReturn error
defer func() {
if errToReturn != nil {
b.log.Info().Err(errToReturn).Msg("failed to get transaction result from execution nodes")
}
}()
if len(execNodes) == 0 {
return nil, errors.New("zero execution nodes provided")
}
var resp *execproto.GetTransactionResultResponse
errToReturn = b.nodeCommunicator.CallAvailableNode(
execNodes,
func(node *flow.IdentitySkeleton) error {
var err error
resp, err = b.tryGetTransactionResultByIndex(ctx, node, req)
if err == nil {
b.log.Debug().
Str("execution_node", node.String()).
Hex("block_id", req.GetBlockId()).
Uint32("index", req.GetIndex()).
Msg("Successfully got transaction results from any node")
return nil
}
return err
},
nil,
)
return resp, errToReturn
}
func (b *backendTransactions) tryGetTransactionResultByIndex(
ctx context.Context,
execNode *flow.IdentitySkeleton,
req *execproto.GetTransactionByIndexRequest,
) (*execproto.GetTransactionResultResponse, error) {
execRPCClient, closer, err := b.connFactory.GetExecutionAPIClient(execNode.Address)
if err != nil {
return nil, err
}
defer closer.Close()
resp, err := execRPCClient.GetTransactionResultByIndex(ctx, req)
if err != nil {
return nil, err
}
return resp, nil
}
// LookupErrorMessageByTransactionID returns transaction error message for specified transaction.
// If an error message for transaction can be found in the cache then it will be used to serve the request, otherwise
// an RPC call will be made to the EN to fetch that error message, fetched value will be cached in the LRU cache.
// Expected errors during normal operation:
// - InsufficientExecutionReceipts - found insufficient receipts for given block ID.
// - status.Error - remote GRPC call to EN has failed.
func (b *backendTransactions) LookupErrorMessageByTransactionID(
ctx context.Context,
blockID flow.Identifier,
transactionID flow.Identifier,
) (string, error) {
var cacheKey flow.Identifier
var value string
if b.txErrorMessagesCache != nil {
cacheKey = flow.MakeIDFromFingerPrint(append(blockID[:], transactionID[:]...))
value, cached := b.txErrorMessagesCache.Get(cacheKey)
if cached {
return value, nil
}
}
execNodes, err := executionNodesForBlockID(ctx, blockID, b.executionReceipts, b.state, b.log)
if err != nil {
if IsInsufficientExecutionReceipts(err) {
return "", status.Errorf(codes.NotFound, err.Error())
}
return "", rpc.ConvertError(err, "failed to select execution nodes", codes.Internal)
}
req := &execproto.GetTransactionErrorMessageRequest{
BlockId: convert.IdentifierToMessage(blockID),
TransactionId: convert.IdentifierToMessage(transactionID),
}
resp, err := b.getTransactionErrorMessageFromAnyEN(ctx, execNodes, req)
if err != nil {
return "", fmt.Errorf("could not fetch error message from ENs: %w", err)