github.com/koko1123/flow-go-1@v0.29.6/utils/unittest/mocks/execution_state.go (about)

     1  package mocks
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/engine/execution/state"
    11  
    12  	"github.com/koko1123/flow-go-1/engine/execution/state/mock"
    13  	"github.com/koko1123/flow-go-1/model/flow"
    14  	"github.com/koko1123/flow-go-1/storage"
    15  	"github.com/koko1123/flow-go-1/utils/unittest"
    16  )
    17  
    18  // ExecutionState is a mocked version of execution state that
    19  // simulates some of its behavior for testing purpose
    20  type ExecutionState struct {
    21  	sync.Mutex
    22  	mock.ExecutionState
    23  	commits map[flow.Identifier]flow.StateCommitment
    24  }
    25  
    26  func NewExecutionState(seal *flow.Seal) *ExecutionState {
    27  	commits := make(map[flow.Identifier]flow.StateCommitment)
    28  	commits[seal.BlockID] = seal.FinalState
    29  	return &ExecutionState{
    30  		commits: commits,
    31  	}
    32  }
    33  
    34  func (es *ExecutionState) PersistStateCommitment(ctx context.Context, blockID flow.Identifier, commit flow.StateCommitment) error {
    35  	es.Lock()
    36  	defer es.Unlock()
    37  	es.commits[blockID] = commit
    38  	return nil
    39  }
    40  
    41  func (es *ExecutionState) StateCommitmentByBlockID(ctx context.Context, blockID flow.Identifier) (flow.StateCommitment, error) {
    42  	es.Lock()
    43  	defer es.Unlock()
    44  	commit, ok := es.commits[blockID]
    45  	if !ok {
    46  		return flow.DummyStateCommitment, storage.ErrNotFound
    47  	}
    48  
    49  	return commit, nil
    50  }
    51  
    52  func (es *ExecutionState) ExecuteBlock(t *testing.T, block *flow.Block) {
    53  	parentExecuted, err := state.IsBlockExecuted(context.Background(), es, block.Header.ParentID)
    54  	require.NoError(t, err)
    55  	require.True(t, parentExecuted, "parent block not executed")
    56  	require.NoError(t,
    57  		es.PersistStateCommitment(
    58  			context.Background(),
    59  			block.ID(),
    60  			unittest.StateCommitmentFixture()))
    61  }