summaryrefslogtreecommitdiff
path: root/ledger/boxtxn_test.go
blob: 3a66583d83ec49120eb0d773c288503b48467796 (plain)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// 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 ledger

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"strings"
	"testing"
	"time"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/data/txntest"
	ledgertesting "github.com/algorand/go-algorand/ledger/testing"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

var boxAppSource = main(`
		txn ApplicationArgs 0
        byte "create"			// create box named arg[1]
        ==
		txn ApplicationArgs 0
		byte "recreate"
		==
		||
        bz del
		txn ApplicationArgs 1
		int 24
        txn NumAppArgs
        int 2
        ==
        bnz default
        pop						// get rid of 24
        txn ApplicationArgs 2
        btoi
     default:
		txn ApplicationArgs 0
		byte "recreate"
		==
		bz first
		box_create
		!
		assert
		b end
	 first:
	    box_create
        assert
        b end
     del:						// delete box arg[1]
		txn ApplicationArgs 0; byte "delete"; ==
        bz set
		txn ApplicationArgs 1
		box_del
        assert
        b end
     set:						// put arg[2] at start of box arg[1]
		txn ApplicationArgs 0; byte "set"; ==
        bz put
		txn ApplicationArgs 1
        int 0
		txn ApplicationArgs 2
		box_replace
        b end
     put:						// box_put arg[2] as replacement for box arg[1]
		txn ApplicationArgs 0; byte "put"; ==
        bz get
		txn ApplicationArgs 1
		txn ApplicationArgs 2
		box_put
        b end
     get:						// log box arg[1], after getting it with box_get
		txn ApplicationArgs 0; byte "get"; ==
        bz check
		txn ApplicationArgs 1
		box_get
        assert
        log
        b end
     check:						// fail unless arg[2] is the prefix of box arg[1]
		txn ApplicationArgs 0; byte "check"; ==
        bz bad
		txn ApplicationArgs 1
        int 0
		txn ApplicationArgs 2
        len
		box_extract
		txn ApplicationArgs 2
        ==
        assert
        b end
     bad:
        err
`)

// Call the app in txn.Applications[1] the same way I was called.
var passThruSource = main(`
  itxn_begin
  txn Applications 1; itxn_field ApplicationID
  txn TypeEnum; itxn_field TypeEnum
  // copy my app args into itxn app args (too lazy to write a loop), these are
  // always called with 2 or 3 args.
  txn ApplicationArgs 0; itxn_field ApplicationArgs
  txn ApplicationArgs 1; itxn_field ApplicationArgs
  txn NumAppArgs; int 2; ==; bnz skip
    txn ApplicationArgs 2; itxn_field ApplicationArgs
  skip:
  itxn_submit
`)

const boxVersion = 36

func boxFee(p config.ConsensusParams, nameAndValueSize uint64) uint64 {
	return p.BoxFlatMinBalance + p.BoxByteMinBalance*(nameAndValueSize)
}

