-
Notifications
You must be signed in to change notification settings - Fork 178
/
protocol_state.go
230 lines (185 loc) · 5.22 KB
/
protocol_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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package mocks
import (
"fmt"
"sync"
"github.com/stretchr/testify/mock"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
protocolmock "github.com/onflow/flow-go/state/protocol/mock"
"github.com/onflow/flow-go/storage"
)
// ProtocolState is a mocked version of protocol state, which
// has very close behavior to the real implementation
// but for testing purpose.
// If you are testing a module that depends on protocol state's
// behavior, but you don't want to mock up the methods and its return
// value, then just use this module
type ProtocolState struct {
sync.Mutex
protocol.ParticipantState
blocks map[flow.Identifier]*flow.Block
children map[flow.Identifier][]flow.Identifier
heights map[uint64]*flow.Block
finalized uint64
root *flow.Block
result *flow.ExecutionResult
seal *flow.Seal
}
func NewProtocolState() *ProtocolState {
return &ProtocolState{
blocks: make(map[flow.Identifier]*flow.Block),
children: make(map[flow.Identifier][]flow.Identifier),
heights: make(map[uint64]*flow.Block),
}
}
type Params struct {
state *ProtocolState
}
func (p *Params) ChainID() (flow.ChainID, error) {
return p.state.root.Header.ChainID, nil
}
func (p *Params) SporkID() (flow.Identifier, error) {
return flow.ZeroID, fmt.Errorf("not implemented")
}
func (p *Params) SporkRootBlockHeight() (uint64, error) {
return 0, fmt.Errorf("not implemented")
}
func (p *Params) ProtocolVersion() (uint, error) {
return 0, fmt.Errorf("not implemented")
}
func (p *Params) EpochCommitSafetyThreshold() (uint64, error) {
return 0, fmt.Errorf("not implemented")
}
func (p *Params) EpochFallbackTriggered() (bool, error) {
return false, fmt.Errorf("not implemented")
}
func (p *Params) FinalizedRoot() (*flow.Header, error) {
return p.state.root.Header, nil
}
func (p *Params) SealedRoot() (*flow.Header, error) {
return p.FinalizedRoot()
}
func (p *Params) Seal() (*flow.Seal, error) {
return nil, fmt.Errorf("not implemented")
}
func (ps *ProtocolState) Params() protocol.Params {
return &Params{
state: ps,
}
}
func (ps *ProtocolState) AtBlockID(blockID flow.Identifier) protocol.Snapshot {
ps.Lock()
defer ps.Unlock()
snapshot := new(protocolmock.Snapshot)
block, ok := ps.blocks[blockID]
if ok {
snapshot.On("Head").Return(block.Header, nil)
} else {
snapshot.On("Head").Return(nil, storage.ErrNotFound)
}
return snapshot
}
func (ps *ProtocolState) AtHeight(height uint64) protocol.Snapshot {
ps.Lock()
defer ps.Unlock()
snapshot := new(protocolmock.Snapshot)
block, ok := ps.heights[height]
if ok {
snapshot.On("Head").Return(block.Header, nil)
mocked := snapshot.On("Descendants")
mocked.RunFn = func(args mock.Arguments) {
pendings := pending(ps, block.Header.ID())
mocked.ReturnArguments = mock.Arguments{pendings, nil}
}
} else {
snapshot.On("Head").Return(nil, storage.ErrNotFound)
}
return snapshot
}
func (ps *ProtocolState) Final() protocol.Snapshot {
ps.Lock()
defer ps.Unlock()
final, ok := ps.heights[ps.finalized]
if !ok {
return nil
}
snapshot := new(protocolmock.Snapshot)
snapshot.On("Head").Return(final.Header, nil)
finalID := final.ID()
mocked := snapshot.On("Descendants")
mocked.RunFn = func(args mock.Arguments) {
// not concurrent safe
pendings := pending(ps, finalID)
mocked.ReturnArguments = mock.Arguments{pendings, nil}
}
return snapshot
}
func pending(ps *ProtocolState, blockID flow.Identifier) []flow.Identifier {
var pendingIDs []flow.Identifier
pendingIDs, ok := ps.children[blockID]
if !ok {
return pendingIDs
}
for _, pendingID := range pendingIDs {
additionalIDs := pending(ps, pendingID)
pendingIDs = append(pendingIDs, additionalIDs...)
}
return pendingIDs
}
func (m *ProtocolState) Bootstrap(root *flow.Block, result *flow.ExecutionResult, seal *flow.Seal) error {
m.Lock()
defer m.Unlock()
if _, ok := m.blocks[root.ID()]; ok {
return storage.ErrAlreadyExists
}
m.blocks[root.ID()] = root
m.root = root
m.result = result
m.seal = seal
m.heights[root.Header.Height] = root
m.finalized = root.Header.Height
return nil
}
func (m *ProtocolState) Extend(block *flow.Block) error {
m.Lock()
defer m.Unlock()
id := block.ID()
if _, ok := m.blocks[id]; ok {
return storage.ErrAlreadyExists
}
if _, ok := m.blocks[block.Header.ParentID]; !ok {
return fmt.Errorf("could not retrieve parent")
}
m.blocks[id] = block
// index children
children, ok := m.children[block.Header.ParentID]
if !ok {
children = make([]flow.Identifier, 0)
}
children = append(children, id)
m.children[block.Header.ParentID] = children
return nil
}
func (m *ProtocolState) Finalize(blockID flow.Identifier) error {
m.Lock()
defer m.Unlock()
block, ok := m.blocks[blockID]
if !ok {
return fmt.Errorf("could not retrieve final header")
}
if block.Header.Height <= m.finalized {
return fmt.Errorf("could not finalize old blocks")
}
// update heights
cur := block
for height := cur.Header.Height; height > m.finalized; height-- {
parent, ok := m.blocks[cur.Header.ParentID]
if !ok {
return fmt.Errorf("parent does not exist for block at height: %v, parentID: %v", cur.Header.Height, cur.Header.ParentID)
}
m.heights[height] = cur
cur = parent
}
m.finalized = block.Header.Height
return nil
}