-
Notifications
You must be signed in to change notification settings - Fork 175
/
state.go
325 lines (277 loc) · 8.57 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
package state
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"sort"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/model/flow"
)
// TODO we started with high numbers here and we might
// tune (reduce) them when we have more data
const (
DefaultMaxKeySize = 16_000 // ~16KB
DefaultMaxValueSize = 256_000_000 // ~256MB
DefaultMaxInteractionSize = 20_000_000 // ~20MB
)
type mapKey struct {
owner, controller, key string
}
// State represents the execution state
// it holds draft of updates and captures
// all register touches
type State struct {
view View
updatedAddresses map[flow.Address]struct{}
updateSize map[mapKey]uint64
maxKeySizeAllowed uint64
maxValueSizeAllowed uint64
maxInteractionAllowed uint64
ReadCounter uint64
WriteCounter uint64
TotalBytesRead uint64
TotalBytesWritten uint64
}
func defaultState(view View) *State {
return &State{
view: view,
updatedAddresses: make(map[flow.Address]struct{}),
updateSize: make(map[mapKey]uint64),
maxKeySizeAllowed: DefaultMaxKeySize,
maxValueSizeAllowed: DefaultMaxValueSize,
maxInteractionAllowed: DefaultMaxInteractionSize,
}
}
func (s *State) View() View {
return s.view
}
type StateOption func(st *State) *State
// NewState constructs a new state
func NewState(view View, opts ...StateOption) *State {
ctx := defaultState(view)
for _, applyOption := range opts {
ctx = applyOption(ctx)
}
return ctx
}
// WithMaxKeySizeAllowed sets limit on max key size
func WithMaxKeySizeAllowed(limit uint64) func(st *State) *State {
return func(st *State) *State {
st.maxKeySizeAllowed = limit
return st
}
}
// WithMaxValueSizeAllowed sets limit on max value size
func WithMaxValueSizeAllowed(limit uint64) func(st *State) *State {
return func(st *State) *State {
st.maxValueSizeAllowed = limit
return st
}
}
// WithMaxInteractionSizeAllowed sets limit on total byte interaction with ledger
func WithMaxInteractionSizeAllowed(limit uint64) func(st *State) *State {
return func(st *State) *State {
st.maxInteractionAllowed = limit
return st
}
}
// InteractionUsed returns the amount of ledger interaction (total ledger byte read + total ledger byte written)
func (s *State) InteractionUsed() uint64 {
return s.TotalBytesRead + s.TotalBytesWritten
}
// Get returns a register value given owner, controller and key
func (s *State) Get(owner, controller, key string, enforceLimit bool) (flow.RegisterValue, error) {
var value []byte
var err error
if enforceLimit {
if err = s.checkSize(owner, controller, key, []byte{}); err != nil {
return nil, err
}
}
if value, err = s.view.Get(owner, controller, key); err != nil {
// wrap error into a fatal error
getError := errors.NewLedgerFailure(err)
// wrap with more info
return nil, fmt.Errorf("failed to read key %s on account %s: %w", PrintableKey(key), hex.EncodeToString([]byte(owner)), getError)
}
// if not part of recent updates count them as read
if _, ok := s.updateSize[mapKey{owner, controller, key}]; !ok {
s.ReadCounter++
s.TotalBytesRead += uint64(len(owner) +
len(controller) + len(key) + len(value))
}
if enforceLimit {
return value, s.checkMaxInteraction()
}
return value, nil
}
// Set updates state delta with a register update
func (s *State) Set(owner, controller, key string, value flow.RegisterValue, enforceLimit bool) error {
if enforceLimit {
if err := s.checkSize(owner, controller, key, value); err != nil {
return err
}
}
if err := s.view.Set(owner, controller, key, value); err != nil {
// wrap error into a fatal error
setError := errors.NewLedgerFailure(err)
// wrap with more info
return fmt.Errorf("failed to update key %s on account %s: %w", PrintableKey(key), hex.EncodeToString([]byte(owner)), setError)
}
if enforceLimit {
if err := s.checkMaxInteraction(); err != nil {
return err
}
}
if address, isAddress := addressFromOwner(owner); isAddress {
s.updatedAddresses[address] = struct{}{}
}
mapKey := mapKey{owner, controller, key}
if old, ok := s.updateSize[mapKey]; ok {
s.WriteCounter--
s.TotalBytesWritten -= old
}
updateSize := uint64(len(owner) + len(controller) + len(key) + len(value))
s.WriteCounter++
s.TotalBytesWritten += updateSize
s.updateSize[mapKey] = updateSize
return nil
}
func (s *State) Delete(owner, controller, key string, enforceLimit bool) error {
return s.Set(owner, controller, key, nil, enforceLimit)
}
// We don't need this later, it should be invisible to the cadence
func (s *State) Touch(owner, controller, key string) error {
return s.view.Touch(owner, controller, key)
}
// NewChild generates a new child state
func (s *State) NewChild() *State {
return NewState(s.view.NewChild(),
WithMaxKeySizeAllowed(s.maxKeySizeAllowed),
WithMaxValueSizeAllowed(s.maxValueSizeAllowed),
WithMaxInteractionSizeAllowed(s.maxInteractionAllowed),
)
}
// MergeState applies the changes from a the given view to this view.
func (s *State) MergeState(other *State, enforceLimit bool) error {
err := s.view.MergeView(other.view)
if err != nil {
return errors.NewStateMergeFailure(err)
}
// apply address updates
for k, v := range other.updatedAddresses {
s.updatedAddresses[k] = v
}
// apply update sizes
for k, v := range other.updateSize {
s.updateSize[k] = v
}
// update ledger interactions
s.ReadCounter += other.ReadCounter
s.WriteCounter += other.WriteCounter
s.TotalBytesRead += other.TotalBytesRead
s.TotalBytesWritten += other.TotalBytesWritten
// check max interaction as last step
if enforceLimit {
return s.checkMaxInteraction()
}
return nil
}
type sortedAddresses []flow.Address
func (a sortedAddresses) Len() int { return len(a) }
func (a sortedAddresses) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortedAddresses) Less(i, j int) bool { return bytes.Compare(a[i][:], a[j][:]) >= 0 }
// UpdatedAddresses returns a sorted list of addresses that were updated (at least 1 register update)
func (s *State) UpdatedAddresses() []flow.Address {
addresses := make(sortedAddresses, len(s.updatedAddresses))
i := 0
for k := range s.updatedAddresses {
addresses[i] = k
i++
}
sort.Sort(addresses)
return addresses
}
func (s *State) checkMaxInteraction() error {
if s.InteractionUsed() > s.maxInteractionAllowed {
return errors.NewLedgerIntractionLimitExceededError(s.InteractionUsed(), s.maxInteractionAllowed)
}
return nil
}
func (s *State) checkSize(owner, controller, key string, value flow.RegisterValue) error {
keySize := uint64(len(owner) + len(controller) + len(key))
valueSize := uint64(len(value))
if keySize > s.maxKeySizeAllowed {
return errors.NewStateKeySizeLimitError(owner, controller, key, keySize, s.maxKeySizeAllowed)
}
if valueSize > s.maxValueSizeAllowed {
return errors.NewStateValueSizeLimitError(value, valueSize, s.maxValueSizeAllowed)
}
return nil
}
func addressFromOwner(owner string) (flow.Address, bool) {
ownerBytes := []byte(owner)
if len(ownerBytes) != flow.AddressLength {
// not an address
return flow.EmptyAddress, false
}
address := flow.BytesToAddress(ownerBytes)
return address, true
}
// IsFVMStateKey returns true if the
// key is controlled by the fvm env and
// return false otherwise (key controlled by the cadence env)
func IsFVMStateKey(owner, controller, key string) bool {
// check if is a service level key (owner and controller is empty)
// cases:
// - "", "", "uuid"
// - "", "", "account_address_state"
if len(owner) == 0 && len(controller) == 0 {
return true
}
// check account level keys
// cases:
// - address, address, "public_key_count"
// - address, address, "public_key_%d" (index)
// - address, address, "contract_names"
// - address, address, "code.%s" (contract name)
// - address, "", exists
// - address, "", "storage_used"
// - address, "", "frozen"
if owner == controller {
if key == KeyPublicKeyCount {
return true
}
if bytes.HasPrefix([]byte(key), []byte("public_key_")) {
return true
}
if key == KeyContractNames {
return true
}
if bytes.HasPrefix([]byte(key), []byte(KeyCode)) {
return true
}
}
if len(controller) == 0 {
if key == KeyExists {
return true
}
if key == KeyStorageUsed {
return true
}
if key == KeyAccountFrozen {
return true
}
}
return false
}
// PrintableKey formats slabs properly and avoids invalid utf8s
func PrintableKey(key string) string {
// slab
if key[0] == '$' && len(key) == 9 {
i := uint64(binary.BigEndian.Uint64([]byte(key[1:])))
return fmt.Sprintf("$%d", i)
}
return fmt.Sprintf("#%x", []byte(key))
}