summaryrefslogtreecommitdiff
path: root/data/transactions/logic/evalAppTxn_test.go
blob: 4c9f3a4f0323f14aae6a87333a810a717294c76c (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
// Copyright (C) 2019-2022 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"
	"testing"

	"github.com/algorand/go-algorand/data/basics"

	"github.com/stretchr/testify/require"
)

func TestInnerTypesV5(t *testing.T) {
	v5, _ := makeSampleEnvWithVersion(5)
	// not alllowed in v5
	testApp(t, "itxn_begin; byte \"keyreg\"; itxn_field Type; itxn_submit; int 1;", v5, "keyreg is not a valid Type for itxn_field")
	testApp(t, "itxn_begin; int keyreg; itxn_field TypeEnum; itxn_submit; int 1;", v5, "keyreg is not a valid Type for itxn_field")
}

func TestCurrentInnerTypes(t *testing.T) {
	ep, ledger := makeSampleEnv()
	testApp(t, "itxn_submit; int 1;", ep, "itxn_submit without itxn_begin")
	testApp(t, "int pay; itxn_field TypeEnum; itxn_submit; int 1;", ep, "itxn_field without itxn_begin")
	testApp(t, "itxn_begin; itxn_submit; int 1;", ep, "unknown tx type")
	// bad type
	testApp(t, "itxn_begin; byte \"pya\"; itxn_field Type; itxn_submit; int 1;", ep, "pya is not a valid Type")
	// mixed up the int form for the byte form
	testApp(t, obfuscate("itxn_begin; int pay; itxn_field Type; itxn_submit; int 1;"), ep, "Type arg not a byte array")
	// or vice versa
	testApp(t, obfuscate("itxn_begin; byte \"pay\"; itxn_field TypeEnum; itxn_submit; int 1;"), ep, "not a uint64")

	// good types, not allowed yet
	testApp(t, "itxn_begin; byte \"appl\"; itxn_field Type; itxn_submit; int 1;", ep, "appl is not a valid Type for itxn_field")
	// same, as enums
	testApp(t, "itxn_begin; int appl; itxn_field TypeEnum; itxn_submit; int 1;", ep, "appl is not a valid Type for itxn_field")
	testApp(t, "itxn_begin; int 42; itxn_field TypeEnum; itxn_submit; int 1;", ep, "42 is not a valid TypeEnum")
	testApp(t, "itxn_begin; int 0; itxn_field TypeEnum; itxn_submit; int 1;", ep, "0 is not a valid TypeEnum")

	// "insufficient balance" because app account is charged fee
	// (defaults make these 0 pay|axfer to zero address, from app account)
	testApp(t, "itxn_begin; byte \"pay\"; itxn_field Type; itxn_submit; int 1;", ep, "insufficient balance")
	testApp(t, "itxn_begin; byte \"axfer\"; itxn_field Type; itxn_submit; int 1;", ep, "insufficient balance")
	testApp(t, "itxn_begin; int pay; itxn_field TypeEnum; itxn_submit; int 1;", ep, "insufficient balance")
	testApp(t, "itxn_begin; int axfer; itxn_field TypeEnum; itxn_submit; int 1;", ep, "insufficient balance")

	testApp(t, "itxn_begin; byte \"acfg\"; itxn_field Type; itxn_submit; int 1;", ep, "insufficient balance")
	testApp(t, "itxn_begin; byte \"afrz\"; itxn_field Type; itxn_submit; int 1;", ep, "insufficient balance")
	testApp(t, "itxn_begin; int acfg; itxn_field TypeEnum; itxn_submit; int 1;", ep, "insufficient balance")
	testApp(t, "itxn_begin; int afrz; itxn_field TypeEnum; itxn_submit; int 1;", ep, "insufficient balance")

	// alllowed since v6
	testApp(t, "itxn_begin; byte \"keyreg\"; itxn_field Type; itxn_submit; int 1;", ep, "insufficient balance")
	testApp(t, "itxn_begin; int keyreg; itxn_field TypeEnum; itxn_submit; int 1;", ep, "insufficient balance")

	// Establish 888 as the app id, and fund it.
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	ledger.NewAccount(basics.AppIndex(888).Address(), 200000)

	testApp(t, "itxn_begin; byte \"pay\"; itxn_field Type; itxn_submit; int 1;", ep)
	testApp(t, "itxn_begin; int pay; itxn_field TypeEnum; itxn_submit; int 1;", ep)
	// Can't submit because we haven't finished setup, but type passes itxn_field
	testApp(t, "itxn_begin; byte \"axfer\"; itxn_field Type; int 1;", ep)
	testApp(t, "itxn_begin; int axfer; itxn_field TypeEnum; int 1;", ep)
	testApp(t, "itxn_begin; byte \"acfg\"; itxn_field Type; int 1;", ep)
	testApp(t, "itxn_begin; int acfg; itxn_field TypeEnum; int 1;", ep)
	testApp(t, "itxn_begin; byte \"afrz\"; itxn_field Type; int 1;", ep)
	testApp(t, "itxn_begin; int afrz; itxn_field TypeEnum; int 1;", ep)
}

