-
Notifications
You must be signed in to change notification settings - Fork 178
/
execution_state.go
321 lines (267 loc) · 8.32 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
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
package state
import (
"fmt"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/meter"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/model/flow"
)
const (
DefaultMaxKeySize = 16_000 // ~16KB
DefaultMaxValueSize = 256_000_000 // ~256MB
)
// State represents the execution state
// it holds draft of updates and captures
// all register touches
type ExecutionState struct {
// NOTE: A finalized state is no longer accessible. It can however be
// re-attached to another transaction and be committed (for cached result
// bookkeeping purpose).
finalized bool
*spockState
meter *meter.Meter
// NOTE: parent and child state shares the same limits controller
*limitsController
}
type StateParameters struct {
meter.MeterParameters
maxKeySizeAllowed uint64
maxValueSizeAllowed uint64
}
func DefaultParameters() StateParameters {
return StateParameters{
MeterParameters: meter.DefaultParameters(),
maxKeySizeAllowed: DefaultMaxKeySize,
maxValueSizeAllowed: DefaultMaxValueSize,
}
}
// WithMeterParameters sets the state's meter parameters
func (params StateParameters) WithMeterParameters(
meterParams meter.MeterParameters,
) StateParameters {
newParams := params
newParams.MeterParameters = meterParams
return newParams
}
// WithMaxKeySizeAllowed sets limit on max key size
func (params StateParameters) WithMaxKeySizeAllowed(
limit uint64,
) StateParameters {
newParams := params
newParams.maxKeySizeAllowed = limit
return newParams
}
// WithMaxValueSizeAllowed sets limit on max value size
func (params StateParameters) WithMaxValueSizeAllowed(
limit uint64,
) StateParameters {
newParams := params
newParams.maxValueSizeAllowed = limit
return newParams
}
type limitsController struct {
enforceLimits bool
maxKeySizeAllowed uint64
maxValueSizeAllowed uint64
}
func newLimitsController(params StateParameters) *limitsController {
return &limitsController{
enforceLimits: true,
maxKeySizeAllowed: params.maxKeySizeAllowed,
maxValueSizeAllowed: params.maxValueSizeAllowed,
}
}
func (controller *limitsController) RunWithAllLimitsDisabled(f func()) {
if f == nil {
return
}
current := controller.enforceLimits
controller.enforceLimits = false
f()
controller.enforceLimits = current
}
// NewExecutionState constructs a new state
func NewExecutionState(
snapshot snapshot.StorageSnapshot,
params StateParameters,
) *ExecutionState {
m := meter.NewMeter(params.MeterParameters)
return &ExecutionState{
finalized: false,
spockState: newSpockState(snapshot),
meter: m,
limitsController: newLimitsController(params),
}
}
// NewChildWithMeterParams generates a new child state using the provide meter
// parameters.
func (state *ExecutionState) NewChildWithMeterParams(
params meter.MeterParameters,
) *ExecutionState {
return &ExecutionState{
finalized: false,
spockState: state.spockState.NewChild(),
meter: meter.NewMeter(params),
limitsController: state.limitsController,
}
}
// NewChild generates a new child state using the parent's meter parameters.
func (state *ExecutionState) NewChild() *ExecutionState {
return state.NewChildWithMeterParams(state.meter.MeterParameters)
}
// InteractionUsed returns the amount of ledger interaction (total ledger byte read + total ledger byte written)
func (state *ExecutionState) InteractionUsed() uint64 {
return state.meter.TotalBytesOfStorageInteractions()
}
// BytesWritten returns the amount of total ledger bytes written
func (state *ExecutionState) BytesWritten() uint64 {
return state.meter.TotalBytesWrittenToStorage()
}
func (state *ExecutionState) DropChanges() error {
if state.finalized {
return fmt.Errorf("cannot DropChanges on a finalized state")
}
return state.spockState.DropChanges()
}
// Get returns a register value given owner and key
func (state *ExecutionState) Get(id flow.RegisterID) (flow.RegisterValue, error) {
if state.finalized {
return nil, fmt.Errorf("cannot Get on a finalized state")
}
var value []byte
var err error
if state.enforceLimits {
if err = state.checkSize(id, []byte{}); err != nil {
return nil, err
}
}
if value, err = state.spockState.Get(id); err != nil {
// wrap error into a fatal error
getError := errors.NewLedgerFailure(err)
// wrap with more info
return nil, fmt.Errorf("failed to read %s: %w", id, getError)
}
err = state.meter.MeterStorageRead(id, value, state.enforceLimits)
return value, err
}
// Set updates state delta with a register update
func (state *ExecutionState) Set(id flow.RegisterID, value flow.RegisterValue) error {
if state.finalized {
return fmt.Errorf("cannot Set on a finalized state")
}
if state.enforceLimits {
if err := state.checkSize(id, value); err != nil {
return err
}
}
if err := state.spockState.Set(id, value); err != nil {
// wrap error into a fatal error
setError := errors.NewLedgerFailure(err)
// wrap with more info
return fmt.Errorf("failed to update %s: %w", id, setError)
}
return state.meter.MeterStorageWrite(id, value, state.enforceLimits)
}
// MeterComputation meters computation usage
func (state *ExecutionState) MeterComputation(kind common.ComputationKind, intensity uint) error {
if state.finalized {
return fmt.Errorf("cannot MeterComputation on a finalized state")
}
if state.enforceLimits {
return state.meter.MeterComputation(kind, intensity)
}
return nil
}
// TotalComputationUsed returns total computation used
func (state *ExecutionState) TotalComputationUsed() uint64 {
return state.meter.TotalComputationUsed()
}
// ComputationIntensities returns computation intensities
func (state *ExecutionState) ComputationIntensities() meter.MeteredComputationIntensities {
return state.meter.ComputationIntensities()
}
// TotalComputationLimit returns total computation limit
func (state *ExecutionState) TotalComputationLimit() uint {
return state.meter.TotalComputationLimit()
}
// MeterMemory meters memory usage
func (state *ExecutionState) MeterMemory(kind common.MemoryKind, intensity uint) error {
if state.finalized {
return fmt.Errorf("cannot MeterMemory on a finalized state")
}
if state.enforceLimits {
return state.meter.MeterMemory(kind, intensity)
}
return nil
}
// MemoryIntensities returns computation intensities
func (state *ExecutionState) MemoryIntensities() meter.MeteredMemoryIntensities {
return state.meter.MemoryIntensities()
}
// TotalMemoryEstimate returns total memory used
func (state *ExecutionState) TotalMemoryEstimate() uint64 {
return state.meter.TotalMemoryEstimate()
}
// TotalMemoryLimit returns total memory limit
func (state *ExecutionState) TotalMemoryLimit() uint {
return uint(state.meter.TotalMemoryLimit())
}
func (state *ExecutionState) MeterEmittedEvent(byteSize uint64) error {
if state.finalized {
return fmt.Errorf("cannot MeterEmittedEvent on a finalized state")
}
if state.enforceLimits {
return state.meter.MeterEmittedEvent(byteSize)
}
return nil
}
func (state *ExecutionState) TotalEmittedEventBytes() uint64 {
return state.meter.TotalEmittedEventBytes()
}
func (state *ExecutionState) Finalize() *snapshot.ExecutionSnapshot {
state.finalized = true
snapshot := state.spockState.Finalize()
snapshot.Meter = state.meter
return snapshot
}
// MergeState the changes from a the given execution snapshot to this state.
func (state *ExecutionState) Merge(other *snapshot.ExecutionSnapshot) error {
if state.finalized {
return fmt.Errorf("cannot Merge on a finalized state")
}
err := state.spockState.Merge(other)
if err != nil {
return errors.NewStateMergeFailure(err)
}
state.meter.MergeMeter(other.Meter)
return nil
}
func (state *ExecutionState) checkSize(
id flow.RegisterID,
value flow.RegisterValue,
) error {
keySize := uint64(len(id.Owner) + len(id.Key))
valueSize := uint64(len(value))
if keySize > state.maxKeySizeAllowed {
return errors.NewStateKeySizeLimitError(
id,
keySize,
state.maxKeySizeAllowed)
}
if valueSize > state.maxValueSizeAllowed {
return errors.NewStateValueSizeLimitError(
value,
valueSize,
state.maxValueSizeAllowed)
}
return nil
}
func (state *ExecutionState) readSetSize() int {
return state.spockState.readSetSize()
}
func (state *ExecutionState) interimReadSet(
accumulator map[flow.RegisterID]struct{},
) {
state.spockState.interimReadSet(accumulator)
}