// TestBoxCreate tests MBR changes around allocation, deallocation
func TestBoxCreate(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	genBalances, addrs, _ := ledgertesting.NewTestGenesis()
	ledgertesting.TestConsensusRange(t, boxVersion, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
		dl := NewDoubleLedger(t, genBalances, cv, cfg)
		defer dl.Close()

		// increment for a size 24 box with 4 letter name
		proto := config.Consensus[cv]
		mbr := boxFee(proto, 28)

		appID := dl.fundedApp(addrs[0], proto.MinBalance+3*mbr, boxAppSource)

		call := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: appID,
		}

		adam := call.Args("create", "adam")
		dl.txn(adam, fmt.Sprintf("invalid Box reference %#x", "adam"))
		adam.Boxes = []transactions.BoxRef{{Index: 0, Name: []byte("adam")}}

		vb := dl.fullBlock(adam)

		// confirm the deltas has the creation
		require.Len(t, vb.Delta().KvMods, 1)
		for _, kvDelta := range vb.Delta().KvMods { // There's only one
			require.Nil(t, kvDelta.OldData) // A creation has nil OldData
			require.Len(t, kvDelta.Data, 24)
		}

		dl.txn(adam.Args("check", "adam", "\x00\x00"))
		dl.txgroup("box_create; assert", adam.Noted("one"), adam.Noted("two"))

		bobo := call.Args("create", "bobo")
		dl.txn(bobo, fmt.Sprintf("invalid Box reference %#x", "bobo"))
		bobo.Boxes = []transactions.BoxRef{{Index: 0, Name: []byte("bobo")}}
		dl.txn(bobo)
		dl.txgroup("box_create; assert", bobo.Noted("one"), bobo.Noted("two"))

		dl.beginBlock()
		chaz := call.Args("create", "chaz")
		chaz.Boxes = []transactions.BoxRef{{Index: 0, Name: []byte("chaz")}}
		dl.txn(chaz)
		dl.txn(chaz.Noted("again"), "box_create; assert")
		dl.endBlock()

		// new block
		dl.txn(chaz.Noted("again"), "box_create; assert")
		dogg := call.Args("create", "dogg")
		dogg.Boxes = []transactions.BoxRef{{Index: 0, Name: []byte("dogg")}}
		dl.txn(dogg, "below min")
		dl.txn(chaz.Args("delete", "chaz"))
		dl.txn(chaz.Args("delete", "chaz").Noted("again"), "box_del; assert")
		dl.txn(dogg)
		dl.txn(bobo.Args("delete", "bobo"))

		// empty name is illegal
		empty := call.Args("create", "")
		dl.txn(empty, "box names may not be zero")
		// and, of course, that's true even if there's a box ref with the empty name
		empty.Boxes = []transactions.BoxRef{{}}
		dl.txn(empty, "box names may not be zero")
	})
}

// TestBoxRecreate tests behavior when box_create is called for a box that already exists
func TestBoxRecreate(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	genBalances, addrs, _ := ledgertesting.NewTestGenesis()
	ledgertesting.TestConsensusRange(t, boxVersion, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
		dl := NewDoubleLedger(t, genBalances, cv, cfg)
		defer dl.Close()

		// increment for a size 4 box with 4 letter name
		proto := config.Consensus[cv]
		mbr := boxFee(proto, 8)

		appID := dl.fundedApp(addrs[0], proto.MinBalance+mbr, boxAppSource)

		call := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: appID,
			Boxes:         []transactions.BoxRef{{Index: 0, Name: []byte("adam")}},
		}

		create := call.Args("create", "adam", "\x04") // box value size is 4 bytes
		recreate := call.Args("recreate", "adam", "\x04")

		dl.txn(recreate, "box_create; !; assert")
		dl.txn(create)
		dl.txn(recreate)
		dl.txn(call.Args("set", "adam", "\x01\x02\x03\x04"))
		dl.txn(call.Args("check", "adam", "\x01\x02\x03\x04"))
		dl.txn(recreate.Noted("again"))
		// a recreate does not change the value
		dl.txn(call.Args("check", "adam", "\x01\x02\x03\x04").Noted("after recreate"))
		// recreating with a smaller size fails
		dl.txn(call.Args("recreate", "adam", "\x03"), "box size mismatch 4 3")
		// recreating with a larger size fails
		dl.txn(call.Args("recreate", "adam", "\x05"), "box size mismatch 4 5")
		dl.txn(call.Args("check", "adam", "\x01\x02\x03\x04").Noted("after failed recreates"))

		// delete and actually create again
		dl.txn(call.Args("delete", "adam"))
		dl.txn(call.Args("create", "adam", "\x03"))

		dl.txn(call.Args("set", "adam", "\x03\x02\x01"))
		dl.txn(call.Args("check", "adam", "\x03\x02\x01"))
		dl.txn(recreate.Noted("after delete"), "box size mismatch 3 4")
		dl.txn(call.Args("recreate", "adam", "\x03"))
		dl.txn(call.Args("check", "adam", "\x03\x02\x01").Noted("after delete and recreate"))
	})
}

