-
Notifications
You must be signed in to change notification settings - Fork 179
/
execution_state.go
61 lines (51 loc) · 1.61 KB
/
execution_state.go
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
package mocks
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/engine/execution/state"
"github.com/onflow/flow-go/engine/execution/state/mock"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/utils/unittest"
)
// ExecutionState is a mocked version of execution state that
// simulates some of its behavior for testing purpose
type ExecutionState struct {
sync.Mutex
mock.ExecutionState
commits map[flow.Identifier]flow.StateCommitment
}
func NewExecutionState(seal *flow.Seal) *ExecutionState {
commits := make(map[flow.Identifier]flow.StateCommitment)
commits[seal.BlockID] = seal.FinalState
return &ExecutionState{
commits: commits,
}
}
func (es *ExecutionState) PersistStateCommitment(ctx context.Context, blockID flow.Identifier, commit flow.StateCommitment) error {
es.Lock()
defer es.Unlock()
es.commits[blockID] = commit
return nil
}
func (es *ExecutionState) StateCommitmentByBlockID(ctx context.Context, blockID flow.Identifier) (flow.StateCommitment, error) {
es.Lock()
defer es.Unlock()
commit, ok := es.commits[blockID]
if !ok {
return flow.DummyStateCommitment, storage.ErrNotFound
}
return commit, nil
}
func (es *ExecutionState) ExecuteBlock(t *testing.T, block *flow.Block) {
parentExecuted, err := state.IsBlockExecuted(context.Background(), es, block.Header.ParentID)
require.NoError(t, err)
require.True(t, parentExecuted, "parent block not executed")
require.NoError(t,
es.PersistStateCommitment(
context.Background(),
block.ID(),
unittest.StateCommitmentFixture()))
}