-
Notifications
You must be signed in to change notification settings - Fork 175
/
block_store.go
135 lines (113 loc) · 3.81 KB
/
block_store.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package mocks
import (
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/engine/execution"
executionUnittest "github.com/onflow/flow-go/engine/execution/state/unittest"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool/entity"
"github.com/onflow/flow-go/utils/unittest"
)
type BlockResult struct {
Block *entity.ExecutableBlock
Result *execution.ComputationResult
}
// MockBlockStore contains mocked block computation result
// it ensures that as blocks computation result are created, the block's start state
// is the same as its parent block's end state.
// it also stores which block is executed, so that the mock execution state or computer
// can determine what result to return
type MockBlockStore struct {
sync.Mutex
ResultByBlock map[flow.Identifier]*BlockResult
Executed map[flow.Identifier]struct{}
RootBlock *flow.Header
}
func NewMockBlockStore(t *testing.T) *MockBlockStore {
rootBlock := unittest.ExecutableBlockFixtureWithParent([][]flow.Identifier{{}},
unittest.BlockHeaderFixture(), unittest.StateCommitmentPointerFixture())
rootResult := executionUnittest.ComputationResultForBlockFixture(t,
unittest.IdentifierFixture(), rootBlock)
blockResult := &BlockResult{
Block: rootBlock,
Result: rootResult,
}
byBlock := make(map[flow.Identifier]*BlockResult)
byBlock[rootResult.Block.ID()] = blockResult
executed := make(map[flow.Identifier]struct{})
executed[rootResult.Block.ID()] = struct{}{}
return &MockBlockStore{
ResultByBlock: byBlock,
Executed: executed,
RootBlock: rootBlock.Block.Header,
}
}
func (bs *MockBlockStore) MarkExecuted(computationResult *execution.ComputationResult) error {
bs.Lock()
defer bs.Unlock()
blockID := computationResult.ExecutableBlock.Block.Header.ID()
_, executed := bs.Executed[blockID]
if executed {
return fmt.Errorf("block %s already executed", blockID)
}
expected, exist := bs.ResultByBlock[blockID]
if !exist {
return fmt.Errorf("block %s not found", blockID)
}
if expected.Result != computationResult {
return fmt.Errorf("block %s expected %v, got %v", blockID, expected, computationResult)
}
bs.Executed[blockID] = struct{}{}
return nil
}
func (bs *MockBlockStore) CreateBlockAndMockResult(t *testing.T, block *entity.ExecutableBlock) *execution.ComputationResult {
bs.Lock()
defer bs.Unlock()
blockID := block.ID()
_, exist := bs.ResultByBlock[blockID]
require.False(t, exist, "block %s already exists", blockID)
parent := block.Block.Header.ParentID
parentResult, ok := bs.ResultByBlock[parent]
require.True(t, ok, "parent block %s not found", parent)
previousExecutionResultID := parentResult.Result.ExecutionReceipt.ExecutionResult.ID()
previousCommit := parentResult.Result.CurrentEndState()
block.StartState = &previousCommit
// mock computation result
cr := executionUnittest.ComputationResultForBlockFixture(t,
previousExecutionResultID,
block,
)
result := &BlockResult{
Block: block,
Result: cr,
}
bs.ResultByBlock[blockID] = result
return cr
}
func (bs *MockBlockStore) GetExecuted(blockID flow.Identifier) (*BlockResult, error) {
bs.Lock()
defer bs.Unlock()
_, exist := bs.Executed[blockID]
if !exist {
return nil, fmt.Errorf("block %s not executed", blockID)
}
result, exist := bs.ResultByBlock[blockID]
if !exist {
return nil, fmt.Errorf("block %s not found", blockID)
}
return result, nil
}
func (bs *MockBlockStore) AssertExecuted(t *testing.T, alias string, block flow.Identifier) {
bs.Lock()
defer bs.Unlock()
_, exist := bs.Executed[block]
require.True(t, exist, "block %s not executed", alias)
}
func (bs *MockBlockStore) AssertNotExecuted(t *testing.T, alias string, block flow.Identifier) {
bs.Lock()
defer bs.Unlock()
_, exist := bs.Executed[block]
require.False(t, exist, "block %s executed", alias)
}