-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathcomputer.go
More file actions
829 lines (725 loc) · 22.6 KB
/
computer.go
File metadata and controls
829 lines (725 loc) · 22.6 KB
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
package computer
import (
"context"
"fmt"
"sync"
"github.com/onflow/crypto/hash"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/attribute"
otelTrace "go.opentelemetry.io/otel/trace"
"github.com/onflow/flow-go/engine/execution"
"github.com/onflow/flow-go/engine/execution/utils"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/blueprints"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/fvm/storage/errors"
"github.com/onflow/flow-go/fvm/storage/logical"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/executiondatasync/provider"
"github.com/onflow/flow-go/module/mempool/entity"
"github.com/onflow/flow-go/module/trace"
"github.com/onflow/flow-go/state/protocol"
)
const (
SystemChunkEventCollectionMaxSize = 256_000_000 // ~256MB
)
type collectionInfo struct {
blockId flow.Identifier
blockIdStr string
blockHeight uint64
collectionIndex int
*entity.CompleteCollection
}
type ComputerTransactionType uint
const (
ComputerTransactionTypeUser ComputerTransactionType = iota
ComputerTransactionTypeSystem
ComputerTransactionTypeScheduled
)
type TransactionRequest struct {
collectionInfo
txnId flow.Identifier
txnIdStr string
txnIndex uint32
transactionType ComputerTransactionType
lastTransactionInCollection bool
ctx fvm.Context
*fvm.TransactionProcedure
}
func newTransactionRequest(
collection collectionInfo,
collectionCtx fvm.Context,
collectionLogger zerolog.Logger,
txnIndex uint32,
txnBody *flow.TransactionBody,
transactionType ComputerTransactionType,
lastTransactionInCollection bool,
) TransactionRequest {
txnId := txnBody.ID()
txnIdStr := txnId.String()
return TransactionRequest{
collectionInfo: collection,
txnId: txnId,
txnIdStr: txnIdStr,
txnIndex: txnIndex,
ctx: fvm.NewContextFromParent(
collectionCtx,
fvm.WithLogger(
collectionLogger.With().
Str("tx_id", txnIdStr).
Uint32("tx_index", txnIndex).
Logger())),
TransactionProcedure: fvm.NewTransaction(
txnId,
txnIndex,
txnBody),
transactionType: transactionType,
lastTransactionInCollection: lastTransactionInCollection,
}
}
// A BlockComputer executes the transactions in a block.
type BlockComputer interface {
ExecuteBlock(
ctx context.Context,
parentBlockExecutionResultID flow.Identifier,
block *entity.ExecutableBlock,
snapshot snapshot.StorageSnapshot,
derivedBlockData *derived.DerivedBlockData,
) (
*execution.ComputationResult,
error,
)
}
type blockComputer struct {
vm fvm.VM
vmCtx fvm.Context
systemChunkCtx fvm.Context
callbackCtx fvm.Context
metrics module.ExecutionMetrics
tracer module.Tracer
log zerolog.Logger
systemTxn *flow.TransactionBody
processCallbackTxn *flow.TransactionBody
committer ViewCommitter
executionDataProvider provider.Provider
signer module.Local
spockHasher hash.Hasher
receiptHasher hash.Hasher
protocolState protocol.SnapshotExecutionSubsetProvider
maxConcurrency int
}
// SystemChunkContext is the context for the system chunk transaction.
func SystemChunkContext(vmCtx fvm.Context, metrics module.ExecutionMetrics) fvm.Context {
return fvm.NewContextFromParent(
vmCtx,
fvm.WithAuthorizationChecksEnabled(false),
fvm.WithSequenceNumberCheckAndIncrementEnabled(false),
fvm.WithTransactionFeesEnabled(false),
fvm.WithMetricsReporter(metrics),
fvm.WithContractDeploymentRestricted(false),
fvm.WithContractRemovalRestricted(false),
fvm.WithEventCollectionSizeLimit(SystemChunkEventCollectionMaxSize),
fvm.WithMemoryAndInteractionLimitsDisabled(),
// only the system transaction is allowed to call the block entropy provider
fvm.WithRandomSourceHistoryCallAllowed(true),
fvm.WithAccountStorageLimit(false),
)
}
// ScheduledTransactionContext is the context for the scheduled transactions.
func ScheduledTransactionContext(vmCtx fvm.Context, metrics module.ExecutionMetrics) fvm.Context {
return fvm.NewContextFromParent(
vmCtx,
fvm.WithAuthorizationChecksEnabled(false),
fvm.WithSequenceNumberCheckAndIncrementEnabled(false),
fvm.WithTransactionFeesEnabled(false),
fvm.WithMetricsReporter(metrics),
)
}
// NewBlockComputer creates a new block executor.
func NewBlockComputer(
vm fvm.VM,
vmCtx fvm.Context,
metrics module.ExecutionMetrics,
tracer module.Tracer,
logger zerolog.Logger,
committer ViewCommitter,
signer module.Local,
executionDataProvider provider.Provider,
state protocol.SnapshotExecutionSubsetProvider,
maxConcurrency int,
) (BlockComputer, error) {
if maxConcurrency < 1 {
return nil, fmt.Errorf("invalid maxConcurrency: %d", maxConcurrency)
}
// this is a safeguard to prevent scripts from writing to the program cache on Execution nodes.
// writes are only allowed by transactions.
if vmCtx.AllowProgramCacheWritesInScripts {
return nil, fmt.Errorf("program cache writes are not allowed in scripts on Execution nodes")
}
vmCtx = fvm.NewContextFromParent(
vmCtx,
fvm.WithMetricsReporter(metrics),
fvm.WithTracer(tracer))
systemTxn, err := blueprints.SystemChunkTransaction(vmCtx.Chain)
if err != nil {
return nil, fmt.Errorf("could not build system chunk transaction: %w", err)
}
processCallbackTxn, err := blueprints.ProcessCallbacksTransaction(vmCtx.Chain)
if err != nil {
return nil, fmt.Errorf("failed to generate callbacks script: %w", err)
}
return &blockComputer{
vm: vm,
vmCtx: vmCtx,
callbackCtx: ScheduledTransactionContext(vmCtx, metrics),
systemChunkCtx: SystemChunkContext(vmCtx, metrics),
metrics: metrics,
tracer: tracer,
log: logger,
systemTxn: systemTxn,
processCallbackTxn: processCallbackTxn,
committer: committer,
executionDataProvider: executionDataProvider,
signer: signer,
spockHasher: utils.NewSPOCKHasher(),
receiptHasher: utils.NewExecutionReceiptHasher(),
protocolState: state,
maxConcurrency: maxConcurrency,
}, nil
}
// ExecuteBlock executes a block and returns the resulting chunks.
func (e *blockComputer) ExecuteBlock(
ctx context.Context,
parentBlockExecutionResultID flow.Identifier,
block *entity.ExecutableBlock,
snapshot snapshot.StorageSnapshot,
derivedBlockData *derived.DerivedBlockData,
) (
*execution.ComputationResult,
error,
) {
results, err := e.executeBlock(
ctx,
parentBlockExecutionResultID,
block,
snapshot,
derivedBlockData)
if err != nil {
return nil, fmt.Errorf("failed to execute transactions: %w", err)
}
return results, nil
}
func (e *blockComputer) userTransactionsCount(collections []*entity.CompleteCollection) int {
count := 0
for _, collection := range collections {
count += len(collection.Collection.Transactions)
}
return count
}
// queueUserTransactions enqueues transaction processing requests for all user and
// system transactions in the given block.
//
// If constructing the Collection for the system transaction fails (for example, because
// NewCollection returned an error due to invalid input), this method returns an
// irrecoverable exception. Such an error indicates a fatal, unexpected condition and
// should abort block execution.
//
// Returns:
// - nil on success,
// - error if an irrecoverable error is received if the system transaction’s Collection cannot be constructed.
func (e *blockComputer) queueUserTransactions(
blockId flow.Identifier,
blockHeader *flow.Header,
rawCollections []*entity.CompleteCollection,
userTxCount int,
) chan TransactionRequest {
txQueue := make(chan TransactionRequest, userTxCount)
defer close(txQueue)
txnIndex := uint32(0)
blockIdStr := blockId.String()
collectionCtx := fvm.NewContextFromParent(
e.vmCtx,
fvm.WithBlockHeader(blockHeader),
fvm.WithProtocolStateSnapshot(e.protocolState.AtBlockID(blockId)),
)
for idx, collection := range rawCollections {
collectionLogger := collectionCtx.Logger.With().
Str("block_id", blockIdStr).
Uint64("height", blockHeader.Height).
Bool("system_chunk", false).
Bool("system_transaction", false).
Logger()
collectionInfo := collectionInfo{
blockId: blockId,
blockIdStr: blockIdStr,
blockHeight: blockHeader.Height,
collectionIndex: idx,
CompleteCollection: collection,
}
for i, txnBody := range collection.Collection.Transactions {
txQueue <- newTransactionRequest(
collectionInfo,
collectionCtx,
collectionLogger,
txnIndex,
txnBody,
ComputerTransactionTypeUser,
i == len(collection.Collection.Transactions)-1,
)
txnIndex += 1
}
}
return txQueue
}
func (e *blockComputer) queueSystemTransactions(
callbackCtx fvm.Context,
systemChunkCtx fvm.Context,
systemCollection collectionInfo,
systemTxn *flow.TransactionBody,
executeCallbackTxs []*flow.TransactionBody,
txnIndex uint32,
systemLogger zerolog.Logger,
) chan TransactionRequest {
systemTxLogger := systemLogger.With().
Uint32("num_txs", uint32(len(systemCollection.CompleteCollection.Collection.Transactions))).
Bool("system_transaction", true).
Logger()
scheduledTxLogger := systemLogger.With().
Uint32("num_txs", uint32(len(systemCollection.CompleteCollection.Collection.Transactions))).
Bool("scheduled_transaction", true).
Logger()
txQueue := make(chan TransactionRequest, len(executeCallbackTxs)+1)
defer close(txQueue)
for _, txBody := range executeCallbackTxs {
txQueue <- newTransactionRequest(
systemCollection,
callbackCtx,
scheduledTxLogger,
txnIndex,
txBody,
ComputerTransactionTypeScheduled,
false,
)
txnIndex++
}
txQueue <- newTransactionRequest(
systemCollection,
systemChunkCtx,
systemTxLogger,
txnIndex,
systemTxn,
ComputerTransactionTypeSystem,
true,
)
return txQueue
}
func (e *blockComputer) executeBlock(
ctx context.Context,
parentBlockExecutionResultID flow.Identifier,
block *entity.ExecutableBlock,
baseSnapshot snapshot.StorageSnapshot,
derivedBlockData *derived.DerivedBlockData,
) (
*execution.ComputationResult,
error,
) {
// check the start state is set
if !block.HasStartState() {
return nil, fmt.Errorf("executable block start state is not set")
}
rawCollections := block.Collections()
userTxCount := e.userTransactionsCount(rawCollections)
blockSpan := e.tracer.StartSpanFromParent(
e.tracer.BlockRootSpan(block.BlockID()),
trace.EXEComputeBlock)
blockSpan.SetAttributes(
attribute.String("block_id", block.BlockID().String()),
attribute.Int("collection_counts", len(rawCollections)))
defer blockSpan.End()
collector := newResultCollector(
e.tracer,
blockSpan,
e.metrics,
e.committer,
e.signer,
e.executionDataProvider,
e.spockHasher,
e.receiptHasher,
parentBlockExecutionResultID,
block,
// Add buffer just in case result collection becomes slower than the execution
e.maxConcurrency*2,
baseSnapshot,
)
defer collector.Stop()
database := newTransactionCoordinator(
e.vm,
baseSnapshot,
derivedBlockData,
collector)
e.executeUserTransactions(
block,
blockSpan,
database,
rawCollections,
userTxCount,
)
err := e.executeSystemTransactions(
block,
blockSpan,
database,
rawCollections,
userTxCount,
)
if err != nil {
return nil, err
}
err = database.Error()
if err != nil {
return nil, err
}
res, err := collector.Finalize(ctx)
if err != nil {
return nil, fmt.Errorf("cannot finalize computation result: %w", err)
}
e.log.Debug().
Str("block_id", block.BlockID().String()).
Msg("all views committed")
e.metrics.ExecutionBlockCachedPrograms(derivedBlockData.CachedPrograms())
return res, nil
}
// executeUserTransactions executes the user transactions in the block.
// It queues the user transactions into a request queue and then executes them in parallel.
func (e *blockComputer) executeUserTransactions(
block *entity.ExecutableBlock,
blockSpan otelTrace.Span,
database *transactionCoordinator,
rawCollections []*entity.CompleteCollection,
userTxCount int,
) {
txQueue := e.queueUserTransactions(
block.BlockID(),
block.Block.ToHeader(),
rawCollections,
userTxCount,
)
e.executeQueue(blockSpan, database, txQueue)
}
// executeSystemTransactions executes all system transactions in the block as part of the system collection.
//
// When scheduled callbacks are enabled, system transactions are executed in the following order:
// 1. Process scheduled transactions transaction - queries the scheduler contract to identify ready transactions
// and emits events containing transaction IDs and execution effort requirements
// 2. Scheduled transaction execution transactions - one transaction per transaction ID from step 1 events,
// each executing a single scheduled transaction with its specified effort limit
// 3. System chunk transaction - performs standard system operations
//
// When scheduled callbacks are disabled, only the system chunk transaction is executed.
//
// All errors are indicators of bugs or corrupted internal state (continuation impossible)
func (e *blockComputer) executeSystemTransactions(
block *entity.ExecutableBlock,
blockSpan otelTrace.Span,
database *transactionCoordinator,
rawCollections []*entity.CompleteCollection,
userTxCount int,
) error {
userCollectionCount := len(rawCollections)
txIndex := uint32(userTxCount)
callbackCtx := fvm.NewContextFromParent(
e.callbackCtx,
fvm.WithBlockHeader(block.Block.ToHeader()),
fvm.WithProtocolStateSnapshot(e.protocolState.AtBlockID(block.BlockID())),
)
systemChunkCtx := fvm.NewContextFromParent(
e.systemChunkCtx,
fvm.WithBlockHeader(block.Block.ToHeader()),
fvm.WithProtocolStateSnapshot(e.protocolState.AtBlockID(block.BlockID())),
)
systemCollectionInfo := collectionInfo{
blockId: block.BlockID(),
blockIdStr: block.BlockID().String(),
blockHeight: block.Block.Height,
collectionIndex: userCollectionCount,
CompleteCollection: nil, // We do not yet know all the scheduled callbacks, so postpone construction of the collection.
}
systemChunkLogger := callbackCtx.Logger.With().
Str("block_id", block.BlockID().String()).
Uint64("height", block.Block.Height).
Bool("system_chunk", true).
Int("num_collections", userCollectionCount).
Logger()
var callbackTxs []*flow.TransactionBody
if e.vmCtx.ScheduledTransactionsEnabled {
// We pass in the `systemCollectionInfo` here. However, note that at this point, the composition of the system chunk
// is not yet known. Specifically, the `entity.CompleteCollection` represents the *final* output of a process and is
// immutable by protocol mandate. If we had a bug in our software that accidentally illegally mutated such structs,
// likely the node encountering that bug would misbehave and get slashed, or in the worst case the flow protocol might
// be compromised. Therefore, we have the rigorous convention in our code base that the `CompleteCollection` is only
// constructed once the final composition of the system chunk has been determined.
// To that end, the CompleteCollection is nil here, such that any attempt to access the Collection will panic.
callbacks, updatedTxnIndex, err := e.executeProcessCallback(
callbackCtx,
systemCollectionInfo,
database,
blockSpan,
txIndex,
systemChunkLogger,
)
if err != nil {
return err
}
callbackTxs = callbacks
txIndex = updatedTxnIndex
finalCollection, err := flow.NewCollection(flow.UntrustedCollection{
Transactions: append(append([]*flow.TransactionBody{e.processCallbackTxn}, callbackTxs...), e.systemTxn),
})
if err != nil {
return err
}
systemCollectionInfo.CompleteCollection = &entity.CompleteCollection{
Collection: finalCollection,
}
} else {
finalCollection, err := flow.NewCollection(flow.UntrustedCollection{
Transactions: []*flow.TransactionBody{e.systemTxn},
})
if err != nil {
return err
}
systemCollectionInfo.CompleteCollection = &entity.CompleteCollection{
Collection: finalCollection,
}
}
txQueue := e.queueSystemTransactions(
callbackCtx,
systemChunkCtx,
systemCollectionInfo,
e.systemTxn,
callbackTxs,
txIndex,
systemChunkLogger,
)
e.executeQueue(blockSpan, database, txQueue)
return nil
}
// executeQueue executes the transactions in the request queue in parallel with the maxConcurrency workers.
func (e *blockComputer) executeQueue(
blockSpan otelTrace.Span,
database *transactionCoordinator,
txQueue chan TransactionRequest,
) {
wg := &sync.WaitGroup{}
wg.Add(e.maxConcurrency)
for range e.maxConcurrency {
go e.executeTransactions(
blockSpan,
database,
txQueue,
wg)
}
wg.Wait()
}
// executeProcessCallback submits a transaction that invokes the `process` method
// on the transaction scheduler contract.
//
// The `process` method scans for scheduled transactions and emits an event for each that should
// be executed. These emitted events are used to construct scheduled transaction execution transactions,
// which are then added to the system transaction collection.
//
// If the `process` transaction fails, a fatal error is returned.
//
// Note: this transaction is executed serially and not concurrently with the system transaction.
// This is because it's unclear whether the scheduled transaction executions triggered by `process`
// will result in additional system transactions.
// In theory, if no additional transactions are emitted, concurrent execution could be optimized.
// However, due to the added complexity, this optimization was deferred.
func (e *blockComputer) executeProcessCallback(
systemCtx fvm.Context,
systemCollectionInfo collectionInfo,
database *transactionCoordinator,
blockSpan otelTrace.Span,
txnIndex uint32,
systemLogger zerolog.Logger,
) ([]*flow.TransactionBody, uint32, error) {
callbackLogger := systemLogger.With().
Bool("system_transaction", true).
Logger()
request := newTransactionRequest(
systemCollectionInfo,
systemCtx,
callbackLogger,
txnIndex,
e.processCallbackTxn,
ComputerTransactionTypeSystem,
false)
txnIndex++
txn, err := e.executeTransactionInternal(blockSpan, database, request, 1)
if err != nil {
snapshotTime := logical.Time(0)
if txn != nil {
snapshotTime = txn.SnapshotTime()
}
return nil, 0, fmt.Errorf(
"failed to execute system process transaction %v (%d@%d) for block %s at height %v: %w",
request.txnIdStr,
request.txnIndex,
snapshotTime,
request.blockIdStr,
request.ctx.BlockHeader.Height,
err)
}
if txn.Output().Err != nil {
// if the process transaction fails we log the critical error but don't return an error
// so that block execution continues and only the scheduled transactions halt
callbackLogger.Error().
Err(txn.Output().Err).
Bool("critical_error", true).
Uint64("height", request.ctx.BlockHeader.Height).
Msg("system process transaction output error")
return nil, txnIndex, nil
}
callbackTxs, err := blueprints.ExecuteCallbacksTransactions(e.vmCtx.Chain, txn.Output().Events)
if err != nil {
return nil, 0, err
}
if len(callbackTxs) > 0 {
// calculate total computation limits for execute scheduled transaction transactions
var totalExecuteComputationLimits uint64
for _, tx := range callbackTxs {
totalExecuteComputationLimits += tx.GasLimit
}
// report metrics for scheduled transactions executed
e.metrics.ExecutionScheduledTransactionsExecuted(
len(callbackTxs),
txn.Output().ComputationUsed,
totalExecuteComputationLimits,
)
}
return callbackTxs, txnIndex, nil
}
func (e *blockComputer) executeTransactions(
blockSpan otelTrace.Span,
database *transactionCoordinator,
requestQueue chan TransactionRequest,
wg *sync.WaitGroup,
) {
defer wg.Done()
for request := range requestQueue {
attempt := 0
for {
request.ctx.Logger.Info().
Int("attempt", attempt).
Msg("executing transaction")
attempt += 1
err := e.executeTransaction(blockSpan, database, request, attempt)
if errors.IsRetryableConflictError(err) {
request.ctx.Logger.Info().
Int("attempt", attempt).
Str("conflict_error", err.Error()).
Msg("conflict detected. retrying transaction")
continue
}
if err != nil {
database.AbortAllOutstandingTransactions(err)
return
}
break // process next transaction
}
}
}
func (e *blockComputer) executeTransaction(
blockSpan otelTrace.Span,
database *transactionCoordinator,
request TransactionRequest,
attempt int,
) error {
txn, err := e.executeTransactionInternal(
blockSpan,
database,
request,
attempt)
if err != nil {
prefix := ""
if request.transactionType == ComputerTransactionTypeSystem {
prefix = "system "
}
snapshotTime := logical.Time(0)
if txn != nil {
snapshotTime = txn.SnapshotTime()
}
return fmt.Errorf(
"failed to execute %stransaction %v (%d@%d) for block %s "+
"at height %v: %w",
prefix,
request.txnIdStr,
request.txnIndex,
snapshotTime,
request.blockIdStr,
request.ctx.BlockHeader.Height,
err)
}
return nil
}
func (e *blockComputer) executeTransactionInternal(
blockSpan otelTrace.Span,
database *transactionCoordinator,
request TransactionRequest,
attempt int,
) (
*transaction,
error,
) {
txSpan := e.tracer.StartSampledSpanFromParent(
blockSpan,
request.txnId,
trace.EXEComputeTransaction)
txSpan.SetAttributes(
attribute.String("tx_id", request.txnIdStr),
attribute.Int64("tx_index", int64(request.txnIndex)),
attribute.Int("col_index", request.collectionIndex),
)
defer txSpan.End()
request.ctx = fvm.NewContextFromParent(request.ctx, fvm.WithSpan(txSpan))
txn, err := database.NewTransaction(request, attempt)
if err != nil {
return nil, err
}
defer txn.Cleanup()
err = txn.Preprocess()
if err != nil {
return txn, err
}
// Validating here gives us an opportunity to early abort/retry the
// transaction in case the conflict is detectable after preprocessing.
// This is strictly an optimization and hence we don't need to wait for
// updates (removing this validate call won't impact correctness).
err = txn.Validate()
if err != nil {
return txn, err
}
err = txn.Execute()
if err != nil {
return txn, err
}
err = txn.Finalize()
if err != nil {
return txn, err
}
// Snapshot time smaller than execution time indicates there are outstanding
// transaction(s) that must be committed before this transaction can be
// committed.
for txn.SnapshotTime() < request.ExecutionTime() {
err = txn.WaitForUpdates()
if err != nil {
return txn, err
}
err = txn.Validate()
if err != nil {
return txn, err
}
}
return txn, txn.Commit()
}