Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions ledger/appcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func errAlreadyStorage(addr basics.Address, aidx basics.AppIndex, global bool) e
}

// Allocate creates kv storage for a given {addr, aidx, global}. It is called on app creation (global) or opting in (local)
func (cb *roundCowState) Allocate(addr basics.Address, aidx basics.AppIndex, global bool, space basics.StateSchema) error {
func (cb *roundCowState) AllocateApp(addr basics.Address, aidx basics.AppIndex, global bool, space basics.StateSchema) error {
// Check that account is not already opted in
allocated, err := cb.allocated(addr, aidx, global)
if err != nil {
Expand All @@ -241,11 +241,21 @@ func (cb *roundCowState) Allocate(addr basics.Address, aidx basics.AppIndex, glo
lsd.action = allocAction
lsd.maxCounts = &space

if global {
cb.mods.Creatables[basics.CreatableIndex(aidx)] = ledgercore.ModifiedCreatable{
Ctype: basics.AppCreatable,
Creator: addr,
Created: true,
}
}

cb.trackCreatable(basics.CreatableIndex(aidx))

return nil
}

// Deallocate clears storage for {addr, aidx, global}. It happens on app deletion (global) or closing out (local)
func (cb *roundCowState) Deallocate(addr basics.Address, aidx basics.AppIndex, global bool) error {
func (cb *roundCowState) DeallocateApp(addr basics.Address, aidx basics.AppIndex, global bool) error {
// Check that account has allocated storage
allocated, err := cb.allocated(addr, aidx, global)
if err != nil {
Expand All @@ -265,6 +275,15 @@ func (cb *roundCowState) Deallocate(addr basics.Address, aidx basics.AppIndex, g
lsd.counts = &basics.StateSchema{}
lsd.maxCounts = &basics.StateSchema{}
lsd.kvCow = make(stateDelta)

if global {
cb.mods.Creatables[basics.CreatableIndex(aidx)] = ledgercore.ModifiedCreatable{
Ctype: basics.AppCreatable,
Creator: addr,
Created: false,
}
}

return nil
}

Expand Down Expand Up @@ -619,7 +638,7 @@ func applyStorageDelta(data basics.AccountData, aapp storagePtr, store *storageD
delete(owned, aapp.aidx)
case allocAction, remainAllocAction:
// note: these should always exist because they were
// at least preceded by a call to PutWithCreatable
// at least preceded by a call to Put()
params, ok := owned[aapp.aidx]
if !ok {
return basics.AccountData{}, fmt.Errorf("could not find existing params for %v", aapp.aidx)
Expand Down
4 changes: 2 additions & 2 deletions ledger/appcow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestCowStorage(t *testing.T) {
NumUint: rand.Uint64(),
NumByteSlice: rand.Uint64(),
}
err := cow.Allocate(addr, sptr.aidx, sptr.global, rschema)
err := cow.AllocateApp(addr, sptr.aidx, sptr.global, rschema)
if actuallyAllocated {
require.Error(t, err)
require.Contains(t, err.Error(), "cannot allocate")
Expand All @@ -256,7 +256,7 @@ func TestCowStorage(t *testing.T) {
// Deallocate
if rand.Float32() < 0.25 {
actuallyAllocated := st.allocated(aapp)
err := cow.Deallocate(addr, sptr.aidx, sptr.global)
err := cow.DeallocateApp(addr, sptr.aidx, sptr.global)
if actuallyAllocated {
require.NoError(t, err)
err := st.dealloc(aapp)
Expand Down
25 changes: 6 additions & 19 deletions ledger/apply/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,14 @@ func createApplication(ac *transactions.ApplicationCallTxnFields, balances Balan
totalExtraPages += ac.ExtraProgramPages
record.TotalExtraAppPages = totalExtraPages

// Tell the cow what app we created
created := &basics.CreatableLocator{
Creator: creator,
Type: basics.AppCreatable,
Index: basics.CreatableIndex(appIdx),
}

// Write back to the creator's balance record
err = balances.PutWithCreatable(creator, record, created, nil)
err = balances.Put(creator, record)
if err != nil {
return 0, err
}

// Allocate global storage
err = balances.Allocate(creator, appIdx, true, ac.GlobalStateSchema)
err = balances.AllocateApp(creator, appIdx, true, ac.GlobalStateSchema)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -169,19 +162,13 @@ func deleteApplication(balances Balances, creator basics.Address, appIdx basics.
record.TotalExtraAppPages = totalExtraPages
}

// Tell the cow what app we deleted
deleted := &basics.CreatableLocator{
Creator: creator,
Type: basics.AppCreatable,
Index: basics.CreatableIndex(appIdx),
}
err = balances.PutWithCreatable(creator, record, nil, deleted)
err = balances.Put(creator, record)
if err != nil {
return err
}

// Deallocate global storage
err = balances.Deallocate(creator, appIdx, true)
err = balances.DeallocateApp(creator, appIdx, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -243,7 +230,7 @@ func optInApplication(balances Balances, sender basics.Address, appIdx basics.Ap
}

// Allocate local storage
err = balances.Allocate(sender, appIdx, false, params.LocalStateSchema)
err = balances.AllocateApp(sender, appIdx, false, params.LocalStateSchema)
if err != nil {
return err
}
Expand Down Expand Up @@ -281,7 +268,7 @@ func closeOutApplication(balances Balances, sender basics.Address, appIdx basics
}

// Deallocate local storage
err = balances.Deallocate(sender, appIdx, false)
err = balances.DeallocateApp(sender, appIdx, false)
if err != nil {
return err
}
Expand Down
Loading