github.com/onflow/flow-go@v0.33.17/engine/execution/state/unittest/fixtures.go (about)

     1  package unittest
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/crypto"
    10  	"github.com/onflow/flow-go/engine/execution"
    11  	"github.com/onflow/flow-go/fvm/meter"
    12  	"github.com/onflow/flow-go/fvm/storage/snapshot"
    13  	"github.com/onflow/flow-go/model/flow"
    14  	"github.com/onflow/flow-go/module/executiondatasync/execution_data"
    15  	"github.com/onflow/flow-go/module/mempool/entity"
    16  	"github.com/onflow/flow-go/utils/unittest"
    17  )
    18  
    19  func StateInteractionsFixture() *snapshot.ExecutionSnapshot {
    20  	return &snapshot.ExecutionSnapshot{
    21  		Meter: meter.NewMeter(meter.DefaultParameters()),
    22  	}
    23  }
    24  
    25  func ComputationResultFixture(
    26  	t *testing.T,
    27  	parentBlockExecutionResultID flow.Identifier,
    28  	collectionsSignerIDs [][]flow.Identifier,
    29  ) *execution.ComputationResult {
    30  
    31  	startState := unittest.StateCommitmentFixture()
    32  	block := unittest.ExecutableBlockFixture(collectionsSignerIDs, &startState)
    33  
    34  	return ComputationResultForBlockFixture(t,
    35  		parentBlockExecutionResultID,
    36  		block)
    37  }
    38  
    39  func ComputationResultForBlockFixture(
    40  	t *testing.T,
    41  	parentBlockExecutionResultID flow.Identifier,
    42  	completeBlock *entity.ExecutableBlock,
    43  ) *execution.ComputationResult {
    44  	collections := completeBlock.Collections()
    45  	computationResult := execution.NewEmptyComputationResult(completeBlock)
    46  
    47  	numberOfChunks := len(collections) + 1
    48  	ceds := make([]*execution_data.ChunkExecutionData, numberOfChunks)
    49  	for i := 0; i < numberOfChunks; i++ {
    50  		ceds[i] = unittest.ChunkExecutionDataFixture(t, 1024)
    51  		computationResult.CollectionExecutionResultAt(i).UpdateExecutionSnapshot(StateInteractionsFixture())
    52  		computationResult.AppendCollectionAttestationResult(
    53  			*completeBlock.StartState,
    54  			*completeBlock.StartState,
    55  			nil,
    56  			unittest.IdentifierFixture(),
    57  			ceds[i],
    58  		)
    59  	}
    60  	bed := unittest.BlockExecutionDataFixture(
    61  		unittest.WithBlockExecutionDataBlockID(completeBlock.Block.ID()),
    62  		unittest.WithChunkExecutionDatas(ceds...),
    63  	)
    64  	executionDataID, err := execution_data.CalculateID(context.Background(), bed, execution_data.DefaultSerializer)
    65  	require.NoError(t, err)
    66  
    67  	_, serviceEventEpochCommitProtocol := unittest.EpochCommitFixtureByChainID(flow.Localnet)
    68  	_, serviceEventEpochSetupProtocol := unittest.EpochSetupFixtureByChainID(flow.Localnet)
    69  	_, serviceEventVersionBeaconProtocol := unittest.VersionBeaconFixtureByChainID(flow.Localnet)
    70  
    71  	convertedServiceEvents := flow.ServiceEventList{
    72  		serviceEventEpochCommitProtocol.ServiceEvent(),
    73  		serviceEventEpochSetupProtocol.ServiceEvent(),
    74  		serviceEventVersionBeaconProtocol.ServiceEvent(),
    75  	}
    76  
    77  	executionResult := flow.NewExecutionResult(
    78  		parentBlockExecutionResultID,
    79  		completeBlock.ID(),
    80  		computationResult.AllChunks(),
    81  		convertedServiceEvents,
    82  		executionDataID)
    83  
    84  	computationResult.ExecutionReceipt = &flow.ExecutionReceipt{
    85  		ExecutionResult:   *executionResult,
    86  		Spocks:            make([]crypto.Signature, numberOfChunks),
    87  		ExecutorSignature: crypto.Signature{},
    88  	}
    89  
    90  	return computationResult
    91  }