func TestBoxCreateAvailability(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	genBalances, addrs, _ := ledgertesting.NewTestGenesis()
	ledgertesting.TestConsensusRange(t, boxVersion, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
		dl := NewDoubleLedger(t, genBalances, cv, cfg)
		defer dl.Close()

		accessInCreate := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: 0, // This is an app-creation
			Boxes:         []transactions.BoxRef{{Index: 0, Name: []byte("hello")}},
			ApprovalProgram: `
              byte "hello"
              int 10
              box_create
`,
		}

		// We know box_create worked because this failure (checking the MBR)
		// happens at the end of the group evaluation.
		dl.txn(&accessInCreate, "balance 0 below min")

		// But let's fund it and be sure. This is "psychic". We're going to fund
		// the app address that we know the app will get. So this is a nice
		// test, but unrealistic way to actual create a box.
		psychic := basics.AppIndex(2)
		proto := config.Consensus[cv]
		if proto.AppForbidLowResources {
			psychic += 1000
		}

		dl.txn(&txntest.Txn{
			Type:     "pay",
			Sender:   addrs[0],
			Receiver: psychic.Address(),
			Amount:   proto.MinBalance + boxFee(proto, 15),
		})
		dl.txn(&accessInCreate)

		// Now, a more realistic, though tricky, way to get a box created during
		// the app's first txgroup in existence is to create it in tx0, and then
		// in tx1 fund it using an inner tx, then invoke it with an inner
		// transaction. During that invocation, the app will have access to the
		// boxes supplied as "0 refs", since they were resolved to the app ID
		// during creation.

		accessWhenCalled := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: 0, // This is a create
			Boxes:         []transactions.BoxRef{{Index: 0, Name: []byte("hello")}},
			// Note that main() wraps the program so it does not run at creation time.
			ApprovalProgram: main(`
              byte "hello"
              int 10
              box_create
              assert
              byte "we did it"
              log
`),
		}

		trampoline := dl.fundedApp(addrs[0], 1_000_000, main(`
            // Fund the app created in the txn behind me.
			txn GroupIndex
            int 1
            -
            gtxns CreatedApplicationID
            dup					// copy for use when calling
            dup					// test copy
            assert
            app_params_get AppAddress
            assert

            itxn_begin
             itxn_field Receiver
             int 500000
             itxn_field Amount
             int pay
             itxn_field TypeEnum
            itxn_submit

            // Now invoke it, so it can intialize (and create the "hello" box)
            itxn_begin
             itxn_field ApplicationID
             int appl
             itxn_field TypeEnum
            itxn_submit
`))

		call := txntest.Txn{
			Sender:        addrs[0],
			Type:          "appl",
			ApplicationID: trampoline,
		}

		payset := dl.txgroup("", &accessWhenCalled, &call)

		// Make sure that we actually did it.
		require.Equal(t, "we did it", payset[1].ApplyData.EvalDelta.InnerTxns[1].EvalDelta.Logs[0])
	})
}

// TestBoxRW tests reading writing boxes in consecutive transactions
func TestBoxRW(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	genBalances, addrs, _ := ledgertesting.NewTestGenesis()
	ledgertesting.TestConsensusRange(t, boxVersion, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
		t.Parallel()
		dl := NewDoubleLedger(t, genBalances, cv, cfg)
		defer dl.Close()

		var bufNewLogger bytes.Buffer
		log := logging.NewLogger()
		log.SetOutput(&bufNewLogger)

		appID := dl.fundedApp(addrs[0], 1_000_000, boxAppSource)
		call := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: appID,
			Boxes:         []transactions.BoxRef{{Index: 0, Name: []byte("x")}},
		}

		dl.txn(call.Args("create", "x", "\x10"))                // 16
		vb := dl.fullBlock(call.Args("set", "x", "ABCDEFGHIJ")) // 10 long
		// confirm the deltas has the change, including the old value
		require.Len(t, vb.Delta().KvMods, 1)
		for _, kvDelta := range vb.Delta().KvMods { // There's only one
			require.Equal(t, kvDelta.OldData,
				[]byte(strings.Repeat("\x00", 16)))
			require.Equal(t, kvDelta.Data,
				[]byte("ABCDEFGHIJ\x00\x00\x00\x00\x00\x00"))
		}

		dl.txn(call.Args("check", "x", "ABCDE"))
		dl.txn(call.Args("check", "x", "ABCDEFGHIJ"))
		dl.txn(call.Args("check", "x", "ABCDEFGHIJ\x00"))

		dl.txn(call.Args("delete", "x"))
		dl.txn(call.Args("check", "x", "ABC"), "no such box")
		dl.txn(call.Args("create", "x", "\x08"))
		dl.txn(call.Args("check", "x", "\x00")) // it was cleared
		dl.txn(call.Args("set", "x", "ABCDEFGHIJ"), "replacement end 10")
		dl.txn(call.Args("check", "x", "\x00")) // still clear
		dl.txn(call.Args("set", "x", "ABCDEFGH"))
		dl.txn(call.Args("check", "x", "ABCDEFGH\x00"), "extraction end 9")
		dl.txn(call.Args("check", "x", "ABCDEFGH"))
		dl.txn(call.Args("set", "x", "ABCDEFGHI"), "replacement end 9")

		// Advance more than 320 rounds, ensure box is still there
		for i := 0; i < 330; i++ {
			dl.fullBlock()
		}
		time.Sleep(5 * time.Second) // balancesFlushInterval, so commit happens
		dl.fullBlock(call.Args("check", "x", "ABCDEFGH"))
		time.Sleep(100 * time.Millisecond) // give commit time to run, and prune au caches
		dl.fullBlock(call.Args("check", "x", "ABCDEFGH"))

		dl.txn(call.Args("create", "yy"), fmt.Sprintf("invalid Box reference %#x", "yy"))
		withBr := call.Args("create", "yy")
		withBr.Boxes = append(withBr.Boxes, transactions.BoxRef{Index: 1, Name: []byte("yy")})
		require.Error(dl.t, withBr.Txn().WellFormed(transactions.SpecialAddresses{}, dl.generator.GenesisProto()))
		withBr.Boxes[1].Index = 0
		dl.txn(withBr)
	})
}

