summaryrefslogtreecommitdiff
path: root/data/transactions/logic/ledger_test.go
blob: 0254a4467823b5db192ff244decf35c1d6da5673 (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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
// Copyright (C) 2019-2024 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

/* This Ledger implements LedgerForLogic for unit tests in the logic package. It
   does *not* carry the protocol around, so it does *not* enforce the various
   limits imposed there.  This helps ensure that the logic package itself
   enforces those limits, rather than rely on the ledger package. (Which should
   also do so, to be defensive.)

   This Ledger is not clever enough to have a good mechanism for making changes
   and rolling them back if the program that makes them fails. It just has a
   Reset() method that throws away all changes made by programs.  Generally,
   it's probably best to call Reset() after any error test, though you can keep
   testing if you take into account that changes made before the failure will
   take effect.
*/

import (
	"errors"
	"fmt"
	"math"
	"math/rand"

	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/data/bookkeeping"
	"github.com/algorand/go-algorand/data/committee"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/ledger/ledgercore"
	"github.com/algorand/go-algorand/protocol"
)

type balanceRecord struct {
	addr     basics.Address
	auth     basics.Address
	balance  uint64
	locals   map[basics.AppIndex]basics.TealKeyValue
	holdings map[basics.AssetIndex]basics.AssetHolding
	mods     map[basics.AppIndex]map[string]basics.ValueDelta
}

func newBalanceRecord(addr basics.Address, balance uint64) balanceRecord {
	return balanceRecord{
		addr:     addr,
		balance:  balance,
		locals:   make(map[basics.AppIndex]basics.TealKeyValue),
		holdings: make(map[basics.AssetIndex]basics.AssetHolding),
		mods:     make(map[basics.AppIndex]map[string]basics.ValueDelta),
	}
}

// In our test ledger, we don't store the creatables with their
// creators, so we need to carry the creator around with them.
type appParams struct {
	basics.AppParams
	Creator basics.Address

	boxes   map[string][]byte // will never contain a nil slice
	boxMods map[string][]byte // nil slice indicates a deletion
}

type asaParams struct {
	basics.AssetParams
	Creator basics.Address
}

// Ledger is a fake ledger that is "good enough" to reasonably test AVM programs.
type Ledger struct {
	balances     map[basics.Address]balanceRecord
	applications map[basics.AppIndex]appParams
	assets       map[basics.AssetIndex]asaParams
	mods         map[basics.AppIndex]map[string]basics.ValueDelta
	rnd          basics.Round
}

// NewLedger constructs a Ledger with the given balances.
func NewLedger(balances map[basics.Address]uint64) *Ledger {
	l := new(Ledger)
	l.balances = make(map[basics.Address]balanceRecord)
	for addr, balance := range balances {
		l.NewAccount(addr, balance)
	}
	l.applications = make(map[basics.AppIndex]appParams)
	l.assets = make(map[basics.AssetIndex]asaParams)
	l.mods = make(map[basics.AppIndex]map[string]basics.ValueDelta)
	return l
}

// Reset removes all of the mods created by previous AVM execution
func (l *Ledger) Reset() {
	l.mods = make(map[basics.AppIndex]map[string]basics.ValueDelta)
	for addr, br := range l.balances {
		br.mods = make(map[basics.AppIndex]map[string]basics.ValueDelta)
		l.balances[addr] = br
	}
	for id, app := range l.applications {
		app.boxMods = nil
		l.applications[id] = app
	}
}

// NewAccount adds a new account with a given balance to the Ledger.
func (l *Ledger) NewAccount(addr basics.Address, balance uint64) {
	l.balances[addr] = newBalanceRecord(addr, balance)
}

// NewApp add a new AVM app to the Ledger.  In most uses, it only sets up the id
// and schema but no code, as testing will want to try many different code
// sequences.
func (l *Ledger) NewApp(creator basics.Address, appID basics.AppIndex, params basics.AppParams) {
	params = params.Clone()
	if params.GlobalState == nil {
		params.GlobalState = make(basics.TealKeyValue)
	}
	l.applications[appID] = appParams{
		Creator:   creator,
		AppParams: params,
	}
}

// NewAsset adds an asset with the given id and params to the ledger.
func (l *Ledger) NewAsset(creator basics.Address, assetID basics.AssetIndex, params basics.AssetParams) {
	l.assets[assetID] = asaParams{
		Creator:     creator,
		AssetParams: params,
	}
	br, ok := l.balances[creator]
	if !ok {
		br = newBalanceRecord(creator, 0)
	}
	br.holdings[assetID] = basics.AssetHolding{Amount: params.Total, Frozen: params.DefaultFrozen}
	l.balances[creator] = br
}

const firstTestID = 5000

// Counter implements LedgerForLogic, but it not really a txn counter, but is
// sufficient for the logic package.
func (l *Ledger) Counter() uint64 {
	for try := firstTestID; true; try++ {
		if _, ok := l.assets[basics.AssetIndex(try)]; ok {
			continue
		}
		if _, ok := l.applications[basics.AppIndex(try)]; ok {
			continue
		}
		return uint64(try)
	}
	panic("wow")
}

// NewHolding sets the ASA balance of a given account.
func (l *Ledger) NewHolding(addr basics.Address, assetID uint64, amount uint64, frozen bool) {
	br, ok := l.balances[addr]
	if !ok {
		br = newBalanceRecord(addr, 0)
	}
	br.holdings[basics.AssetIndex(assetID)] = basics.AssetHolding{Amount: amount, Frozen: frozen}
	l.balances[addr] = br
}

// NewLocals essentially "opts in" an address to an app id.
func (l *Ledger) NewLocals(addr basics.Address, appID uint64) {
	if _, ok := l.balances[addr]; !ok {
		l.balances[addr] = newBalanceRecord(addr, 0)
	}
	l.balances[addr].locals[basics.AppIndex(appID)] = basics.TealKeyValue{}
}

// NewLocal sets a local value of an app on an address
func (l *Ledger) NewLocal(addr basics.Address, appID uint64, key string, value basics.TealValue) {
	l.balances[addr].locals[basics.AppIndex(appID)][key] = value
}

// NoLocal removes a key from an address locals for an app.
func (l *Ledger) NoLocal(addr basics.Address, appID uint64, key string) {
	delete(l.balances[addr].locals[basics.AppIndex(appID)], key)
}

// NewGlobal sets a global value for an app
func (l *Ledger) NewGlobal(appID uint64, key string, value basics.TealValue) {
	l.applications[basics.AppIndex(appID)].GlobalState[key] = value
}

// NoGlobal removes a global key for an app
func (l *Ledger) NoGlobal(appID uint64, key string) {
	delete(l.applications[basics.AppIndex(appID)].GlobalState, key)
}

// Rekey sets the authAddr for an address.
func (l *Ledger) Rekey(addr basics.Address, auth basics.Address) {
	if br, ok := l.balances[addr]; ok {
		br.auth = auth
		l.balances[addr] = br
	}
}

// LatestTimestamp gives a uint64, chosen randomly.  It should
// probably increase monotonically, but no tests care yet.
func (l *Ledger) PrevTimestamp() int64 {
	return int64(rand.Uint32() + 1)
}

// BlockHdr returns the block header for the given round, if it is available
func (l *Ledger) BlockHdr(round basics.Round) (bookkeeping.BlockHeader, error) {
	hdr := bookkeeping.BlockHeader{}
	// Return a fake seed that is different for each round
	seed := committee.Seed{}
	seed[0] = byte(round)
	seed[1] = byte(round >> 8)
	seed[2] = byte(round >> 16)
	seed[3] = byte(round >> 24)
	seed[4] = byte(round >> 32)
	seed[5] = byte(round >> 40)
	seed[6] = byte(round >> 48)
	seed[7] = byte(round >> 56)
	hdr.Seed = seed
	hdr.TimeStamp = 100 + (9 * int64(round) / 2)
	return hdr, nil
	// perhaps should add an error when requesting old round for better testing
}

// AccountData returns a version of the account that is good enough for
// satisfying AVM needs. (balance, calc minbalance, and authaddr)
func (l *Ledger) AccountData(addr basics.Address) (ledgercore.AccountData, error) {
	br := l.balances[addr]
	// br may come back empty if addr doesn't exist.  That's fine for our needs.
	assets := make(map[basics.AssetIndex]basics.AssetParams)
	for a, p := range l.assets {
		if p.Creator == addr {
			assets[a] = p.AssetParams
		}
	}

	schemaTotal := basics.StateSchema{}
	pagesTotal := uint32(0)

	boxesTotal := 0
	boxBytesTotal := 0

	apps := make(map[basics.AppIndex]basics.AppParams)
	for a, p := range l.applications {
		if p.Creator == addr {
			apps[a] = p.AppParams
			schemaTotal = schemaTotal.AddSchema(p.GlobalStateSchema)
			pagesTotal += p.ExtraProgramPages
		}
		if a.Address() == addr {
			// We found the app that corresponds to this app account. Get box info from there.
			boxesTotal = len(p.boxes)
			for k, v := range p.boxes {
				boxBytesTotal += len(k) + len(v)
			}
			for k, v := range p.boxMods {
				base, ok := p.boxes[k]
				if ok {
					if v == nil {
						// deleted, so remove from totals
						boxesTotal--
						boxBytesTotal -= len(k) + len(base)
						continue
					}
					if len(v) != len(base) {
						panic(fmt.Sprintf("mismatch %v %v", v, base))
					}
					continue
				}
				// fresh box in mods, count it
				boxesTotal++
				boxBytesTotal += len(k) + len(v)
			}
		}
	}

	locals := map[basics.AppIndex]basics.AppLocalState{}
	for a := range br.locals {
		locals[a] = basics.AppLocalState{} // No need to fill in
		schemaTotal = schemaTotal.AddSchema(l.applications[a].LocalStateSchema)
	}

	return ledgercore.AccountData{
		AccountBaseData: ledgercore.AccountBaseData{
			MicroAlgos:          basics.MicroAlgos{Raw: br.balance},
			AuthAddr:            br.auth,
			TotalAppSchema:      schemaTotal,
			TotalExtraAppPages:  pagesTotal,
			TotalAppParams:      uint64(len(apps)),
			TotalAppLocalStates: uint64(len(locals)),
			TotalAssetParams:    uint64(len(assets)),
			TotalAssets:         uint64(len(br.holdings)),

			TotalBoxes:    uint64(boxesTotal),
			TotalBoxBytes: uint64(boxBytesTotal),
		},
	}, nil
}

// Authorizer returns the address that must authorize txns from a
// given address.  It's either the address itself, or the value it has
// been rekeyed to.
func (l *Ledger) Authorizer(addr basics.Address) (basics.Address, error) {
	br, ok := l.balances[addr]
	if !ok {
		return addr, nil // Not rekeyed if not present
	}
	if !br.auth.IsZero() {
		return br.auth, nil
	}
	return br.addr, nil
}

// GetGlobal returns the current value of a global in an app, taking
// into account the mods created by earlier teal execution.
func (l *Ledger) GetGlobal(appIdx basics.AppIndex, key string) (basics.TealValue, bool, error) {
	params, ok := l.applications[appIdx]
	if !ok {
		return basics.TealValue{}, false, fmt.Errorf("no app %d", appIdx)
	}

	// return most recent value if available
	tkvm, ok := l.mods[appIdx]
	if ok {
		val, ok := tkvm[key]
		if ok {
			tv, ok := val.ToTealValue()
			return tv, ok, nil
		}
	}

	// otherwise return original one
	val, ok := params.GlobalState[key]
	return val, ok, nil
}

// SetGlobal "sets" a global, but only through the mods mechanism, so
// it can be removed with Reset()
func (l *Ledger) SetGlobal(appIdx basics.AppIndex, key string, value basics.TealValue) error {
	params, ok := l.applications[appIdx]
	if !ok {
		return fmt.Errorf("no app %d", appIdx)
	}

	// if writing the same value, return
	// this simulates real ledger behavior for tests
	val, ok := params.GlobalState[key]
	if ok && val == value {
		return nil
	}

	// write to deltas
	_, ok = l.mods[appIdx]
	if !ok {
		l.mods[appIdx] = make(map[string]basics.ValueDelta)
	}
	l.mods[appIdx][key] = value.ToValueDelta()
	return nil
}

// DelGlobal "deletes" a global, but only through the mods mechanism, so
// the deletion can be Reset()
func (l *Ledger) DelGlobal(appIdx basics.AppIndex, key string) error {
	params, ok := l.applications[appIdx]
	if !ok {
		return fmt.Errorf("no app %d", appIdx)
	}

	exist := false
	if _, ok := params.GlobalState[key]; ok {
		exist = true
	}

	_, ok = l.mods[appIdx]
	if !ok && !exist {
		// nothing to delete
		return nil
	}
	if !ok {
		l.mods[appIdx] = make(map[string]basics.ValueDelta)
	}
	_, ok = l.mods[appIdx][key]
	if ok || exist {
		l.mods[appIdx][key] = basics.ValueDelta{Action: basics.DeleteAction}
	}
	return nil
}

// NewBox makes a new box, through the boxMods mechanism. It can be Reset()
func (l *Ledger) NewBox(appIdx basics.AppIndex, key string, value []byte, appAddr basics.Address) error {
	if appIdx.Address() != appAddr {
		panic(fmt.Sprintf("%d %v %v", appIdx, appIdx.Address(), appAddr))
	}
	params, ok := l.applications[appIdx]
	if !ok {
		return fmt.Errorf("no app %d", appIdx)
	}
	if params.boxMods == nil {
		params.boxMods = make(map[string][]byte)
	}
	if current, ok := params.boxMods[key]; ok {
		if current != nil {
			return fmt.Errorf("attempt to recreate box %#v", key)
		}
	} else if _, ok := params.boxes[key]; ok {
		return fmt.Errorf("attempt to recreate box %#x", key)
	}
	params.boxMods[key] = value
	l.applications[appIdx] = params
	return nil
}

func (l *Ledger) GetBox(appIdx basics.AppIndex, key string) ([]byte, bool, error) {
	params, ok := l.applications[appIdx]
	if !ok {
		return nil, false, nil
	}
	if params.boxMods != nil {
		if ps, ok := params.boxMods[key]; ok {
			if ps == nil { // deletion in mod
				return nil, false, nil
			}
			return ps, true, nil
		}
	}
	if params.boxes == nil {
		return nil, false, nil
	}
	box, ok := params.boxes[key]
	return box, ok, nil
}

// SetBox set a box value through the boxMods mechanism. It can be Reset()
func (l *Ledger) SetBox(appIdx basics.AppIndex, key string, value []byte) error {
	current, ok, err := l.GetBox(appIdx, key)
	if err != nil {
		return err
	}
	if !ok {
		return fmt.Errorf("no box %d", appIdx)
	}
	params := l.applications[appIdx] // assured, based on above
	if params.boxMods == nil {
		params.boxMods = make(map[string][]byte)
	}
	if len(current) != len(value) {
		return fmt.Errorf("wrong box size %#v %d != %d", key, len(current), len(value))
	}
	params.boxMods[key] = value
	return nil
}

// DelBox deletes a value through boxMods mechanism
func (l *Ledger) DelBox(appIdx basics.AppIndex, key string, appAddr basics.Address) (bool, error) {
	if appIdx.Address() != appAddr {
		panic(fmt.Sprintf("%d %v %v", appIdx, appIdx.Address(), appAddr))
	}
	_, ok, err := l.GetBox(appIdx, key)
	if err != nil {
		return false, err
	}
	if !ok {
		return false, nil
	}
	params := l.applications[appIdx] // assured, based on above
	if params.boxMods == nil {
		params.boxMods = make(map[string][]byte)
	}
	params.boxMods[key] = nil
	return true, nil
}

// GetLocal returns the current value bound to a local key, taking
// into account mods caused by earlier executions.
func (l *Ledger) GetLocal(addr basics.Address, appIdx basics.AppIndex, key string, accountIdx uint64) (basics.TealValue, bool, error) {
	br, ok := l.balances[addr]
	if !ok {
		return basics.TealValue{}, false, fmt.Errorf("no account: %s", addr)
	}
	tkvd, ok := br.locals[appIdx]
	if !ok {
		return basics.TealValue{}, false, fmt.Errorf("account %s is not opted into %d", addr, appIdx)
	}

	// check deltas first
	tkvm, ok := br.mods[appIdx]
	if ok {
		val, ok := tkvm[key]
		if ok {
			tv, ok := val.ToTealValue()
			return tv, ok, nil
		}
	}

	val, ok := tkvd[key]
	return val, ok, nil
}

// SetLocal "sets" the current value bound to a local key using the
// mods mechanism, so it can be Reset()
func (l *Ledger) SetLocal(addr basics.Address, appIdx basics.AppIndex, key string, value basics.TealValue, accountIdx uint64) error {
	br, ok := l.balances[addr]
	if !ok {
		return fmt.Errorf("no account: %s", addr)
	}
	tkv, ok := br.locals[appIdx]
	if !ok {
		return fmt.Errorf("account %s is not opted into %d", addr, appIdx)
	}

	// if writing the same value, return
	// this simulates real ledger behavior for tests
	val, ok := tkv[key]
	if ok && val == value {
		return nil
	}

	// write to deltas
	_, ok = br.mods[appIdx]
	if !ok {
		br.mods[appIdx] = make(map[string]basics.ValueDelta)
	}
	br.mods[appIdx][key] = value.ToValueDelta()
	return nil
}

// DelLocal "deletes" the current value bound to a local key using the
// mods mechanism, so it can be Reset()
func (l *Ledger) DelLocal(addr basics.Address, appIdx basics.AppIndex, key string, accountIdx uint64) error {
	br, ok := l.balances[addr]
	if !ok {
		return fmt.Errorf("no account: %s", addr)
	}
	tkv, ok := br.locals[appIdx]
	if !ok {
		return fmt.Errorf("account %s is not opted into %d", addr, appIdx)
	}
	exist := false
	if _, ok := tkv[key]; ok {
		exist = true
	}

	_, ok = br.mods[appIdx]
	if !ok && !exist {
		// nothing to delete
		return nil
	}
	if !ok {
		br.mods[appIdx] = make(map[string]basics.ValueDelta)
	}
	_, ok = br.mods[appIdx][key]
	if ok || exist {
		br.mods[appIdx][key] = basics.ValueDelta{Action: basics.DeleteAction}
	}
	return nil
}

// OptedIn returns whether an Address has opted into the app (usually
// from NewLocals, but potentially from executing AVM inner
// transactions.
func (l *Ledger) OptedIn(addr basics.Address, appIdx basics.AppIndex) (bool, error) {
	br, ok := l.balances[addr]
	if !ok {
		return false, fmt.Errorf("no account: %s", addr)
	}
	_, ok = br.locals[appIdx]
	return ok, nil
}

// AssetHolding gives the amount of an ASA held by an account, or
// error if the account is not opted into the asset.
func (l *Ledger) AssetHolding(addr basics.Address, assetID basics.AssetIndex) (basics.AssetHolding, error) {
	if br, ok := l.balances[addr]; ok {
		if asset, ok := br.holdings[assetID]; ok {
			return asset, nil
		}
		return basics.AssetHolding{}, fmt.Errorf("no asset %d for account %s", assetID, addr)
	}
	return basics.AssetHolding{}, fmt.Errorf("no account: %s", addr)
}

// AssetParams gives the parameters of an ASA if it exists
func (l *Ledger) AssetParams(assetID basics.AssetIndex) (basics.AssetParams, basics.Address, error) {
	if asset, ok := l.assets[assetID]; ok {
		return asset.AssetParams, asset.Creator, nil
	}
	return basics.AssetParams{}, basics.Address{}, fmt.Errorf("no asset %d", assetID)
}

// AppParams gives the parameters of an App if it exists
func (l *Ledger) AppParams(appID basics.AppIndex) (basics.AppParams, basics.Address, error) {
	if app, ok := l.applications[appID]; ok {
		return app.AppParams, app.Creator, nil
	}
	return basics.AppParams{}, basics.Address{}, fmt.Errorf("no app %d", appID)
}

var testGenHash = crypto.Digest{0x03, 0x02, 0x03}

// GenesisHash returns a phony genesis hash that can be tested against
func (l *Ledger) GenesisHash() crypto.Digest {
	return testGenHash
}

func (l *Ledger) move(from basics.Address, to basics.Address, amount uint64) error {
	fbr, ok := l.balances[from]
	if !ok {
		fbr = newBalanceRecord(from, 0)
	}
	tbr, ok := l.balances[to]
	if !ok {
		tbr = newBalanceRecord(to, 0)
	}
	if fbr.balance < amount {
		return fmt.Errorf("insufficient balance in %v. %d < %d", from, fbr.balance, amount)
	}
	fbr.balance -= amount
	tbr.balance += amount
	// We do not check min balances yet. They are checked when txn is complete.
	l.balances[from] = fbr
	l.balances[to] = tbr
	return nil
}

func (l *Ledger) rekey(tx *transactions.Transaction) error {
	// rekeying: update br.auth to tx.RekeyTo if provided
	if (tx.RekeyTo != basics.Address{}) {
		br, ok := l.balances[tx.Sender]
		if !ok {
			return fmt.Errorf("no account")
		}
		if tx.RekeyTo == tx.Sender {
			br.auth = basics.Address{}
		} else {
			br.auth = tx.RekeyTo
		}
		l.balances[tx.Sender] = br
	}
	return nil
}

func (l *Ledger) pay(from basics.Address, pay transactions.PaymentTxnFields) error {
	err := l.move(from, pay.Receiver, pay.Amount.Raw)
	if err != nil {
		return err
	}
	if !pay.CloseRemainderTo.IsZero() {
		sbr := l.balances[from]
		if len(sbr.holdings) > 0 {
			return fmt.Errorf("unable to close, Sender (%s) has holdings", from)
		}
		if len(sbr.locals) > 0 {
			return fmt.Errorf("unable to close, Sender (%s) is opted in to apps", from)
		}
		// Should also check app creations.
		// Need not check asa creations, as you can't opt out if you created.
		// (though this test ledger doesn't know that)
		remainder := sbr.balance
		if remainder > 0 {
			return l.move(from, pay.CloseRemainderTo, remainder)
		}
	}
	return nil
}

func (l *Ledger) axfer(from basics.Address, xfer transactions.AssetTransferTxnFields) error {
	to := xfer.AssetReceiver
	aid := xfer.XferAsset
	amount := xfer.AssetAmount
	close := xfer.AssetCloseTo

	fbr, ok := l.balances[from]
	if !ok {
		fbr = newBalanceRecord(from, 0)
	}
	fholding, ok := fbr.holdings[aid]
	if !ok {
		if from == to && amount == 0 {
			// opt in
			if params, exists := l.assets[aid]; exists {
				fbr.holdings[aid] = basics.AssetHolding{
					Frozen: params.DefaultFrozen,
				}
				return nil
			}
			return fmt.Errorf("Asset (%d) does not exist", aid)
		}
		return fmt.Errorf("Sender (%s) not opted in to %d", from, aid)
	}
	if fholding.Frozen {
		return fmt.Errorf("Sender (%s) is frozen for %d", from, aid)
	}
	tbr, ok := l.balances[to]
	if !ok {
		tbr = newBalanceRecord(to, 0)
	}
	tholding, ok := tbr.holdings[aid]
	if !ok && amount > 0 {
		return fmt.Errorf("AssetReceiver (%s) not opted in to %d", to, aid)
	}
	if fholding.Amount < amount {
		return fmt.Errorf("insufficient balance")
	}

	// Not just an optimization.
	//   amount >0 : allows axfer to not opted in account
	//   from != to : prevents overwriting the same balance record with only
	//                the second change, and ensures fholding remains correct
	//                for closeTo handling.
	if amount > 0 && from != to {
		fholding.Amount -= amount
		fbr.holdings[aid] = fholding
		l.balances[from] = fbr

		tholding.Amount += amount
		tbr.holdings[aid] = tholding
		l.balances[to] = tbr
	}

	if !close.IsZero() && fholding.Amount > 0 {
		cbr, ok := l.balances[close]
		if !ok {
			cbr = newBalanceRecord(close, 0)
		}
		cholding, ok := cbr.holdings[aid]
		if !ok {
			return fmt.Errorf("AssetCloseTo (%s) not opted in to %d", to, aid)
		}

		// Opt out
		delete(fbr.holdings, aid)
		l.balances[from] = fbr

		cholding.Amount += fholding.Amount
		cbr.holdings[aid] = cholding
		l.balances[close] = cbr
	}

	return nil
}

func (l *Ledger) acfg(from basics.Address, cfg transactions.AssetConfigTxnFields, ad *transactions.ApplyData) error {
	if cfg.ConfigAsset == 0 {
		aid := basics.AssetIndex(l.Counter())
		l.NewAsset(from, aid, cfg.AssetParams)
		ad.ConfigAsset = aid
		return nil
	}
	// This is just a mock.  We don't check all the rules about
	// not setting fields that have been zeroed. Nor do we keep
	// anything from before.
	l.assets[cfg.ConfigAsset] = asaParams{
		Creator:     from,
		AssetParams: cfg.AssetParams,
	}
	return nil
}

func (l *Ledger) afrz(from basics.Address, frz transactions.AssetFreezeTxnFields) error {
	aid := frz.FreezeAsset
	params, ok := l.assets[aid]
	if !ok {
		return fmt.Errorf("Asset (%d) does not exist", aid)
	}
	if params.Freeze != from {
		return fmt.Errorf("Asset (%d) can not be frozen by %s", aid, from)
	}
	br, ok := l.balances[frz.FreezeAccount]
	if !ok {
		return fmt.Errorf("%s does not hold Asset (%d)", frz.FreezeAccount, aid)
	}
	holding, ok := br.holdings[aid]
	if !ok {
		return fmt.Errorf("%s does not hold Asset (%d)", frz.FreezeAccount, aid)
	}
	holding.Frozen = frz.AssetFrozen
	br.holdings[aid] = holding
	return nil
}

func (l *Ledger) appl(from basics.Address, appl transactions.ApplicationCallTxnFields, ad *transactions.ApplyData, gi int, ep *EvalParams) error {
	aid := appl.ApplicationID
	if aid == 0 {
		aid = basics.AppIndex(l.Counter())
		params := basics.AppParams{
			ApprovalProgram:   appl.ApprovalProgram,
			ClearStateProgram: appl.ClearStateProgram,
			GlobalState:       map[string]basics.TealValue{},
			StateSchemas: basics.StateSchemas{
				LocalStateSchema: basics.StateSchema{
					NumUint:      appl.LocalStateSchema.NumUint,
					NumByteSlice: appl.LocalStateSchema.NumByteSlice,
				},
				GlobalStateSchema: basics.StateSchema{
					NumUint:      appl.GlobalStateSchema.NumUint,
					NumByteSlice: appl.GlobalStateSchema.NumByteSlice,
				},
			},
			ExtraProgramPages: appl.ExtraProgramPages,
		}
		l.NewApp(from, aid, params)
		ad.ApplicationID = aid
	}

	if appl.OnCompletion == transactions.ClearStateOC {
		return errors.New("not implemented in test ledger")
	}

	if appl.OnCompletion == transactions.OptInOC {
		br, ok := l.balances[from]
		if !ok {
			return errors.New("no account")
		}
		br.locals[aid] = make(map[string]basics.TealValue)
	}

	// Execute the Approval program
	params, ok := l.applications[aid]
	if !ok {
		return errors.New("No application")
	}
	pass, cx, err := EvalContract(params.ApprovalProgram, gi, aid, ep)
	if err != nil {
		ad.EvalDelta = transactions.EvalDelta{}
		return err
	}
	if !pass {
		ad.EvalDelta = transactions.EvalDelta{}
		return errors.New("Approval program failed")
	}
	ad.EvalDelta = cx.txn.EvalDelta

	switch appl.OnCompletion {
	case transactions.NoOpOC:
	case transactions.OptInOC:
		// done earlier so locals could be changed
	case transactions.CloseOutOC:
		// get the local state, error if not exists, delete it
		br, ok := l.balances[from]
		if !ok {
			return errors.New("no account")
		}
		_, ok = br.locals[aid]
		if !ok {
			return errors.New("not opted in")
		}
		delete(br.locals, aid)
	case transactions.DeleteApplicationOC:
		// get the global object, delete it
		_, ok := l.applications[aid]
		if !ok {
			return errors.New("no app")
		}
		delete(l.applications, aid)
	case transactions.UpdateApplicationOC:
		app, ok := l.applications[aid]
		if !ok {
			return errors.New("no app")
		}
		app.ApprovalProgram = appl.ApprovalProgram
		app.ClearStateProgram = appl.ClearStateProgram
		l.applications[aid] = app
	}
	return nil
}

// Perform causes txn to "occur" against the ledger.
func (l *Ledger) Perform(gi int, ep *EvalParams) error {
	txn := &ep.TxnGroup[gi]
	err := l.move(txn.Txn.Sender, ep.Specials.FeeSink, txn.Txn.Fee.Raw)
	if err != nil {
		return err
	}

	err = l.rekey(&txn.Txn)
	if err != nil {
		return err
	}

	switch txn.Txn.Type {
	case protocol.PaymentTx:
		return l.pay(txn.Txn.Sender, txn.Txn.PaymentTxnFields)
	case protocol.AssetTransferTx:
		return l.axfer(txn.Txn.Sender, txn.Txn.AssetTransferTxnFields)
	case protocol.AssetConfigTx:
		return l.acfg(txn.Txn.Sender, txn.Txn.AssetConfigTxnFields, &txn.ApplyData)
	case protocol.AssetFreezeTx:
		return l.afrz(txn.Txn.Sender, txn.Txn.AssetFreezeTxnFields)
	case protocol.ApplicationCallTx:
		return l.appl(txn.Txn.Sender, txn.Txn.ApplicationCallTxnFields, &txn.ApplyData, gi, ep)
	case protocol.KeyRegistrationTx:
		return nil // For now, presume success in test ledger
	default:
		return fmt.Errorf("%s txn in AVM", txn.Txn.Type)
	}
}

// Get returns the AccountData of an address. This test ledger does
// not handle rewards, so the pening rewards flag is ignored.
func (l *Ledger) Get(addr basics.Address, withPendingRewards bool) (basics.AccountData, error) {
	br, ok := l.balances[addr]
	if !ok {
		return basics.AccountData{}, fmt.Errorf("no account %s", addr)
	}
	return basics.AccountData{
		MicroAlgos:     basics.MicroAlgos{Raw: br.balance},
		AssetParams:    map[basics.AssetIndex]basics.AssetParams{},
		Assets:         map[basics.AssetIndex]basics.AssetHolding{},
		AppLocalStates: map[basics.AppIndex]basics.AppLocalState{},
		AppParams:      map[basics.AppIndex]basics.AppParams{},
	}, nil
}

// GetCreator returns the creator of the given creatable, an app or asa.
func (l *Ledger) GetCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (basics.Address, bool, error) {
	if ctype == basics.AssetCreatable {
		params, found := l.assets[basics.AssetIndex(cidx)]
		return params.Creator, found, nil
	}
	if ctype == basics.AppCreatable {
		params, found := l.applications[basics.AppIndex(cidx)]
		return params.Creator, found, nil
	}
	return basics.Address{}, false, fmt.Errorf("no creatable %v %d", ctype, cidx)
}

// SetKey creates a new key-value in {addr, aidx, global} storage
func (l *Ledger) SetKey(addr basics.Address, aidx basics.AppIndex, global bool, key string, value basics.TealValue, accountIdx uint64) error {
	if global {
		l.NewGlobal(uint64(aidx), key, value)
	} else {
		l.NewLocal(addr, uint64(aidx), key, value)
	}
	return nil
}

// DelKey removes a key from {addr, aidx, global} storage
func (l *Ledger) DelKey(addr basics.Address, aidx basics.AppIndex, global bool, key string, accountIdx uint64) error {
	if global {
		l.NoGlobal(uint64(aidx), key)
	} else {
		l.NoLocal(addr, uint64(aidx), key)
	}
	return nil
}

func (l *Ledger) Round() basics.Round {
	if l.rnd == basics.Round(0) {
		// Something big enough to shake out bugs from width
		l.rnd = basics.Round(uint64(math.MaxUint32) + 5)
	}
	return l.rnd
}