func TestFieldTypes(t *testing.T) {
	ep, _ := makeSampleEnv()
	testApp(t, "itxn_begin; byte \"pay\"; itxn_field Sender;", ep, "not an address")
	testApp(t, obfuscate("itxn_begin; int 7; itxn_field Receiver;"), ep, "not an address")
	testApp(t, "itxn_begin; byte \"\"; itxn_field CloseRemainderTo;", ep, "not an address")
	testApp(t, "itxn_begin; byte \"\"; itxn_field AssetSender;", ep, "not an address")
	// can't really tell if it's an addres, so 32 bytes gets further
	testApp(t, "itxn_begin; byte \"01234567890123456789012345678901\"; itxn_field AssetReceiver;",
		ep, "invalid Account reference")
	// but a b32 string rep is not an account
	testApp(t, "itxn_begin; byte \"GAYTEMZUGU3DOOBZGAYTEMZUGU3DOOBZGAYTEMZUGU3DOOBZGAYZIZD42E\"; itxn_field AssetCloseTo;",
		ep, "not an address")

	testApp(t, obfuscate("itxn_begin; byte \"pay\"; itxn_field Fee;"), ep, "not a uint64")
	testApp(t, obfuscate("itxn_begin; byte 0x01; itxn_field Amount;"), ep, "not a uint64")
	testApp(t, obfuscate("itxn_begin; byte 0x01; itxn_field XferAsset;"), ep, "not a uint64")
	testApp(t, obfuscate("itxn_begin; byte 0x01; itxn_field AssetAmount;"), ep, "not a uint64")

}