// TestBoxAccountData tests that an account's data changes when boxes are created
func TestBoxAccountData(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	uint64ToArgStr := func(i uint64) string {
		encoded := make([]byte, 8)
		binary.BigEndian.PutUint64(encoded, i)
		return string(encoded)
	}

	genBalances, addrs, _ := ledgertesting.NewTestGenesis()
	ledgertesting.TestConsensusRange(t, boxVersion, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
		dl := NewDoubleLedger(t, genBalances, cv, cfg)
		defer dl.Close()

		proto := config.Consensus[cv]

		var bufNewLogger bytes.Buffer
		log := logging.NewLogger()
		log.SetOutput(&bufNewLogger)

		appID := dl.fundedApp(addrs[0], 1_000_000, boxAppSource)
		call := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: appID,
			Boxes:         []transactions.BoxRef{{Index: 0, Name: []byte("x")}, {Index: 0, Name: []byte("y")}},
		}

		verifyAppSrc := main(`
txn ApplicationArgs 0
btoi
txn Accounts 1
acct_params_get AcctMinBalance
assert
==
assert

txn ApplicationArgs 1
btoi
txn Accounts 1
acct_params_get AcctTotalBoxes
assert
==
assert

txn ApplicationArgs 2
btoi
txn Accounts 1
acct_params_get AcctTotalBoxBytes
assert
==
assert
`)
		verifyAppID := dl.fundedApp(addrs[0], 0, verifyAppSrc)
		verifyAppCall := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: verifyAppID,
			Accounts:      []basics.Address{appID.Address()},
		}

		// The app account has no box data initially
		dl.txn(verifyAppCall.Args(uint64ToArgStr(proto.MinBalance), "\x00", "\x00"))

		dl.txn(call.Args("create", "x", "\x10")) // 16

		// It gets updated when a new box is created
		dl.txn(verifyAppCall.Args(uint64ToArgStr(proto.MinBalance+proto.BoxFlatMinBalance+17*proto.BoxByteMinBalance), "\x01", "\x11"))

		dl.txn(call.Args("create", "y", "\x05"))

		// And again
		dl.txn(verifyAppCall.Args(uint64ToArgStr(proto.MinBalance+2*proto.BoxFlatMinBalance+23*proto.BoxByteMinBalance), "\x02", "\x17"))

		// Advance more than 320 rounds, ensure box is still there
		for i := 0; i < 330; i++ {
			dl.fullBlock()
		}
		time.Sleep(5 * time.Second) // balancesFlushInterval, so commit happens
		dl.fullBlock(call.Args("check", "x", string(make([]byte, 16))))
		time.Sleep(100 * time.Millisecond) // give commit time to run, and prune au caches
		dl.fullBlock(call.Args("check", "x", string(make([]byte, 16))))

		// Still the same after caches are flushed
		dl.txn(verifyAppCall.Args(uint64ToArgStr(proto.MinBalance+2*proto.BoxFlatMinBalance+23*proto.BoxByteMinBalance), "\x02", "\x17"))

		dl.txns(call.Args("delete", "x"), call.Args("delete", "y"))

		// Data gets removed after boxes are deleted
		dl.txn(verifyAppCall.Args(uint64ToArgStr(proto.MinBalance), "\x00", "\x00"))
	})
}

