-
Notifications
You must be signed in to change notification settings - Fork 179
/
backend_transactions.go
502 lines (430 loc) · 16.3 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
package backend
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/go-multierror"
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/common/rpc/convert"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
const collectionNodesToTry uint = 3
type backendTransactions struct {
staticCollectionRPC accessproto.AccessAPIClient // rpc client tied to a fixed collection node
transactions storage.Transactions
executionReceipts storage.ExecutionReceipts
collections storage.Collections
blocks storage.Blocks
state protocol.State
chainID flow.ChainID
transactionMetrics module.TransactionMetrics
transactionValidator *access.TransactionValidator
retry *Retry
connFactory ConnectionFactory
previousAccessNodes []accessproto.AccessAPIClient
log zerolog.Logger
}
// 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 status.Error(codes.Internal, fmt.Sprintf("failed to send transaction to a collection node: %v", err))
}
b.transactionMetrics.TransactionReceived(tx.ID(), now)
// store the transaction locally
err = b.transactions.Store(tx)
if err != nil {
return status.Error(codes.InvalidArgument, fmt.Sprintf("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 a random set of collections nodes to try
collAddrs, err := b.chooseCollectionNodes(tx, collectionNodesToTry)
if err != nil {
return fmt.Errorf("failed to determine collection node for tx %x: %w", tx, err)
}
var sendErrors *multierror.Error
logAnyError := func() {
err = sendErrors.ErrorOrNil()
if err != 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
for _, addr := range collAddrs {
err = b.sendTransactionToCollector(ctx, tx, addr)
if err == nil {
return nil
}
sendErrors = multierror.Append(sendErrors, err)
}
return sendErrors.ErrorOrNil()
}
// 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(tx *flow.TransactionBody, sampleSize uint) ([]string, 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
txCluster, ok := clusters.ByTxID(tx.ID())
if !ok {
return nil, fmt.Errorf("could not get local cluster by txID: %x", tx.ID())
}
// select a random subset of collection nodes from the cluster to be tried in order
targetNodes := txCluster.Sample(sampleSize)
// collect the addresses of all the chosen collection nodes
var targetAddrs = make([]string, len(targetNodes))
for i, id := range targetNodes {
targetAddrs[i] = id.Address
}
return targetAddrs, 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 {
// TODO: Use a connection pool to cache connections
collectionRPC, conn, err := b.connFactory.GetAccessAPIClient(collectionNodeAddr)
if err != nil {
return fmt.Errorf("failed to connect to collection node at %s: %w", collectionNodeAddr, err)
}
defer conn.Close()
err = b.grpcTxSend(ctx, collectionRPC, tx)
if err != nil {
return fmt.Errorf("failed to send transaction to collection node at %s: %v", 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 := 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) GetTransactionResult(
ctx context.Context,
txID flow.Identifier,
) (*access.TransactionResult, error) {
// look up transaction from storage
tx, err := b.transactions.ByID(txID)
txErr := convertStorageError(err)
if txErr != nil {
if status.Code(txErr) == codes.NotFound {
// Tx not found. If we have historical Sporks setup, lets look through those as well
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
status := flow.TransactionStatusUnknown
return &access.TransactionResult{
Status: status,
StatusCode: uint(status),
}, nil
}
return historicalTxResult, nil
}
return nil, txErr
}
// find the block for the transaction
block, err := b.lookupBlock(txID)
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return nil, convertStorageError(err)
}
var blockID flow.Identifier
var transactionWasExecuted bool
var events []flow.Event
var txError string
var statusCode uint32
// 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 {
blockID = block.ID()
transactionWasExecuted, events, statusCode, txError, err = b.lookupTransactionResult(ctx, txID, blockID)
if err != nil {
return nil, convertStorageError(err)
}
}
// derive status of the transaction
status, err := b.deriveTransactionStatus(tx, transactionWasExecuted, block)
if err != nil {
return nil, convertStorageError(err)
}
return &access.TransactionResult{
Status: status,
StatusCode: uint(statusCode),
Events: events,
ErrorMessage: txError,
BlockID: blockID,
}, nil
}
// deriveTransactionStatus derives the transaction status based on current protocol state
func (b *backendTransactions) deriveTransactionStatus(
tx *flow.TransactionBody,
executed bool,
block *flow.Block,
) (flow.TransactionStatus, error) {
if block == nil {
// Not in a block, let's see if it's expired
referenceBlock, err := b.state.AtBlockID(tx.ReferenceBlockID).Head()
if err != nil {
return flow.TransactionStatusUnknown, err
}
refHeight := referenceBlock.Height
// get the latest finalized block from the state
finalized, err := b.state.Final().Head()
if err != nil {
return flow.TransactionStatusUnknown, err
}
finalizedHeight := finalized.Height
// if we haven't seen the expiry block for this transaction, it's not expired
if !b.isExpired(refHeight, finalizedHeight) {
return flow.TransactionStatusPending, nil
}
// At this point, we have seen the expiry block for the transaction.
// This means that, if no collections prior to the expiry block contain
// the transaction, it can never be included and is expired.
//
// To ensure this, we need to have received all collections up to the
// expiry block to ensure the transaction did not appear in any.
// the last full height is the height where we have received all
// collections for all blocks with a lower height
fullHeight, err := b.blocks.GetLastFullBlockHeight()
if err != nil {
return flow.TransactionStatusUnknown, err
}
// if we have received collections for all blocks up to the expiry block, the transaction is expired
if b.isExpired(refHeight, fullHeight) {
return flow.TransactionStatusExpired, err
}
// tx found in transaction storage and collection storage but not in block storage
// However, this will not happen as of now since the ingestion engine doesn't subscribe
// for collections
return flow.TransactionStatusPending, nil
}
if !executed {
// If we've gotten here, but the block has not yet been executed, report it as only been finalized
return flow.TransactionStatusFinalized, nil
}
// From this point on, we know for sure this transaction has at least been executed
// get the latest sealed block from the state
sealed, err := b.state.Sealed().Head()
if err != nil {
return flow.TransactionStatusUnknown, err
}
if block.Header.Height > sealed.Height {
// The block is not yet sealed, so we'll report it as only executed
return flow.TransactionStatusExecuted, nil
}
// otherwise, this block has been executed, and sealed, so report as sealed
return flow.TransactionStatusSealed, nil
}
// isExpired checks whether a transaction is expired given the height of the
// transaction's reference block and the height to compare against.
func (b *backendTransactions) isExpired(refHeight, compareToHeight uint64) bool {
if compareToHeight <= refHeight {
return false
}
return compareToHeight-refHeight > flow.DefaultTransactionExpiry
}
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,
blockID flow.Identifier,
) (bool, []flow.Event, uint32, string, error) {
events, txStatus, message, err := b.getTransactionResultFromExecutionNode(ctx, blockID, txID[:])
if err != nil {
// if either the execution node reported no results or the execution node could not be chosen
if status.Code(err) == codes.NotFound {
// No result yet, indicate that it has not been executed
return false, nil, 0, "", nil
}
// Other Error trying to retrieve the result, return with err
return false, nil, 0, "", err
}
// considered executed as long as some result is returned, even if it's an error message
return true, events, txStatus, message, 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())
// Found on a historical node. Report
return &tx, err
}
// 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_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
} else 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
}
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,
blockID flow.Identifier,
transactionID []byte,
) ([]flow.Event, uint32, string, error) {
// 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 errors.As(err, &InsufficientExecutionReceipts{}) {
return nil, 0, "", status.Errorf(codes.NotFound, err.Error())
}
return nil, 0, "", status.Errorf(codes.Internal, "failed to retrieve result from any execution node: %v", err)
}
resp, err := b.getTransactionResultFromAnyExeNode(ctx, execNodes, req)
if err != nil {
if status.Code(err) == codes.NotFound {
return nil, 0, "", err
}
return nil, 0, "", status.Errorf(codes.Internal, "failed to retrieve result from execution node: %v", err)
}
events := convert.MessagesToEvents(resp.GetEvents())
return events, resp.GetStatusCode(), resp.GetErrorMessage(), nil
}
func (b *backendTransactions) NotifyFinalizedBlockHeight(height uint64) {
b.retry.Retry(height)
}
func (b *backendTransactions) getTransactionResultFromAnyExeNode(ctx context.Context, execNodes flow.IdentityList, req execproto.GetTransactionResultRequest) (*execproto.GetTransactionResultResponse, error) {
var errors *multierror.Error
// try to execute the script on one of the execution nodes
for _, execNode := range execNodes {
resp, err := b.tryGetTransactionResult(ctx, execNode, req)
if err == nil {
b.log.Debug().
Str("execution_node", execNode.String()).
Hex("block_id", req.GetBlockId()).
Hex("transaction_id", req.GetTransactionId()).
Msg("Successfully got account info")
return resp, nil
}
if status.Code(err) == codes.NotFound {
return nil, err
}
errors = multierror.Append(errors, err)
}
return nil, errors.ErrorOrNil()
}
func (b *backendTransactions) tryGetTransactionResult(ctx context.Context, execNode *flow.Identity, 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
}