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
Show file tree
Hide file tree
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
addHolding
  • Loading branch information
joe-p committed Jun 6, 2024
commit d9e77effd5c9168a50d3b273fdf2a860c647e2cb
72 changes: 63 additions & 9 deletions ledger/simulation/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@
return p.tracker.addBox(app, name, readSize, *p.initialBoxSurplusReadBudget, p.ep.Proto.BytesPerBoxReference)
}

type TxnResources struct {

Check failure on line 609 in ledger/simulation/resources.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 exported: exported type TxnResources should have comment or be unexported (revive) Raw Output: ledger/simulation/resources.go:609:6: exported: exported type TxnResources should have comment or be unexported (revive) type TxnResources struct { ^
// The static fields are resource arrays that were given in the transaciton group and thus cannot be removed

Check failure on line 610 in ledger/simulation/resources.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 `transaciton` is a misspelling of `transactions` (misspell) Raw Output: ledger/simulation/resources.go:610:66: `transaciton` is a misspelling of `transactions` (misspell) // The static fields are resource arrays that were given in the transaciton group and thus cannot be removed ^
// 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 @@ -671,7 +671,22 @@
_, hasStatic := r.StaticAccounts[addr]
_, hasRef := r.Accounts[addr]
_, hasField := r.AccountsFromFields[addr]
// TODO: Check app addresses

if r.AppFromField.Address() == addr {
return true
}

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

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

return hasField || hasStatic || hasRef
}
Expand All @@ -692,7 +707,7 @@
r.Boxes[logic.BoxRef{App: app, Name: name}] = struct{}{}
}

type PopulatedArrays struct {

Check failure on line 710 in ledger/simulation/resources.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 exported: exported type PopulatedArrays should have comment or be unexported (revive) Raw Output: ledger/simulation/resources.go:710:6: exported: exported type PopulatedArrays should have comment or be unexported (revive) type PopulatedArrays struct { ^
Accounts []basics.Address
Assets []basics.AssetIndex
Apps []basics.AppIndex
Expand Down Expand Up @@ -728,7 +743,7 @@
}
}