func TestBoxIOBudgets(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	genBalances, addrs, _ := ledgertesting.NewTestGenesis()
	ledgertesting.TestConsensusRange(t, boxVersion, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
		dl := NewDoubleLedger(t, genBalances, cv, cfg)
		defer dl.Close()

		appID := dl.fundedApp(addrs[0], 0, boxAppSource)
		call := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: appID,
			Boxes:         []transactions.BoxRef{{Index: 0, Name: []byte("x")}},
		}
		dl.txn(call.Args("create", "x", "\x10\x00"), // 4096
			"write budget (1024) exceeded")
		call.Boxes = append(call.Boxes, transactions.BoxRef{})
		dl.txn(call.Args("create", "x", "\x10\x00"), // 4096
			"write budget (2048) exceeded")
		call.Boxes = append(call.Boxes, transactions.BoxRef{})
		dl.txn(call.Args("create", "x", "\x10\x00"), // 4096
			"write budget (3072) exceeded")
		call.Boxes = append(call.Boxes, transactions.BoxRef{})
		dl.txn(call.Args("create", "x", "\x10\x00"), // now there are 4 box refs
			"below min") // big box would need more balance
		dl.txn(call.Args("create", "x", "\x10\x01"), // 4097
			"write budget (4096) exceeded")

		// Create 4,096 byte box
		proto := config.Consensus[cv]
		fundApp := txntest.Txn{
			Type:     "pay",
			Sender:   addrs[0],
			Receiver: appID.Address(),
			Amount:   proto.MinBalance + boxFee(proto, 4096+1), // remember key len!
		}
		create := call.Args("create", "x", "\x10\x00")

		// Slight detour - Prove insufficient funding fails creation.
		fundApp.Amount--
		dl.txgroup("below min", &fundApp, create)
		fundApp.Amount++

		// Confirm desired creation happens.
		dl.txgroup("", &fundApp, create)

		// Now that we've created a 4,096 byte box, test READ budget
		// It works at the start, because call still has 4 brs.
		dl.txn(call.Args("check", "x", "\x00"))
		call.Boxes = call.Boxes[:3]
		dl.txn(call.Args("check", "x", "\x00"),
			"box read budget (3072) exceeded")

		// Give a budget over 32768, confirm failure anyway
		empties := [32]transactions.BoxRef{}
		// These tests skip WellFormed, so the huge Boxes is ok
		call.Boxes = append(call.Boxes, empties[:]...)
		dl.txn(call.Args("create", "x", "\x80\x01"), "box size too large") // 32769
	})
}

