Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simulate: resource population #6015

Draft
wants to merge 35 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5ba0a9a
ResourcePopulator
joe-p Jun 5, 2024
028b8c4
TestPopulatorWithGlobalResources (arrays only)
joe-p Jun 6, 2024
941ba6e
test addBox
joe-p Jun 6, 2024
a69091c
use ElementsMatch
joe-p Jun 6, 2024
d9e77ef
addHolding
joe-p Jun 6, 2024
4667647
test variable renaming
joe-p Jun 6, 2024
cbd1d8a
appLocals
joe-p Jun 6, 2024
d79780f
restore default limits
joe-p Jun 6, 2024
e6d59ba
fix rekey field
joe-p Jun 6, 2024
54cd38c
populate with static properties and remove zeroAddr
joe-p Jun 6, 2024
673b03d
empty boxes
joe-p Jun 6, 2024
ceddaa2
ensure duplicates are removed
joe-p Jun 6, 2024
62213a0
overflow txn resources
joe-p Jun 6, 2024
f616449
use ConsensusParams
joe-p Jun 6, 2024
6a4f1ee
fix empty box count
joe-p Jun 6, 2024
0524c3c
golangci-lint
joe-p Jun 6, 2024
73e5b4c
Merge branch 'master' into feat/populate_resources
joe-p Jun 14, 2024
6d8c161
PopulateResourceArrays in simulate (untested)
joe-p Jun 15, 2024
022c565
populate from ResourceTracker
joe-p Jun 18, 2024
93dd4e9
initial TestPopulateResources
joe-p Jun 18, 2024
3f78752
group sharing and no group sharing TestPopulateResources
joe-p Jun 18, 2024
359b5b0
test with non appl in group
joe-p Jun 19, 2024
2229b0a
modify visibility on resource pop stuff
joe-p Jun 19, 2024
ec7a36a
remove duplicate partitiontest.PartitionTest(t)
joe-p Jun 19, 2024
351f915
don't make RekeyTo address available
joe-p Jun 19, 2024
99abd2f
add PopulateResourceArrays to simulate API (WIP)
joe-p Jul 24, 2024
96f1af1
Apply suggestions from code review
joe-p Oct 2, 2024
02534ae
Apply suggestions from code review
joe-p Oct 2, 2024
0815e29
PopulateResourceArrays -> PopulateResources
joe-p Oct 2, 2024
b4f7f54
static -> prefilled
joe-p Oct 2, 2024
e665817
replace ifs with switch
joe-p Oct 2, 2024
c9e6e5d
hasAccount short circuit logic
joe-p Oct 2, 2024
2ea34f2
check for room and return error in add... methods
joe-p Oct 2, 2024
93e7b81
mixed resources test
joe-p Oct 2, 2024
292a9b9
use arrays in txn and group result rather than map[int] for API (WIP)
joe-p Oct 9, 2024
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
Prev Previous commit
Next Next commit
golangci-lint
  • Loading branch information
joe-p committed Jun 6, 2024
commit 0524c3c9ea0e1cb990a1502df9831c6f60cc395e
20 changes: 10 additions & 10 deletions ledger/simulation/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,9 @@
return p.tracker.addBox(app, name, readSize, *p.initialBoxSurplusReadBudget, p.ep.Proto.BytesPerBoxReference)
}

// TxnResources tracks the resources being added to a tranasction during resource population
type TxnResources struct {
// The static fields are resource arrays that were given in the transaciton group and thus cannot be removed
// The static fields are resource arrays that were given in the txn group and thus cannot be removed
joe-p marked this conversation as resolved.
Show resolved Hide resolved
// The assumption is that these are prefilled because of one of the following reaons:
joe-p marked this conversation as resolved.
Show resolved Hide resolved
// - This transaction has already been signed
// - One of the foreign arrays is accessed on-chain
joe-p marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -678,13 +679,13 @@

for app := range r.Apps {
if app.Address() == addr {
return true

Check warning on line 682 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L682

Added line #L682 was not covered by tests
}
}

for app := range r.StaticApps {
if app.Address() == addr {
return true

Check warning on line 688 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L687-L688

Added lines #L687 - L688 were not covered by tests
}
}

Expand All @@ -709,10 +710,11 @@

func (r *TxnResources) addAddressFromField(addr basics.Address) {
if !addr.IsZero() {
r.AccountsFromFields[addr] = struct{}{}

Check warning on line 713 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L713

Added line #L713 was not covered by tests
}
}

// PopulatedArrays is a struct that contains all the populated arrays for a txn
type PopulatedArrays struct {
Accounts []basics.Address
Assets []basics.AssetIndex
Expand All @@ -726,7 +728,7 @@
accounts = append(accounts, account)
}
for account := range r.StaticAccounts {
accounts = append(accounts, account)

Check warning on line 731 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L731

Added line #L731 was not covered by tests
}

assets := make([]basics.AssetIndex, 0, len(r.Assets)+len(r.StaticAssets))
Expand All @@ -734,7 +736,7 @@
assets = append(assets, asset)
}
for asset := range r.StaticAssets {
assets = append(assets, asset)

Check warning on line 739 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L739

Added line #L739 was not covered by tests
}