type ResourcePopulator struct {

Check failure on line 746 in ledger/simulation/resources.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 exported: exported type ResourcePopulator should have comment or be unexported (revive) Raw Output: ledger/simulation/resources.go:746:6: exported: exported type ResourcePopulator should have comment or be unexported (revive) type ResourcePopulator struct { ^
TxnResources []TxnResources
}

Expand Down Expand Up @@ -818,7 +833,7 @@

func (p *ResourcePopulator) addAsset(asset basics.AssetIndex) error {
for i := range p.TxnResources {
if p.TxnResources[i].hasRoomForCrossRef() {
if p.TxnResources[i].hasRoom() {
p.TxnResources[i].addAsset(asset)
return nil
}
Expand All @@ -828,7 +843,7 @@

func (p *ResourcePopulator) addApp(app basics.AppIndex) error {
for i := range p.TxnResources {
if p.TxnResources[i].hasRoomForCrossRef() {
if p.TxnResources[i].hasRoom() {
p.TxnResources[i].addApp(app)
return nil
}
Expand Down Expand Up @@ -859,6 +874,38 @@
return fmt.Errorf("no room for box")
}

func (p *ResourcePopulator) addHolding(addr basics.Address, aid basics.AssetIndex) error {
// First try to find txn with account already available
for i := range p.TxnResources {
if p.TxnResources[i].hasAccount(addr) {
if p.TxnResources[i].hasRoom() {
p.TxnResources[i].addAsset(aid)
return nil
}
}
}

// Then try to find txn with asset already available
for i := range p.TxnResources {
if p.TxnResources[i].hasAsset(aid) {
if p.TxnResources[i].hasRoomForAccount() {
p.TxnResources[i].addAccount(addr)
return nil
}
}
}

// Finally try to find txn with room for both account and holding
for i := range p.TxnResources {
if p.TxnResources[i].hasRoomForCrossRef() {
p.TxnResources[i].addAccount(addr)
p.TxnResources[i].addAsset(aid)
return nil
}
}
return fmt.Errorf("no room for holding")
joe-p marked this conversation as resolved.
Show resolved Hide resolved
}

func (p *ResourcePopulator) populateResources(groupResourceTracker groupResourceTracker) error {
for i, tracker := range groupResourceTracker.localTxnResources {
for asset := range tracker.Assets {
Expand All @@ -874,15 +921,15 @@
}
}

for asset := range groupResourceTracker.globalResources.Assets {
err := p.addAsset(asset)
for holding := range groupResourceTracker.globalResources.AssetHoldings {
err := p.addHolding(holding.Address, holding.Asset)
if err != nil {
return err
}
}

for app := range groupResourceTracker.globalResources.Apps {
err := p.addApp(app)
for box := range groupResourceTracker.globalResources.Boxes {
err := p.addBox(box.App, box.Name)
if err != nil {
return err
}
Expand All @@ -895,8 +942,15 @@
}
}

for box := range groupResourceTracker.globalResources.Boxes {
err := p.addBox(box.App, box.Name)
for app := range groupResourceTracker.globalResources.Apps {
err := p.addApp(app)
if err != nil {
return err
}
}

for asset := range groupResourceTracker.globalResources.Assets {
err := p.addAsset(asset)
if err != nil {
return err
}
Expand All @@ -905,7 +959,7 @@
return nil
}

func MakeResourcePopulator(txnGroup []transactions.SignedTxnWithAD) ResourcePopulator {

Check failure on line 962 in ledger/simulation/resources.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 exported: exported function MakeResourcePopulator should have comment or be unexported (revive) Raw Output: ledger/simulation/resources.go:962:1: exported: exported function MakeResourcePopulator should have comment or be unexported (revive) func MakeResourcePopulator(txnGroup []transactions.SignedTxnWithAD) ResourcePopulator { ^
populator := ResourcePopulator{
TxnResources: make([]TxnResources, len(txnGroup)),
}
Expand Down
23 changes: 18 additions & 5 deletions ledger/simulation/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/data/transactions"
"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/algorand/go-algorand/ledger/ledgercore"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -351,22 +352,34 @@ func TestPopulatorWithGlobalResources(t *testing.T) {
proto := config.Consensus[protocol.ConsensusFuture]
groupTracker := makeGroupResourceTracker(txns, &proto)

// Resources that will go in the first transaction
addr := basics.Address{1, 1, 1}
app := basics.AppIndex(12345)
asset := basics.AssetIndex(12345)
box1 := logic.BoxRef{App: basics.AppIndex(1), Name: "box"}
box100 := logic.BoxRef{App: basics.AppIndex(100), Name: "box"}
addr100 := basics.Address{100}
asa100 := basics.AssetIndex(100)

// Resources that will go in the second transaction
box1 := logic.BoxRef{App: basics.AppIndex(1), Name: "box"}
app1Addr := basics.AppIndex(1).Address()
asa1 := basics.AssetIndex(1)
addr1 := basics.Address{1}

groupTracker.globalResources.Accounts = make(map[basics.Address]struct{})
groupTracker.globalResources.Assets = make(map[basics.AssetIndex]struct{})
groupTracker.globalResources.Apps = make(map[basics.AppIndex]struct{})
groupTracker.globalResources.Boxes = make(map[logic.BoxRef]uint64)
groupTracker.globalResources.AssetHoldings = make(map[ledgercore.AccountAsset]struct{})

groupTracker.globalResources.Accounts[addr] = struct{}{}
groupTracker.globalResources.Assets[asset] = struct{}{}
groupTracker.globalResources.Apps[app] = struct{}{}
groupTracker.globalResources.Boxes[box1] = 1
groupTracker.globalResources.Boxes[box100] = 1
groupTracker.globalResources.AssetHoldings[ledgercore.AccountAsset{Address: addr100, Asset: asa100}] = struct{}{}
groupTracker.globalResources.AssetHoldings[ledgercore.AccountAsset{Address: app1Addr, Asset: asa1}] = struct{}{}
groupTracker.globalResources.AssetHoldings[ledgercore.AccountAsset{Address: addr1, Asset: asa1}] = struct{}{}

err := populator.populateResources(groupTracker)
require.NoError(t, err)
Expand All @@ -376,12 +389,12 @@ func TestPopulatorWithGlobalResources(t *testing.T) {

require.ElementsMatch(t, pop0.Apps, []basics.AppIndex{app, box100.App})
require.ElementsMatch(t, pop0.Boxes, []logic.BoxRef{box100})
require.ElementsMatch(t, pop0.Accounts, []basics.Address{addr})
require.ElementsMatch(t, pop0.Assets, []basics.AssetIndex{asset})
require.ElementsMatch(t, pop0.Accounts, []basics.Address{addr, addr100})
require.ElementsMatch(t, pop0.Assets, []basics.AssetIndex{asset, asa100})

// Txn 1 has all the resources that had partial requirements already in tnx 1
require.ElementsMatch(t, pop1.Apps, []basics.AppIndex{})
require.ElementsMatch(t, pop1.Boxes, []logic.BoxRef{box1})
require.ElementsMatch(t, pop1.Accounts, []basics.Address{})
require.ElementsMatch(t, pop1.Assets, []basics.AssetIndex{})
require.ElementsMatch(t, pop1.Accounts, []basics.Address{addr1})
require.ElementsMatch(t, pop1.Assets, []basics.AssetIndex{asa1})
}
Loading