github.com/koko1123/flow-go-1@v0.29.6/engine/execution/messages.go (about)

     1  package execution
     2  
     3  import (
     4  	"github.com/onflow/flow-go/crypto"
     5  	"github.com/koko1123/flow-go-1/engine/execution/state/delta"
     6  	"github.com/koko1123/flow-go-1/fvm"
     7  	"github.com/koko1123/flow-go-1/fvm/meter"
     8  	"github.com/koko1123/flow-go-1/ledger"
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  	"github.com/koko1123/flow-go-1/module"
    11  	"github.com/koko1123/flow-go-1/module/mempool/entity"
    12  )
    13  
    14  // TODO If the executor will be a separate process/machine we would need to rework
    15  // sending view as local data, but that would be much greater refactor of storage anyway
    16  
    17  type ComputationOrder struct {
    18  	Block      *entity.ExecutableBlock
    19  	View       *delta.View
    20  	StartState flow.StateCommitment
    21  }
    22  
    23  type ComputationResult struct {
    24  	ExecutableBlock        *entity.ExecutableBlock
    25  	StateSnapshots         []*delta.SpockSnapshot
    26  	StateCommitments       []flow.StateCommitment
    27  	Proofs                 [][]byte
    28  	Events                 []flow.EventsList
    29  	EventsHashes           []flow.Identifier
    30  	ServiceEvents          flow.EventsList
    31  	TransactionResults     []flow.TransactionResult
    32  	TransactionResultIndex []int
    33  	ComputationIntensities meter.MeteredComputationIntensities
    34  	TrieUpdates            []*ledger.TrieUpdate
    35  	ExecutionDataID        flow.Identifier
    36  	SpockSignatures        []crypto.Signature
    37  }
    38  
    39  func NewEmptyComputationResult(block *entity.ExecutableBlock) *ComputationResult {
    40  	numCollections := len(block.CompleteCollections) + 1
    41  	return &ComputationResult{
    42  		ExecutableBlock:        block,
    43  		Events:                 make([]flow.EventsList, numCollections),
    44  		ServiceEvents:          make(flow.EventsList, 0),
    45  		TransactionResults:     make([]flow.TransactionResult, 0),
    46  		TransactionResultIndex: make([]int, 0),
    47  		StateCommitments:       make([]flow.StateCommitment, 0, numCollections),
    48  		Proofs:                 make([][]byte, 0, numCollections),
    49  		TrieUpdates:            make([]*ledger.TrieUpdate, 0, numCollections),
    50  		EventsHashes:           make([]flow.Identifier, 0, numCollections),
    51  		ComputationIntensities: make(meter.MeteredComputationIntensities),
    52  	}
    53  }
    54  
    55  func (cr *ComputationResult) AddTransactionResult(
    56  	collectionIndex int,
    57  	txn *fvm.TransactionProcedure,
    58  ) {
    59  	cr.Events[collectionIndex] = append(
    60  		cr.Events[collectionIndex],
    61  		txn.Events...)
    62  	cr.ServiceEvents = append(cr.ServiceEvents, txn.ServiceEvents...)
    63  
    64  	txnResult := flow.TransactionResult{
    65  		TransactionID:   txn.ID,
    66  		ComputationUsed: txn.ComputationUsed,
    67  		MemoryUsed:      txn.MemoryEstimate,
    68  	}
    69  	if txn.Err != nil {
    70  		txnResult.ErrorMessage = txn.Err.Error()
    71  	}
    72  
    73  	cr.TransactionResults = append(cr.TransactionResults, txnResult)
    74  
    75  	for computationKind, intensity := range txn.ComputationIntensities {
    76  		cr.ComputationIntensities[computationKind] += intensity
    77  	}
    78  }
    79  
    80  func (cr *ComputationResult) AddCollection(snapshot *delta.SpockSnapshot) {
    81  	cr.TransactionResultIndex = append(
    82  		cr.TransactionResultIndex,
    83  		len(cr.TransactionResults))
    84  	cr.StateSnapshots = append(cr.StateSnapshots, snapshot)
    85  }
    86  
    87  func (cr *ComputationResult) CollectionStats(
    88  	collectionIndex int,
    89  ) module.ExecutionResultStats {
    90  	var startTxnIndex int
    91  	if collectionIndex > 0 {
    92  		startTxnIndex = cr.TransactionResultIndex[collectionIndex-1]
    93  	}
    94  	endTxnIndex := cr.TransactionResultIndex[collectionIndex]
    95  
    96  	var computationUsed uint64
    97  	var memoryUsed uint64
    98  	for _, txn := range cr.TransactionResults[startTxnIndex:endTxnIndex] {
    99  		computationUsed += txn.ComputationUsed
   100  		memoryUsed += txn.MemoryUsed
   101  	}
   102  
   103  	events := cr.Events[collectionIndex]
   104  	snapshot := cr.StateSnapshots[collectionIndex]
   105  	return module.ExecutionResultStats{
   106  		ComputationUsed:                 computationUsed,
   107  		MemoryUsed:                      memoryUsed,
   108  		EventCounts:                     len(events),
   109  		EventSize:                       events.ByteSize(),
   110  		NumberOfRegistersTouched:        snapshot.NumberOfRegistersTouched,
   111  		NumberOfBytesWrittenToRegisters: snapshot.NumberOfBytesWrittenToRegisters,
   112  		NumberOfCollections:             1,
   113  		NumberOfTransactions:            endTxnIndex - startTxnIndex,
   114  	}
   115  }
   116  
   117  func (cr *ComputationResult) BlockStats() module.ExecutionResultStats {
   118  	stats := module.ExecutionResultStats{}
   119  	for idx := 0; idx < len(cr.StateCommitments); idx++ {
   120  		stats.Merge(cr.CollectionStats(idx))
   121  	}
   122  
   123  	return stats
   124  }