func TestAppPay(t *testing.T) {
	pay := `
  itxn_begin
  itxn_field Amount
  itxn_field Receiver
  itxn_field Sender
  int pay
  itxn_field TypeEnum
  itxn_submit
  int 1
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	testApp(t, "txn Sender; balance; int 0; ==;", ep)
	testApp(t, "txn Sender; txn Accounts 1; int 100"+pay, ep, "unauthorized")
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100"+pay, ep,
		"insufficient balance")
	ledger.NewAccount(ledger.ApplicationID().Address(), 1000000)

	// You might expect this to fail because of min balance issue
	// (receiving account only gets 100 microalgos).  It does not fail at
	// this level, instead, we must be certain that the existing min
	// balance check in eval.transaction() properly notices and fails
	// the transaction later.  This fits with the model that we check
	// min balances once at the end of each "top-level" transaction.
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100"+pay, ep)

	// 100 of 1000000 spent, plus MinTxnFee in our fake protocol is 1001
	testApp(t, "global CurrentApplicationAddress; balance; int 998899; ==", ep)
	testApp(t, "txn Receiver; balance; int 100; ==", ep)

	close := `
  itxn_begin
  int pay;      itxn_field TypeEnum
  txn Receiver; itxn_field CloseRemainderTo
  itxn_submit
  int 1
`
	testApp(t, close, ep)
	testApp(t, "global CurrentApplicationAddress; balance; !", ep)
	// Receiver got most of the algos (except 1001 for fee)
	testApp(t, "txn Receiver; balance; int 997998; ==", ep)
}

func TestAppAssetOptIn(t *testing.T) {
	ep, ledger := makeSampleEnv()
	// Establish 888 as the app id, and fund it.
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	ledger.NewAccount(basics.AppIndex(888).Address(), 200000)

	axfer := `
itxn_begin
int axfer;  itxn_field TypeEnum;
int 25;     itxn_field XferAsset;
int 2;      itxn_field AssetAmount;
txn Sender; itxn_field AssetReceiver;
itxn_submit
int 1
`
	testApp(t, axfer, ep, "invalid Asset reference")
	ep.Txn.Txn.ForeignAssets = append(ep.Txn.Txn.ForeignAssets, 25)
	testApp(t, axfer, ep, "not opted in") // app account not opted in
	optin := `
itxn_begin
int axfer; itxn_field TypeEnum;
int 25;    itxn_field XferAsset;
int 0;     itxn_field AssetAmount;
global CurrentApplicationAddress; itxn_field AssetReceiver;
itxn_submit
int 1
`
	testApp(t, optin, ep, "does not exist")
	// Asset 25
	ledger.NewAsset(ep.Txn.Txn.Sender, 25, basics.AssetParams{
		Total:     10,
		UnitName:  "x",
		AssetName: "Cross",
	})
	testApp(t, optin, ep)

	testApp(t, axfer, ep, "insufficient balance") // opted in, but balance=0

	// Fund the app account with the asset
	ledger.NewHolding(basics.AppIndex(888).Address(), 25, 5, false)
	testApp(t, axfer, ep)
	testApp(t, axfer, ep)
	testApp(t, axfer, ep, "insufficient balance") // balance = 1, tried to move 2)
	testApp(t, "global CurrentApplicationAddress; int 25; asset_holding_get AssetBalance; assert; int 1; ==", ep)

	close := `
itxn_begin
int axfer;  itxn_field TypeEnum;
int 25;     itxn_field XferAsset;
int 0;      itxn_field AssetAmount;
txn Sender; itxn_field AssetReceiver;
txn Sender; itxn_field AssetCloseTo;
itxn_submit
int 1
`
	testApp(t, close, ep)
	testApp(t, "global CurrentApplicationAddress; int 25; asset_holding_get AssetBalance; !; assert; !", ep)
}

func TestRekeyPay(t *testing.T) {
	pay := `
  itxn_begin
  itxn_field Amount
  itxn_field Receiver
  itxn_field Sender
  int pay
  itxn_field TypeEnum
  itxn_submit
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	testApp(t, "txn Sender; balance; int 0; ==;", ep)
	testApp(t, "txn Sender; txn Accounts 1; int 100"+pay, ep, "unauthorized")
	ledger.NewAccount(ep.Txn.Txn.Sender, 120+ep.Proto.MinTxnFee)
	ledger.Rekey(ep.Txn.Txn.Sender, basics.AppIndex(888).Address())
	testApp(t, "txn Sender; txn Accounts 1; int 100"+pay+"; int 1", ep)
	// Note that the Sender would fail min balance check if we did it here.
	// It seems proper to wait until end of txn though.
	// See explanation in logicLedger's Perform()
}

func TestRekeyBack(t *testing.T) {
	payAndUnkey := `
  itxn_begin
  itxn_field Amount
  itxn_field Receiver
  itxn_field Sender
  int pay
  itxn_field TypeEnum
  txn Sender
  itxn_field RekeyTo
  itxn_submit
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	testApp(t, "txn Sender; balance; int 0; ==;", ep)
	testApp(t, "txn Sender; txn Accounts 1; int 100"+payAndUnkey, ep, "unauthorized")
	ledger.NewAccount(ep.Txn.Txn.Sender, 120+3*ep.Proto.MinTxnFee)
	ledger.Rekey(ep.Txn.Txn.Sender, basics.AppIndex(888).Address())
	testApp(t, "txn Sender; txn Accounts 1; int 100"+payAndUnkey+"; int 1", ep)
	// now rekeyed back to original
	testApp(t, "txn Sender; txn Accounts 1; int 100"+payAndUnkey, ep, "unauthorized")
}

func TestDefaultSender(t *testing.T) {
	pay := `
  itxn_begin
  itxn_field Amount
  itxn_field Receiver
  int pay
  itxn_field TypeEnum
  itxn_submit
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	ep.Txn.Txn.Accounts = append(ep.Txn.Txn.Accounts, ledger.ApplicationID().Address())
	testApp(t, "txn Accounts 1; int 100"+pay, ep, "insufficient balance")
	ledger.NewAccount(ledger.ApplicationID().Address(), 1000000)
	testApp(t, "txn Accounts 1; int 100"+pay+"int 1", ep)
	testApp(t, "global CurrentApplicationAddress; balance; int 998899; ==", ep)
}

