-
Notifications
You must be signed in to change notification settings - Fork 175
/
state.go
396 lines (330 loc) · 11.2 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package state
import (
"context"
"errors"
"fmt"
"sync"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/engine/execution"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/trace"
"github.com/onflow/flow-go/storage"
badgerstorage "github.com/onflow/flow-go/storage/badger"
"github.com/onflow/flow-go/storage/badger/operation"
"github.com/onflow/flow-go/storage/badger/procedure"
)
// ReadOnlyExecutionState allows to read the execution state
type ReadOnlyExecutionState interface {
// NewStorageSnapshot creates a new ready-only view at the given state commitment.
NewStorageSnapshot(flow.StateCommitment) snapshot.StorageSnapshot
// StateCommitmentByBlockID returns the final state commitment for the provided block ID.
StateCommitmentByBlockID(context.Context, flow.Identifier) (flow.StateCommitment, error)
// HasState returns true if the state with the given state commitment exists in memory
HasState(flow.StateCommitment) bool
// ChunkDataPackByChunkID retrieve a chunk data pack given the chunk ID.
ChunkDataPackByChunkID(flow.Identifier) (*flow.ChunkDataPack, error)
GetExecutionResultID(context.Context, flow.Identifier) (flow.Identifier, error)
GetHighestExecutedBlockID(context.Context) (uint64, flow.Identifier, error)
}
// TODO Many operations here are should be transactional, so we need to refactor this
// to store a reference to DB and compose operations and procedures rather then
// just being amalgamate of proxies for single transactions operation
// ExecutionState is an interface used to access and mutate the execution state of the blockchain.
type ExecutionState interface {
ReadOnlyExecutionState
UpdateHighestExecutedBlockIfHigher(context.Context, *flow.Header) error
SaveExecutionResults(
ctx context.Context,
result *execution.ComputationResult,
) error
}
const (
KeyPartOwner = uint16(0)
// @deprecated - controller was used only by the very first
// version of cadence for access controll which was retired later on
// KeyPartController = uint16(1)
KeyPartKey = uint16(2)
)
type state struct {
tracer module.Tracer
ls ledger.Ledger
commits storage.Commits
blocks storage.Blocks
headers storage.Headers
collections storage.Collections
chunkDataPacks storage.ChunkDataPacks
results storage.ExecutionResults
myReceipts storage.MyExecutionReceipts
events storage.Events
serviceEvents storage.ServiceEvents
transactionResults storage.TransactionResults
db *badger.DB
}
func RegisterIDToKey(reg flow.RegisterID) ledger.Key {
return ledger.NewKey([]ledger.KeyPart{
ledger.NewKeyPart(KeyPartOwner, []byte(reg.Owner)),
ledger.NewKeyPart(KeyPartKey, []byte(reg.Key)),
})
}
// NewExecutionState returns a new execution state access layer for the given ledger storage.
func NewExecutionState(
ls ledger.Ledger,
commits storage.Commits,
blocks storage.Blocks,
headers storage.Headers,
collections storage.Collections,
chunkDataPacks storage.ChunkDataPacks,
results storage.ExecutionResults,
myReceipts storage.MyExecutionReceipts,
events storage.Events,
serviceEvents storage.ServiceEvents,
transactionResults storage.TransactionResults,
db *badger.DB,
tracer module.Tracer,
) ExecutionState {
return &state{
tracer: tracer,
ls: ls,
commits: commits,
blocks: blocks,
headers: headers,
collections: collections,
chunkDataPacks: chunkDataPacks,
results: results,
myReceipts: myReceipts,
events: events,
serviceEvents: serviceEvents,
transactionResults: transactionResults,
db: db,
}
}
func makeSingleValueQuery(commitment flow.StateCommitment, id flow.RegisterID) (*ledger.QuerySingleValue, error) {
return ledger.NewQuerySingleValue(ledger.State(commitment),
RegisterIDToKey(id),
)
}
func RegisterEntriesToKeysValues(
entries flow.RegisterEntries,
) (
[]ledger.Key,
[]ledger.Value,
) {
keys := make([]ledger.Key, len(entries))
values := make([]ledger.Value, len(entries))
for i, entry := range entries {
keys[i] = RegisterIDToKey(entry.Key)
values[i] = entry.Value
}
return keys, values
}
// TODO(patrick): revisit caching. readCache needs to be mutex guarded for
// parallel execution.
type LedgerStorageSnapshot struct {
ledger ledger.Ledger
commitment flow.StateCommitment
mutex sync.RWMutex
readCache map[flow.RegisterID]flow.RegisterValue // Guarded by mutex.
}
func NewLedgerStorageSnapshot(
ldg ledger.Ledger,
commitment flow.StateCommitment,
) snapshot.StorageSnapshot {
return &LedgerStorageSnapshot{
ledger: ldg,
commitment: commitment,
readCache: make(map[flow.RegisterID]flow.RegisterValue),
}
}
func (storage *LedgerStorageSnapshot) getFromCache(
id flow.RegisterID,
) (
flow.RegisterValue,
bool,
) {
storage.mutex.RLock()
defer storage.mutex.RUnlock()
value, ok := storage.readCache[id]
return value, ok
}
func (storage *LedgerStorageSnapshot) getFromLedger(
id flow.RegisterID,
) (
flow.RegisterValue,
error,
) {
query, err := makeSingleValueQuery(storage.commitment, id)
if err != nil {
return nil, fmt.Errorf("cannot create ledger query: %w", err)
}
value, err := storage.ledger.GetSingleValue(query)
if err != nil {
return nil, fmt.Errorf(
"error getting register (%s) value at %x: %w",
id,
storage.commitment,
err)
}
return value, nil
}
func (storage *LedgerStorageSnapshot) Get(
id flow.RegisterID,
) (
flow.RegisterValue,
error,
) {
value, ok := storage.getFromCache(id)
if ok {
return value, nil
}
value, err := storage.getFromLedger(id)
if err != nil {
return nil, err
}
storage.mutex.Lock()
defer storage.mutex.Unlock()
storage.readCache[id] = value
return value, nil
}
func (s *state) NewStorageSnapshot(
commitment flow.StateCommitment,
) snapshot.StorageSnapshot {
return NewLedgerStorageSnapshot(s.ls, commitment)
}
type RegisterUpdatesHolder interface {
UpdatedRegisters() flow.RegisterEntries
}
func CommitDelta(ldg ledger.Ledger, ruh RegisterUpdatesHolder, baseState flow.StateCommitment) (flow.StateCommitment, *ledger.TrieUpdate, error) {
keys, values := RegisterEntriesToKeysValues(ruh.UpdatedRegisters())
update, err := ledger.NewUpdate(ledger.State(baseState), keys, values)
if err != nil {
return flow.DummyStateCommitment, nil, fmt.Errorf("cannot create ledger update: %w", err)
}
commit, trieUpdate, err := ldg.Set(update)
if err != nil {
return flow.DummyStateCommitment, nil, err
}
return flow.StateCommitment(commit), trieUpdate, nil
}
func (s *state) HasState(commitment flow.StateCommitment) bool {
return s.ls.HasState(ledger.State(commitment))
}
func (s *state) StateCommitmentByBlockID(ctx context.Context, blockID flow.Identifier) (flow.StateCommitment, error) {
return s.commits.ByBlockID(blockID)
}
func (s *state) ChunkDataPackByChunkID(chunkID flow.Identifier) (*flow.ChunkDataPack, error) {
chunkDataPack, err := s.chunkDataPacks.ByChunkID(chunkID)
if err != nil {
return nil, fmt.Errorf("could not retrieve stored chunk data pack: %w", err)
}
return chunkDataPack, nil
}
func (s *state) GetExecutionResultID(ctx context.Context, blockID flow.Identifier) (flow.Identifier, error) {
span, _ := s.tracer.StartSpanFromContext(ctx, trace.EXEGetExecutionResultID)
defer span.End()
result, err := s.results.ByBlockID(blockID)
if err != nil {
return flow.ZeroID, err
}
return result.ID(), nil
}
func (s *state) SaveExecutionResults(
ctx context.Context,
result *execution.ComputationResult,
) error {
span, childCtx := s.tracer.StartSpanFromContext(
ctx,
trace.EXEStateSaveExecutionResults)
defer span.End()
header := result.ExecutableBlock.Block.Header
blockID := header.ID()
// Write Batch is BadgerDB feature designed for handling lots of writes
// in efficient and automatic manner, hence pushing all the updates we can
// as tightly as possible to let Badger manage it.
// Note, that it does not guarantee atomicity as transactions has size limit,
// but it's the closest thing to atomicity we could have
batch := badgerstorage.NewBatch(s.db)
for _, chunkDataPack := range result.AllChunkDataPacks() {
err := s.chunkDataPacks.BatchStore(chunkDataPack, batch)
if err != nil {
return fmt.Errorf("cannot store chunk data pack: %w", err)
}
}
err := s.events.BatchStore(blockID, []flow.EventsList{result.AllEvents()}, batch)
if err != nil {
return fmt.Errorf("cannot store events: %w", err)
}
err = s.serviceEvents.BatchStore(blockID, result.AllServiceEvents(), batch)
if err != nil {
return fmt.Errorf("cannot store service events: %w", err)
}
err = s.transactionResults.BatchStore(
blockID,
result.AllTransactionResults(),
batch)
if err != nil {
return fmt.Errorf("cannot store transaction result: %w", err)
}
executionResult := &result.ExecutionReceipt.ExecutionResult
err = s.results.BatchStore(executionResult, batch)
if err != nil {
return fmt.Errorf("cannot store execution result: %w", err)
}
err = s.results.BatchIndex(blockID, executionResult.ID(), batch)
if err != nil {
return fmt.Errorf("cannot index execution result: %w", err)
}
err = s.myReceipts.BatchStoreMyReceipt(result.ExecutionReceipt, batch)
if err != nil {
return fmt.Errorf("could not persist execution result: %w", err)
}
// the state commitment is the last data item to be stored, so that
// IsBlockExecuted can be implemented by checking whether state commitment exists
// in the database
err = s.commits.BatchStore(blockID, result.CurrentEndState(), batch)
if err != nil {
return fmt.Errorf("cannot store state commitment: %w", err)
}
err = batch.Flush()
if err != nil {
return fmt.Errorf("batch flush error: %w", err)
}
//outside batch because it requires read access
err = s.UpdateHighestExecutedBlockIfHigher(childCtx, header)
if err != nil {
return fmt.Errorf("cannot update highest executed block: %w", err)
}
return nil
}
func (s *state) UpdateHighestExecutedBlockIfHigher(ctx context.Context, header *flow.Header) error {
if s.tracer != nil {
span, _ := s.tracer.StartSpanFromContext(ctx, trace.EXEUpdateHighestExecutedBlockIfHigher)
defer span.End()
}
return operation.RetryOnConflict(s.db.Update, procedure.UpdateHighestExecutedBlockIfHigher(header))
}
func (s *state) GetHighestExecutedBlockID(ctx context.Context) (uint64, flow.Identifier, error) {
var blockID flow.Identifier
var height uint64
err := s.db.View(procedure.GetHighestExecutedBlock(&height, &blockID))
if err != nil {
return 0, flow.ZeroID, err
}
return height, blockID, nil
}
// IsBlockExecuted returns whether the block has been executed.
// it checks whether the state commitment exists in execution state.
func IsBlockExecuted(ctx context.Context, state ReadOnlyExecutionState, block flow.Identifier) (bool, error) {
_, err := state.StateCommitmentByBlockID(ctx, block)
// statecommitment exists means the block has been executed
if err == nil {
return true, nil
}
// statecommitment not exists means the block hasn't been executed yet
if errors.Is(err, storage.ErrNotFound) {
return false, nil
}
return false, err
}