-
Notifications
You must be signed in to change notification settings - Fork 179
/
state.go
296 lines (266 loc) · 9.22 KB
/
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package badger
import (
"errors"
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/operation"
)
type State struct {
metrics module.ComplianceMetrics
db *badger.DB
headers storage.Headers
blocks storage.Blocks
seals storage.Seals
epoch struct {
setups storage.EpochSetups
commits storage.EpochCommits
statuses storage.EpochStatuses
}
}
func Bootstrap(
metrics module.ComplianceMetrics,
db *badger.DB,
headers storage.Headers,
seals storage.Seals,
blocks storage.Blocks,
setups storage.EpochSetups,
commits storage.EpochCommits,
statuses storage.EpochStatuses,
stateRoot *StateRoot,
) (*State, error) {
isBootstrapped, err := IsBootstrapped(db)
if err != nil {
return nil, fmt.Errorf("failed to determine whether database contains bootstrapped state: %w", err)
}
if isBootstrapped {
return nil, fmt.Errorf("expected empty database")
}
state := newState(metrics, db, headers, seals, blocks, setups, commits, statuses)
err = operation.RetryOnConflict(db.Update, func(tx *badger.Txn) error {
// 1) insert the root block with its payload into the state and index it
err = state.blocks.StoreTx(stateRoot.Block())(tx)
if err != nil {
return fmt.Errorf("could not insert root block: %w", err)
}
err = operation.InsertBlockValidity(stateRoot.Block().ID(), true)(tx)
if err != nil {
return fmt.Errorf("could not mark root block as valid: %w", err)
}
err = operation.IndexBlockHeight(stateRoot.Block().Header.Height, stateRoot.Block().ID())(tx)
if err != nil {
return fmt.Errorf("could not index root block: %w", err)
}
// root block has no parent, so only needs to add one index
// to indicate the root block has no child yet
err = operation.InsertBlockChildren(stateRoot.Block().ID(), nil)(tx)
if err != nil {
return fmt.Errorf("could not initialize root child index: %w", err)
}
// 2) insert the root execution result into the database and index it
err = operation.InsertExecutionResult(stateRoot.Result())(tx)
if err != nil {
return fmt.Errorf("could not insert root result: %w", err)
}
err = operation.IndexExecutionResult(stateRoot.Block().ID(), stateRoot.Result().ID())(tx)
if err != nil {
return fmt.Errorf("could not index root result: %w", err)
}
// 3) insert the root block seal into the database and index it
err = operation.InsertSeal(stateRoot.Seal().ID(), stateRoot.Seal())(tx)
if err != nil {
return fmt.Errorf("could not insert root seal: %w", err)
}
err = operation.IndexBlockSeal(stateRoot.Block().ID(), stateRoot.Seal().ID())(tx)
if err != nil {
return fmt.Errorf("could not index root block seal: %w", err)
}
// 4) initialize the current protocol state values
err = operation.InsertStartedView(stateRoot.Block().Header.ChainID, stateRoot.Block().Header.View)(tx)
if err != nil {
return fmt.Errorf("could not insert started view: %w", err)
}
err = operation.InsertVotedView(stateRoot.Block().Header.ChainID, stateRoot.Block().Header.View)(tx)
if err != nil {
return fmt.Errorf("could not insert started view: %w", err)
}
err = operation.InsertRootHeight(stateRoot.Block().Header.Height)(tx)
if err != nil {
return fmt.Errorf("could not insert root height: %w", err)
}
err = operation.InsertFinalizedHeight(stateRoot.Block().Header.Height)(tx)
if err != nil {
return fmt.Errorf("could not insert finalized height: %w", err)
}
err = operation.InsertSealedHeight(stateRoot.Block().Header.Height)(tx)
if err != nil {
return fmt.Errorf("could not insert sealed height: %w", err)
}
// 5) initialize values related to the epoch logic
err = state.epoch.setups.StoreTx(stateRoot.EpochSetupEvent())(tx)
if err != nil {
return fmt.Errorf("could not insert EpochSetup event: %w", err)
}
err = state.epoch.commits.StoreTx(stateRoot.EpochCommitEvent())(tx)
if err != nil {
return fmt.Errorf("could not insert EpochCommit event: %w", err)
}
status, err := flow.NewEpochStatus(stateRoot.Block().ID(), stateRoot.EpochSetupEvent().ID(), stateRoot.EpochCommitEvent().ID(), flow.ZeroID, flow.ZeroID)
if err != nil {
return fmt.Errorf("could not construct root epoch status: %w", err)
}
err = state.epoch.statuses.StoreTx(stateRoot.Block().ID(), status)(tx)
if err != nil {
return fmt.Errorf("could not insert EpochStatus: %w", err)
}
state.metrics.FinalizedHeight(stateRoot.Block().Header.Height)
state.metrics.BlockFinalized(stateRoot.Block())
state.metrics.SealedHeight(stateRoot.Block().Header.Height)
state.metrics.BlockSealed(stateRoot.Block())
return nil
})
if err != nil {
return nil, fmt.Errorf("bootstrapping failed: %w", err)
}
return state, nil
}
func OpenState(
metrics module.ComplianceMetrics,
db *badger.DB,
headers storage.Headers,
seals storage.Seals,
blocks storage.Blocks,
setups storage.EpochSetups,
commits storage.EpochCommits,
statuses storage.EpochStatuses,
) (*State, *StateRoot, error) {
isBootstrapped, err := IsBootstrapped(db)
if err != nil {
return nil, nil, fmt.Errorf("failed to determine whether database contains bootstrapped state: %w", err)
}
if !isBootstrapped {
return nil, nil, fmt.Errorf("expected database to contain bootstrapped state")
}
state := newState(metrics, db, headers, seals, blocks, setups, commits, statuses)
// read root block from database:
var rootHeight uint64
err = db.View(operation.RetrieveRootHeight(&rootHeight))
if err != nil {
return nil, nil, fmt.Errorf("failed retrieve root height: %w", err)
}
rootBlock, err := blocks.ByHeight(rootHeight)
if err != nil {
return nil, nil, fmt.Errorf("failed retrieve root block: %w", err)
}
// read root execution result
var resultID flow.Identifier
err = db.View(operation.LookupExecutionResult(rootBlock.ID(), &resultID))
if err != nil {
return nil, nil, fmt.Errorf("failed retrieve root block's execution result ID: %w", err)
}
var result flow.ExecutionResult
err = db.View(operation.RetrieveExecutionResult(resultID, &result))
if err != nil {
return nil, nil, fmt.Errorf("failed retrieve root block's execution result: %w", err)
}
// read root seal
seal, err := seals.ByBlockID(rootBlock.ID())
if err != nil {
return nil, nil, fmt.Errorf("failed retrieve root block's seal: %w", err)
}
// read root seal
epochStatus, err := statuses.ByBlockID(rootBlock.ID())
if err != nil {
return nil, nil, fmt.Errorf("failed retrieve root block's epoch status: %w", err)
}
epochSetup, err := setups.ByID(epochStatus.CurrentEpoch.SetupID)
if err != nil {
return nil, nil, fmt.Errorf("failed retrieve root epochs's setup event: %w", err)
}
// construct state Root
stateRoot, err := NewStateRoot(rootBlock, &result, seal, epochSetup.FirstView)
if err != nil {
return nil, nil, fmt.Errorf("constructing state root failed: %w", err)
}
return state, stateRoot, nil
}
func (s *State) Params() protocol.Params {
return &Params{state: s}
}
func (s *State) Sealed() protocol.Snapshot {
// retrieve the latest sealed height
var sealed uint64
err := s.db.View(operation.RetrieveSealedHeight(&sealed))
if err != nil {
return NewInvalidSnapshot(fmt.Errorf("could not retrieve sealed height: %w", err))
}
return s.AtHeight(sealed)
}
func (s *State) Final() protocol.Snapshot {
// retrieve the latest finalized height
var finalized uint64
err := s.db.View(operation.RetrieveFinalizedHeight(&finalized))
if err != nil {
return NewInvalidSnapshot(fmt.Errorf("could not retrieve finalized height: %w", err))
}
return s.AtHeight(finalized)
}
func (s *State) AtHeight(height uint64) protocol.Snapshot {
// retrieve the block ID for the finalized height
var blockID flow.Identifier
err := s.db.View(operation.LookupBlockHeight(height, &blockID))
if err != nil {
return NewInvalidSnapshot(fmt.Errorf("could not look up block by height: %w", err))
}
return s.AtBlockID(blockID)
}
func (s *State) AtBlockID(blockID flow.Identifier) protocol.Snapshot {
return NewSnapshot(s, blockID)
}
// newState initializes a new state backed by the provided a badger database,
// mempools and service components.
// The parameter `expectedBootstrappedState` indicates whether or not the database
// is expected to contain a an already bootstrapped state or not
func newState(
metrics module.ComplianceMetrics,
db *badger.DB,
headers storage.Headers,
seals storage.Seals,
blocks storage.Blocks,
setups storage.EpochSetups,
commits storage.EpochCommits,
statuses storage.EpochStatuses,
) *State {
return &State{
metrics: metrics,
db: db,
headers: headers,
seals: seals,
blocks: blocks,
epoch: struct {
setups storage.EpochSetups
commits storage.EpochCommits
statuses storage.EpochStatuses
}{
setups: setups,
commits: commits,
statuses: statuses,
},
}
}
// IsBootstrapped returns whether or not the database contains a bootstrapped state
func IsBootstrapped(db *badger.DB) (bool, error) {
var finalized uint64
err := db.View(operation.RetrieveFinalizedHeight(&finalized))
if errors.Is(err, storage.ErrNotFound) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("retrieving finalized height failed: %w", err)
}
return true, nil
}