func TestAppAxfer(t *testing.T) {
	axfer := `
  itxn_begin
  int 77
  itxn_field XferAsset
  itxn_field AssetAmount
  itxn_field AssetReceiver
  itxn_field Sender
  int axfer
  itxn_field TypeEnum
  itxn_submit
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	ledger.NewAsset(ep.Txn.Txn.Receiver, 777, basics.AssetParams{}) // not in foreign-assets of sample
	ledger.NewAsset(ep.Txn.Txn.Receiver, 77, basics.AssetParams{})  // in foreign-assets of sample
	testApp(t, "txn Sender; int 777; asset_holding_get AssetBalance; assert; int 0; ==;", ep,
		"invalid Asset reference") // 777 not in foreign-assets
	testApp(t, "txn Sender; int 77; asset_holding_get AssetBalance; assert; int 0; ==;", ep,
		"assert failed") // because Sender not opted-in
	testApp(t, "global CurrentApplicationAddress; int 77; asset_holding_get AssetBalance; assert; int 0; ==;", ep,
		"assert failed") // app account not opted in

	ledger.NewAccount(ledger.ApplicationID().Address(), 10000) // plenty for fees
	ledger.NewHolding(ledger.ApplicationID().Address(), 77, 3000, false)
	testApp(t, "global CurrentApplicationAddress; int 77; asset_holding_get AssetBalance; assert; int 3000; ==;", ep)

	testApp(t, "txn Sender; txn Accounts 1; int 100"+axfer, ep, "unauthorized")
	testApp(t, "global CurrentApplicationAddress; txn Accounts 0; int 100"+axfer, ep,
		fmt.Sprintf("Receiver (%s) not opted in", ep.Txn.Txn.Sender)) // txn.Sender (receiver of the axfer) isn't opted in
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100000"+axfer, ep,
		"insufficient balance")

	// Temporarily remove from ForeignAssets to ensure App Account
	// doesn't get some sort of free pass to send arbitrary assets.
	save := ep.Txn.Txn.ForeignAssets
	ep.Txn.Txn.ForeignAssets = []basics.AssetIndex{6, 10}
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100000"+axfer, ep,
		"invalid Asset reference 77")
	ep.Txn.Txn.ForeignAssets = save

	noid := `
  itxn_begin
  itxn_field AssetAmount
  itxn_field AssetReceiver
  itxn_field Sender
  int axfer
  itxn_field TypeEnum
  itxn_submit
`
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100"+noid+"int 1", ep,
		fmt.Sprintf("Sender (%s) not opted in to 0", ledger.ApplicationID().Address()))

	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100"+axfer+"int 1", ep)

	// 100 of 3000 spent
	testApp(t, "global CurrentApplicationAddress; int 77; asset_holding_get AssetBalance; assert; int 2900; ==", ep)
	testApp(t, "txn Accounts 1; int 77; asset_holding_get AssetBalance; assert; int 100; ==", ep)
}

func TestExtraFields(t *testing.T) {
	pay := `
  itxn_begin
  int 7; itxn_field AssetAmount;
  itxn_field Amount
  itxn_field Receiver
  itxn_field Sender
  int pay
  itxn_field TypeEnum
  itxn_submit
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	testApp(t, "txn Sender; balance; int 0; ==;", ep)
	testApp(t, "txn Sender; txn Accounts 1; int 100"+pay, ep, "unauthorized")
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100"+pay, ep,
		"non-zero fields for type axfer")
}

func TestBadFieldV5(t *testing.T) {
	pay := `
  itxn_begin
  int 7; itxn_field AssetAmount;
  itxn_field Amount
  itxn_field Receiver
  itxn_field Sender
  int pay
  itxn_field TypeEnum
  txn Receiver
  itxn_field RekeyTo				// NOT ALLOWED
  itxn_submit
`

	ep, ledger := makeSampleEnvWithVersion(5)
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100"+pay, ep,
		"invalid itxn_field RekeyTo")
}