apps := make([]basics.AppIndex, 0, len(r.Apps)+len(r.StaticApps))
Expand All @@ -742,16 +744,12 @@
apps = append(apps, app)
}
for app := range r.StaticApps {
apps = append(apps, app)

Check warning on line 747 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L747

Added line #L747 was not covered by tests
}

boxes := make([]logic.BoxRef, 0, len(r.Boxes)+len(r.StaticBoxes))
for _, box := range r.Boxes {
boxes = append(boxes, box)
}
for _, box := range r.StaticBoxes {
boxes = append(boxes, box)
}
boxes = append(boxes, r.Boxes...)
boxes = append(boxes, r.StaticBoxes...)

return PopulatedArrays{
Accounts: accounts,
Expand All @@ -761,6 +759,7 @@
}
}

// ResourcePopulator is used to populate app resources for a transaction group
type ResourcePopulator struct {
TxnResources []TxnResources
}
Expand All @@ -786,20 +785,20 @@

if txn.Type == protocol.ApplicationCallTx {
for _, asset := range txn.ForeignAssets {
p.TxnResources[groupIndex].StaticAssets[asset] = struct{}{}

Check warning on line 788 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L788

Added line #L788 was not covered by tests
}

for _, app := range txn.ForeignApps {
p.TxnResources[groupIndex].StaticApps[app] = struct{}{}

Check warning on line 792 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L792

Added line #L792 was not covered by tests
}

for _, account := range txn.Accounts {
p.TxnResources[groupIndex].StaticAccounts[account] = struct{}{}

Check warning on line 796 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L796

Added line #L796 was not covered by tests
}

for _, box := range txn.Boxes {
ref := logic.BoxRef{App: txn.ForeignApps[box.Index], Name: string(box.Name)}
p.TxnResources[groupIndex].StaticBoxes = append(p.TxnResources[groupIndex].StaticBoxes, ref)

Check warning on line 801 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L800-L801

Added lines #L800 - L801 were not covered by tests
}

p.TxnResources[groupIndex].AppFromField = txn.ApplicationID
Expand All @@ -807,33 +806,33 @@
return
}

if txn.Type == protocol.AssetTransferTx {
joe-p marked this conversation as resolved.
Show resolved Hide resolved
p.TxnResources[groupIndex].AssetFromField = txn.XferAsset
p.TxnResources[groupIndex].addAddressFromField(txn.AssetReceiver)
p.TxnResources[groupIndex].addAddressFromField(txn.AssetCloseTo)
p.TxnResources[groupIndex].addAddressFromField(txn.AssetSender)

Check warning on line 813 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L809-L813

Added lines #L809 - L813 were not covered by tests

return

Check warning on line 815 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L815

Added line #L815 was not covered by tests
}

if txn.Type == protocol.PaymentTx {
p.TxnResources[groupIndex].addAddressFromField(txn.Receiver)
p.TxnResources[groupIndex].addAddressFromField(txn.CloseRemainderTo)

Check warning on line 820 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L818-L820

Added lines #L818 - L820 were not covered by tests

return

Check warning on line 822 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L822

Added line #L822 was not covered by tests
}

if txn.Type == protocol.AssetConfigTx {
p.TxnResources[groupIndex].AssetFromField = txn.ConfigAsset

Check warning on line 826 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L825-L826

Added lines #L825 - L826 were not covered by tests

return

Check warning on line 828 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L828

Added line #L828 was not covered by tests
}

if txn.Type == protocol.AssetFreezeTx {
p.TxnResources[groupIndex].AssetFromField = txn.FreezeAsset
p.TxnResources[groupIndex].addAddressFromField(txn.FreezeAccount)

Check warning on line 833 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L831-L833

Added lines #L831 - L833 were not covered by tests

return

Check warning on line 835 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L835

Added line #L835 was not covered by tests
}
}

Expand All @@ -844,7 +843,7 @@
return nil
}
}
return fmt.Errorf("no room for account")