// TestBoxInners trys various box manipulations through inner transactions
func TestBoxInners(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	genBalances, addrs, _ := ledgertesting.NewTestGenesis()
	ledgertesting.TestConsensusRange(t, boxVersion, 0, func(t *testing.T, ver int, cv protocol.ConsensusVersion, cfg config.Local) {
		dl := NewDoubleLedger(t, genBalances, cv, cfg)
		defer dl.Close()

		// Advance the creatable counter, so we don't have very low app ids that
		// could be mistaken for indices into ForeignApps.
		dl.txn(&txntest.Txn{Type: "pay", Sender: addrs[0], Receiver: addrs[0]})
		dl.txn(&txntest.Txn{Type: "pay", Sender: addrs[0], Receiver: addrs[0]})
		dl.txn(&txntest.Txn{Type: "pay", Sender: addrs[0], Receiver: addrs[0]})
		dl.txn(&txntest.Txn{Type: "pay", Sender: addrs[0], Receiver: addrs[0]})

		boxID := dl.fundedApp(addrs[0], 2_000_000, boxAppSource)  // there are some big boxes made
		passID := dl.fundedApp(addrs[0], 120_000, passThruSource) // lowish, show it's not paying for boxes
		call := txntest.Txn{
			Type:          "appl",
			Sender:        addrs[0],
			ApplicationID: passID,
			ForeignApps:   []basics.AppIndex{boxID},
			Boxes:         []transactions.BoxRef{{Index: 0, Name: []byte("x")}},
		}
		// The current Boxes gives top-level access to "x", not the inner app
		dl.txn(call.Args("create", "x", "\x10"), // 8
			fmt.Sprintf("invalid Box reference %#x", 'x'))

		// This isn't right: Index should be index into ForeignApps
		call.Boxes = []transactions.BoxRef{{Index: uint64(boxID), Name: []byte("x")}}
		require.Error(t, call.Txn().WellFormed(transactions.SpecialAddresses{}, dl.generator.genesisProto))

		call.Boxes = []transactions.BoxRef{{Index: 1, Name: []byte("x")}}
		dl.txn(call.Args("create", "x", "\x10\x00"), // 4096
			"write budget (1024) exceeded")
		dl.txn(call.Args("create", "x", "\x04\x00")) // 1024
		call.Boxes = append(call.Boxes, transactions.BoxRef{Index: 1, Name: []byte("y")})
		dl.txn(call.Args("create", "y", "\x08\x00")) // 2048

		require.Len(t, call.Boxes, 2)
		setX := call.Args("set", "x", "A")
		dl.txn(setX, "read budget") // Boxes has x and y, their combined length is too big
		setX.Boxes = []transactions.BoxRef{{Index: 1, Name: []byte("x")}}
		dl.txn(setX)

		setY := call.Args("set", "y", "B")
		dl.txn(setY, "read budget") // Boxes has x and y, their combined length is too big
		setY.Boxes = []transactions.BoxRef{{Index: 1, Name: []byte("y")}}
		dl.txn(setY, "read budget") // Y is bigger needs more than 1 br
		// We recommend "empty" br, but a duplicate is also ok
		setY.Boxes = append(setY.Boxes, transactions.BoxRef{Index: 1, Name: []byte("y")})
		dl.txn(setY) // len(y) = 2048, io budget is 2*1024 right now

		// non-existent box also works
		setY.Boxes = []transactions.BoxRef{{Index: 1, Name: []byte("y")}, {Index: 0, Name: []byte("nope")}}
		dl.txn(setY) // len(y) = 2048, io budget is 2*1024 right now

		// now show can read both boxes based on brs in tx1
		checkX := call.Args("check", "x", "A")
		checkX.Boxes = nil
		checkY := call.Args("check", "y", "B")
		require.Len(t, checkY.Boxes, 2)
		// can't see x and y because read budget is only 2*1024
		dl.txgroup("box read budget", checkX, checkY)
		checkY.Boxes = append(checkY.Boxes, transactions.BoxRef{})
		dl.txgroup("", checkX, checkY)

		require.Len(t, setY.Boxes, 2) // recall that setY has ("y", "nope") right now. no "x"
		dl.txgroup(fmt.Sprintf("invalid Box reference %#x", 'x'), checkX, setY)

		setY.Boxes = append(setY.Boxes, transactions.BoxRef{Index: 1, Name: []byte("x")})
		dl.txgroup("", checkX, setY)

		// Cleanup
		dl.txn(call.Args("del", "x"), "read budget")
		dl.txn(call.Args("del", "y"), "read budget")
		// surprising but correct: they work when combined, because both txns
		// have both box refs, so the read budget goes up.
		dl.txgroup("", call.Args("delete", "x"), call.Args("delete", "y"))

		// Try some get/put action
		dl.txn(call.Args("put", "x", "john doe"))
		tib := dl.txn(call.Args("get", "x"))
		// we are passing this thru to the underlying box app which logs the get
		require.Equal(t, "john doe", tib.ApplyData.EvalDelta.InnerTxns[0].EvalDelta.Logs[0])
		dl.txn(call.Args("check", "x", "john"))

		// bad change because of length
		dl.txn(call.Args("put", "x", "steve doe"), "box_put wrong size")
		tib = dl.txn(call.Args("get", "x"))
		require.Equal(t, "john doe", tib.ApplyData.EvalDelta.InnerTxns[0].EvalDelta.Logs[0])

		// good change
		dl.txn(call.Args("put", "x", "mark doe"))
		dl.txn(call.Args("check", "x", "mark d"))
	})
}