github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/execution/ingestion/uploader/model.go (about)

     1  package uploader
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/fxamacker/cbor/v2"
     8  
     9  	"github.com/onflow/flow-go/engine/execution"
    10  	"github.com/onflow/flow-go/ledger"
    11  	"github.com/onflow/flow-go/model/flow"
    12  	"github.com/onflow/flow-go/module/mempool/entity"
    13  )
    14  
    15  type BlockData struct {
    16  	Block                *flow.Block
    17  	Collections          []*entity.CompleteCollection
    18  	TxResults            []*flow.TransactionResult
    19  	Events               []*flow.Event
    20  	TrieUpdates          []*ledger.TrieUpdate
    21  	FinalStateCommitment flow.StateCommitment
    22  }
    23  
    24  func ComputationResultToBlockData(computationResult *execution.ComputationResult) *BlockData {
    25  
    26  	AllResults := computationResult.AllTransactionResults()
    27  	txResults := make([]*flow.TransactionResult, len(AllResults))
    28  	for i := 0; i < len(AllResults); i++ {
    29  		txResults[i] = &AllResults[i]
    30  	}
    31  
    32  	eventsList := computationResult.AllEvents()
    33  	events := make([]*flow.Event, len(eventsList))
    34  	for i := 0; i < len(eventsList); i++ {
    35  		events[i] = &eventsList[i]
    36  	}
    37  
    38  	trieUpdates := make(
    39  		[]*ledger.TrieUpdate,
    40  		0,
    41  		len(computationResult.ChunkExecutionDatas))
    42  	for _, chunk := range computationResult.ChunkExecutionDatas {
    43  		trieUpdates = append(trieUpdates, chunk.TrieUpdate)
    44  	}
    45  
    46  	return &BlockData{
    47  		Block:                computationResult.ExecutableBlock.Block,
    48  		Collections:          computationResult.ExecutableBlock.Collections(),
    49  		TxResults:            txResults,
    50  		Events:               events,
    51  		TrieUpdates:          trieUpdates,
    52  		FinalStateCommitment: computationResult.CurrentEndState(),
    53  	}
    54  }
    55  
    56  func WriteComputationResultsTo(computationResult *execution.ComputationResult, writer io.Writer) error {
    57  	blockData := ComputationResultToBlockData(computationResult)
    58  
    59  	mode, err := cbor.CoreDetEncOptions().EncMode()
    60  	if err != nil {
    61  		return fmt.Errorf("cannot create deterministic cbor encoding mode: %w", err)
    62  	}
    63  	encoder := mode.NewEncoder(writer)
    64  
    65  	return encoder.Encode(blockData)
    66  }