Skip to content

Commit

Permalink
WIP hiding AccountData maps to catch ledger users
Browse files Browse the repository at this point in the history
  • Loading branch information
cce committed Nov 1, 2021
1 parent c4f64e1 commit 1b91783
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 295 deletions.
452 changes: 226 additions & 226 deletions data/basics/msgp_gen.go

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions data/basics/userBalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type AccountData struct {
// NOTE: do not modify this value in-place in existing AccountData
// structs; allocate a copy and modify that instead. AccountData
// is expected to have copy-by-value semantics.
AssetParams map[AssetIndex]AssetParams `codec:"apar,allocbound=encodedMaxAssetsPerAccount"`
XAssetParams map[AssetIndex]AssetParams `codec:"apar,allocbound=encodedMaxAssetsPerAccount"`

// Assets is the set of assets that can be held by this
// account. Assets (i.e., slots in this map) are explicitly
Expand All @@ -196,7 +196,7 @@ type AccountData struct {
// NOTE: do not modify this value in-place in existing AccountData
// structs; allocate a copy and modify that instead. AccountData
// is expected to have copy-by-value semantics.
Assets map[AssetIndex]AssetHolding `codec:"asset,allocbound=encodedMaxAssetsPerAccount"`
XAssets map[AssetIndex]AssetHolding `codec:"asset,allocbound=encodedMaxAssetsPerAccount"`

// AuthAddr is the address against which signatures/multisigs/logicsigs should be checked.
// If empty, the address of the account whose AccountData this is is used.
Expand All @@ -206,11 +206,11 @@ type AccountData struct {

// AppLocalStates stores the local states associated with any applications
// that this account has opted in to.
AppLocalStates map[AppIndex]AppLocalState `codec:"appl,allocbound=EncodedMaxAppLocalStates"`
XAppLocalStates map[AppIndex]AppLocalState `codec:"appl,allocbound=EncodedMaxAppLocalStates"`

// AppParams stores the global parameters and state associated with any
// applications that this account has created.
AppParams map[AppIndex]AppParams `codec:"appp,allocbound=EncodedMaxAppParams"`
XAppParams map[AppIndex]AppParams `codec:"appp,allocbound=EncodedMaxAppParams"`

// TotalAppSchema stores the sum of all of the LocalStateSchemas
// and GlobalStateSchemas in this account (global for applications
Expand Down Expand Up @@ -461,22 +461,22 @@ func (u AccountData) WithUpdatedRewards(proto config.ConsensusParams, rewardsLev
// MinBalance computes the minimum balance requirements for an account based on
// some consensus parameters. MinBalance should correspond roughly to how much
// storage the account is allowed to store on disk.
func (u AccountData) MinBalance(proto *config.ConsensusParams) (res MicroAlgos) {
func (u AccountData) MinBalance(proto *config.ConsensusParams, totalAssets, totalAppParams, totalAppLocalStates uint64) (res MicroAlgos) {
var min uint64

// First, base MinBalance
min = proto.MinBalance

// MinBalance for each Asset
assetCost := MulSaturate(proto.MinBalance, uint64(len(u.Assets)))
assetCost := MulSaturate(proto.MinBalance, uint64(totalAssets))
min = AddSaturate(min, assetCost)

// Base MinBalance for each created application
appCreationCost := MulSaturate(proto.AppFlatParamsMinBalance, uint64(len(u.AppParams)))
appCreationCost := MulSaturate(proto.AppFlatParamsMinBalance, uint64(totalAppParams))
min = AddSaturate(min, appCreationCost)

// Base MinBalance for each opted in application
appOptInCost := MulSaturate(proto.AppFlatOptInMinBalance, uint64(len(u.AppLocalStates)))
appOptInCost := MulSaturate(proto.AppFlatOptInMinBalance, uint64(totalAppLocalStates))
min = AddSaturate(min, appOptInCost)

// MinBalance for state usage measured by LocalStateSchemas and
Expand Down Expand Up @@ -530,8 +530,8 @@ func (u OnlineAccountData) KeyDilution(proto config.ConsensusParams) uint64 {

// IsZero checks if an AccountData value is the same as its zero value.
func (u AccountData) IsZero() bool {
if u.Assets != nil && len(u.Assets) == 0 {
u.Assets = nil
if u.XAssets != nil && len(u.XAssets) == 0 {
u.XAssets = nil
}

return reflect.DeepEqual(u, AccountData{})
Expand Down
8 changes: 4 additions & 4 deletions ledger/accountdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,17 @@ func writeCatchpointStagingCreatable(ctx context.Context, tx *sql.Tx, bals []nor

for _, balance := range bals {
// if the account has any asset params, it means that it's the creator of an asset.
if len(balance.accountData.AssetParams) > 0 {
for aidx := range balance.accountData.AssetParams {
if len(balance.accountData.XAssetParams) > 0 {
for aidx := range balance.accountData.XAssetParams {
_, err := insertStmt.ExecContext(ctx, basics.CreatableIndex(aidx), balance.address[:], basics.AssetCreatable)
if err != nil {
return err
}
}
}

if len(balance.accountData.AppParams) > 0 {
for aidx := range balance.accountData.AppParams {
if len(balance.accountData.XAppParams) > 0 {
for aidx := range balance.accountData.XAppParams {
_, err := insertStmt.ExecContext(ctx, basics.CreatableIndex(aidx), balance.address[:], basics.AppCreatable)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion ledger/catchupaccessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (c *CatchpointCatchupAccessorImpl) processStagingBalances(ctx context.Conte
defer wg.Done()
hasCreatables := false
for _, accBal := range normalizedAccountBalances {
if len(accBal.accountData.AssetParams) > 0 || len(accBal.accountData.AppParams) > 0 {
if len(accBal.accountData.XAssetParams) > 0 || len(accBal.accountData.XAppParams) > 0 {
hasCreatables = true
break
}
Expand Down
16 changes: 8 additions & 8 deletions ledger/internal/appcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,9 @@ func applyStorageDelta(data basics.AccountData, aapp storagePtr, store *storageD
// having basics.AppParams and basics.AppLocalState under a common interface with additional loops and type assertions
if aapp.global {
var owned map[basics.AppIndex]basics.AppParams
if len(data.AppParams) > 0 {
owned = make(map[basics.AppIndex]basics.AppParams, len(data.AppParams))
for k, v := range data.AppParams {
if len(data.XAppParams) > 0 {
owned = make(map[basics.AppIndex]basics.AppParams, len(data.XAppParams))
for k, v := range data.XAppParams {
owned[k] = v
}
}
Expand Down Expand Up @@ -683,13 +683,13 @@ func applyStorageDelta(data basics.AccountData, aapp storagePtr, store *storageD
owned[aapp.aidx] = params
}

data.AppParams = owned
data.XAppParams = owned

} else {
var owned map[basics.AppIndex]basics.AppLocalState
if len(data.AppLocalStates) > 0 {
owned = make(map[basics.AppIndex]basics.AppLocalState, len(data.AppLocalStates))
for k, v := range data.AppLocalStates {
if len(data.XAppLocalStates) > 0 {
owned = make(map[basics.AppIndex]basics.AppLocalState, len(data.XAppLocalStates))
for k, v := range data.XAppLocalStates {
owned[k] = v
}
}
Expand Down Expand Up @@ -725,7 +725,7 @@ func applyStorageDelta(data basics.AccountData, aapp storagePtr, store *storageD
owned[aapp.aidx] = states
}

data.AppLocalStates = owned
data.XAppLocalStates = owned
}
return data, nil
}
17 changes: 16 additions & 1 deletion ledger/internal/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type cowForLogicLedger interface {
GetAppParams(addr basics.Address, aidx basics.AppIndex) (basics.AppParams, error)
GetAssetParams(addr basics.Address, aidx basics.AssetIndex) (basics.AssetParams, error)
GetAssetHolding(addr basics.Address, aidx basics.AssetIndex) (basics.AssetHolding, error)
TotalAssetHolding(addr basics.Address) (int, error)
TotalAppParams(addr basics.Address) (int, error)
TotalAppLocalState(addr basics.Address) (int, error)
GetCreatableID(groupIdx int) basics.CreatableIndex
GetCreator(cidx basics.CreatableIndex, ctype basics.CreatableType) (basics.Address, bool, error)
GetKey(addr basics.Address, aidx basics.AppIndex, global bool, key string, accountIdx uint64) (basics.TealValue, bool, error)
Expand Down Expand Up @@ -88,8 +91,20 @@ func (al *logicLedger) MinBalance(addr basics.Address, proto *config.ConsensusPa
if err != nil {
return
}
totalAssets, err := al.cow.TotalAssetHolding(addr)
if err != nil {
return
}
totalAppParams, err := al.cow.TotalAppParams(addr)
if err != nil {
return
}
totalAppLocalStates, err := al.cow.TotalAppLocalState(addr)
if err != nil {
return
}

return record.MinBalance(proto), nil
return record.MinBalance(proto, uint64(totalAssets), uint64(totalAppParams), uint64(totalAppLocalStates)), nil
}

func (al *logicLedger) Authorizer(addr basics.Address) (basics.Address, error) {
Expand Down
52 changes: 26 additions & 26 deletions ledger/internal/cow_creatables.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,36 @@ func (cs *roundCowState) TotalAppParams(creator basics.Address) (int, error) {
if err != nil {
return 0, err
}
return len(acct.AppParams), nil
return len(acct.XAppParams), nil
}
func (cs *roundCowState) TotalAppLocalState(addr basics.Address) (int, error) {
acct, err := cs.lookup(addr)
if err != nil {
return 0, err
}
return len(acct.AppLocalStates), nil
return len(acct.XAppLocalStates), nil
}
func (cs *roundCowState) TotalAssetHolding(addr basics.Address) (int, error) {
acct, err := cs.lookup(addr)
if err != nil {
return 0, err
}
return len(acct.Assets), nil
return len(acct.XAssets), nil
}
func (cs *roundCowState) TotalAssetParams(addr basics.Address) (int, error) {
acct, err := cs.lookup(addr)
if err != nil {
return 0, err
}
return len(acct.AssetParams), nil
return len(acct.XAssetParams), nil
}

func (cs *roundCowState) GetAppParams(creator basics.Address, aidx basics.AppIndex) (params basics.AppParams, err error) {
acct, err := cs.lookup(creator)
if err != nil {
return
}
params, ok := acct.AppParams[aidx]
params, ok := acct.XAppParams[aidx]
if !ok {
err = apply.ErrCreatableNotFound
return
Expand All @@ -67,7 +67,7 @@ func (cs *roundCowState) GetAppLocalState(addr basics.Address, aidx basics.AppIn
if err != nil {
return
}
state, ok := acct.AppLocalStates[aidx]
state, ok := acct.XAppLocalStates[aidx]
if !ok {
err = apply.ErrCreatableNotFound
return
Expand All @@ -79,7 +79,7 @@ func (cs *roundCowState) GetAssetHolding(addr basics.Address, aidx basics.AssetI
if err != nil {
return
}
holding, ok := acct.Assets[aidx]
holding, ok := acct.XAssets[aidx]
if !ok {
err = apply.ErrCreatableNotFound
return
Expand All @@ -91,7 +91,7 @@ func (cs *roundCowState) GetAssetParams(addr basics.Address, aidx basics.AssetIn
if err != nil {
return
}
params, ok := acct.AssetParams[aidx]
params, ok := acct.XAssetParams[aidx]
if !ok {
err = apply.ErrCreatableNotFound
return
Expand All @@ -104,51 +104,51 @@ func (cs *roundCowState) PutAppParams(addr basics.Address, aidx basics.AppIndex,
if err != nil {
return err
}
ap := make(map[basics.AppIndex]basics.AppParams, len(acct.AppParams))
for k, v := range acct.AppParams {
ap := make(map[basics.AppIndex]basics.AppParams, len(acct.XAppParams))
for k, v := range acct.XAppParams {
ap[k] = v
}
ap[aidx] = params
acct.AppParams = ap
acct.XAppParams = ap
return cs.Put(addr, acct)
}
func (cs *roundCowState) PutAppLocalState(addr basics.Address, aidx basics.AppIndex, state basics.AppLocalState) error {
acct, err := cs.lookup(addr)
if err != nil {
return err
}
als := make(map[basics.AppIndex]basics.AppLocalState, len(acct.AppLocalStates))
for k, v := range acct.AppLocalStates {
als := make(map[basics.AppIndex]basics.AppLocalState, len(acct.XAppLocalStates))
for k, v := range acct.XAppLocalStates {
als[k] = v
}
als[aidx] = state
acct.AppLocalStates = als
acct.XAppLocalStates = als
return cs.Put(addr, acct)
}
func (cs *roundCowState) PutAssetHolding(addr basics.Address, aidx basics.AssetIndex, data basics.AssetHolding) error {
acct, err := cs.lookup(addr)
if err != nil {
return err
}
ah := make(map[basics.AssetIndex]basics.AssetHolding, len(acct.Assets))
for k, v := range acct.Assets {
ah := make(map[basics.AssetIndex]basics.AssetHolding, len(acct.XAssets))
for k, v := range acct.XAssets {
ah[k] = v
}
ah[aidx] = data
acct.Assets = ah
acct.XAssets = ah
return cs.Put(addr, acct)
}
func (cs *roundCowState) PutAssetParams(addr basics.Address, aidx basics.AssetIndex, data basics.AssetParams) error {
acct, err := cs.lookup(addr)
if err != nil {
return err
}
ap := make(map[basics.AssetIndex]basics.AssetParams, len(acct.AssetParams))
for k, v := range acct.AssetParams {
ap := make(map[basics.AssetIndex]basics.AssetParams, len(acct.XAssetParams))
for k, v := range acct.XAssetParams {
ap[k] = v
}
ap[aidx] = data
acct.AssetParams = ap
acct.XAssetParams = ap
return cs.Put(addr, acct)
}

Expand All @@ -157,31 +157,31 @@ func (cs *roundCowState) DeleteAppParams(addr basics.Address, aidx basics.AppInd
if err != nil {
return err
}
delete(acct.AppParams, aidx)
delete(acct.XAppParams, aidx)
return cs.Put(addr, acct)
}
func (cs *roundCowState) DeleteAppLocalState(addr basics.Address, aidx basics.AppIndex) error {
acct, err := cs.lookup(addr)
if err != nil {
return err
}
delete(acct.AppLocalStates, aidx)
delete(acct.XAppLocalStates, aidx)
return cs.Put(addr, acct)
}
func (cs *roundCowState) DeleteAssetHolding(addr basics.Address, aidx basics.AssetIndex) error {
acct, err := cs.lookup(addr)
if err != nil {
return err
}
delete(acct.Assets, aidx)
delete(acct.XAssets, aidx)
return cs.Put(addr, acct)
}
func (cs *roundCowState) DeleteAssetParams(addr basics.Address, aidx basics.AssetIndex) error {
acct, err := cs.lookup(addr)
if err != nil {
return err
}
delete(acct.AssetParams, aidx)
delete(acct.XAssetParams, aidx)
return cs.Put(addr, acct)
}

Expand All @@ -190,7 +190,7 @@ func (cs *roundCowState) CheckAppLocalState(addr basics.Address, aidx basics.App
if err != nil {
return
}
_, ok = acct.AppLocalStates[aidx]
_, ok = acct.XAppLocalStates[aidx]
return
}

Expand All @@ -199,6 +199,6 @@ func (cs *roundCowState) CheckAssetParams(addr basics.Address, aidx basics.Asset
if err != nil {
return
}
_, ok = acct.AssetParams[aidx]
_, ok = acct.XAssetParams[aidx]
return
}
Loading

0 comments on commit 1b91783

Please sign in to comment.