Check warning on line 846 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L846

Added line #L846 was not covered by tests
}

func (p *ResourcePopulator) addAsset(asset basics.AssetIndex) error {
Expand All @@ -854,7 +853,7 @@
return nil
}
}
return fmt.Errorf("no room for asset")

Check warning on line 856 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L856

Added line #L856 was not covered by tests
}

func (p *ResourcePopulator) addApp(app basics.AppIndex) error {
Expand All @@ -864,7 +863,7 @@
return nil
}
}
return fmt.Errorf("no room for app")

Check warning on line 866 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L866

Added line #L866 was not covered by tests
}

func (p *ResourcePopulator) addBox(app basics.AppIndex, name string) error {
Expand All @@ -887,7 +886,7 @@
}
}

return fmt.Errorf("no room for box")

Check warning on line 889 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L889

Added line #L889 was not covered by tests
}

func (p *ResourcePopulator) addHolding(addr basics.Address, aid basics.AssetIndex) error {
Expand Down Expand Up @@ -919,7 +918,7 @@
return nil
}
}
return fmt.Errorf("no room for holding")

Check warning on line 921 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L921

Added line #L921 was not covered by tests
joe-p marked this conversation as resolved.
Show resolved Hide resolved
}

func (p *ResourcePopulator) addLocal(addr basics.Address, aid basics.AppIndex) error {
Expand Down Expand Up @@ -951,7 +950,7 @@
return nil
}
}
return fmt.Errorf("no room for local")

Check warning on line 953 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L953

Added line #L953 was not covered by tests
}

func (p *ResourcePopulator) populateResources(groupResourceTracker groupResourceTracker) error {
Expand All @@ -974,10 +973,10 @@
for holding := range groupResourceTracker.globalResources.AssetHoldings {
err := p.addHolding(holding.Address, holding.Asset)
if err != nil {
return err

Check warning on line 976 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L976

Added line #L976 was not covered by tests
}

// Remove the resources from the global tracker in case they were added seperately
// Remove the resources from the global tracker in case they were added separately
delete(groupResourceTracker.globalResources.Assets, holding.Asset)
delete(groupResourceTracker.globalResources.Accounts, holding.Address)
}
Expand All @@ -985,10 +984,10 @@
for local := range groupResourceTracker.globalResources.AppLocals {
err := p.addLocal(local.Address, local.App)
if err != nil {
return err

Check warning on line 987 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L987

Added line #L987 was not covered by tests
}

// Remove the resources from the global tracker in case they were added seperately
// Remove the resources from the global tracker in case they were added separately
delete(groupResourceTracker.globalResources.Apps, local.App)
delete(groupResourceTracker.globalResources.Accounts, local.Address)
}
Expand All @@ -997,10 +996,10 @@
for box := range groupResourceTracker.globalResources.Boxes {
err := p.addBox(box.App, box.Name)
if err != nil {
return err

Check warning on line 999 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L999

Added line #L999 was not covered by tests
}

// Remove the app from the global tracker in case it was added seperately
// Remove the app from the global tracker in case it was added separately
delete(groupResourceTracker.globalResources.Apps, box.App)
}

Expand All @@ -1008,7 +1007,7 @@
for account := range groupResourceTracker.globalResources.Accounts {
err := p.addAccount(account)
if err != nil {
return err

Check warning on line 1010 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L1010

Added line #L1010 was not covered by tests
}
}

Expand All @@ -1016,27 +1015,28 @@
for app := range groupResourceTracker.globalResources.Apps {
err := p.addApp(app)
if err != nil {
return err

Check warning on line 1018 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L1018

Added line #L1018 was not covered by tests
}
}

for asset := range groupResourceTracker.globalResources.Assets {
err := p.addAsset(asset)
if err != nil {
return err

Check warning on line 1025 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L1025

Added line #L1025 was not covered by tests
}
}

for i := 0; i < groupResourceTracker.globalResources.NumEmptyBoxRefs; i++ {
err := p.addBox(0, "")
if err != nil {
return err

Check warning on line 1032 in ledger/simulation/resources.go

View check run for this annotation

Codecov / codecov/patch

ledger/simulation/resources.go#L1032

Added line #L1032 was not covered by tests
}
}

return nil
}

// MakeResourcePopulator creates a ResourcePopulator from a transaction group
func MakeResourcePopulator(txnGroup []transactions.SignedTxnWithAD, consensusParams config.ConsensusParams) ResourcePopulator {
populator := ResourcePopulator{
TxnResources: make([]TxnResources, consensusParams.MaxTxGroupSize),
Expand Down
Loading