func TestBadField(t *testing.T) {
	pay := `
  itxn_begin
  int 7; itxn_field AssetAmount;
  itxn_field Amount
  itxn_field Receiver
  itxn_field Sender
  int pay
  itxn_field TypeEnum
  txn Receiver
  itxn_field RekeyTo				// ALLOWED, since v6
  int 10
  itxn_field FirstValid				// NOT ALLOWED
  itxn_submit
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	testApp(t, "global CurrentApplicationAddress; txn Accounts 1; int 100"+pay, ep,
		"invalid itxn_field FirstValid")
}

func TestNumInner(t *testing.T) {
	pay := `
  itxn_begin
  int 1
  itxn_field Amount
  txn Accounts 1
  itxn_field Receiver
  int pay
  itxn_field TypeEnum
  itxn_submit
`

	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	ledger.NewAccount(ledger.ApplicationID().Address(), 1000000)
	testApp(t, pay+";int 1", ep)
	testApp(t, pay+pay+";int 1", ep)
	testApp(t, pay+pay+pay+";int 1", ep)
	testApp(t, pay+pay+pay+pay+";int 1", ep)
	// In the sample proto, MaxInnerTransactions = 4
	testApp(t, pay+pay+pay+pay+pay+";int 1", ep, "too many inner transactions")
}

func TestAssetCreate(t *testing.T) {
	create := `
  itxn_begin
  int acfg
  itxn_field TypeEnum
  int 1000000
  itxn_field ConfigAssetTotal
  int 3
  itxn_field ConfigAssetDecimals
  byte "oz"
  itxn_field ConfigAssetUnitName
  byte "Gold"
  itxn_field ConfigAssetName
  byte "https://gold.rush/"
  itxn_field ConfigAssetURL
  itxn_submit
  int 1
`
	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	testApp(t, create, ep, "insufficient balance")
	// Give it enough for fee.  Recall that we don't check min balance at this level.
	ledger.NewAccount(ledger.ApplicationID().Address(), defaultEvalProto().MinTxnFee)
	testApp(t, create, ep)
}

func TestAssetFreeze(t *testing.T) {
	create := `
  itxn_begin
  int acfg                         ; itxn_field TypeEnum
  int 1000000                      ; itxn_field ConfigAssetTotal
  int 3                            ; itxn_field ConfigAssetDecimals
  byte "oz"                        ; itxn_field ConfigAssetUnitName
  byte "Gold"                      ; itxn_field ConfigAssetName
  byte "https://gold.rush/"        ; itxn_field ConfigAssetURL
  global CurrentApplicationAddress ; itxn_field ConfigAssetFreeze;
  itxn_submit
  itxn CreatedAssetID
  int 889
  ==
`
	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	// Give it enough for fees.  Recall that we don't check min balance at this level.
	ledger.NewAccount(ledger.ApplicationID().Address(), 12*defaultEvalProto().MinTxnFee)
	testApp(t, create, ep)

	freeze := `
  itxn_begin
  int afrz                    ; itxn_field TypeEnum
  int 889                     ; itxn_field FreezeAsset
  txn ApplicationArgs 0; btoi ; itxn_field FreezeAssetFrozen
  txn Accounts 1              ; itxn_field FreezeAssetAccount
  itxn_submit
  int 1
`
	testApp(t, freeze, ep, "invalid Asset reference")
	ep.Txn.Txn.ForeignAssets = []basics.AssetIndex{basics.AssetIndex(889)}
	ep.Txn.Txn.ApplicationArgs = [][]byte{{0x01}}
	testApp(t, freeze, ep, "does not hold Asset")
	ledger.NewHolding(ep.Txn.Txn.Receiver, 889, 55, false)
	testApp(t, freeze, ep)
	holding, err := ledger.AssetHolding(ep.Txn.Txn.Receiver, 889)
	require.NoError(t, err)
	require.Equal(t, true, holding.Frozen)
	ep.Txn.Txn.ApplicationArgs = [][]byte{{0x00}}
	testApp(t, freeze, ep)
	holding, err = ledger.AssetHolding(ep.Txn.Txn.Receiver, 889)
	require.NoError(t, err)
	require.Equal(t, false, holding.Frozen)
}

func TestFieldSetting(t *testing.T) {
	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	ledger.NewAccount(ledger.ApplicationID().Address(), 10*defaultEvalProto().MinTxnFee)
	testApp(t, "itxn_begin; int 500; bzero; itxn_field Note; int 1", ep)
	testApp(t, "itxn_begin; int 501; bzero; itxn_field Note; int 1", ep,
		"Note may not exceed")

	testApp(t, "itxn_begin; int 32; bzero; itxn_field VotePK; int 1", ep)
	testApp(t, "itxn_begin; int 31; bzero; itxn_field VotePK; int 1", ep,
		"VotePK must be 32")

	testApp(t, "itxn_begin; int 32; bzero; itxn_field SelectionPK; int 1", ep)
	testApp(t, "itxn_begin; int 33; bzero; itxn_field SelectionPK; int 1", ep,
		"SelectionPK must be 32")

	testApp(t, "itxn_begin; int 32; bzero; itxn_field RekeyTo; int 1", ep)
	testApp(t, "itxn_begin; int 31; bzero; itxn_field RekeyTo; int 1", ep,
		"not an address")

	testApp(t, "itxn_begin; int 6; bzero; itxn_field ConfigAssetUnitName; int 1", ep)
	testApp(t, "itxn_begin; int 7; bzero; itxn_field ConfigAssetUnitName; int 1", ep,
		"value is too long")

	testApp(t, "itxn_begin; int 12; bzero; itxn_field ConfigAssetName; int 1", ep)
	testApp(t, "itxn_begin; int 13; bzero; itxn_field ConfigAssetName; int 1", ep,
		"value is too long")
}

func TestInnerGroup(t *testing.T) {
	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	// Need both fees and both payments
	ledger.NewAccount(ledger.ApplicationID().Address(), 999+2*defaultEvalProto().MinTxnFee)
	pay := `
int pay;    itxn_field TypeEnum;
int 500;    itxn_field Amount;
txn Sender; itxn_field Receiver;
`
	testApp(t, "itxn_begin"+pay+"itxn_next"+pay+"itxn_submit; int 1", ep,
		"insufficient balance")

	// NewAccount overwrites the existing balance
	ledger.NewAccount(ledger.ApplicationID().Address(), 1000+2*defaultEvalProto().MinTxnFee)
	testApp(t, "itxn_begin"+pay+"itxn_next"+pay+"itxn_submit; int 1", ep)
}

func TestInnerFeePooling(t *testing.T) {
	ep, ledger := makeSampleEnv()
	ledger.NewApp(ep.Txn.Txn.Receiver, 888, basics.AppParams{})
	ledger.NewAccount(ledger.ApplicationID().Address(), 50_000)
	pay := `
int pay;    itxn_field TypeEnum;
int 500;    itxn_field Amount;
txn Sender; itxn_field Receiver;
`
	// Force the first fee to 3, but the second will default to 2*fee-3 = 2002-3
	testApp(t, "itxn_begin"+
		pay+
		"int 3; itxn_field Fee;"+
		"itxn_next"+
		pay+
		"itxn_submit; itxn Fee; int 1999; ==", ep)

	// Same first, but force the second too low
	testApp(t, "itxn_begin"+
		pay+
		"int 3; itxn_field Fee;"+
		"itxn_next"+
		pay+
		"int 1998; itxn_field Fee;"+
		"itxn_submit; int 1", ep, "fee too small")

	// Overpay in first itxn, the second will default to less
	testApp(t, "itxn_begin"+
		pay+
		"int 2000; itxn_field Fee;"+
		"itxn_next"+
		pay+
		"itxn_submit; itxn Fee; int 2; ==", ep)

	// Same first, but force the second too low
	testApp(t, "itxn_begin"+
		pay+
		"int 2000; itxn_field Fee;"+
		"itxn_next"+
		pay+
		"int 1; itxn_field Fee;"+
		"itxn_submit; itxn Fee; int 1", ep, "fee too small")
}