Skip to content

Commit

Permalink
move account storage functions to separate value
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 11, 2023
1 parent fccfd09 commit ed79460
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 95 deletions.
1 change: 1 addition & 0 deletions runtime/common/metering.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ var (
AccountAccountCapabilitiesStringMemoryUsage = NewRawStringMemoryUsage(len("Account.AccountCapabilities()"))
AccountCapabilitiesStringMemoryUsage = NewRawStringMemoryUsage(len("Account.Capabilities()"))
AccountInboxStringMemoryUsage = NewRawStringMemoryUsage(len("Account.Inbox()"))
AccountStorageStringMemoryUsage = NewRawStringMemoryUsage(len("Account.Storage()"))
IDCapabilityValueStringMemoryUsage = NewRawStringMemoryUsage(len("Capability<>(address: , id: )"))
StorageCapabilityControllerValueStringMemoryUsage = NewRawStringMemoryUsage(len("StorageCapabilityController(borrowType: , capabilityID: , target: )"))
AccountCapabilityControllerValueStringMemoryUsage = NewRawStringMemoryUsage(len("AccountCapabilityController(borrowType: , capabilityID: )"))
Expand Down
11 changes: 6 additions & 5 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3955,7 +3955,12 @@ func (interpreter *Interpreter) domainPaths(address common.Address, domain commo
return paths
}

func (interpreter *Interpreter) accountPaths(addressValue AddressValue, locationRange LocationRange, domain common.PathDomain, pathType StaticType) *ArrayValue {
func (interpreter *Interpreter) accountPaths(
addressValue AddressValue,
locationRange LocationRange,
domain common.PathDomain,
pathType StaticType,
) *ArrayValue {
address := addressValue.ToAddress()
values := interpreter.domainPaths(address, domain)
return NewArrayValue(
Expand All @@ -3971,10 +3976,6 @@ func (interpreter *Interpreter) publicAccountPaths(addressValue AddressValue, lo
return interpreter.accountPaths(addressValue, locationRange, common.PathDomainPublic, PrimitiveStaticTypePublicPath)
}

func (interpreter *Interpreter) privateAccountPaths(addressValue AddressValue, locationRange LocationRange) *ArrayValue {
return interpreter.accountPaths(addressValue, locationRange, common.PathDomainPrivate, PrimitiveStaticTypePrivatePath)
}

func (interpreter *Interpreter) storageAccountPaths(addressValue AddressValue, locationRange LocationRange) *ArrayValue {
return interpreter.accountPaths(addressValue, locationRange, common.PathDomainStorage, PrimitiveStaticTypeStoragePath)
}
Expand Down
97 changes: 9 additions & 88 deletions runtime/interpreter/value_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var accountTypeID = sema.AccountType.ID()
var accountStaticType StaticType = PrimitiveStaticTypeAccount // unmetered
var accountFieldNames = []string{
sema.AccountTypeAddressFieldName,
sema.AccountTypeStorageFieldName,
sema.AccountTypeContractsFieldName,
sema.AccountTypeKeysFieldName,
sema.AccountTypeInboxFieldName,
Expand All @@ -43,8 +44,7 @@ func NewAccountValue(
address AddressValue,
accountBalanceGet func() UFix64Value,
accountAvailableBalanceGet func() UFix64Value,
storageUsedGet func(interpreter *Interpreter) UInt64Value,
storageCapacityGet func(interpreter *Interpreter) UInt64Value,
storageConstructor func() Value,
contractsConstructor func() Value,
keysConstructor func() Value,
inboxConstructor func() Value,
Expand All @@ -55,13 +55,20 @@ func NewAccountValue(
sema.AccountTypeAddressFieldName: address,
}

var storage Value
var contracts Value
var keys Value
var inbox Value
var capabilities Value

computeField := func(name string, inter *Interpreter, locationRange LocationRange) Value {
switch name {
case sema.AccountTypeStorageFieldName:
if storage == nil {
storage = storageConstructor()
}
return storage

case sema.AccountTypeContractsFieldName:
if contracts == nil {
contracts = contractsConstructor()
Expand Down Expand Up @@ -91,92 +98,6 @@ func NewAccountValue(

case sema.AccountTypeAvailableBalanceFieldName:
return accountAvailableBalanceGet()

// TODO: refactor storage members to Account.Storage value
//
//case sema.AuthAccountTypePublicPathsFieldName:
// return inter.publicAccountPaths(address, locationRange)
//
//case sema.AuthAccountTypePrivatePathsFieldName:
// return inter.privateAccountPaths(address, locationRange)
//
//case sema.AuthAccountTypeStoragePathsFieldName:
// return inter.storageAccountPaths(address, locationRange)
//
//case sema.AuthAccountTypeForEachPublicFunctionName:
// if forEachPublicFunction == nil {
// forEachPublicFunction = inter.newStorageIterationFunction(
// sema.AuthAccountTypeForEachPublicFunctionType,
// address,
// common.PathDomainPublic,
// sema.PublicPathType,
// )
// }
// return forEachPublicFunction
//
//case sema.AuthAccountTypeForEachPrivateFunctionName:
// if forEachPrivateFunction == nil {
// forEachPrivateFunction = inter.newStorageIterationFunction(
// sema.AuthAccountTypeForEachPrivateFunctionType,
// address,
// common.PathDomainPrivate,
// sema.PrivatePathType,
// )
// }
// return forEachPrivateFunction
//
//case sema.AuthAccountTypeForEachStoredFunctionName:
// if forEachStoredFunction == nil {
// forEachStoredFunction = inter.newStorageIterationFunction(
// sema.AuthAccountTypeForEachStoredFunctionType,
// address,
// common.PathDomainStorage,
// sema.StoragePathType,
// )
// }
// return forEachStoredFunction
//
//case sema.AuthAccountTypeStorageUsedFieldName:
// return storageUsedGet(inter)
//
//case sema.AuthAccountTypeStorageCapacityFieldName:
// return storageCapacityGet(inter)
//
//case sema.AuthAccountTypeTypeFunctionName:
// if typeFunction == nil {
// typeFunction = inter.authAccountTypeFunction(address)
// }
// return typeFunction
//
//case sema.AuthAccountTypeLoadFunctionName:
// if loadFunction == nil {
// loadFunction = inter.authAccountLoadFunction(address)
// }
// return loadFunction
//
//case sema.AuthAccountTypeCopyFunctionName:
// if copyFunction == nil {
// copyFunction = inter.authAccountCopyFunction(address)
// }
// return copyFunction
//
//case sema.AuthAccountTypeSaveFunctionName:
// if saveFunction == nil {
// saveFunction = inter.authAccountSaveFunction(address)
// }
// return saveFunction
//
//case sema.AuthAccountTypeBorrowFunctionName:
// if borrowFunction == nil {
// borrowFunction = inter.authAccountBorrowFunction(address)
// }
// return borrowFunction
//
//case sema.AuthAccountTypeCheckFunctionName:
// if checkFunction == nil {
// checkFunction = inter.authAccountCheckFunction(address)
// }
// return checkFunction
}

return nil
Expand Down
146 changes: 146 additions & 0 deletions runtime/interpreter/value_account_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package interpreter

import (
"fmt"

"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/sema"
)

// Account.Storage

var account_StorageTypeID = sema.Account_StorageType.ID()
var account_StorageStaticType StaticType = PrimitiveStaticTypeAccount_Storage

// NewAccountStorageValue constructs an Account.Storage value.
func NewAccountStorageValue(
gauge common.MemoryGauge,
address AddressValue,
storageUsedGet func(interpreter *Interpreter) UInt64Value,
storageCapacityGet func(interpreter *Interpreter) UInt64Value,
) Value {

var forEachStoredFunction *HostFunctionValue
var forEachPublicFunction *HostFunctionValue
var typeFunction *HostFunctionValue
var loadFunction *HostFunctionValue
var copyFunction *HostFunctionValue
var saveFunction *HostFunctionValue
var borrowFunction *HostFunctionValue
var checkFunction *HostFunctionValue

computeField := func(name string, inter *Interpreter, locationRange LocationRange) Value {
switch name {
case sema.Account_StorageTypePublicPathsFieldName:
return inter.publicAccountPaths(address, locationRange)

case sema.Account_StorageTypeStoragePathsFieldName:
return inter.storageAccountPaths(address, locationRange)

case sema.Account_StorageTypeForEachPublicFunctionName:
if forEachPublicFunction == nil {
forEachPublicFunction = inter.newStorageIterationFunction(
sema.Account_StorageTypeForEachPublicFunctionType,
address,
common.PathDomainPublic,
sema.PublicPathType,
)
}
return forEachPublicFunction

case sema.Account_StorageTypeForEachStoredFunctionName:
if forEachStoredFunction == nil {
forEachStoredFunction = inter.newStorageIterationFunction(
sema.Account_StorageTypeForEachStoredFunctionType,
address,
common.PathDomainStorage,
sema.StoragePathType,
)
}
return forEachStoredFunction

case sema.Account_StorageTypeUsedFieldName:
return storageUsedGet(inter)

case sema.Account_StorageTypeCapacityFieldName:
return storageCapacityGet(inter)

case sema.Account_StorageTypeTypeFunctionName:
if typeFunction == nil {
typeFunction = inter.authAccountTypeFunction(address)
}
return typeFunction

case sema.Account_StorageTypeLoadFunctionName:
if loadFunction == nil {
loadFunction = inter.authAccountLoadFunction(address)
}
return loadFunction

case sema.Account_StorageTypeCopyFunctionName:
if copyFunction == nil {
copyFunction = inter.authAccountCopyFunction(address)
}
return copyFunction

case sema.Account_StorageTypeSaveFunctionName:
if saveFunction == nil {
saveFunction = inter.authAccountSaveFunction(address)
}
return saveFunction

case sema.Account_StorageTypeBorrowFunctionName:
if borrowFunction == nil {
borrowFunction = inter.authAccountBorrowFunction(address)
}
return borrowFunction

case sema.Account_StorageTypeCheckFunctionName:
if checkFunction == nil {
checkFunction = inter.authAccountCheckFunction(address)
}
return checkFunction
}

return nil
}

var str string
stringer := func(memoryGauge common.MemoryGauge, seenReferences SeenReferences) string {
if str == "" {
common.UseMemory(memoryGauge, common.AccountStorageStringMemoryUsage)
addressStr := address.MeteredString(memoryGauge, seenReferences)
str = fmt.Sprintf("Account.Storage(%s)", addressStr)
}
return str
}

return NewSimpleCompositeValue(
gauge,
account_StorageTypeID,
account_StorageStaticType,
nil,
nil,
computeField,
nil,
stringer,
)
}
8 changes: 6 additions & 2 deletions runtime/stdlib/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ func NewAccountValue(
addressValue,
newAccountBalanceGetFunction(gauge, handler, addressValue),
newAccountAvailableBalanceGetFunction(gauge, handler, addressValue),
newStorageUsedGetFunction(handler, addressValue),
newStorageCapacityGetFunction(handler, addressValue),
func() interpreter.Value {
// TODO:
//newStorageUsedGetFunction(handler, addressValue),
//newStorageCapacityGetFunction(handler, addressValue),
return nil
},
func() interpreter.Value {
return newAccountContractsValue(
gauge,
Expand Down

0 comments on commit ed79460

Please sign in to comment.