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
397
398
399
400
401
402
403
404
|
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package logic
import (
"fmt"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
)
// BoxOperation is an enum of box operation types
type BoxOperation int
const (
// BoxCreateOperation creates a box
BoxCreateOperation BoxOperation = iota
// BoxReadOperation reads a box
BoxReadOperation
// BoxWriteOperation writes to a box
BoxWriteOperation
// BoxDeleteOperation deletes a box
BoxDeleteOperation
// BoxResizeOperation resizes a box
BoxResizeOperation
)
func (cx *EvalContext) availableBox(name string, operation BoxOperation, createSize uint64) ([]byte, bool, error) {
if cx.txn.Txn.OnCompletion == transactions.ClearStateOC {
return nil, false, fmt.Errorf("boxes may not be accessed from ClearState program")
}
dirty, ok := cx.available.boxes[BoxRef{cx.appID, name}]
if !ok && cx.UnnamedResources != nil {
ok = cx.UnnamedResources.AvailableBox(cx.appID, name, operation, createSize)
}
if !ok {
return nil, false, fmt.Errorf("invalid Box reference %#x", name)
}
// Since the box is in cx.available, we know this GetBox call is cheap. It
// will go (at most) to the cowRoundBase. Knowledge about existence
// simplifies write budget tracking, then we return the info to avoid yet
// another call to GetBox which most ops need anyway.
content, exists, err := cx.Ledger.GetBox(cx.appID, name)
if err != nil {
return nil, false, err
}
switch operation {
case BoxCreateOperation:
if exists {
if createSize != uint64(len(content)) {
return nil, false, fmt.Errorf("box size mismatch %d %d", uint64(len(content)), createSize)
}
// Since it exists, we have no dirty work to do. The weird case of
// box_put, which seems like a combination of create and write, is
// properly handled because already used boxWrite to declare the
// intent to write (and track dirtiness).
return content, exists, nil
}
fallthrough // If it doesn't exist, a create is like write
case BoxWriteOperation:
writeSize := createSize
if exists {
writeSize = uint64(len(content))
}
if !dirty {
cx.available.dirtyBytes += writeSize
}
dirty = true
case BoxResizeOperation:
newSize := createSize
if dirty {
cx.available.dirtyBytes -= uint64(len(content))
}
cx.available.dirtyBytes += newSize
dirty = true
case BoxDeleteOperation:
if dirty {
cx.available.dirtyBytes -= uint64(len(content))
}
dirty = false
case BoxReadOperation:
/* nothing to do */
}
cx.available.boxes[BoxRef{cx.appID, name}] = dirty
if cx.available.dirtyBytes > cx.ioBudget {
return nil, false, fmt.Errorf("write budget (%d) exceeded %d", cx.ioBudget, cx.available.dirtyBytes)
}
return content, exists, nil
}
func argCheck(cx *EvalContext, name string, size uint64) error {
// Enforce length rules. Currently these are the same as enforced by
// ledger. If these were ever to change in proto, we would need to isolate
// changes to different program versions. (so a v7 app could not see a
// bigger box than expected, for example)
if len(name) == 0 {
return fmt.Errorf("box names may not be zero length")
}
if len(name) > cx.Proto.MaxAppKeyLen {
return fmt.Errorf("name too long: length was %d, maximum is %d", len(name), cx.Proto.MaxAppKeyLen)
}
if size > cx.Proto.MaxBoxSize {
return fmt.Errorf("box size too large: %d, maximum is %d", size, cx.Proto.MaxBoxSize)
}
return nil
}
func opBoxCreate(cx *EvalContext) error {
last := len(cx.Stack) - 1 // size
prev := last - 1 // name
name := string(cx.Stack[prev].Bytes)
size := cx.Stack[last].Uint
err := argCheck(cx, name, size)
if err != nil {
return err
}
_, exists, err := cx.availableBox(name, BoxCreateOperation, size)
if err != nil {
return err
}
if !exists {
appAddr := cx.GetApplicationAddress(cx.appID)
err = cx.Ledger.NewBox(cx.appID, name, make([]byte, size), appAddr)
if err != nil {
return err
}
}
cx.Stack[prev] = boolToSV(!exists)
cx.Stack = cx.Stack[:last]
return err
}
func opBoxExtract(cx *EvalContext) error {
last := len(cx.Stack) - 1 // length
prev := last - 1 // start
pprev := prev - 1 // name
name := string(cx.Stack[pprev].Bytes)
start := cx.Stack[prev].Uint
length := cx.Stack[last].Uint
err := argCheck(cx, name, basics.AddSaturate(start, length))
if err != nil {
return err
}
contents, exists, err := cx.availableBox(name, BoxReadOperation, 0)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("no such box %#x", name)
}
bytes, err := extractCarefully(contents, start, length)
cx.Stack[pprev].Bytes = bytes
cx.Stack = cx.Stack[:prev]
return err
}
func opBoxReplace(cx *EvalContext) error {
last := len(cx.Stack) - 1 // replacement
prev := last - 1 // start
pprev := prev - 1 // name
replacement := cx.Stack[last].Bytes
start := cx.Stack[prev].Uint
name := string(cx.Stack[pprev].Bytes)
err := argCheck(cx, name, basics.AddSaturate(start, uint64(len(replacement))))
if err != nil {
return err
}
contents, exists, err := cx.availableBox(name, BoxWriteOperation, 0 /* size is already known */)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("no such box %#x", name)
}
bytes, err := replaceCarefully(contents, replacement, start)
if err != nil {
return err
}
cx.Stack = cx.Stack[:pprev]
return cx.Ledger.SetBox(cx.appID, name, bytes)
}
func opBoxSplice(cx *EvalContext) error {
last := len(cx.Stack) - 1 // replacement
replacement := cx.Stack[last].Bytes
length := cx.Stack[last-1].Uint
start := cx.Stack[last-2].Uint
name := string(cx.Stack[last-3].Bytes)
err := argCheck(cx, name, 0)
if err != nil {
return err
}
contents, exists, err := cx.availableBox(name, BoxWriteOperation, 0 /* size is already known */)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("no such box %#x", name)
}
bytes, err := spliceCarefully(contents, replacement, start, length)
if err != nil {
return err
}
cx.Stack = cx.Stack[:last-3]
return cx.Ledger.SetBox(cx.appID, name, bytes)
}
func opBoxDel(cx *EvalContext) error {
last := len(cx.Stack) - 1 // name
name := string(cx.Stack[last].Bytes)
err := argCheck(cx, name, 0)
if err != nil {
return err
}
_, exists, err := cx.availableBox(name, BoxDeleteOperation, 0)
if err != nil {
return err
}
if exists {
appAddr := cx.GetApplicationAddress(cx.appID)
_, err := cx.Ledger.DelBox(cx.appID, name, appAddr)
if err != nil {
return err
}
}
cx.Stack[last] = boolToSV(exists)
return nil
}
func opBoxResize(cx *EvalContext) error {
last := len(cx.Stack) - 1 // size
prev := last - 1 // name
name := string(cx.Stack[prev].Bytes)
size := cx.Stack[last].Uint
err := argCheck(cx, name, size)
if err != nil {
return err
}
contents, exists, err := cx.availableBox(name, BoxResizeOperation, size)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("no such box %#x", name)
}
appAddr := cx.GetApplicationAddress(cx.appID)
_, err = cx.Ledger.DelBox(cx.appID, name, appAddr)
if err != nil {
return err
}
var resized []byte
if size > uint64(len(contents)) {
resized = make([]byte, size)
copy(resized, contents)
} else {
resized = contents[:size]
}
err = cx.Ledger.NewBox(cx.appID, name, resized, appAddr)
if err != nil {
return err
}
cx.Stack = cx.Stack[:prev]
return err
}
func opBoxLen(cx *EvalContext) error {
last := len(cx.Stack) - 1 // name
name := string(cx.Stack[last].Bytes)
err := argCheck(cx, name, 0)
if err != nil {
return err
}
contents, exists, err := cx.availableBox(name, BoxReadOperation, 0)
if err != nil {
return err
}
cx.Stack[last] = stackValue{Uint: uint64(len(contents))}
cx.Stack = append(cx.Stack, boolToSV(exists))
return nil
}
func opBoxGet(cx *EvalContext) error {
last := len(cx.Stack) - 1 // name
name := string(cx.Stack[last].Bytes)
err := argCheck(cx, name, 0)
if err != nil {
return err
}
contents, exists, err := cx.availableBox(name, BoxReadOperation, 0)
if err != nil {
return err
}
if !exists {
contents = []byte{}
}
cx.Stack[last].Bytes = contents // Will rightly panic if too big
cx.Stack = append(cx.Stack, boolToSV(exists))
return nil
}
func opBoxPut(cx *EvalContext) error {
last := len(cx.Stack) - 1 // value
prev := last - 1 // name
value := cx.Stack[last].Bytes
name := string(cx.Stack[prev].Bytes)
err := argCheck(cx, name, uint64(len(value)))
if err != nil {
return err
}
// This boxWrite usage requires the size, because the box may not exist.
contents, exists, err := cx.availableBox(name, BoxWriteOperation, uint64(len(value)))
if err != nil {
return err
}
cx.Stack = cx.Stack[:prev]
if exists {
/* the replacement must match existing size */
if len(contents) != len(value) {
return fmt.Errorf("attempt to box_put wrong size %d != %d", len(contents), len(value))
}
return cx.Ledger.SetBox(cx.appID, name, value)
}
/* The box did not exist, so create it. */
appAddr := cx.GetApplicationAddress(cx.appID)
return cx.Ledger.NewBox(cx.appID, name, value, appAddr)
}
// spliceCarefully is used to make a NEW byteslice copy of original, with
// replacement written over the bytes from start to start+length. Returned slice
// is always the same size as original. Zero bytes are "shifted in" or high
// bytes are "shifted out" as needed.
func spliceCarefully(original []byte, replacement []byte, start uint64, olen uint64) ([]byte, error) {
if start > uint64(len(original)) {
return nil, fmt.Errorf("replacement start %d beyond length: %d", start, len(original))
}
oend := start + olen
if oend < start {
return nil, fmt.Errorf("splice end exceeds uint64")
}
if oend > uint64(len(original)) {
return nil, fmt.Errorf("splice end %d beyond original length: %d", oend, len(original))
}
// Do NOT use the append trick to make a copy here.
// append(nil, []byte{}...) would return a nil, which means "not a bytearray" to AVM.
clone := make([]byte, len(original))
copy(clone[:start], original)
copied := copy(clone[start:], replacement)
if copied != len(replacement) {
return nil, fmt.Errorf("splice inserted bytes too long")
}
// If original is "too short" we get zeros at the end. If original is "too
// long" we lose some bytes. Fortunately, that's what we want.
copy(clone[int(start)+copied:], original[oend:])
return clone, nil
}
|