Skip to content

Commit 2235976

Browse files
committed
Replace Balances.PutWithCreatable().
1 parent 916154b commit 2235976

File tree

9 files changed

+95
-114
lines changed

9 files changed

+95
-114
lines changed

ledger/apply/application.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,14 @@ func createApplication(ac *transactions.ApplicationCallTxnFields, balances Balan
121121
totalExtraPages += ac.ExtraProgramPages
122122
record.TotalExtraAppPages = totalExtraPages
123123

124-
// Tell the cow what app we created
125-
created := &basics.CreatableLocator{
126-
Creator: creator,
127-
Type: basics.AppCreatable,
128-
Index: basics.CreatableIndex(appIdx),
124+
// Write back to the creator's balance record
125+
err = balances.Put(creator, record)
126+
if err != nil {
127+
return 0, err
129128
}
130129

131-
// Write back to the creator's balance record
132-
err = balances.PutWithCreatable(creator, record, created, nil)
130+
// Tell the cow what app we created
131+
err = balances.CreatableCreated(basics.AppCreatable, creator, basics.CreatableIndex(appIdx))
133132
if err != nil {
134133
return 0, err
135134
}
@@ -169,13 +168,13 @@ func deleteApplication(balances Balances, creator basics.Address, appIdx basics.
169168
record.TotalExtraAppPages = totalExtraPages
170169
}
171170

172-
// Tell the cow what app we deleted
173-
deleted := &basics.CreatableLocator{
174-
Creator: creator,
175-
Type: basics.AppCreatable,
176-
Index: basics.CreatableIndex(appIdx),
171+
err = balances.Put(creator, record)
172+
if err != nil {
173+
return err
177174
}
178-
err = balances.PutWithCreatable(creator, record, nil, deleted)
175+
176+
// Tell the cow what app we deleted
177+
err = balances.CreatableDeleted(basics.AppCreatable, creator, basics.CreatableIndex(appIdx))
179178
if err != nil {
180179
return err
181180
}

ledger/apply/application_test.go

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,9 @@ type testBalances struct {
106106
proto config.ConsensusParams
107107

108108
put int // Put calls counter
109-
putWith int // PutWithCreatable calls counter
110109
putBalances map[basics.Address]basics.AccountData
111-
putWithBalances map[basics.Address]basics.AccountData
112-
putWithNew []basics.CreatableLocator
113-
putWithDel []basics.CreatableLocator
110+
createdCreatables []basics.CreatableLocator
111+
deletedCreatables []basics.CreatableLocator
114112
allocatedAppIdx basics.AppIndex
115113
deAllocatedAppIdx basics.AppIndex
116114

@@ -144,18 +142,27 @@ func (b *testBalances) Put(addr basics.Address, ad basics.AccountData) error {
144142
return nil
145143
}
146144

147-
func (b *testBalances) PutWithCreatable(addr basics.Address, ad basics.AccountData, newCreatable *basics.CreatableLocator, deletedCreatable *basics.CreatableLocator) error {
148-
b.putWith++
149-
if b.putWithBalances == nil {
150-
b.putWithBalances = make(map[basics.Address]basics.AccountData)
145+
func (b *testBalances) CreatableCreated(
146+
creatableType basics.CreatableType, creator basics.Address,
147+
index basics.CreatableIndex) error {
148+
locator := basics.CreatableLocator{
149+
Type: creatableType,
150+
Creator: creator,
151+
Index: index,
151152
}
152-
b.putWithBalances[addr] = ad
153-
if newCreatable != nil {
154-
b.putWithNew = append(b.putWithNew, *newCreatable)
155-
}
156-
if deletedCreatable != nil {
157-
b.putWithDel = append(b.putWithDel, *deletedCreatable)
153+
b.createdCreatables = append(b.createdCreatables, locator)
154+
return nil
155+
}
156+
157+
func (b *testBalances) CreatableDeleted(
158+
creatableType basics.CreatableType, creator basics.Address,
159+
index basics.CreatableIndex) error {
160+
locator := basics.CreatableLocator{
161+
Type: creatableType,
162+
Creator: creator,
163+
Index: index,
158164
}
165+
b.deletedCreatables = append(b.deletedCreatables, locator)
159166
return nil
160167
}
161168

@@ -236,11 +243,9 @@ func (b *testBalancesPass) StatefulEval(params logic.EvalParams, aidx basics.App
236243
// ResetWrites clears side effects of Put/PutWithCreatable
237244
func (b *testBalances) ResetWrites() {
238245
b.put = 0
239-
b.putWith = 0
240246
b.putBalances = nil
241-
b.putWithBalances = nil
242-
b.putWithNew = []basics.CreatableLocator{}
243-
b.putWithDel = []basics.CreatableLocator{}
247+
b.createdCreatables = []basics.CreatableLocator{}
248+
b.deletedCreatables = []basics.CreatableLocator{}
244249
b.allocatedAppIdx = 0
245250
}
246251

@@ -424,19 +429,16 @@ func TestAppCallCreate(t *testing.T) {
424429
appIdx, err = createApplication(&ac, &b, creator, txnCounter)
425430
a.NoError(err)
426431
a.Equal(txnCounter+1, uint64(appIdx))
427-
a.Equal(0, b.put)
428-
a.Equal(1, b.putWith)
432+
a.Equal(1, b.put)
429433
nbr, ok := b.putBalances[creator]
430-
a.False(ok)
431-
nbr, ok = b.putWithBalances[creator]
432434
a.True(ok)
433435
params, ok := nbr.AppParams[appIdx]
434436
a.True(ok)
435437
a.Equal(ac.ApprovalProgram, params.ApprovalProgram)
436438
a.Equal(ac.ClearStateProgram, params.ClearStateProgram)
437439
a.Equal(ac.LocalStateSchema, params.LocalStateSchema)
438440
a.Equal(ac.GlobalStateSchema, params.GlobalStateSchema)
439-
a.True(len(b.putWithNew) > 0)
441+
a.Equal(1, len(b.createdCreatables))
440442
}
441443

442444
// TestAppCallApplyCreate carefully tracks and validates balance record updates
@@ -463,7 +465,6 @@ func TestAppCallApplyCreate(t *testing.T) {
463465
a.Error(err)
464466
a.Contains(err.Error(), "ApplicationCall cannot have nil ApplyData")
465467
a.Equal(0, b.put)
466-
a.Equal(0, b.putWith)
467468

468469
b.balances = make(map[basics.Address]basics.AccountData)
469470
b.balances[creator] = basics.AccountData{}
@@ -473,7 +474,6 @@ func TestAppCallApplyCreate(t *testing.T) {
473474
a.Error(err)
474475
a.Contains(err.Error(), "max created apps per acct is 0")
475476
a.Equal(0, b.put)
476-
a.Equal(0, b.putWith)
477477

478478
b.SetProto(protocol.ConsensusFuture)
479479
proto := b.ConsensusParams()
@@ -485,14 +485,13 @@ func TestAppCallApplyCreate(t *testing.T) {
485485
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
486486
a.Error(err)
487487
a.Contains(err.Error(), "applications that do not exist")
488-
a.Equal(0, b.put)
489-
a.Equal(1, b.putWith)
488+
a.Equal(1, b.put)
490489

491490
appIdx := basics.AppIndex(txnCounter + 1)
492491
b.appCreators = map[basics.AppIndex]basics.Address{appIdx: creator}
493492

494493
// save the created app info to the side
495-
saved := b.putWithBalances[creator]
494+
saved := b.putBalances[creator]
496495

497496
b.ResetWrites()
498497

@@ -501,8 +500,7 @@ func TestAppCallApplyCreate(t *testing.T) {
501500
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
502501
a.Error(err)
503502
a.Contains(err.Error(), fmt.Sprintf("app %d not found in account", appIdx))
504-
a.Equal(0, b.put)
505-
a.Equal(1, b.putWith)
503+
a.Equal(1, b.put)
506504

507505
b.ResetWrites()
508506

@@ -515,15 +513,14 @@ func TestAppCallApplyCreate(t *testing.T) {
515513
a.Error(err)
516514
a.Contains(err.Error(), "transaction rejected by ApprovalProgram")
517515
a.Equal(uint64(b.allocatedAppIdx), txnCounter+1)
518-
a.Equal(0, b.put)
519-
a.Equal(1, b.putWith)
516+
a.Equal(1, b.put)
520517
// ensure original balance record in the mock was not changed
521518
// this ensure proper cloning and any in-intended in-memory modifications
522519
//
523520
// known artefact of cloning AppLocalState even with empty update, nil map vs empty map
524521
saved.AppLocalStates = map[basics.AppIndex]basics.AppLocalState{}
525522
a.Equal(saved, b.balances[creator])
526-
saved = b.putWithBalances[creator]
523+
saved = b.putBalances[creator]
527524

528525
b.ResetWrites()
529526

@@ -538,10 +535,9 @@ func TestAppCallApplyCreate(t *testing.T) {
538535
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
539536
a.NoError(err)
540537
a.Equal(appIdx, b.allocatedAppIdx)
541-
a.Equal(0, b.put)
542-
a.Equal(1, b.putWith)
538+
a.Equal(1, b.put)
543539
a.Equal(saved, b.balances[creator])
544-
br := b.putWithBalances[creator]
540+
br := b.putBalances[creator]
545541
a.Equal([]byte{1}, br.AppParams[appIdx].ApprovalProgram)
546542
a.Equal([]byte{1}, br.AppParams[appIdx].ClearStateProgram)
547543
a.Equal(basics.TealKeyValue(nil), br.AppParams[appIdx].GlobalState)
@@ -553,7 +549,7 @@ func TestAppCallApplyCreate(t *testing.T) {
553549
ac.ExtraProgramPages = 1
554550
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
555551
a.NoError(err)
556-
br = b.putWithBalances[creator]
552+
br = b.putBalances[creator]
557553
a.Equal(uint32(1), br.AppParams[appIdx].ExtraProgramPages)
558554
a.Equal(uint32(1), br.TotalExtraAppPages)
559555
}
@@ -625,13 +621,11 @@ func TestAppCallOptIn(t *testing.T) {
625621
a.Error(err)
626622
a.Contains(err.Error(), "cannot opt in app")
627623
a.Equal(0, b.put)
628-
a.Equal(0, b.putWith)
629624

630625
b.SetProto(protocol.ConsensusFuture)
631626
err = optInApplication(&b, sender, appIdx, params)
632627
a.NoError(err)
633628
a.Equal(1, b.put)
634-
a.Equal(0, b.putWith)
635629
br := b.putBalances[sender]
636630
a.Equal(basics.AccountData{AppLocalStates: map[basics.AppIndex]basics.AppLocalState{appIdx: {}}}, br)
637631

@@ -644,7 +638,6 @@ func TestAppCallOptIn(t *testing.T) {
644638
a.Error(err)
645639
a.Contains(err.Error(), "has already opted in to app")
646640
a.Equal(0, b.put)
647-
a.Equal(0, b.putWith)
648641

649642
b.ResetWrites()
650643

@@ -654,7 +647,6 @@ func TestAppCallOptIn(t *testing.T) {
654647
err = optInApplication(&b, sender, appIdx, params)
655648
a.NoError(err)
656649
a.Equal(1, b.put)
657-
a.Equal(0, b.putWith)
658650

659651
b.ResetWrites()
660652

@@ -667,7 +659,6 @@ func TestAppCallOptIn(t *testing.T) {
667659
err = optInApplication(&b, sender, appIdx, params)
668660
a.NoError(err)
669661
a.Equal(1, b.put)
670-
a.Equal(0, b.putWith)
671662
br = b.putBalances[sender]
672663
a.Equal(
673664
basics.AccountData{
@@ -728,7 +719,6 @@ func TestAppCallClearState(t *testing.T) {
728719
a.Error(err)
729720
a.Contains(err.Error(), "is not currently opted in to app")
730721
a.Equal(0, b.put)
731-
a.Equal(0, b.putWith)
732722

733723
// check non-existing app with empty opt-in
734724
b.balances[sender] = basics.AccountData{
@@ -737,7 +727,6 @@ func TestAppCallClearState(t *testing.T) {
737727
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
738728
a.NoError(err)
739729
a.Equal(1, b.put)
740-
a.Equal(0, b.putWith)
741730
br := b.putBalances[sender]
742731
a.Equal(0, len(br.AppLocalStates))
743732
a.Equal(basics.StateSchema{}, br.TotalAppSchema)
@@ -756,7 +745,6 @@ func TestAppCallClearState(t *testing.T) {
756745
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
757746
a.NoError(err)
758747
a.Equal(1, b.put)
759-
a.Equal(0, b.putWith)
760748
br = b.putBalances[sender]
761749
a.Equal(0, len(br.AppLocalStates))
762750
a.Equal(basics.StateSchema{}, br.TotalAppSchema)
@@ -783,7 +771,6 @@ func TestAppCallClearState(t *testing.T) {
783771
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
784772
a.NoError(err)
785773
a.Equal(1, b.put)
786-
a.Equal(0, b.putWith)
787774
br = b.putBalances[sender]
788775
a.Equal(0, len(br.AppLocalStates))
789776
a.Equal(basics.StateSchema{}, br.TotalAppSchema)
@@ -799,7 +786,6 @@ func TestAppCallClearState(t *testing.T) {
799786
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
800787
a.NoError(err)
801788
a.Equal(1, b.put)
802-
a.Equal(0, b.putWith)
803789
br = b.putBalances[sender]
804790
a.Equal(0, len(br.AppLocalStates))
805791
a.Equal(basics.StateSchema{}, br.TotalAppSchema)
@@ -830,7 +816,6 @@ func TestAppCallClearState(t *testing.T) {
830816
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
831817
a.NoError(err)
832818
a.Equal(1, b.put)
833-
a.Equal(0, b.putWith)
834819
a.Equal(appIdx, b.deAllocatedAppIdx)
835820
a.Equal(0, len(br.AppLocalStates))
836821
a.Equal(basics.StateSchema{}, br.TotalAppSchema)
@@ -883,7 +868,6 @@ func TestAppCallApplyCloseOut(t *testing.T) {
883868
a.Error(err)
884869
a.Contains(err.Error(), "transaction rejected by ApprovalProgram")
885870
a.Equal(0, b.put)
886-
a.Equal(0, b.putWith)
887871
br := b.balances[creator]
888872
a.Equal(cbr, br)
889873
a.Equal(basics.EvalDelta{}, ad.EvalDelta)
@@ -895,7 +879,6 @@ func TestAppCallApplyCloseOut(t *testing.T) {
895879
a.Error(err)
896880
a.Contains(err.Error(), "is not opted in to app")
897881
a.Equal(0, b.put)
898-
a.Equal(0, b.putWith)
899882
br = b.balances[creator]
900883
a.Equal(cbr, br)
901884
a.Equal(basics.EvalDelta{}, ad.EvalDelta)
@@ -911,7 +894,6 @@ func TestAppCallApplyCloseOut(t *testing.T) {
911894
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
912895
a.NoError(err)
913896
a.Equal(1, b.put)
914-
a.Equal(0, b.putWith)
915897
br = b.putBalances[creator]
916898
a.NotEqual(cbr, br)
917899
a.Equal(basics.TealKeyValue(nil), br.AppParams[appIdx].GlobalState)
@@ -969,7 +951,6 @@ func TestAppCallApplyUpdate(t *testing.T) {
969951
a.Error(err)
970952
a.Contains(err.Error(), "transaction rejected by ApprovalProgram")
971953
a.Equal(0, b.put)
972-
a.Equal(0, b.putWith)
973954
br := b.balances[creator]
974955
a.Equal(cbr, br)
975956
a.Equal(basics.EvalDelta{}, ad.EvalDelta)
@@ -980,7 +961,6 @@ func TestAppCallApplyUpdate(t *testing.T) {
980961
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
981962
a.NoError(err)
982963
a.Equal(1, b.put)
983-
a.Equal(0, b.putWith)
984964
br = b.balances[creator]
985965
a.Equal(cbr, br)
986966
br = b.putBalances[creator]
@@ -1035,7 +1015,6 @@ func TestAppCallApplyDelete(t *testing.T) {
10351015
a.Error(err)
10361016
a.Contains(err.Error(), "transaction rejected by ApprovalProgram")
10371017
a.Equal(0, b.put)
1038-
a.Equal(0, b.putWith)
10391018
br := b.balances[creator]
10401019
a.Equal(cbr, br)
10411020
a.Equal(basics.EvalDelta{}, ad.EvalDelta)
@@ -1046,8 +1025,7 @@ func TestAppCallApplyDelete(t *testing.T) {
10461025
err = ApplicationCall(ac, h, &b, ad, &ep, txnCounter)
10471026
a.NoError(err)
10481027
a.Equal(appIdx, b.deAllocatedAppIdx)
1049-
a.Equal(0, b.put)
1050-
a.Equal(1, b.putWith)
1028+
a.Equal(1, b.put)
10511029
br = b.balances[creator]
10521030
a.Equal(cbr, br)
10531031
br = b.putBalances[creator]

ledger/apply/apply.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ type Balances interface {
3333

3434
Put(basics.Address, basics.AccountData) error
3535

36-
// PutWithCreatable is like Put, but should be used when creating or deleting an asset or application.
37-
PutWithCreatable(addr basics.Address, acct basics.AccountData, newCreatable *basics.CreatableLocator, deletedCreatable *basics.CreatableLocator) error
36+
// Notify COW that a creatable was created or deleted.
37+
CreatableCreated(
38+
creatableType basics.CreatableType, creator basics.Address,
39+
index basics.CreatableIndex) error
40+
CreatableDeleted(
41+
creatableType basics.CreatableType, creator basics.Address,
42+
index basics.CreatableIndex) error
3843

3944
// GetCreator gets the address of the account that created a given creatable
4045
GetCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (basics.Address, bool, error)

0 commit comments

Comments
 (0)